public async Task CreateNewTeamRankingAsync(Team team, Season season) { if (team == null) { throw new ArgumentNullException(nameof(team)); } if (season == null) { throw new ArgumentNullException(nameof(season)); } // Find the worst rank in the basic bracket var neverLucky = await _db.RankBrackets .Include(x => x.Season) .SingleOrDefaultAsync(x => x.Type == RankBrackets.NeverLucky && x.Season.SeasonId == season.SeasonId); if (neverLucky == null) { throw new InvalidProgramException("Unable to load baseline bracket for new team"); } var rank = new Rank { RankBracket = neverLucky, Rating = new Rating { MatchMakingRating = 1500 } }; team.Rank = rank; _db.Add(rank); _db.Add(rank.Rating); _db.Update(team); await _db.SaveChangesAsync(); }
private async Task CreatePlayerForUserAsync(ApplicationUser user) { if (user == null) { throw new ArgumentNullException(nameof(user)); } // Make sure the user has a player entity var player = await _db.Players .Include(x => x.User) .SingleOrDefaultAsync(x => x.User.Id == user.Id); if (player == null) { var rating = new Rating { MatchMakingRating = 1500 }; _db.Add(rating); // Setup default rank var rank = new Rank { Rating = rating }; var defaultSeason = await _db.DefaultSeasons .Include(x => x.Season) .FirstOrDefaultAsync(); if (defaultSeason != null) { var season = await _db.Seasons .Include(x => x.Brackets) .SingleOrDefaultAsync(x => x.SeasonId == defaultSeason.Season.SeasonId); if (season != null && season.Brackets.Any(x => x.Type == RankBrackets.NeverLucky)) { rank.RankBracket = season.Brackets.Single(x => x.Type == RankBrackets.NeverLucky); } } _db.Add(rank); player = new Player { User = user, Rank = rank }; _db.Add(player); await _db.SaveChangesAsync(); } }