public async Task<GamesViewModel> Fill(League league) { League = league.Name; Games = await _db.Games.Where(x => x.LeagueId == league.Id).OrderByDescending(x => x.Date).Select(x => new Game { Id = x.Id, Date = x.Date, RedScore = x.RedScore, BlueScore = x.BlueScore }).ToListAsync(); var allPlayers = await _db.Players.Where(x => x.LeagueId == league.Id).Select(x => new { x.Id, x.Name }).ToListAsync(); foreach (var game in Games) { var positions = await _db.PlayerPositions.Where(x => x.GameId == game.Id).ToListAsync(); game.RedPlayers = positions.Where(x => x.IsRedPosition).Select(x => allPlayers.Single(y => y.Id == x.PlayerId).Name).Aggregate("", (seed, value) => seed + ", " + value).Trim(',', ' '); game.BluePlayers = positions.Where(x => x.IsBluePosition) .Select(x => allPlayers.Single(y => y.Id == x.PlayerId).Name) .Aggregate("", (seed, value) => seed + ", " + value) .Trim(',', ' '); } return this; }
//TODO: All these methods should move to a dedicated service public async Task<PostScoreViewModel> Save(League league, Db db) { if (!BluePlayer.IsNoE() && !RedPlayer.IsNoE()) { await SavePlayerScore(league, db); } else { await SaveTeamScore(league, db); } return this; }
private async Task<Models.Player> GetOrCreatePlayer(string name, League league, Db db) { var playerQuery = db.Players.Where(x => x.LeagueId == league.Id); var player = await playerQuery.FirstOrDefaultAsync(x => x.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)); if (player == null) { player = new Models.Player { LeagueId = league.Id, Name = name }; db.Players.Add(player); await db.SaveChangesAsync(); } return player; }
private async Task SavePlayerScore(League league, Db db) { if (BluePlayerScore <= 0 && RedPlayerScore <= 0) { throw new Exception("Invalid scores, both must be greater zero."); } if (BluePlayer.IsNoE() || RedPlayer.IsNoE()) { throw new Exception("One or more player names empty."); } var bluePlayer = await GetOrCreatePlayer(BluePlayer, league, db); var redPlayer = await GetOrCreatePlayer(RedPlayer, league, db); var game = await CreateGame(RedPlayerScore, BluePlayerScore, league, db); await CreatePosition(game, redPlayer, PlayerPositionTypes.Red, db); await CreatePosition(game, bluePlayer, PlayerPositionTypes.Blue, db); }
private async Task<Game> CreateGame(int redScore, int blueScore, League league, Db db) { var game = new Game { BlueScore = blueScore, RedScore = redScore, Date = DateTime.UtcNow, LeagueId = league.Id }; db.Games.Add(game); await db.SaveChangesAsync(); return game; }
public async Task<LeagueStatisticsViewModel> Fill(League league) { daysForStatistics = _settings.DaysForStatistics; var gamesQuery = _db.Games.AsNoTracking().Where(x => x.LeagueId == league.Id); var date = DateTime.UtcNow.Date.AddDays(-_settings.DaysForStatistics); gamesQuery = gamesQuery.Where(x => x.Date >= date); // EF7 beta4 does not support navigation properties in queries yet // this complicates the code a lot, because we need joins :( var players = new List<Player>(); var teams = new List<Team>(); var redPlayerIds = new List<int>(); var bluePlayerIds = new List<int>(); var goalDifferences = new List<int>(); foreach (var game in await gamesQuery.ToListAsync()) { // league stats games++; if (game.BlueWins) { blueWins++; } else if (game.RedWins) { redWins++; } blueGoals += game.BlueScore; redGoals += game.RedScore; goalDifferences.Add(Math.Max(game.BlueScore, game.RedScore) - Math.Min(game.BlueScore, game.RedScore)); var positions = await (from position in _db.PlayerPositions.AsNoTracking() join player in _db.Players.AsNoTracking() on position.PlayerId equals player.Id where position.GameId == game.Id select new { position.Position, Player = player }).ToListAsync(); // player stats foreach (var position in positions) { var player = players.FirstOrDefault(x => x.Equals(position.Player)); if (player == null) { player = Player.Create(position.Player); players.Add(player); } if (position.Position == PlayerPositionTypes.Blue || position.Position == PlayerPositionTypes.BlueDefense || position.Position == PlayerPositionTypes.BlueOffense) { bluePlayerIds.Add(position.Player.Id); } else if (position.Position == PlayerPositionTypes.Red || position.Position == PlayerPositionTypes.RedDefense || position.Position == PlayerPositionTypes.RedOffense) { redPlayerIds.Add(position.Player.Id); } // don't count ties if (game.BlueWins && (position.Position == PlayerPositionTypes.Blue || position.Position == PlayerPositionTypes.BlueDefense || position.Position == PlayerPositionTypes.BlueOffense)) { player.wins++; } else if (game.RedWins && (position.Position == PlayerPositionTypes.Red || position.Position == PlayerPositionTypes.RedDefense || position.Position == PlayerPositionTypes.RedOffense)) { player.wins++; } else if (game.RedWins && (position.Position == PlayerPositionTypes.Blue || position.Position == PlayerPositionTypes.BlueDefense || position.Position == PlayerPositionTypes.BlueOffense)) { player.losses++; } else if (game.BlueWins && (position.Position == PlayerPositionTypes.Red || position.Position == PlayerPositionTypes.RedDefense || position.Position == PlayerPositionTypes.RedOffense)) { player.losses++; } } // team stats if (positions.Count(x => x.Position == PlayerPositionTypes.BlueDefense) == 1 && positions.Count(x => x.Position == PlayerPositionTypes.BlueOffense) == 1 && positions.Count(x => x.Position == PlayerPositionTypes.RedDefense) == 1 && positions.Count(x => x.Position == PlayerPositionTypes.RedOffense) == 1) { var redOffense = players.Single(x => x.id == positions.Single(y => y.Position == PlayerPositionTypes.RedOffense).Player.Id); var redDefense = players.Single(x => x.id == positions.Single(y => y.Position == PlayerPositionTypes.RedDefense).Player.Id); var blueOffense = players.Single(x => x.id == positions.Single(y => y.Position == PlayerPositionTypes.BlueOffense).Player.Id); var blueDefense = players.Single(x => x.id == positions.Single(y => y.Position == PlayerPositionTypes.BlueDefense).Player.Id); var redTeam = teams.FirstOrDefault(x => x.Equals(redOffense, redDefense)); if (redTeam == null) { redTeam = Team.Create(redOffense, redDefense); teams.Add(redTeam); } var blueTeam = teams.FirstOrDefault(x => x.Equals(blueOffense, blueDefense)); if (blueTeam == null) { blueTeam = Team.Create(blueOffense, blueDefense); teams.Add(blueTeam); } if (game.BlueWins) { redTeam.losses++; blueTeam.wins++; } else if (game.RedWins) { redTeam.wins++; blueTeam.losses++; } } } redPlayers = redPlayerIds.Distinct().Count(); bluePlayers = bluePlayerIds.Distinct().Count(); bestPlayers = players.OrderByDescending(x => x.rank).Take(5).ToList(); worstPlayers = players.OrderBy(x => x.rank).Take(5).ToList(); bestTeams = teams.OrderByDescending(x => x.rank).Take(3).ToList(); worstTeams = teams.OrderBy(x => x.rank).Take(3).ToList(); if (goalDifferences.Any()) { goalDifference = goalDifferences.Average(x => x); } var mostActivePlayerEntity = players.OrderByDescending(x => x.losses + x.wins).FirstOrDefault(); mostActivePlayer = mostActivePlayerEntity != null ? mostActivePlayerEntity.name : ""; return this; }
public IndexViewModel Fill(League league) { Name = league.Name; return this; }