private void SaveTeams(Allegiance.CommunitySecuritySystem.DataAccess.CSSDataContext db, DataAccess.CSSStatsDataContext statsDB, Allegiance.CommunitySecuritySystem.Server.Data.GameDataset.GameRow gameRow, int gameIdentID) { foreach (Data.GameDataset.TeamRow teamRow in gameRow.GetTeamRows()) { var commanderAlias = DataAccess.Alias.GetAliasByCallsign(db, teamRow.Commander); int commanderLoginID = 0; if (commanderAlias != null) { commanderLoginID = commanderAlias.Login.Id; } DataAccess.GameTeam team = new Allegiance.CommunitySecuritySystem.DataAccess.GameTeam() { GameID = gameIdentID, GameTeamCommander = TrimString(teamRow.Commander, 49), GameTeamCommanderLoginID = commanderLoginID, GameTeamExpansion = teamRow.ResearchedExpansion, GameTeamFaction = teamRow.Faction, GameTeamID = teamRow.TeamID, GameTeamName = TrimString(teamRow.TeamName, 49), GameTeamNumber = teamRow.TeamNumber, GameTeamShipyard = teamRow.ResearchedShipyard, GameTeamStarbase = teamRow.ResearchedStarbase, GameTeamSupremacy = teamRow.ResearchedSupremacy, GameTeamTactical = teamRow.ResearchedTactical, GameTeamWinner = teamRow.Won }; statsDB.GameTeams.InsertOnSubmit(team); statsDB.SubmitChanges(); foreach (Data.GameDataset.TeamMemberRow teamMemberRow in teamRow.GetTeamMemberRows()) { var teamMemberAlias = DataAccess.Alias.GetAliasByCallsign(db, teamMemberRow.Callsign); int loginID = 0; if (teamMemberAlias != null) { loginID = teamMemberAlias.Login.Id; } DataAccess.GameTeamMember teamMember = new Allegiance.CommunitySecuritySystem.DataAccess.GameTeamMember() { GameTeamID = team.GameTeamIdentID, GameTeamMemberCallsign = TrimString(DataAccess.Alias.GetCallsignFromStringWithTokensAndTags(db, teamMemberRow.Callsign), 49), GameTeamMemberDuration = teamMemberRow.Duration, GameTeamMemberLoginID = loginID }; statsDB.GameTeamMembers.InsertOnSubmit(teamMember); } statsDB.SubmitChanges(); } }
public int SaveGameData(string gameData, bool isCompressedAndBase64Encoded, out string message) { try { Data.GameDataset gameDataset = new Data.GameDataset(); //gameDataset.EnforceConstraints = false; //gameDataset. if (isCompressedAndBase64Encoded == true) { byte[] binaryGameData = Convert.FromBase64String(gameData); MemoryStream memoryStream = new MemoryStream(binaryGameData); ICSharpCode.SharpZipLib.GZip.GZipInputStream zipStream = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(memoryStream); StreamReader streamReader = new StreamReader(zipStream, System.Text.Encoding.Unicode); //string gameDataXml = streamReader.ReadToEnd(); gameDataset.ReadXml(streamReader, System.Data.XmlReadMode.IgnoreSchema); } else { //gameDataset.ReadXml(new StringReader(gameData)); gameDataset.ReadXml(new StringReader(gameData), System.Data.XmlReadMode.IgnoreSchema); } if (String.IsNullOrEmpty(Settings.Default.TagLastGameDataXmlFileLogLocation) == false) { File.WriteAllText(Path.Combine(Settings.Default.TagLastGameDataXmlFileLogLocation, Guid.NewGuid().ToString() + ".xml"), gameDataset.GetXml()); } string currentIPAddress; if (OperationContext.Current != null) { //http://nayyeri.net/detect-client-ip-in-wcf-3-5 OperationContext context = OperationContext.Current; MessageProperties messageProperties = context.IncomingMessageProperties; RemoteEndpointMessageProperty endpointProperty = (RemoteEndpointMessageProperty)messageProperties[RemoteEndpointMessageProperty.Name]; currentIPAddress = endpointProperty.Address; } else { currentIPAddress = "127.0.0.1"; // Supports unit tests. } int gameID = 0; using (DataAccess.CSSDataContext db = new DataAccess.CSSDataContext()) { using (DataAccess.CSSStatsDataContext statsDB = new DataAccess.CSSStatsDataContext()) { var gameServer = statsDB.GameServers.FirstOrDefault(p => p.GameServerIPs.Where(r => r.IPAddress == currentIPAddress).Count() > 0); if (gameServer == null) { throw new Exception("You may not upload data from this address: " + currentIPAddress); } try { foreach (Data.GameDataset.GameRow gameRow in gameDataset.Game) { gameID = SaveGame(db, statsDB, gameServer, gameRow); } } catch (Exception ex) { if (String.IsNullOrEmpty(Settings.Default.TagExceptionLogFileName) == false) { File.AppendAllText(Settings.Default.TagExceptionLogFileName, DateTime.Now.ToString() + ": " + ex.ToString() + "\n\n\n"); } throw; } statsDB.SubmitChanges(); db.SubmitChanges(); } } // Update AllegSkill rank. AllegSkill.Calculator.UpdateAllegSkillForGame(gameID); // Update Prestige Rank. using (DataAccess.CSSStatsDataContext statsDB = new DataAccess.CSSStatsDataContext()) { var game = statsDB.Games.FirstOrDefault(p => p.GameIdentID == gameID); if (game == null) { Error.Write(new Exception("Tag.SaveGameData(): Couldn't get game for GameID: " + gameID)); } else { PrestigeRankCalculator psc = new PrestigeRankCalculator(); psc.Calculate(statsDB, game); } } message = "Game saved."; return(gameID); } catch (Exception ex) { message = ex.ToString(); return(-1); } }
private void SaveGameEvents(Allegiance.CommunitySecuritySystem.DataAccess.CSSDataContext db, DataAccess.CSSStatsDataContext statsDB, Allegiance.CommunitySecuritySystem.Server.Data.GameDataset.GameRow gameRow, int gameIdentID) { foreach (Data.GameDataset.GameEventRow gameEventRow in gameRow.GetGameEventRows()) { int performerLoginID = 0; int targetLoginID = 0; try { var performerAlias = DataAccess.Alias.GetAliasByCallsign(db, gameEventRow.PerformerName); if (performerAlias != null) { performerLoginID = performerAlias.Login.Id; } var targetAlias = DataAccess.Alias.GetAliasByCallsign(db, gameEventRow.TargetName); if (targetAlias != null) { targetLoginID = targetAlias.Login.Id; } DataAccess.GameEvent gameEvent = new DataAccess.GameEvent() { GameEventTime = gameEventRow.EventTime, GameID = gameIdentID, GameEventIndirectID = gameEventRow.IndirectID, GameEventIndirectName = TrimString(DataAccess.Alias.GetCallsignFromStringWithTokensAndTags(db, gameEventRow.IndirectName), 254), GameEventPerformerID = gameEventRow.PerformerID, GameEventPerformerName = TrimString(DataAccess.Alias.GetCallsignFromStringWithTokensAndTags(db, gameEventRow.PerformerName), 254), GameEventPerformerLoginID = performerLoginID, GameEventTargetID = gameEventRow.TargetID, GameEventTargetName = TrimString(DataAccess.Alias.GetCallsignFromStringWithTokensAndTags(db, gameEventRow.TargetName), 254), GameEventTargetLoginID = targetLoginID, EventID = gameEventRow.EventID }; statsDB.GameEvents.InsertOnSubmit(gameEvent); statsDB.SubmitChanges(); } catch (Exception ex) { string errorDetails = String.Format(@"SaveGameEvents(): Insert error! gameIdentID = {0}, gameEventRow.EventID = {1}, gameEventRow.EventTime = {2}, gameEventRow.GameID = {3}, gameEventRow.IndirectID = {4}, gameEventRow.IndirectName = {5}, gameEventRow.PerformerID = {6}, gameEventRow.PerformerName = {7}, gameEventRow.TargetID = {8}, gameEventRow.TargetName = {9}, performerLoginID = {10}, targetLoginID = {11}, DataAccess.Alias.GetCallsignFromStringWithTokensAndTags(db, gameEventRow.IndirectName) = {12}, DataAccess.Alias.GetCallsignFromStringWithTokensAndTags(db, gameEventRow.PerformerName) = {13}, DataAccess.Alias.GetCallsignFromStringWithTokensAndTags(db, gameEventRow.TargetName) = {14} =================================================== {15} ", gameIdentID, gameEventRow.EventID, gameEventRow.EventTime, gameEventRow.GameID, gameEventRow.IndirectID, gameEventRow.IndirectName, gameEventRow.PerformerID, gameEventRow.PerformerName, gameEventRow.TargetID, gameEventRow.TargetName, performerLoginID, targetLoginID, DataAccess.Alias.GetCallsignFromStringWithTokensAndTags(db, gameEventRow.IndirectName), DataAccess.Alias.GetCallsignFromStringWithTokensAndTags(db, gameEventRow.PerformerName), DataAccess.Alias.GetCallsignFromStringWithTokensAndTags(db, gameEventRow.TargetName), ex.ToString()); if (String.IsNullOrEmpty(Settings.Default.TagExceptionLogFileName) == false) { File.AppendAllText(Settings.Default.TagExceptionLogFileName, DateTime.Now.ToString() + ": " + errorDetails + "\n\n\n"); } } } }
private int SaveGame(DataAccess.CSSDataContext db, DataAccess.CSSStatsDataContext statsDB, DataAccess.GameServer gameServer, Data.GameDataset.GameRow gameRow) { DataAccess.Game game = new Allegiance.CommunitySecuritySystem.DataAccess.Game() { GameDefections = gameRow.AllowDefections, GameDevelopments = gameRow.AllowDevelopments, GameShipyards = gameRow.AllowShipyards, GameConquest = gameRow.Conquest, GameCore = TrimString(gameRow.CoreFile, 50), GameDeathMatch = gameRow.DeathMatch, GameDeathmatchGoal = gameRow.DeathmatchGoal, GameEndTime = gameRow.EndTime, GameFriendlyFire = gameRow.FriendlyFire, GameName = TrimString(gameRow.GameName, 254), GameInvulStations = gameRow.InvulnerableStations, GameMap = TrimString(gameRow.MapName, 49), GameMaxImbalance = gameRow.MaxImbalance, GameResources = gameRow.Resources, GameRevealMap = gameRow.RevealMap, GameSquadGame = gameRow.SquadGame, GameStartingMoney = gameRow.StartingMoney, GameStartTime = gameRow.StartTime, GameStatsCount = gameRow.StatsCount, GameTotalMoney = gameRow.TotalMoney, GameID = gameRow.GameID, GameServer = gameServer.GameServerID }; statsDB.Games.InsertOnSubmit(game); try { statsDB.SubmitChanges(); } catch (Exception ex) { string dbLengthErrors = Utilities.LinqErrorDetector.AnalyzeDBChanges(statsDB); throw new Exception("CSSStats[games]: DB Error, Linq Length Analysis: " + dbLengthErrors + "\r\n", ex); } SaveGameEvents(db, statsDB, gameRow, game.GameIdentID); SaveTeams(db, statsDB, gameRow, game.GameIdentID); SaveChatLog(db, statsDB, gameRow, game.GameIdentID); try { statsDB.SubmitChanges(); } catch (Exception ex) { string dbLengthErrors = Utilities.LinqErrorDetector.AnalyzeDBChanges(statsDB); throw new Exception("CSSStats[game data]: DB Error, Linq Length Analysis: " + dbLengthErrors + "\r\n", ex); } try { db.SubmitChanges(); } catch (Exception ex) { string dbLengthErrors = Utilities.LinqErrorDetector.AnalyzeDBChanges(db); throw new Exception("CSS DB Error, Linq Length Analysis: " + dbLengthErrors + "\r\n", ex); } statsDB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, game); UpdateLeaderboard(game.GameIdentID); UpdateFactionStats(game.GameIdentID); UpdateMetrics(game.GameIdentID); statsDB.SubmitChanges(); db.SubmitChanges(); return(game.GameIdentID); }
public int SaveGameData(string gameData, bool isCompressedAndBase64Encoded, out string message) { try { Data.GameDataset gameDataset = new Data.GameDataset(); //gameDataset.EnforceConstraints = false; //gameDataset. if (isCompressedAndBase64Encoded == true) { byte[] binaryGameData = Convert.FromBase64String(gameData); MemoryStream memoryStream = new MemoryStream(binaryGameData); ICSharpCode.SharpZipLib.GZip.GZipInputStream zipStream = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(memoryStream); StreamReader streamReader = new StreamReader(zipStream, System.Text.Encoding.Unicode); //string gameDataXml = streamReader.ReadToEnd(); gameDataset.ReadXml(streamReader, System.Data.XmlReadMode.IgnoreSchema); } else { //gameDataset.ReadXml(new StringReader(gameData)); gameDataset.ReadXml(new StringReader(gameData), System.Data.XmlReadMode.IgnoreSchema); } if (String.IsNullOrEmpty(Settings.Default.TagLastGameDataXmlFileLogLocation) == false) File.WriteAllText(Path.Combine(Settings.Default.TagLastGameDataXmlFileLogLocation, Guid.NewGuid().ToString() + ".xml"), gameDataset.GetXml()); string currentIPAddress; if (OperationContext.Current != null) { //http://nayyeri.net/detect-client-ip-in-wcf-3-5 OperationContext context = OperationContext.Current; MessageProperties messageProperties = context.IncomingMessageProperties; RemoteEndpointMessageProperty endpointProperty = (RemoteEndpointMessageProperty)messageProperties[RemoteEndpointMessageProperty.Name]; currentIPAddress = endpointProperty.Address; } else currentIPAddress = "127.0.0.1"; // Supports unit tests. int gameID = 0; using (DataAccess.CSSDataContext db = new DataAccess.CSSDataContext()) { using (DataAccess.CSSStatsDataContext statsDB = new DataAccess.CSSStatsDataContext()) { var gameServer = statsDB.GameServers.FirstOrDefault(p => p.GameServerIPs.Where(r => r.IPAddress == currentIPAddress).Count() > 0); if (gameServer == null) throw new Exception("You may not upload data from this address: " + currentIPAddress); try { foreach (Data.GameDataset.GameRow gameRow in gameDataset.Game) gameID = SaveGame(db, statsDB, gameServer, gameRow); } catch (Exception ex) { if (String.IsNullOrEmpty(Settings.Default.TagExceptionLogFileName) == false) File.AppendAllText(Settings.Default.TagExceptionLogFileName, DateTime.Now.ToString() + ": " + ex.ToString() + "\n\n\n"); throw; } statsDB.SubmitChanges(); db.SubmitChanges(); } } // Update AllegSkill rank. AllegSkill.Calculator.UpdateAllegSkillForGame(gameID); // Update Prestige Rank. using (DataAccess.CSSStatsDataContext statsDB = new DataAccess.CSSStatsDataContext()) { var game = statsDB.Games.FirstOrDefault(p => p.GameIdentID == gameID); if (game == null) { Error.Write(new Exception("Tag.SaveGameData(): Couldn't get game for GameID: " + gameID)); } else { PrestigeRankCalculator psc = new PrestigeRankCalculator(); psc.Calculate(statsDB, game); } } message = "Game saved."; return gameID; } catch (Exception ex) { message = ex.ToString(); return -1; } }