Advertisement
Guest User

AutoRPG

a guest
Apr 24th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using BattleCore;
  7. using BattleCore.Events;
  8. using BattleCorePsyOps;
  9. using System.Timers;
  10.  
  11. namespace AutoRPG
  12. {
  13.     [Behavior("Main", "true", "0.01", "KenNPsy", "Rpg game that plays itself.")]
  14.     public class Main: BotEventListener
  15.     {
  16.         public Main()
  17.         {
  18.             //Makes sure spam control is on
  19.             msg.SpamControl = true;
  20.             //Log bot into channel
  21.             msg.SCPubCommand(rpg_JoinChatCommand);
  22.         }
  23.  
  24.         // ---------------------------------- Variables and needed Classes -------------------------- //
  25.         //Psy's Module to help with chat events
  26.         ShortChat msg = new ShortChat();
  27.  
  28.         //Stores botname and arena
  29.         string BotName, Arena = "";
  30.  
  31.         //Game Timer
  32.         Timer GameTimer = new Timer();
  33.  
  34.         //Random for dice
  35.         Random random = new Random();
  36.  
  37.         // ---------------------------------- Subspace Game Events ---------------------------------- //
  38.         //Monitor all incoming chat events
  39.         public void MonitorChatEvents(object sender, ChatEvent c)
  40.         {
  41.             //Core sends each bot a pm with its own info 500ms after startup
  42.             //Use it to Initialize Bot
  43.             if (c.ChatType == ChatTypes.Private && c.Message.StartsWith("@BotInfo@"))
  44.             {
  45.                 // Catch and store bot info that core sends
  46.                 string[] data = c.Message.Split(':');
  47.                 BotName = data[1];
  48.                 Arena = data[2];
  49.  
  50.                 // Initialize everything needed to get bot started
  51.                 InitializeBot();
  52.             }
  53.         }
  54.  
  55.         // All players coming in should be in spec because of arena settings
  56.         // Have the game check their status here - registered/unregistered
  57.         public void MonitorPlayerEnter(object sender, PlayerEnteredEvent pe)
  58.         {   rpg_PlayerJoinGame(pe.PlayerName);}
  59.  
  60.         // Player needs to be updated as he leaves, and take him out of game
  61.         public void MonitorPlayerLeave(object sender, PlayerLeftEvent pl)
  62.         {   rpg_PlayerLeave(pl.PlayerName); }
  63.  
  64.         // Check to see if players are going into/out of spectator mode
  65.         public void MonitorPlayerShipChanges(object sender, ShipChangeEvent sc)
  66.         {
  67.             // Coming into spec
  68.             if (sc.PreviousShipType != ShipTypes.Spectator && sc.ShipType == ShipTypes.Spectator)
  69.             {
  70.                 rpg_PlayerJoinGame(sc.PlayerName);
  71.                 return;
  72.             }
  73.  
  74.             // Going out of spec
  75.             if (sc.PreviousShipType == ShipTypes.Spectator && sc.ShipType != ShipTypes.Spectator)
  76.             {
  77.                 rpg_PlayerLeave(sc.PlayerName);
  78.                 return;
  79.             }
  80.         }
  81.  
  82.         public void InitializeBot()
  83.         {
  84.             //Configure and start main game timer
  85.             GameTimer.Elapsed += new ElapsedEventHandler(TimerMethod);
  86.             GameTimer.Interval = 10;
  87.             GameTimer.Start();
  88.  
  89.             //update chat with bot initialized msg
  90.             msg.SCChanMessage(1, BotName + " initialized.");
  91.         }
  92.  
  93.         // ---------------------------------- GAME TIMER ----------------------------------------- //
  94.         public void TimerMethod(object sender, ElapsedEventArgs e)
  95.         {
  96.             //Check to see if we have any commands in our command chat q
  97.             if (msg.NeedUpdate) Game(msg.sendNextBCommand());
  98.  
  99.             // Safety chat rejoin in case bot lags out - NOT a crash
  100.             if ((DateTime.Now - ts_RejoinChats).TotalMinutes > delay_RejoinChats)
  101.             {
  102.                 // Update time stamp
  103.                 ts_RejoinChats = DateTime.Now;
  104.                 msg.SCPubCommand(rpg_JoinChatCommand);
  105.             }
  106.         }
  107.  
  108.         // ---------------------------------- MAIN CODE FOR RPG ---------------------------------- //
  109.        
  110.         // Join chat command for bot to send
  111.         string rpg_JoinChatCommand = "?chat=battledev";
  112.  
  113.         // Main RPG Player List
  114.         List<ARPlayer> rpg_PlayerList = new List<ARPlayer>();
  115.  
  116.         // In case of a Bot disconnect BUT not a crash we need bot to rejoin chats
  117.         DateTime ts_RejoinChats = DateTime.Now;
  118.         // Time in minutes that you want bot to rejoin chats
  119.         double delay_RejoinChats = 5;
  120.  
  121.         // Player is joining game
  122.         public void rpg_PlayerJoinGame(string PlayerName)
  123.         {
  124.             // Var to hold player
  125.             ARPlayer rpg_Player;
  126.  
  127.             // Check DB for updating player info
  128.             rpg_Player = DB_GetPlayer(PlayerName);
  129.  
  130.             // A safety for player NOT found in DB and player IS in current game somehow
  131.             if (GetPlayer(PlayerName) != null)
  132.             {
  133.                 msg.SCChanMessage(1, "*** Check Game Logic *** rpg_PlayerJoinGame found player INGame. Exiting method.");
  134.                 return;
  135.             }
  136.  
  137.             // Player Not found - register player here
  138.             if (rpg_Player == null)
  139.             {
  140.                 rpg_Player = rpg_MakeNewPlayer(PlayerName);
  141.                 msg.SCChanMessage(1,"A baby in BattleRPG has been born. Welcome baby " + PlayerName + "!");
  142.             }
  143.  
  144.             // Add player to ongoing game here - (PlayerList)
  145.             rpg_PlayerList.Add(rpg_Player);
  146.         }
  147.  
  148.         public void rpg_PlayerLeave(string PlayerName)
  149.         {
  150.             // Grab player info from list
  151.             ARPlayer rpg_Player = GetPlayer(PlayerName);
  152.  
  153.             // If the player was not playing we stop code here
  154.             // example being player left from a non spec ship
  155.             if (rpg_Player == null) return;
  156.  
  157.             // Update player on the DB if needed
  158.             DB_UpdatePlayerInfo(rpg_Player);
  159.  
  160.             // Take Player out of the ongoing game
  161.             // not sure if this is going to work
  162.             // may have to loop through list and take out that way
  163.             rpg_PlayerList.Remove(rpg_Player);
  164.         }
  165.  
  166.         // get player from player list using name
  167.         public ARPlayer GetPlayer(string PlayerName)
  168.         {
  169.             // Loop through list
  170.             for (int i = 0; i < rpg_PlayerList.Count; i+=1)
  171.             {
  172.                 // if there is a name match, send back player
  173.                 if (PlayerName == rpg_PlayerList[i].PlayerName)
  174.                     return rpg_PlayerList[i];
  175.             }
  176.  
  177.             // No matches were found, send back null
  178.             return null;
  179.         }
  180.  
  181.         // Create a player and set all default start variables here
  182.         public ARPlayer rpg_MakeNewPlayer(string PlayerName)
  183.         {
  184.             // set default values
  185.             // Maybe have options for a PREMIUM higher lvl start char?!?!?
  186.             ARPlayer np = new ARPlayer();
  187.             np.PlayerName = PlayerName;
  188.             np.Experience = 0;
  189.             np.Level = 0;
  190.  
  191.             // Possibly update database here to include new player
  192.             // --------------------- //
  193.  
  194.             // return configured player
  195.             return np;
  196.         }
  197.  
  198.         // Grab all the needed player info from db here
  199.         // If the player is not found we will return null
  200.         public ARPlayer DB_GetPlayer(string PlayerName)
  201.         {
  202.             // Player was not found - return empty (null)
  203.             return null;
  204.         }
  205.  
  206.         // Update the databse with new player stats
  207.         public void DB_UpdatePlayerInfo(ARPlayer rpg_Player)
  208.         {
  209.             // Update Player Info on DB
  210.         }
  211.  
  212.         public override void Dispose()
  213.         {
  214.             throw new NotImplementedException();
  215.         }
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement