/// <summary> /// Handles the ChatMessage AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void ChatMessageAGCEventHandler(object sender, ChatMessageAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "Chat Event received"); Game CurrentGame = GameServer.Games.GetGameByID(e.GameID); if (CurrentGame != null) { // Ignore all chats made by cons/drones if (!e.SpeakerName.StartsWith(".")) { string TargetName = ChatTargetHelper.CleanTargetName(e.RecipientName, e.ChatType); TagTrace.WriteLine(TraceLevel.Verbose, "Chat Event waiting to lock GameData..."); lock (CurrentGame.GetSyncRoot()) { CurrentGame.GameData.LogChatMessage(e.Time, e.SpeakerID, e.SpeakerName, e.RecipientID, TargetName, e.CommandID, e.VoiceID, e.Text); TagTrace.WriteLine(TraceLevel.Verbose, "Chat logged."); } } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling ChatMessage event: {0}", ex.Message); } }
/// <summary> /// Handles the LobbyLost AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void LobbyLostAGCEventHandler(object sender, LobbyLostAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "LobbyLost event received."); // Send a chat to all games informing that lobby was lost GameServer.SendChat("This server has disconnected from the lobby. New players will be unable to find this game until the server reconnects."); // Log lobby losts to all current games foreach (Game CurrentGame in GameServer.Games) { TagTrace.WriteLine(TraceLevel.Verbose, "LobbyLost Event waiting to lock Game {0}'s GameData...", CurrentGame.GameID); lock (CurrentGame.GetSyncRoot()) { CurrentGame.GameData.LogLobbyLost(e.Time); TagTrace.WriteLine(TraceLevel.Info, "LobbyLost event logged to game {0}.", CurrentGame.GameID); } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling LobbyLost event: {0}", ex.Message); } }
/// <summary> /// Handles the GameStarted AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void GameStartedAGCEventHandler(object sender, GameStartedAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "GameStarted event received from game {0}", e.GameID); Game CurrentGame = GameServer.Games.GetGameByID(e.GameID); if (CurrentGame != null) { TagTrace.WriteLine(TraceLevel.Verbose, "GameStarted Event waiting to lock GameData..."); lock (CurrentGame.GetSyncRoot()) { // Initialize game CurrentGame.Start(e.Time); // Log event CurrentGame.GameData.LogGameStarted(e.Time); TagTrace.WriteLine(TraceLevel.Verbose, "Game {0} Started.", e.GameID); } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling GameStarted event: {0}", ex.Message); } }
/// <summary> /// Handles the StationCreated AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void StationCreatedAGCEventHandler(object sender, StationCreatedAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "StationCreated event received."); // Get current game Game CurrentGame = null; string StationName = (e.StationName.Equals(string.Empty)) ? "Unknown Station" : e.StationName; CurrentGame = GameServer.Games.GetGameByID(e.GameID); if (CurrentGame != null) { lock (CurrentGame.GetSyncRoot()) { CurrentGame.GameData.LogStationCreated(e.Time, e.TeamID, e.TeamName, StationName); TagTrace.WriteLine(TraceLevel.Verbose, "{0} built a(n) {1}", e.TeamName, StationName); } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling StationBuilt event: {0}", ex.Message); TagTrace.WriteLine(TraceLevel.Error, "GameID: {0}", e.GameID); TagTrace.WriteLine(TraceLevel.Error, "TeamID: {0}", e.TeamID); TagTrace.WriteLine(TraceLevel.Error, "TeamName: {0}", (e.TeamName != null) ? e.TeamName : "NULL"); TagTrace.WriteLine(TraceLevel.Error, "StationName: {0}", (e.StationName != null) ? e.StationName : "NULL"); } }
/// <summary> /// Handles the PlayerLoggedIn AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void PlayerLoggedInAGCEventHandler(object sender, PlayerLoggedInAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "PlayerLoggedIn event received."); // Get game Game CurrentGame = GameServer.Games.GetGameByID(e.GameID); if (CurrentGame != null) { lock (CurrentGame.GetSyncRoot()) { // Add player to list GameServer.Players.AddPlayer(e.PlayerName, e.GameID, -1); CurrentGame.GameData.LogPlayerLoggedIn(e.Time, e.PlayerID, e.PlayerName); TagTrace.WriteLine(TraceLevel.Verbose, "{0} logged into {1}", e.PlayerName, CurrentGame.GameName); } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling PlayerLoggedIn event: {0}", ex.Message); } }
/// <summary> /// Attempts a reconnection with Allsrv /// </summary> /// <param name="state">Data asynchronously passed to this method</param> private static void AttemptReconnect(object state) { try { _retryCount++; if (_retryCount > _maxRetries) { TagTrace.WriteLine(TraceLevel.Info, "TAG has reached the maximum amount of Allsrv reconnect attempts. Shutting down..."); Stop(); if (TagServiceController.IsTagServiceStarted()) { TagServiceController.StopTagService(); } else { ShutdownTagEvent(null); } return; } TagTrace.WriteLine(TraceLevel.Verbose, "Attempting to reconnect to Allsrv..."); GameServer.Initialize(); // If it worked, kill the reconnect timer! TagTrace.WriteLine(TraceLevel.Info, "Allsrv reconnection successful."); Stop(); } catch (Exception) { TagTrace.WriteLine(TraceLevel.Verbose, "Allsrv reconnection failed. Will retry in {0} seconds", _interval); } }
/// <summary> /// Disconnects from allsrv, logs games that were in progress and shuts down TAG. /// </summary> public static void Stop() { TagTrace.WriteLine(TraceLevel.Info, "Shutting down..."); // De-initialize core (unhook events) AGCEventHandler.Uninitialize(); TagTrace.WriteLine(TraceLevel.Verbose, "Events unhooked."); TagTrace.WriteLine(TraceLevel.Info, "Shutting the logger down..."); // Shutdown the logger GameLogger.Uninitialize(); TagTrace.WriteLine(TraceLevel.Verbose, "Logger shut down."); TagTrace.WriteLine(TraceLevel.Info, "Disconnecting from Allsrv..."); // Disconnect from allsrv GameServer.Disconnect(); TagTrace.WriteLine(TraceLevel.Verbose, "Disconnected."); TagTrace.WriteLine(TraceLevel.Info, "Exiting..."); // Signal the "Main" thread to end processing if (_closeAppResetEvent != null) { _closeAppResetEvent.Set(); } // Allow the main thread to end, or force-close after 5s if (_mainThread != null) { _mainThread.Join(5000); } // Shutdown Tracing and Debugging TagTrace.Uninitialize(); TagTrace.Uninitialize(); }
/// <summary> /// Handles the GameOver AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void GameOverAGCEventHandler(object sender, GameOverAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "GameOver Event received from game {0}", e.GameID); Game CurrentGame = GameServer.Games.GetGameByID(e.GameID); if (CurrentGame != null) { // Prevent GameOver from continuing until GameEnded has completed CurrentGame.GetGameEndedMRE().WaitOne(); TagTrace.WriteLine(TraceLevel.Verbose, "GameOver Event waiting to lock GameData..."); lock (CurrentGame.GetSyncRoot()) { // Switch out the existing GameData for a new one GameData EndedGameData = CurrentGame.GameData; CurrentGame.GameData = new GameData(); // Queue posting ThreadPool.QueueUserWorkItem(new WaitCallback(PostGame), EndedGameData); TagTrace.WriteLine(TraceLevel.Verbose, "Game {0} is reset, and its data queued for posting", e.GameID); } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling GameOver event: {0}", ex.Message); } }
/// <summary> /// Handles the PlayerDropped AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void PlayerDroppedAGCEventHandler(object sender, PlayerDroppedAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "PlayerDropped event received."); // Grab the game Game CurrentGame = GameServer.Players.GetGameByPlayer(e.PlayerName); if (CurrentGame != null) { lock (CurrentGame.GetSyncRoot()) { // Get their current team int TeamNumber = GameServer.Players.GetTeamByPlayer(e.PlayerName); // Remove the player GameServer.Players.RemovePlayer(e.PlayerName); // Only log playerdrops if they're in a game in progress if (CurrentGame.InProgress && TeamNumber > -1) { CurrentGame.GameData.LogPlayerDropped(e.Time, e.PlayerName); TagTrace.WriteLine(TraceLevel.Verbose, "{0} Dropped", e.PlayerName); } } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling PlayerDropped event: {0}", ex.Message); TagTrace.WriteLine(TraceLevel.Error, "PlayerName: {1}", (e.PlayerName != null) ? e.PlayerName : "NULL"); } }
/// <summary> /// Logs a chat message with the specified parameters to this game's dataset /// </summary> /// <param name="time">The time this chat message occurred</param> /// <param name="speakerID">The ID of the speaker within Allsrv</param> /// <param name="speakerName">The name of the player or drone who sent this message</param> /// <param name="recipientID">The ID of the recipient within Allsrv</param> /// <param name="recipientName">The name of the recipient player or drone</param> /// <param name="commandID">The ID of the command associated with this chat message (if any)</param> /// <param name="voiceID">The ID of the voicechat associated with this chat message (if any)</param> /// <param name="text">The contents of the message</param> public void LogChatMessage(DateTime time, int speakerID, string speakerName, int recipientID, string recipientName, int commandID, int voiceID, string text) { string Speaker = (speakerName.Equals("Admin")) ? "HQ" : speakerName; string Text = (voiceID > -1) ? "Voice Chat: " + voiceID : text; _gameDataset.ChatLog.AddChatLogRow(GameRow, time, Speaker, recipientName, Text); if (speakerID == -1) { // Admin has spoken. Check for resign and draw calls... if (Speaker.Equals("HQ")) { string ResignString = "'s proposal to resign has "; int ResignIndex = -1; ResignIndex = Text.IndexOf(ResignString); if (ResignIndex > -1) { // A resign was typed! // Did it pass or fail? int ResignEventID = (int)HandledEventIDs.ResignFailed; // Fail by default if (Text.IndexOf("passed", ResignIndex) == ResignIndex + ResignString.Length) { ResignEventID = (int)HandledEventIDs.ResignPassed; // It passed! } string Proposer = Text.Substring(0, ResignIndex); string Votes = Text.Substring(ResignIndex + ResignString.Length + 9); _gameDataset.GameEvent.AddGameEventRow(GameRow, ResignEventID, time, GameRow.GameID, Proposer, -1, Votes, -1, string.Empty); TagTrace.WriteLine(TraceLevel.Verbose, "{0}'s Resign proposal logged.", Proposer); } // Check for draw string DrawString = "proposal to offer a draw has"; int DrawIndex = -1; DrawIndex = Text.IndexOf(DrawString); if (DrawIndex > -1) { // A draw was typed! // Did it pass or fail? int DrawEventID = (int)HandledEventIDs.DrawFailed; // Fail by default if (Text.IndexOf("passed", DrawIndex) == DrawIndex + DrawString.Length) { DrawEventID = (int)HandledEventIDs.DrawPassed; } string Proposer = Text.Substring(0, DrawIndex - 3); string Votes = Text.Substring(DrawIndex + DrawString.Length + 10); _gameDataset.GameEvent.AddGameEventRow(GameRow, DrawEventID, time, GameRow.GameID, Proposer, -1, Votes, -1, string.Empty); TagTrace.WriteLine(TraceLevel.Verbose, "{0}'s Draw proposal logged.", Proposer); } } } }
/// <summary> /// Checks to see if updates to TAG are available /// </summary> /// <returns>True if updates are available. False if TAG is up to date.</returns> public static bool UpdateAvailable() { bool Result = false; try { // Get tagfilelist.txt from server string FileListUrl = string.Concat(TAGUPDATEURL, "/", FILELIST); WebClient Client = new WebClient(); byte[] Filelist = Client.DownloadData(FileListUrl); // open tagfilelist.txt StreamReader FilelistReader = new StreamReader(new MemoryStream(Filelist)); while (true) { string Line = FilelistReader.ReadLine(); // Quit if we're at the end of the file if (Line == null) { break; } if (Line.Equals(string.Empty)) { continue; } if (Line.StartsWith("//")) { continue; } // parse filename and version int SpacePosition = Line.IndexOf(" "); string Filename = Line.Substring(0, SpacePosition); string Version = Line.Substring(SpacePosition + 1); // If we have an older one than specified... if (NeedsUpdating(Filename, Version)) { Result = true; break; } } FilelistReader.Close(); Client.Dispose(); } catch (Exception e) { TagTrace.WriteLine(TraceLevel.Error, "Error checking for updates: {0}", e.Message); } return(Result); }
/// <summary> /// Initializes the GameServer and reads the games from the server /// </summary> public static void Initialize() { _players = new Players(_server); // Start Allsrv if not already started if (!AllsrvConnector.IsRunning()) { AllsrvConnector.StartAllsrv(); } // Connect to Allsrv and retrieve the server object _connector = AllsrvConnector.Connect(); _server = _connector.Session.Server; TagTrace.WriteLine(TraceLevel.Info, "Connection to Allsrv established."); // Load Player list TagTrace.WriteLine(TraceLevel.Verbose, "Retrieving players..."); _players.Initialize(_server); TagTrace.WriteLine(TraceLevel.Info, "Playerlist loaded. {0} players online.", _players.Count); // Read the games TagTrace.WriteLine(TraceLevel.Verbose, "Reading Games..."); _games = new Games(); for (int i = 0; i < _server.Games.Count; i++) { object Index = (object)i; IAGCGame TempGame = _server.Games.get_Item(ref Index); Game NewGame = new Game(TempGame); _games.Add(NewGame); // Initialize game if in progress if (NewGame.InProgress) { // Get StartTime DateTime StartTime = DateTime.FromOADate(TempGame.GameParameters.TimeStart); // Initialize game NewGame.Start(StartTime); // Log event NewGame.GameData.LogGameStarted(StartTime); } } TagTrace.WriteLine(TraceLevel.Info, "Games read. {0} games online.", _games.Count); // Connect event handlers TagTrace.WriteLine(TraceLevel.Verbose, "Hooking up events..."); AGCEventHandler.Initialize(_connector); TagTrace.WriteLine(TraceLevel.Verbose, "Events connected."); }
/// <summary> /// Reads the config file and initializes tracing and logging /// </summary> public static void LoadConfiguration() { if (_config == null) { // Try to read the configuration try { _config = (TagConfig)ConfigurationSettings.GetConfig("TagConfig"); if (_config == null) { throw new ConfigurationException("Error parsing TagConfig section of config file"); } } catch (Exception e) { Console.WriteLine("Error loading Config file: {0}", e.Message); Console.WriteLine("Exiting..."); return; } // Make the configuration available to the CallsignHelper for ServerAdmin tags CallsignHelper.SetServerAdmins(_config.ServerAdmins); // Initialize tracing TagTrace.Initialize(_config.TraceLevel, _config.TracePath, _config.TraceArchiveDir, _config.TraceConsole, null); TagTrace.WriteLine(TraceLevel.Info, "TAG Build {0} is starting...", Build.ToString()); // Initialize reconnect timer ReconnectTimer.Initialize(_config.ReconnectInterval, _config.MaxRetries); ReconnectTimer.ShutdownTagEvent += new AsyncCallback(ReconnectShutdown); // Configure ASGS as necessary if (_config.AsgsUrl != null) { AsgsConnector.Initialize(_config.AsgsUrl, _config.PostTimeout); } if (_config.CssUrl != null) { CssConnector.Initialize(_config.CssUrl, _config.PostTimeout, _config.UseCss); } TagTrace.WriteLine(TraceLevel.Verbose, "Initializing logging..."); GameLogger.Initialize(_config.XmlPath); TagTrace.WriteLine(TraceLevel.Info, "Configuration Loaded."); } }
/// <summary> /// Checks for updates to TAG /// </summary> /// <param name="info">Additional info used by the Update timer (null)</param> public static void CheckForUpdates(object info) { TagTrace.WriteLine(TraceLevel.Verbose, "Checking for updates..."); if (TagUpdate.IsAbleToUpdate()) { if (TagUpdate.UpdateAvailable()) { TagTrace.WriteLine(TraceLevel.Info, "An update to TAG is available. Performing update..."); TagUpdate.InitiateUpdate(); } } else { TagTrace.WriteLine(TraceLevel.Verbose, "TAG can't update now. Games in progress."); } }
/// <summary> /// Posts the specified game to ASGS, and logs it /// </summary> /// <param name="data">The data to post</param> public static void PostGame(object data) { // Unpackage and prepare data GameData Data = (GameData)data; GameDataset Set = Data.GetDataset(); string Xml = Set.GetXml(); string CompressedXml = Compression.Compress(Xml); TagTrace.WriteLine(TraceLevel.Verbose, "Game data ready to post. Sending to stats server..."); TagTrace.WriteLine(TraceLevel.Verbose, "Use CSS: " + CssConnector.UseCss + ", CSS Url: " + CssConnector.CssUrl); // Post game to ASGS string PostMessage; int GameID; if (CssConnector.UseCss == true) { GameID = CssConnector.PostGame(CompressedXml, out PostMessage); } else { GameID = AsgsConnector.PostGame(CompressedXml, out PostMessage); } TagTrace.WriteLine(TraceLevel.Info, "Game #{0} Posted: {1}", GameID, PostMessage); // Post message to all servers GameServer.SendChat(PostMessage); // Log games as configured XmlLogger.LogGame(Data, GameID); // Get rid of this GameData since we no longer need it Data.Dispose(); TagTrace.WriteLine(TraceLevel.Verbose, "Game data disposed"); // If no games are in progress... if (TagUpdate.IsAbleToUpdate()) { TagTrace.WriteLine(TraceLevel.Verbose, "TAG is able to update. No games are in progress"); // And an update is available... if (TagUpdate.UpdateAvailable()) { TagUpdate.InitiateUpdate(); // Update! } } }
/// <summary> /// Handles the ShipKilled AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void ShipKilledAGCEventHandler(object sender, ShipKilledAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "ShipKilled event received."); Game CurrentGame = GameServer.Games.GetGameByID(e.GameID); if (CurrentGame != null) { string KillerName = e.KillerName; // If something unknown killed this ship if (e.KillerID == -1 && e.KillerName.Equals(string.Empty)) { KillerName = "Caltrop/Alephres/Con-bomb"; // Ignore cons building if (e.KilledName.StartsWith(".")) { TagTrace.WriteLine(TraceLevel.Verbose, "Ignoring ShipKilled because {0} built", e.KilledName); return; } } // If a lifepod wasn't killed... if (e.IsLifepod == 0) { lock (CurrentGame.GetSyncRoot()) { CurrentGame.GameData.LogShipKilled(e.Time, e.KilledID, e.KilledName, e.KilledPosition, e.KillerID, KillerName, e.KillerPosition, e.Amount, e.IsLifepod); // Spit the kill out if we're debugging them if (CurrentGame.DebugKills) { CurrentGame.SendChat(KillerName + " killed " + e.KilledName); } TagTrace.WriteLine(TraceLevel.Verbose, "{0} killed {1} in {2}", KillerName, e.KilledName, CurrentGame.GameName); } } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling ShipKilled event: {0}", ex.Message); } }
/// <summary> /// Handles the GameCreated AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void GameCreatedAGCEventHandler(object sender, GameCreatedAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "GameCreated event received."); // Create the new game Game CurrentGame = GameServer.LoadGame(e.GameID); if (CurrentGame == null) { throw new ApplicationException("Unknown error while creating game!"); } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling GameCreated event: {0}", ex.Message); } }
/// <summary> /// Logs a player leaving a team to this game's dataset /// </summary> /// <param name="time">The time this player left the team</param> /// <param name="playerID">The ID of the player leaving the team</param> /// <param name="playerName">The name of the player leaving the team</param> /// <param name="teamID">The ID of the team that was left</param> /// <param name="teamName">The name of the team that was left</param> /// <param name="booterName">The name of the player who ejected this player from his team, or NULL if the player left on their own</param> public void LogPlayerLeftTeam(DateTime time, int playerID, string playerName, int teamID, string teamName, string booterName) { // Remove their gametime GameDataset.TeamMemberRow[] TeamMembers = (GameDataset.TeamMemberRow[])_gameDataset.TeamMember.Select("Callsign = '" + playerName + "'"); if (TeamMembers.Length > 0) { TeamMembers[0].LeaveTime = time; } else { TagTrace.WriteLine(TraceLevel.Error, "Error logging PlayerLeftTeam: Couldn't find TeamMember {0}", playerName); } // Log event _gameDataset.GameEvent.AddGameEventRow(GameRow, (int)HandledEventIDs.LeftTeam, time, playerID, playerName, teamID, teamName, -1, booterName); }
/// <summary> /// Posts the specified game to the ASGS database /// </summary> /// <param name="gamedata">The compressed xml of a TagDataset</param> /// <param name="message">A reply message from the ASGS server</param> /// <returns>Positive if successful (GameID) /// -1 if error occurred</returns> public static int PostGame(string gamedata, out string message) { int Result = -1; message = "An error occurred while posting stats to ASGS."; try { Result = _services.PostGameStatistics(gamedata, out message); } catch (Exception e) { message = "Error posting game: " + e.Message; TagTrace.WriteLine(TraceLevel.Error, message); } return(Result); }
/// <summary> /// Starts Allsrv in desktop or service mode as installed /// </summary> public static void StartAllsrv() { try { // If Allsrv is a stopped service, start it! if (TagServiceController.AllsrvService != null) { if (TagServiceController.IsAllsrvServiceStarted() == false) { TagServiceController.StartAllsrvService(); TagTrace.WriteLine(TraceLevel.Info, "Allsrv Service started."); } } else { // Start Allsrv in console mode string ServerEXEPath = null; // Get registry key RegistryKey ServerKey = Registry.LocalMachine.OpenSubKey(ALLEGIANCESERVERREGKEY); if (ServerKey != null) { ServerEXEPath = ServerKey.GetValue("EXE Path").ToString(); ServerKey.Close(); } else { throw new ApplicationException("Could not find Allsrv.exe"); } // start executable if (ServerEXEPath != null) { Process.Start(ServerEXEPath); TagTrace.WriteLine(TraceLevel.Info, "Allsrv started in console mode."); } } Thread.Sleep(1500); // Wait a second for Allsrv to start } catch (Exception e) { TagTrace.WriteLine(TraceLevel.Error, "Error starting Allsrv: {0}", e.Message); } }
/// <summary> /// Handles the PlayerLeftTeam AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void PlayerLeftTeamAGCEventHandler(object sender, PlayerLeftTeamAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "PlayerLeftTeam event received."); // Get game Game CurrentGame = GameServer.Games.GetGameByID(e.GameID); if (CurrentGame != null) { lock (CurrentGame.GetSyncRoot()) { // Update team GameServer.Players.UpdateTeam(e.PlayerName, -1); // Ignore leaving NOAT (unless booted) if ((e.TeamID != -2 && e.TeamID != -131060) || !e.BooterName.Equals(string.Empty)) { // Only log LeaveTeams if in progress (or boots) if (CurrentGame.InProgress || !e.BooterName.Equals(string.Empty)) { CurrentGame.GameData.LogPlayerLeftTeam(e.Time, e.PlayerID, e.PlayerName, e.TeamID, e.TeamName, e.BooterName); TagTrace.WriteLine(TraceLevel.Verbose, "{0} left {1}", e.PlayerName, e.TeamName); // Inform everyone of team ban if (!e.BooterName.Equals(string.Empty)) { CurrentGame.SendChat(string.Concat(e.BooterName, " has booted ", e.PlayerName, " from team ", e.TeamName, ".")); } } } } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling PlayerLeft event: {0}", ex.Message); TagTrace.WriteLine(TraceLevel.Error, "GameID: {0}, PlayerID: {1}, PlayerName: {2}, TeamID: {3}, TeamName: {4}", e.GameID, e.PlayerID, (e.PlayerName != null) ? e.PlayerName : "NULL", e.TeamID, (e.TeamName != null) ? e.TeamName : "NULL"); } }
/// <summary> /// Handles the Terminate AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void TerminateAGCEventHandler(object sender, TerminateAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "Terminate event received."); // De-initialize core (unhook events) AGCEventHandler.Uninitialize(); // Kill all game info GameServer.Disconnect(); // Start reconnect timer ReconnectTimer.Start(); } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling Terminate event: {0}", ex.Message); } }
/// <summary> /// Performs the TAG update /// </summary> /// <param name="sender">The object that raised this event</param> /// <param name="e">The event arguments</param> public static void PerformUpdate(object sender, EventArgs e) { string UpdaterPath = Application.StartupPath + "\\" + UPDATERNAME; // Launch the updater Process.Start(new ProcessStartInfo(UpdaterPath)); TagTrace.WriteLine(TraceLevel.Verbose, "Updater application launched"); if (TagServiceController.IsTagServiceStarted()) { // TAG is a service. Stop it! TagServiceController.StopTagService(); TagTrace.WriteLine(TraceLevel.Verbose, "TAG Service stopped"); } else { // TAG is console. Stop it! Tag.Stop(); } }
/// <summary> /// Handles the LobbyDisconnecting AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void LobbyDisconnectingAGCEventHandler(object sender, LobbyDisconnectingAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "LobbyDisconnecting event received."); // Log lobby disconnectings to all current games foreach (Game CurrentGame in GameServer.Games) { TagTrace.WriteLine(TraceLevel.Verbose, "LobbyDisconnecting Event waiting to lock Game {0}'s GameData...", CurrentGame.GameID); lock (CurrentGame.GetSyncRoot()) { CurrentGame.GameData.LogLobbyDisconnecting(e.Time, e.LobbyIP); TagTrace.WriteLine(TraceLevel.Info, "LobbyDisconnecting event logged to game {0}.", CurrentGame.GameID); } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling LobbyDisconnecting event: {0}", ex.Message); } }
/// <summary> /// Handles the StationDestroyed AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void StationDestroyedAGCEventHandler(object sender, StationDestroyedAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "StationDestroyed event received."); // Get current game Game CurrentGame = null; string StationName = "Unknown Station"; if (e.GameID == -1) { CurrentGame = GameServer.Players.GetGameByPlayer(e.KillerName); } else { CurrentGame = GameServer.Games.GetGameByID(e.GameID); StationName = e.StationName; } // Log the event if we know the game if (CurrentGame != null) { lock (CurrentGame.GetSyncRoot()) { CurrentGame.GameData.LogStationDestroyed(e.Time, e.KillerID, e.KillerName, e.TeamID, e.TeamName, StationName); TagTrace.WriteLine(TraceLevel.Verbose, "{0} destroyed {1}'s {2}", e.KillerName, e.TeamName, StationName); } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling StationDestroyed event: {0}", ex.Message); TagTrace.WriteLine(TraceLevel.Error, "GameID: {0}, TeamID: {1}, TeamName: {2}, StationName: {3}", e.GameID, e.TeamID, (e.TeamName != null) ? e.TeamName : "NULL", (e.StationName != null) ? e.StationName : "NULL"); } }
/// <summary> /// Handles the PlayerLoggedOut AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void PlayerLoggedOutAGCEventHandler(object sender, PlayerLoggedOutAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "PlayerLoggedOut event received."); // Get current game Game CurrentGame = GameServer.Games.GetGameByID(e.GameID); if (CurrentGame != null) { lock (CurrentGame.GetSyncRoot()) { // Get their current team int TeamNumber = GameServer.Players.GetTeamByPlayer(e.PlayerName); // Remove player from player list GameServer.Players.RemovePlayer(e.PlayerName); // Only log LoggedOut messages for players in a game in progress. Always log boots if ((CurrentGame.InProgress && TeamNumber > -1) || !e.BooterName.Equals(string.Empty)) { CurrentGame.GameData.LogPlayerLoggedOut(e.Time, e.PlayerID, e.PlayerName, e.BooterName); TagTrace.WriteLine(TraceLevel.Verbose, "{0} logged out of {1}", e.PlayerName, e.GameName); // Inform everyone of lobby ban if (!e.BooterName.Equals(string.Empty)) { CurrentGame.SendChat(string.Concat(e.BooterName, " has lobby-banned ", e.PlayerName, ".")); } } } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling PlayerLoggedOut event: {0}", ex.Message); } }
/// <summary> /// Archives the TraceFile according to the archive settings /// </summary> private static void ArchiveTracefile() { // If we're not logging and not archiving, quit now if (_archiveDir == null || _tracePath == null) { return; } try { // Get the path of the new file string NewFileName = string.Format("Trace_{0}.{1:00}.{2:00}.txt", _lastTraceTime.Year, _lastTraceTime.Month, _lastTraceTime.Day); string Destination = string.Concat(_archiveDir, "\\", NewFileName); // Create the new folder if needed if (!Directory.Exists(_archiveDir)) { Directory.CreateDirectory(_archiveDir); } // Remove and close the listener so the file is not held TraceListener Listener = Trace.Listeners["TAG Trace File"]; if (Listener != null) { Trace.Listeners.Remove(Listener); Listener.Close(); } // Move the file File.Move(_tracePath, Destination); // Re-add new listener to continue tracing Trace.Listeners.Add(new TextWriterTraceListener(_tracePath, "TAG Trace File")); } catch (Exception e) { TagTrace.WriteLine(TraceLevel.Error, "Error archiving trace file: {0}", e.Message); } }
/// <summary> /// Retrieves the team's index that has the specified ID. Returns -1 for NOAT /// </summary> /// <param name="gameID">The ID of the game</param> /// <param name="teamID">The ID of the team whose index should be retrieved</param> /// <returns>The index of the team with the specified ID</returns> private int GetTeamIndexFromTeamID(int gameID, int teamID) { int Result = -1; try { // If they're NOAT, return -1 if (teamID != NOATID && teamID != -2) { // Grab the game Game CurrentGame = GameServer.Games.GetGameByID(gameID); // Grab the index Result = CurrentGame.GetTeamIndex(teamID); } } catch (Exception e) { TagTrace.WriteLine(TraceLevel.Error, "Error retrieving Index from GameID:TeamID({0}:{1}): {2}", gameID, teamID, e.Message); } return(Result); }
/// <summary> /// Default Constructor /// </summary> private AllsrvConnector() { IAdminSession IAS = null; // Call into AGCLib to get the AdminSession int ErrorCode = GetAdminSession(out IAS); if (ErrorCode != 0) { throw new ApplicationException("Attempt to retrieve Admin Session failed. Error " + ErrorCode.ToString()); } _session = IAS; TagTrace.WriteLine(TraceLevel.Verbose, "Admin Session retrieved from Allsrv."); // Hook into events UCOMIConnectionPointContainer uCOMIConnectionPointContainer = (UCOMIConnectionPointContainer)_session; uCOMIConnectionPointContainer.FindConnectionPoint(ref _sessionGuid, out _icp); _cookie = 0; _icp.Advise(this, out _cookie); _session.ActivateAllEvents(); }
/// <summary> /// Handles the PlayerJoinedTeam AGCEvent /// </summary> /// <param name="sender">The object firing the event</param> /// <param name="e">The arguments of the event</param> public static void PlayerJoinedTeamAGCEventHandler(object sender, PlayerJoinedTeamAGCEventArgs e) { try { TagTrace.WriteLine(TraceLevel.Verbose, "PlayerJoinedTeam event received."); // Get game Game CurrentGame = GameServer.Games.GetGameByID(e.GameID); if (CurrentGame != null) { lock (CurrentGame.GetSyncRoot()) { // Update team GameServer.Players.UpdateTeam(e.PlayerName, e.TeamID); // Ignore joining NOAT if (e.TeamID != -2 && e.TeamID != -131060) { // Only log joiners when game is in progress if (CurrentGame.InProgress) { CurrentGame.GameData.LogPlayerJoinedTeam(e.Time, e.PlayerID, e.PlayerName, e.TeamID, e.TeamName); TagTrace.WriteLine(TraceLevel.Verbose, "{0} joined {1}", e.PlayerName, e.TeamName); } } } } } catch (Exception ex) { TagTrace.WriteLine(TraceLevel.Error, "Error handling PlayerJoined event: {0}", ex.Message); TagTrace.WriteLine(TraceLevel.Error, "GameID: {0}, PlayerID: {1}, PlayerName: {2}, TeamID: {3}, TeamName: {4}", e.GameID, e.PlayerID, (e.PlayerName != null) ? e.PlayerName : "NULL", e.TeamID, (e.TeamName != null) ? e.TeamName : "NULL"); } }