Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- // Needed for timers
- using System.Timers;
- using BattleCore;
- using BattleCore.Events;
- using BattleCorePsyOps;
- namespace BaseDuel
- {
- // Add the attribute and the base class
- [Behavior("DevaMain", "false", "0.01", "PsyOps", "Main module to control deva.(rewrite)")]
- public class Main: BotEventListener
- {
- public Main()
- {
- // Register Events / Regions / and Commands here // commands in .command format too
- RegisterCommand("!reloadbd",Request_ReloadModule); RegisterCommand(".reloadbd", Request_ReloadModule);
- RegisterCommand("!bdplayerlist", Request_PlayerList); RegisterCommand(".bdplayerlist", Request_PlayerList);
- RegisterCommand("!bdcfg", Request_Cfg); RegisterCommand(".bdcfg", Request_Cfg);
- //debug stuff
- RegisterCommand("!test1", Test1); RegisterCommand(".test2", Test2);
- RegisterCommand("!test2", Test2); RegisterCommand(".test2", Test2);
- RegisterTimedEvent("LoadTimer", 1000, LoadTimer);
- }
- public ShortChat msg = new ShortChat(); // Custom module to help with making chat messages
- private bool m_AutoLoad = true; // Probably just for debug - autoloads cfgs n stuff
- private string m_BotName = null; // Holds Botname
- private string m_Arena; // Name of arena Bot is in - do i need this ?!?!?!
- private bool m_Initialized = false; // If bot is initialized
- private Timer m_DevaTimer; // Main Game timer
- private double m_DevaTimerInterval = 5; // in milliseconds
- private GameState m_GameState; // Game state duh
- private DateTime m_GameStartTimeStamp; // Timestamp for game start
- private double m_GameStartTimer = 30; // Seconds
- private BaseManager m_BaseManager; // Base manager
- private DateTime m_SafeGameTimeStamp = DateTime.Now;
- private double m_SafeGameTime = 25; // milliseconds
- private SpecManager m_Spec; // Controls where bot specs
- private BillerCommands m_Biller; // Module to make sending biller messages safer - avoid flooding
- private Configuration m_Config; // Grabs info from cfg and loads to memory
- private List<String> m_SpectatorList; // Player Lists
- private List<String> m_LobbyList; // <--------------->
- //----------------------------------------------------------------------//
- // Chat Commands //
- //----------------------------------------------------------------------//
- public void Request_ReloadModule(ChatEvent c)
- {
- if (c.ModLevel < ModLevels.Sysop) return; // Only allow for sysops
- LoadConfiguration();
- }
- public void Request_PlayerList(ChatEvent c)
- {
- Game(msg.pm(c.PlayerName, "-----------------<[ Spectator List ]>-----------------"));
- for (int i = 0; i < m_SpectatorList.Count; i+= 1)
- Game(msg.pm(c.PlayerName, "[ "+i+" ] " + m_SpectatorList[i]));
- Game(msg.pm(c.PlayerName, "-----------------<[ Lobby List ]>-----------------"));
- for (int i = 0; i < m_LobbyList.Count; i += 1)
- Game(msg.pm(c.PlayerName, "[ " + i + " ] " + m_LobbyList[i]));
- }
- public void Request_Cfg(ChatEvent c)
- {
- MsgDumper(m_Config.GetSettingList(c.PlayerName));
- }
- //----------------------------------------------------------------------//
- // Incoming Events //
- //----------------------------------------------------------------------//
- public void MonitorPlayerEnter(object sender, PlayerEnteredEvent e)
- {
- }
- public void MonitorPlayerLeft(object sender, PlayerLeftEvent e)
- {
- }
- public void MonitorPlayerDeath(object sender, PlayerDeathEvent e)
- {
- }
- public void MonitorFreqChange(object sender, TeamChangeEvent e)
- {
- }
- public void MonitorShipChange(object sender, ShipChangeEvent e)
- {
- if (e.ShipType == ShipTypes.Spectator)
- AddToSpectatorList(e.PlayerName);
- else if (e.PreviousShipType == ShipTypes.Spectator)
- AddToLobbyList(e.PlayerName);
- }
- public void GrabPlayerInfo(object sender, PlayerInfoEvent e)
- {
- for (int i = 0; i < e.PlayerList.Count; i += 1)
- {
- if (e.PlayerList[i].Ship == ShipTypes.Spectator)
- AddToSpectatorList(e.PlayerList[i].PlayerName);
- else
- AddToLobbyList(e.PlayerList[i].PlayerName);
- }
- }
- public void GrabBotInfo(object sender, BotInfoRequest e)// Catches the incoming request we made for bot info
- {
- if (m_BotName != null) return; // One time only request - Guard against other module requests
- m_BotName = e.BotName; // Store the name
- if (m_AutoLoad) LoadConfiguration();// Load cfg if auto is enabled
- }
- //----------------------------------------------------------------------//
- // Timer Functions //
- //----------------------------------------------------------------------//
- // Start timer - sends request for bot info
- public void LoadTimer()
- {
- RemoveTimedEvent("LoadTimer"); // destroy our load timer
- Game(new BotInfoRequest()); // Request Bot info from core
- Game(msg.pub("?listmod")); // I disabled listmod timer in core - this asks for list on load
- }
- public void MainDevaTimer(object source, ElapsedEventArgs e)
- {
- MsgDumper(m_Spec.UpdateSpecPosition); // Update spec region for bot
- MsgDumper(m_BaseManager.Messages); // Messages from basemanager
- SafeGame(m_Biller.MessageToSend); // Send any q'ed Biller commands
- if (m_GameState == GameState.Starting && (DateTime.Now - m_GameStartTimeStamp).TotalSeconds > m_GameStartTimer) StartGame();
- }
- //----------------------------------------------------------------------//
- // Game Fuctions //
- //----------------------------------------------------------------------//
- public void StartingGameTimer()
- {
- m_GameState = GameState.Starting;
- m_GameStartTimeStamp = DateTime.Now;
- Game(msg.arena("Game will begin in (" + m_GameStartTimer + ")seconds. If you wish to play please select a ship."));
- }
- public void StartGame()
- {
- if (m_LobbyList.Count < 2)
- {
- CancelStart();
- return;
- }
- SortTeams();
- m_GameState = GameState.On;
- Game(msg.arena("Game start timer expired. Split teams here -"));
- }
- public void CancelStart()
- {
- m_GameState = GameState.Off;
- Game(msg.arena("Not enough players to start game. Start Cancelled."));
- }
- public void SortTeams()
- {
- }
- //----------------------------------------------------------------------//
- // Player Functions //
- //----------------------------------------------------------------------//
- public void AddToSpectatorList(string PlayerName)
- {
- if (PlayerOnList(m_SpectatorList, PlayerName)) return;
- m_LobbyList.Remove(PlayerName);
- m_SpectatorList.Add(PlayerName);
- }
- public void AddToLobbyList(string PlayerName)
- {
- if (PlayerOnList(m_LobbyList, PlayerName)) return;
- m_SpectatorList.Remove(PlayerName);
- m_LobbyList.Add(PlayerName);
- if (m_LobbyList.Count > 1)
- {
- if (m_GameState == GameState.Off)
- StartingGameTimer();
- else if (m_GameState == GameState.On)
- {
- }
- }
- else
- Game(msg.pm(PlayerName,"One more person is needed to start a game. Please wait patiently. Satan loves you."));
- }
- private bool PlayerOnList(List<string> List, string PlayerName)
- {
- for (int i = 0; i < List.Count; i += 1)
- if (List[i] == PlayerName)
- return true;
- return false;
- }
- //----------------------------------------------------------------------//
- // Misc Stuffs //
- //----------------------------------------------------------------------//
- // MsgDumper takes a single event or a Queue of events:
- // Checks to see if they are valid and also checks to see if its a biller message,
- // then sends it out
- private void MsgDumper(Queue<EventArgs> q)
- {
- if (q == null || q.Count == 0) return;
- while (q.Count > 0)
- MsgDumper(q.Dequeue());
- }
- private void MsgDumper(EventArgs e)
- {
- if (e == null) return;
- if (e is ChatEvent)
- {
- ChatEvent c = e as ChatEvent;
- if (c.ChatType == ChatTypes.Channel || c.Message.StartsWith("?"))
- m_Biller.SendMessage(c);
- else Game(e);
- }
- else Game(e);
- }
- // Added security to biller module to avoid flooding
- private void SafeGame(EventArgs e)
- {
- if ( e == null || (DateTime.Now - m_SafeGameTimeStamp).TotalMilliseconds < m_SafeGameTime) return;
- m_SafeGameTimeStamp = DateTime.Now;
- Game(e);
- }
- //----------------------------------------------------------------------//
- // Configuration //
- //----------------------------------------------------------------------//
- public void LoadConfiguration()
- {
- if (m_DevaTimer != null && m_DevaTimer.Enabled) m_DevaTimer.Stop();// Stop timer if its on
- m_SpectatorList = new List<String>();
- m_LobbyList = new List<String>();
- m_GameState = GameState.Off;
- m_Biller = new BillerCommands();
- m_Config = new Configuration(m_BotName); // Load cfg from file - need botname to check inside right folder
- MsgDumper(m_Config.LoadCFG());
- m_Spec = new SpecManager(m_BotName, m_Config); // Load cfg to spec class
- m_BaseManager = new BaseManager(m_Config); // Load cfg to base manager
- if (m_Config.GetSetting("botchats") != null) // Chat login info
- Game(msg.pub("?chat=" + m_Config.GetSetting("botchats").StringSingleSetting));
- m_DevaTimer = new Timer(); // Configure main game timer
- m_DevaTimer.Elapsed += new ElapsedEventHandler(MainDevaTimer);
- m_DevaTimer.Interval = m_DevaTimerInterval;
- m_DevaTimer.Start();
- Game(new PlayerInfoEvent()); // Request player info from core
- m_Biller.SendMessage(msg.chan(1, "Bot [ " + m_BotName + " ] CFG load finished."));
- LoadFakePlayers();
- }
- //----------------------------------------------------------------------//
- // DEBUG Stuffs //
- //----------------------------------------------------------------------//
- public void Test1(ChatEvent c)
- {
- string[] data = c.Message.Split(' ');
- AddToLobbyList(FakePlayers[int.Parse(data[1])]);
- Game(msg.arena("Player[ " + FakePlayers[int.Parse(data[1])] + " ] added to Lobby."));
- }
- public void Test2(ChatEvent c)
- {
- string[] data = c.Message.Split(' ');
- AddToSpectatorList(FakePlayers[int.Parse(data[1])]);
- Game(msg.arena("Player[ " + FakePlayers[int.Parse(data[1])] + " ] added to Spec."));
- }
- string[] FakePlayers = new string[] { "Larry", "Curly", "Moe", "Your Mom", "Joe", "Satan","Cookie Monster","God" };
- public void LoadFakePlayers()
- {
- for (int i = 0; i < FakePlayers.Length; i += 1)
- m_SpectatorList.Add(FakePlayers[i]);
- }
- public override void Dispose()
- {
- throw new NotImplementedException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement