PsyOps

Help.cs

May 19th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using System.IO;
  7.  
  8. // Needed core files
  9. using BattleCore;
  10. using BattleCore.Events;
  11. using BattleCorePsyOps;
  12.  
  13. namespace Devastation
  14. {
  15.     // Add the attribute and the base class
  16.     [Behavior("Help", "true", "0.01", "PsyOps", "Module to assist with Help commands.")]
  17.     public class Help : BotEventListener
  18.     {
  19.         public Help()
  20.         {
  21.             RegisterCommand("!refreshhelp", Refresh);
  22.             RegisterCommand(".refreshhelp", Refresh);
  23.             RegisterTimedEvent("HelpStart", 1000, StartBot);
  24.         }
  25.  
  26.         private List<HelpMessage> m_MasterHelpList = new List<HelpMessage>();
  27.  
  28.         class HelpMessage
  29.         {
  30.             private string m_Command;
  31.             public string Command { get { return m_Command; } }
  32.  
  33.             private string[] m_Description;
  34.             public string[] Description { get { return m_Description; } }
  35.  
  36.             public HelpMessage(string command, string[] description)
  37.             {
  38.                 this.m_Command = command;
  39.                 this.m_Description = description;
  40.             }
  41.         }
  42.  
  43.         // Custom chat module
  44.         private ShortChat msg = new ShortChat();
  45.  
  46.         private string m_BotName = null;
  47.         bool Initialized = false;
  48.  
  49.         public void StartBot()
  50.         {
  51.             RemoveTimedEvent("HelpStart");
  52.             if (Initialized) return;
  53.             Initialized = true;
  54.             Game(new BotInfoRequest());
  55.         }
  56.         public void GetBotInfo(object sender, BotInfoRequest b)
  57.         {
  58.             if (m_BotName != null) return;
  59.             m_BotName = b.BotName;
  60.             LoadCFG();
  61.         }
  62.         public void Refresh(ChatEvent c)
  63.         {
  64.             if (c.ModLevel >= ModLevels.Sysop) LoadCFG();
  65.         }
  66.         public void MonitorPlayerChat(object sender, ChatEvent c)
  67.         {
  68.             if (c.Message.ToLower().StartsWith("!help") || c.Message.ToLower().StartsWith(".help") || c.Message.ToLower().StartsWith("!halp") || c.Message.ToLower().StartsWith(".halp")) DoHelpCommand(c);
  69.         }
  70.         public void DoHelpCommand(ChatEvent c)
  71.         {
  72.             string cmd = c.Message.Trim().ToLower().Remove(0, 1);
  73.             if (cmd == "help" || cmd == "halp")
  74.             {
  75.                 PrintHelpMessage(c.PlayerName, "overview");
  76.                 return;
  77.             }
  78.             else if (c.Message.Contains(" ") || c.Message.Length > 5)
  79.             {
  80.                 string command = c.Message.Remove(0, 5).Trim().ToLower();
  81.                 PrintHelpMessage(c.PlayerName, command);
  82.                 return;
  83.             }
  84.  
  85.             // Code shouldnt get here.
  86.             Game(msg.arena("I wear a pink tutu with orange socks."));
  87.         }
  88.  
  89.         public void PrintHelpMessage(string player, string command)
  90.         {
  91.             for (int i = 0; i < m_MasterHelpList.Count; i += 1)
  92.             {
  93.                 if (m_MasterHelpList[i].Command == command)
  94.                 {
  95.                     for (int j = 0; j < m_MasterHelpList[i].Description.Length; j += 1)
  96.                     {
  97.                         Game(msg.pm(player, m_MasterHelpList[i].Description[j]));
  98.                     }
  99.                     return;
  100.                 }
  101.             }
  102.             Game(msg.pm(player, "Im sorry I do not understand the command [" + command + "]."));
  103.             return;
  104.         }
  105.         public void LoadCFG()
  106.         {
  107.             m_MasterHelpList = new List<HelpMessage>();
  108.             string BotPath = Directory.GetCurrentDirectory();
  109.  
  110.             // Check to see if user setup folder properly
  111.             if (Directory.Exists(BotPath + "/" + m_BotName + "/help"))
  112.             {
  113.                 // grab all help file names
  114.                 string[] filePaths = Directory.GetFiles(BotPath + "/" + m_BotName + "/help", "*.help");
  115.  
  116.                 // Go through each file
  117.                 for (int i = 0; i < filePaths.Length; i += 1)
  118.                 {
  119.                     string command = System.IO.Path.GetFileNameWithoutExtension(filePaths[i]);
  120.  
  121.                     try
  122.                     {
  123.                         //Opening appropriate file
  124.                         StreamReader SR = File.OpenText(filePaths[i]);
  125.                         //used to read each line
  126.                         string S;
  127.  
  128.                         // Temp list to store printout
  129.                         List<string> tmp = new List<string>();
  130.  
  131.                         // Extract and save all info from the cfg file
  132.                         while ((S = SR.ReadLine()) != null)
  133.                             tmp.Add(S.Trim());
  134.  
  135.                         // Only add command if the file contained info
  136.                         if (tmp.Count > 0)
  137.                             m_MasterHelpList.Add(new HelpMessage(command.ToLower(), tmp.ToArray()));
  138.  
  139.                         // Closing file
  140.                         SR.Close();
  141.                     }
  142.                     catch (Exception ex)
  143.                     { Game(msg.arena("Help File [" + command + "] Load Error:" + ex)); }
  144.                 }
  145.  
  146.                 Game(msg.arena("Help Files Loaded. Command total:[" + m_MasterHelpList.Count + "]"));
  147.             }
  148.             else
  149.                 Game(msg.arena("Help directory not found."));
  150.         }
  151.         public override void Dispose()
  152.         {
  153.             throw new NotImplementedException();
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment