private static void AddPlayerHeroStatToDatabase(ReplayData replayData, IEnumerable <player> dbPlayers,
                                                        frsDb db, server dbServer)
        {
            foreach (player player in dbPlayers)
            {
                PlayerInfo playerInfo = replayData.GetPlayerInfoByPlayerName(player.PlayerName);
                if (playerInfo == null)
                {
                    throw new Exception(String.Format("Player Name not found during PlayerHeroStat module. Input: {0}",
                                                      player.PlayerName));
                }

                herotype playerHeroType = db.herotype.First(x => x.HeroUnitTypeID == playerInfo.ServantId);

                playerherostat playerHeroStat =
                    db.playerherostat.FirstOrDefault(
                        x =>
                        x.FK_ServerID == dbServer.ServerID && x.FK_PlayerID == player.PlayerID &&
                        x.FK_HeroTypeID == playerHeroType.HeroTypeID);
                bool isNewHeroStat = false;
                if (playerHeroStat == null)
                {
                    playerHeroStat = new playerherostat
                    {
                        FK_PlayerID   = player.PlayerID,
                        FK_ServerID   = dbServer.ServerID,
                        FK_HeroTypeID = playerHeroType.HeroTypeID
                    };
                    db.playerherostat.Add(playerHeroStat);
                    isNewHeroStat = true;
                }
                playerHeroStat.HeroPlayCount++;
                playerHeroStat.TotalHeroKills   += playerInfo.Kills;
                playerHeroStat.TotalHeroDeaths  += playerInfo.Deaths;
                playerHeroStat.TotalHeroAssists += playerInfo.Assists;

                //Upsert
                db.playerherostat.Attach(playerHeroStat);
                var playerHeroStatEntry = db.Entry(playerHeroStat);
                playerHeroStatEntry.State = isNewHeroStat ? EntityState.Added : EntityState.Modified;
            }
        }
        private void AddPlayerStatToDatabase(ReplayData replayData, IEnumerable <player> dbPlayers, frsDb db,
                                             server dbServer, game fateGame)
        {
            foreach (player player in dbPlayers)
            {
                bool       isNewPlayerStat = false;
                playerstat playerStat      =
                    db.playerstat.FirstOrDefault(
                        x => x.FK_ServerID == dbServer.ServerID && x.FK_PlayerID == player.PlayerID);
                if (playerStat == null)
                {
                    playerStat             = new playerstat();
                    playerStat.FK_PlayerID = player.PlayerID;
                    playerStat.FK_ServerID = dbServer.ServerID;
                    db.playerstat.Add(playerStat);
                    isNewPlayerStat = true;
                }
                playerStat.PlayCount++;

                PlayerInfo playerInfo = replayData.GetPlayerInfoByPlayerName(player.PlayerName);
                if (playerInfo == null)
                {
                    throw new Exception(String.Format("Player Name not found during PlayerStatList module. Input: {0}",
                                                      player.PlayerName));
                }

                if (fateGame.Result == GameResult.NONE.ToString())
                {
                    continue;
                }

                if (fateGame.Result == GameResult.T1W.ToString())
                {
                    if (playerInfo.Team == 0)
                    {
                        playerStat.Win++;
                    }
                    else if (playerInfo.Team == 1)
                    {
                        playerStat.Loss++;
                    }
                    else
                    {
                        throw new Exception(String.Format("Unexpected playerInfo team at PlayerStatListModule. Input: {0}",
                                                          playerInfo.Team));
                    }
                }
                else if (fateGame.Result == GameResult.T2W.ToString())
                {
                    if (playerInfo.Team == 0)
                    {
                        playerStat.Loss++;
                    }
                    else if (playerInfo.Team == 1)
                    {
                        playerStat.Win++;
                    }
                    else
                    {
                        throw new Exception(String.Format("Unexpected playerInfo team at PlayerStatListModule. Input: {0}",
                                                          playerInfo.Team));
                    }
                }
                else
                {
                    throw new Exception(String.Format("Unexpected GameResult enumeration at PlayerStatListModule. Input: {0}",
                                                      fateGame.Result));
                }

                //Upsert
                db.playerstat.Attach(playerStat);
                var playerStatEntry = db.Entry(playerStat);
                playerStatEntry.State = isNewPlayerStat ? EntityState.Added : EntityState.Modified;
            }
        }