Advertisement
PsyOps

DuelBase Main.cs

Dec 4th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. // Needed for timers
  7. using System.Timers;
  8.  
  9. using BattleCore;
  10. using BattleCore.Events;
  11. using BattleCorePsyOps;
  12.  
  13. namespace BaseDuel
  14. {
  15.     // Add the attribute and the base class
  16.     [Behavior("DevaMain", "false", "0.01", "PsyOps", "Main module to control deva.(rewrite)")]
  17.     public class Main: BotEventListener
  18.     {
  19.         public Main()
  20.         {
  21.             // Register Events / Regions / and Commands here        // commands in .command format too
  22.             RegisterCommand("!reloadbd",Request_ReloadModule);      RegisterCommand(".reloadbd", Request_ReloadModule);
  23.             RegisterCommand("!bdplayerlist", Request_PlayerList);   RegisterCommand(".bdplayerlist", Request_PlayerList);
  24.             RegisterCommand("!bdcfg", Request_Cfg);                 RegisterCommand(".bdcfg", Request_Cfg);
  25.  
  26.             //debug stuff
  27.             RegisterCommand("!test1", Test1);                       RegisterCommand(".test2", Test2);
  28.             RegisterCommand("!test2", Test2);                       RegisterCommand(".test2", Test2);
  29.  
  30.             RegisterTimedEvent("LoadTimer", 1000, LoadTimer);
  31.         }
  32.  
  33.         public ShortChat msg = new ShortChat(); // Custom module to help with making chat messages
  34.  
  35.         private bool m_AutoLoad = true;         // Probably just for debug - autoloads cfgs n stuff
  36.         private string m_BotName = null;        // Holds Botname
  37.         private string m_Arena;                 // Name of arena Bot is in   - do i need this ?!?!?!
  38.         private bool m_Initialized = false;     // If bot is initialized
  39.  
  40.         private Timer m_DevaTimer;              // Main Game timer
  41.         private double m_DevaTimerInterval = 5; // in milliseconds
  42.  
  43.         private GameState m_GameState;          // Game state duh
  44.         private DateTime m_GameStartTimeStamp;  // Timestamp for game start
  45.         private double m_GameStartTimer = 30;   // Seconds
  46.  
  47.         private BaseManager m_BaseManager;      // Base manager
  48.  
  49.         private DateTime m_SafeGameTimeStamp = DateTime.Now;
  50.         private double m_SafeGameTime = 25;     // milliseconds
  51.  
  52.         private SpecManager m_Spec;             // Controls where bot specs
  53.         private BillerCommands m_Biller;        // Module to make sending biller messages safer - avoid flooding
  54.         private Configuration m_Config;         // Grabs info from cfg and loads to memory
  55.  
  56.         private List<String> m_SpectatorList;   //   Player Lists
  57.         private List<String> m_LobbyList;       // <--------------->
  58.  
  59.         //----------------------------------------------------------------------//
  60.         //                         Chat Commands                                //
  61.         //----------------------------------------------------------------------//
  62.         public void Request_ReloadModule(ChatEvent c)
  63.         {
  64.             if (c.ModLevel < ModLevels.Sysop) return;       // Only allow for sysops
  65.             LoadConfiguration();
  66.         }
  67.         public void Request_PlayerList(ChatEvent c)
  68.         {
  69.             Game(msg.pm(c.PlayerName, "-----------------<[  Spectator List ]>-----------------"));
  70.             for (int i = 0; i < m_SpectatorList.Count; i+= 1)
  71.                 Game(msg.pm(c.PlayerName, "[ "+i+" ]  " + m_SpectatorList[i]));
  72.             Game(msg.pm(c.PlayerName, "-----------------<[    Lobby List   ]>-----------------"));
  73.             for (int i = 0; i < m_LobbyList.Count; i += 1)
  74.                 Game(msg.pm(c.PlayerName, "[ " + i + " ]  " + m_LobbyList[i]));
  75.         }
  76.         public void Request_Cfg(ChatEvent c)
  77.         {
  78.             MsgDumper(m_Config.GetSettingList(c.PlayerName));
  79.         }
  80.         //----------------------------------------------------------------------//
  81.         //                         Incoming Events                              //
  82.         //----------------------------------------------------------------------//
  83.         public void MonitorPlayerEnter(object sender, PlayerEnteredEvent e)
  84.         {
  85.         }
  86.         public void MonitorPlayerLeft(object sender, PlayerLeftEvent e)
  87.         {
  88.         }
  89.         public void MonitorPlayerDeath(object sender, PlayerDeathEvent e)
  90.         {
  91.         }
  92.         public void MonitorFreqChange(object sender, TeamChangeEvent e)
  93.         {
  94.         }
  95.         public void MonitorShipChange(object sender, ShipChangeEvent e)
  96.         {
  97.             if (e.ShipType == ShipTypes.Spectator)
  98.                 AddToSpectatorList(e.PlayerName);
  99.             else if (e.PreviousShipType == ShipTypes.Spectator)
  100.                 AddToLobbyList(e.PlayerName);
  101.         }
  102.         public void GrabPlayerInfo(object sender, PlayerInfoEvent e)
  103.         {
  104.             for (int i = 0; i < e.PlayerList.Count; i += 1)
  105.             {
  106.                 if (e.PlayerList[i].Ship == ShipTypes.Spectator)
  107.                     AddToSpectatorList(e.PlayerList[i].PlayerName);
  108.                 else
  109.                     AddToLobbyList(e.PlayerList[i].PlayerName);
  110.             }
  111.         }
  112.         public void GrabBotInfo(object sender, BotInfoRequest e)// Catches the incoming request we made for bot info
  113.         {
  114.             if (m_BotName != null) return;      // One time only request - Guard against other module requests
  115.             m_BotName = e.BotName;              // Store the name
  116.  
  117.             if (m_AutoLoad) LoadConfiguration();// Load cfg if auto is enabled
  118.         }
  119.         //----------------------------------------------------------------------//
  120.         //                         Timer Functions                              //
  121.         //----------------------------------------------------------------------//
  122.         // Start timer - sends request for bot info
  123.         public void LoadTimer()
  124.         {
  125.             RemoveTimedEvent("LoadTimer");      // destroy our load timer
  126.             Game(new BotInfoRequest());         // Request Bot info from core
  127.             Game(msg.pub("?listmod"));          // I disabled listmod timer in core - this asks for list on load
  128.         }
  129.         public void MainDevaTimer(object source, ElapsedEventArgs e)
  130.         {
  131.             MsgDumper(m_Spec.UpdateSpecPosition);   // Update spec region for bot
  132.             MsgDumper(m_BaseManager.Messages);      // Messages from basemanager
  133.             SafeGame(m_Biller.MessageToSend);       // Send any q'ed Biller commands
  134.  
  135.             if (m_GameState == GameState.Starting && (DateTime.Now - m_GameStartTimeStamp).TotalSeconds > m_GameStartTimer) StartGame();
  136.         }
  137.         //----------------------------------------------------------------------//
  138.         //                         Game Fuctions                                //
  139.         //----------------------------------------------------------------------//
  140.         public void StartingGameTimer()
  141.         {
  142.             m_GameState = GameState.Starting;
  143.             m_GameStartTimeStamp = DateTime.Now;
  144.             Game(msg.arena("Game will begin in (" + m_GameStartTimer + ")seconds. If you wish to play please select a ship."));
  145.         }
  146.         public void StartGame()
  147.         {
  148.             if (m_LobbyList.Count < 2)
  149.             {
  150.                 CancelStart();
  151.                 return;
  152.             }
  153.  
  154.             SortTeams();
  155.             m_GameState = GameState.On;
  156.             Game(msg.arena("Game start timer expired. Split teams here -"));
  157.         }
  158.         public void CancelStart()
  159.         {
  160.             m_GameState = GameState.Off;
  161.             Game(msg.arena("Not enough players to start game. Start Cancelled."));
  162.         }
  163.         public void SortTeams()
  164.         {
  165.  
  166.         }
  167.         //----------------------------------------------------------------------//
  168.         //                         Player Functions                             //
  169.         //----------------------------------------------------------------------//
  170.         public void AddToSpectatorList(string PlayerName)
  171.         {
  172.             if (PlayerOnList(m_SpectatorList, PlayerName)) return;
  173.             m_LobbyList.Remove(PlayerName);
  174.  
  175.             m_SpectatorList.Add(PlayerName);
  176.         }
  177.         public void AddToLobbyList(string PlayerName)
  178.         {
  179.             if (PlayerOnList(m_LobbyList, PlayerName)) return;
  180.             m_SpectatorList.Remove(PlayerName);
  181.  
  182.             m_LobbyList.Add(PlayerName);
  183.  
  184.             if (m_LobbyList.Count > 1)
  185.             {
  186.                 if (m_GameState == GameState.Off)
  187.                     StartingGameTimer();
  188.                 else if (m_GameState == GameState.On)
  189.                 {
  190.                 }
  191.             }
  192.             else
  193.                 Game(msg.pm(PlayerName,"One more person is needed to start a game. Please wait patiently. Satan loves you."));
  194.         }
  195.         private bool PlayerOnList(List<string> List, string PlayerName)
  196.         {
  197.             for (int i = 0; i < List.Count; i += 1)
  198.                 if (List[i] == PlayerName)
  199.                     return true;
  200.             return false;
  201.         }
  202.         //----------------------------------------------------------------------//
  203.         //                         Misc Stuffs                                  //
  204.         //----------------------------------------------------------------------//
  205.         // MsgDumper takes a single event or a Queue of events:
  206.         // Checks to see if they are valid and also checks to see if its a biller message,
  207.         // then sends it out
  208.         private void MsgDumper(Queue<EventArgs> q)
  209.         {
  210.             if (q == null || q.Count == 0) return;
  211.  
  212.             while (q.Count > 0)
  213.                 MsgDumper(q.Dequeue());
  214.         }
  215.         private void MsgDumper(EventArgs e)
  216.         {
  217.             if (e == null) return;
  218.  
  219.             if (e is ChatEvent)
  220.             {
  221.                 ChatEvent c = e as ChatEvent;
  222.  
  223.                 if (c.ChatType == ChatTypes.Channel || c.Message.StartsWith("?"))
  224.                     m_Biller.SendMessage(c);
  225.                 else Game(e);
  226.             }
  227.             else Game(e);
  228.         }
  229.         // Added security to biller module to avoid flooding
  230.         private void SafeGame(EventArgs e)
  231.         {
  232.             if ( e == null || (DateTime.Now - m_SafeGameTimeStamp).TotalMilliseconds < m_SafeGameTime) return;
  233.             m_SafeGameTimeStamp = DateTime.Now;
  234.             Game(e);
  235.         }
  236.         //----------------------------------------------------------------------//
  237.         //                         Configuration                                //
  238.         //----------------------------------------------------------------------//
  239.         public void LoadConfiguration()
  240.         {
  241.             if (m_DevaTimer != null && m_DevaTimer.Enabled) m_DevaTimer.Stop();// Stop timer if its on
  242.  
  243.             m_SpectatorList = new List<String>();
  244.             m_LobbyList = new List<String>();
  245.             m_GameState = GameState.Off;
  246.  
  247.             m_Biller = new BillerCommands();
  248.  
  249.             m_Config = new Configuration(m_BotName);        // Load cfg from file - need botname to check inside right folder
  250.             MsgDumper(m_Config.LoadCFG());
  251.  
  252.             m_Spec = new SpecManager(m_BotName, m_Config);  // Load cfg to spec class
  253.             m_BaseManager = new BaseManager(m_Config);      // Load cfg to base manager
  254.  
  255.             if (m_Config.GetSetting("botchats") != null)    // Chat login info
  256.                 Game(msg.pub("?chat=" + m_Config.GetSetting("botchats").StringSingleSetting));
  257.  
  258.             m_DevaTimer = new Timer();                      // Configure main game timer
  259.             m_DevaTimer.Elapsed += new ElapsedEventHandler(MainDevaTimer);
  260.             m_DevaTimer.Interval = m_DevaTimerInterval;
  261.             m_DevaTimer.Start();
  262.  
  263.             Game(new PlayerInfoEvent());                    // Request player info from core
  264.             m_Biller.SendMessage(msg.chan(1, "Bot [ " + m_BotName + " ] CFG load finished."));
  265.  
  266.             LoadFakePlayers();
  267.         }
  268.         //----------------------------------------------------------------------//
  269.         //                         DEBUG Stuffs                                 //
  270.         //----------------------------------------------------------------------//
  271.         public void Test1(ChatEvent c)
  272.         {
  273.             string[] data = c.Message.Split(' ');
  274.             AddToLobbyList(FakePlayers[int.Parse(data[1])]);
  275.             Game(msg.arena("Player[ " + FakePlayers[int.Parse(data[1])] + " ] added to Lobby."));
  276.         }
  277.         public void Test2(ChatEvent c)
  278.         {
  279.             string[] data = c.Message.Split(' ');
  280.             AddToSpectatorList(FakePlayers[int.Parse(data[1])]);
  281.             Game(msg.arena("Player[ " + FakePlayers[int.Parse(data[1])] + " ] added to Spec."));
  282.         }
  283.         string[] FakePlayers = new string[] { "Larry", "Curly", "Moe", "Your Mom", "Joe", "Satan","Cookie Monster","God" };
  284.         public void LoadFakePlayers()
  285.         {
  286.             for (int i = 0; i < FakePlayers.Length; i += 1)
  287.                 m_SpectatorList.Add(FakePlayers[i]);
  288.         }
  289.         public override void Dispose()
  290.         {
  291.             throw new NotImplementedException();
  292.         }
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement