private game GetNewGame(ReplayData replayData, server dbServer, frsDb db) { int gameId = 0; if (db.game.Any()) { gameId = db.game.Max(g => g.GameID) + 1; //Generate Max GameID + 1 } game fateGame = new game { GameID = gameId, GameName = replayData.GameName, Log = String.Join("\n", replayData.GameChatMessage.ToArray()), MatchType = replayData.GameMode.ToString(), MapVersion = ReplayData.MapVersion, Duration = new TimeSpan(0, 0, 0, 0, (int)replayData.ReplayHeader.ReplayLength), PlayedDate = replayData.GameDateTime, ReplayUrl = replayData.ReplayUrl, FK_ServerID = dbServer.ServerID, TeamOneWinCount = replayData.TeamOneVictoryCount, TeamTwoWinCount = replayData.TeamTwoVictoryCount }; if (replayData.IsForfeitedGame) { fateGame.IsForfeitedGame = true; if (replayData.ForfeitedTeam == ForfeitedTeam.T1) { fateGame.Result = GameResult.T2W.ToString(); } else if (replayData.ForfeitedTeam == ForfeitedTeam.T2) { fateGame.Result = GameResult.T1W.ToString(); } else { throw new InvalidDataException("Could not determine forfeiting team"); } } else { if (replayData.TeamOneVictoryCount > replayData.TeamTwoVictoryCount) { fateGame.Result = GameResult.T1W.ToString(); } else if (replayData.TeamOneVictoryCount < replayData.TeamTwoVictoryCount) { fateGame.Result = GameResult.T2W.ToString(); } else { fateGame.Result = GameResult.NONE.ToString(); } } return(fateGame); }
public void InsertReplayData(ReplayData replayData, string serverName) { using (var db = frsDb.Create()) { using (var trans = db.Database.BeginTransaction()) { try { if (!db.server.Any(x => x.ServerName == serverName && x.IsServiced)) { throw new Exception( String.Format("DB Error: Either server name doesn't exist or it is not serviced: {0}", serverName)); } server dbServer = db.server.FirstOrDefault(x => x.ServerName == serverName); //Player collection based on server var dbPlayers = db.Set <player>().Where(x => x.FK_ServerID == dbServer.ServerID); List <player> fatePlayerList = AddPlayerList(replayData, dbPlayers, dbServer, db); db.SaveChanges(); game fateGame = GetNewGame(replayData, dbServer, db); db.game.Add(fateGame); List <gameplayerdetail> fateGamePlayerDetailList = GetGamePlayerDetailList(replayData, fatePlayerList, fateGame, dbServer, db); db.gameplayerdetail.AddRange(fateGamePlayerDetailList); AddPlayerStatToDatabase(replayData, fatePlayerList, db, dbServer, fateGame); AddPlayerHeroStatToDatabase(replayData, fatePlayerList, db, dbServer); db.SaveChanges(); //Save changes at this point to assign IDs to tables AddItemPurchaseDetailToDatabase(replayData, db, fateGamePlayerDetailList, fatePlayerList); AddHeroStatLearnDetailToDatabase(replayData, db, fateGamePlayerDetailList, fatePlayerList); AddGodsHelpUseToDatabase(replayData, db, fateGamePlayerDetailList, fatePlayerList); AddAttributeLearnToDatabase(replayData, db, fateGamePlayerDetailList, fatePlayerList); AddCommandSealUseToDatabase(replayData, db, fateGamePlayerDetailList, fatePlayerList); db.SaveChanges(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); logger.Error(ex.ToString()); throw; } } } }
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 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; } }