private List <gameplayerdetail> GetGamePlayerDetailList(ReplayData replayData, IEnumerable <player> dbPlayers, game fateGame, server dbServer,
                                                                frsDb db)
        {
            List <gameplayerdetail> fateGamePlayerDetailList = new List <gameplayerdetail>();

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

                gameplayerdetail fateGamePlayerDetail = new gameplayerdetail();
                fateGamePlayerDetail.FK_GameID   = fateGame.GameID;
                fateGamePlayerDetail.FK_PlayerID = player.PlayerID;
                fateGamePlayerDetail.FK_ServerID = dbServer.ServerID;
                herotype playerHeroType = db.herotype.FirstOrDefault(x => x.HeroUnitTypeID == playerInfo.ServantId);
                if (playerHeroType == null)
                {
                    throw new Exception(String.Format("DB Error: Unknown hero type id: {0}", playerInfo.ServantId));
                }
                fateGamePlayerDetail.FK_HeroTypeID = playerHeroType.HeroTypeID;
                fateGamePlayerDetail.Kills         = playerInfo.Kills;
                fateGamePlayerDetail.Deaths        = playerInfo.Deaths;
                fateGamePlayerDetail.Assists       = playerInfo.Assists;
                fateGamePlayerDetail.Team          = (playerInfo.Team + 1).ToString();
                fateGamePlayerDetail.DamageTaken   = playerInfo.DamageTaken;
                fateGamePlayerDetail.DamageDealt   = playerInfo.DamageDealt;
                fateGamePlayerDetail.HeroLevel     = playerInfo.ServantLevel;
                if (fateGame.Result == GameResult.NONE.ToString())
                {
                    fateGamePlayerDetail.Result = GamePlayerResult.NONE.ToString();
                }
                else if (fateGame.Result == GameResult.T1W.ToString())
                {
                    fateGamePlayerDetail.Result = playerInfo.Team == 0
                                                      ? GamePlayerResult.WIN.ToString()
                                                      : GamePlayerResult.LOSS.ToString();
                }
                else if (fateGame.Result == GameResult.T2W.ToString())
                {
                    fateGamePlayerDetail.Result = playerInfo.Team == 1
                                                      ? GamePlayerResult.WIN.ToString()
                                                      : GamePlayerResult.LOSS.ToString();
                }
                else
                {
                    throw new Exception(String.Format("Unexpected GameResult enumeration. Input: {0}",
                                                      fateGame.Result));
                }
                fateGamePlayerDetailList.Add(fateGamePlayerDetail);
            }
            return(fateGamePlayerDetailList);
        }
        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;
            }
        }