public DrinkBurpee GetDrinkBurpee(User user, Drink drink) { var result = new DrinkBurpee { Count = BusinessConstants.DefaultDrinkBurpeeCount[drink.DrinkType] + user.Level - 1 }; var penaltyDrink = GetPenaltyDrink(user); if (penaltyDrink != null) { result.Count *= BusinessConstants.DRINK_BURPEE_COUNT_DOUBLE_RATE_MULTIPLIER; result.BurpeeType = DrinkBurpeeType.DoubleRate; } return(result); }
public async Task AddPenaltyDrinkBurpeesAsync(User user) { var firstPenaltyDrink = GetPenaltyDrink(user); if (firstPenaltyDrink == null) { return; } var lastPenaltyBurpee = firstPenaltyDrink .DrinkBurpees .Where(db => db.BurpeeType == DrinkBurpeeType.Penalty) .OrderByDescending(db => db.CreatedDateTime) .FirstOrDefault(); if (lastPenaltyBurpee == null) { lastPenaltyBurpee = user.Drinks .SelectMany(d => d.DrinkBurpees) .Where(db => db.BurpeeType == DrinkBurpeeType.Penalty && db.CreatedDateTime.Date > firstPenaltyDrink.CreatedDateTime.Date) .OrderByDescending(db => db.CreatedDateTime) .FirstOrDefault(); } var hasChanges = false; while (true) { if (lastPenaltyBurpee == null) { lastPenaltyBurpee = new DrinkBurpee { BurpeeType = DrinkBurpeeType.Penalty, Count = BusinessConstants.DRINK_BURPEE_COUNT_PENALTY + user.Level - 1, CreatedDateTime = firstPenaltyDrink.CreatedDateTime.AddDays(1), RealCreatedDateTime = DateTime.Now }; } else { lastPenaltyBurpee = new DrinkBurpee { BurpeeType = DrinkBurpeeType.Penalty, Count = BusinessConstants.DRINK_BURPEE_COUNT_PENALTY + user.Level - 1, CreatedDateTime = lastPenaltyBurpee.CreatedDateTime.AddDays(1).Date.Add( firstPenaltyDrink.CreatedDateTime.TimeOfDay ), RealCreatedDateTime = DateTime.Now }; } if (lastPenaltyBurpee.CreatedDateTime > DateTime.Now) { break; } firstPenaltyDrink.DrinkBurpees.Add(lastPenaltyBurpee); hasChanges = true; } if (hasChanges) { await _userService.UpdateUserAsync(user); } }