PsyOps

Main

Apr 24th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.15 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(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.         //Game Tick
  38.         DateTime ts_GameUpdate = DateTime.Now;
  39.         //Time in Mintues to tick
  40.         double delay_GameUpdate = 10000;
  41.  
  42.         // In case of a Bot disconnect BUT not a crash we need bot to rejoin chats
  43.         DateTime ts_RejoinChats = DateTime.Now;
  44.         // Time in minutes that you want bot to rejoin chats
  45.         double delay_RejoinChats = 5;
  46.  
  47.         //Areas
  48.         string[] WorldMap = new string[] { "Beach", "Dark Forest", "Uncharted Desert", "Specorian Plains", "Specoria Town", " Specorian Mountains", "Burnt Forest", "Dragons Burden", "Undead Forest" };
  49.  
  50.         // ---------------------------------- Subspace Game Events ---------------------------------- //
  51.         //Monitor all incoming chat events
  52.         public void MonitorChatEvents(object sender, ChatEvent c)
  53.         {
  54.             //Core sends each bot a pm with its own info 500ms after startup
  55.             //Use it to Initialize Bot
  56.             if (c.ChatType == ChatTypes.Private && c.Message.StartsWith("@BotInfo@"))
  57.             {
  58.                 // Catch and store bot info that core sends
  59.                 string[] data = c.Message.Split(':');
  60.                 BotName = data[1];
  61.                 Arena = data[2];
  62.  
  63.                 // Initialize everything needed to get bot started
  64.                 InitializeBot();
  65.             }
  66.         }
  67.  
  68.         // All players coming in should be in spec because of arena settings
  69.         // Have the game check their status here - registered/unregistered
  70.         public void MonitorPlayerEnter(object sender, PlayerEnteredEvent pe)
  71.         {  
  72.             if (pe.ShipType == ShipTypes.Spectator)
  73.                 rpg_PlayerJoinGame(pe.PlayerName);
  74.         }
  75.  
  76.         // Player needs to be updated as he leaves, and take him out of game
  77.         public void MonitorPlayerLeave(object sender, PlayerLeftEvent pl)
  78.         {   rpg_PlayerLeave(pl.PlayerName); }
  79.  
  80.         // Check to see if players are going into/out of spectator mode
  81.         public void MonitorPlayerShipChanges(object sender, ShipChangeEvent sc)
  82.         {
  83.             // Coming into spec
  84.             if (sc.PreviousShipType != ShipTypes.Spectator && sc.ShipType == ShipTypes.Spectator)
  85.             {
  86.                 rpg_PlayerJoinGame(sc.PlayerName);
  87.                 return;
  88.             }
  89.  
  90.             // Going out of spec
  91.             if (sc.PreviousShipType == ShipTypes.Spectator && sc.ShipType != ShipTypes.Spectator)
  92.             {
  93.                 rpg_PlayerLeave(sc.PlayerName);
  94.                 return;
  95.             }
  96.         }
  97.  
  98.         // ---------------------------------- GAME TIMER ----------------------------------------- //
  99.         public void TimerMethod(object sender, ElapsedEventArgs e)
  100.         {
  101.             //Check to see if we have any commands in our command chat q
  102.             if (msg.NeedUpdate) Game(msg.sendNextBCommand());
  103.  
  104.             // One game cycle
  105.             rpg_GameUpdate();
  106.  
  107.             // Safety in case bot lags out
  108.             // Joins chat every 5 min
  109.             ReJoinChatCheck();
  110.         }
  111.  
  112.         // ---------------------------------- MAIN CODE FOR RPG ---------------------------------- //
  113.  
  114.         // Main RPG Player List
  115.         List<ARPlayer> rpg_PlayerList = new List<ARPlayer>();
  116.  
  117.         // Update game - Basically one game cycle
  118.         public void rpg_GameUpdate()
  119.         {
  120.             if ((DateTime.Now - ts_GameUpdate).TotalMilliseconds > delay_GameUpdate)
  121.             {
  122.                 // Update time stamp
  123.                 ts_GameUpdate = DateTime.Now;
  124.  
  125.                 ARPlayer rpg_Player = rpg_GetRandomPlayer();
  126.  
  127.                 rpg_MovePlayer(rpg_Player);
  128.  
  129.                 msg.SCChanMessage(1,"Player[ "+rpg_Player.PlayerName+" ] has moved to " + WorldMap[rpg_Player.AreaName]);
  130.             }
  131.         }
  132.  
  133.         // Player is joining game
  134.         public void rpg_PlayerJoinGame(string PlayerName)
  135.         {
  136.             // Var to hold player
  137.             ARPlayer rpg_Player;
  138.  
  139.             // Check DB for updating player info
  140.             rpg_Player = DB_GetPlayer(PlayerName);
  141.  
  142.             // A safety for player NOT found in DB and player IS in current game somehow
  143.             if (rpg_GetPlayer(PlayerName) != null)
  144.             {
  145.                 msg.SCChanMessage(1, "*** Check Game Logic *** rpg_PlayerJoinGame found player INGame. Exiting method.");
  146.                 return;
  147.             }
  148.  
  149.             // Player Not found - register player here
  150.             if (rpg_Player == null)
  151.             {
  152.                 rpg_Player = rpg_MakeNewPlayer(PlayerName);
  153.                 msg.SCChanMessage(1,"A baby in BattleRPG has been born. Welcome baby " + PlayerName + "!");
  154.             }
  155.  
  156.             // Add player to ongoing game here - (PlayerList)
  157.             rpg_PlayerList.Add(rpg_Player);
  158.         }
  159.  
  160.         public void rpg_PlayerLeave(string PlayerName)
  161.         {
  162.             // Grab player info from list
  163.             ARPlayer rpg_Player = rpg_GetPlayer(PlayerName);
  164.  
  165.             // If the player was not playing we stop code here
  166.             // example being player left from a non spec ship
  167.             if (rpg_Player == null) return;
  168.  
  169.             // Update player on the DB if needed
  170.             DB_UpdatePlayerInfo(rpg_Player);
  171.  
  172.             // Take Player out of the ongoing game
  173.             // not sure if this is going to work
  174.             // may have to loop through list and take out that way
  175.             rpg_PlayerList.Remove(rpg_Player);
  176.         }
  177.  
  178.         // Have the player move to new area
  179.         public void rpg_MovePlayer(ARPlayer rpg_Player)
  180.         {
  181.             // Player is far left
  182.             if (rpg_Player.AreaName == 0)
  183.             {
  184.                 rpg_Player.AreaName++;
  185.                 return;
  186.             }
  187.  
  188.             // Player is far right
  189.             if (rpg_Player.AreaName == WorldMap.Length - 1)
  190.             {
  191.                 rpg_Player.AreaName--;
  192.                 return;
  193.             }
  194.  
  195.             // Player is not on either edge
  196.             if (DiceRoll(0, 100) < 50)
  197.             {
  198.                 rpg_Player.AreaName++;
  199.                 return;
  200.             }
  201.  
  202.             rpg_Player.AreaName--;
  203.         }
  204.  
  205.         //Get Random Player
  206.         public ARPlayer rpg_GetRandomPlayer()
  207.         {
  208.             return rpg_PlayerList[DiceRoll(0, rpg_PlayerList.Count)];
  209.         }
  210.  
  211.         // get player from player list using name
  212.         public ARPlayer rpg_GetPlayer(string PlayerName)
  213.         {
  214.             // Loop through list
  215.             for (int i = 0; i < rpg_PlayerList.Count; i+=1)
  216.             {
  217.                 // if there is a name match, send back player
  218.                 if (PlayerName == rpg_PlayerList[i].PlayerName)
  219.                     return rpg_PlayerList[i];
  220.             }
  221.  
  222.             // No matches were found, send back null
  223.             return null;
  224.         }
  225.  
  226.         // Create a player and set all default start variables here
  227.         public ARPlayer rpg_MakeNewPlayer(string PlayerName)
  228.         {
  229.             // set default values
  230.             // Maybe have options for a PREMIUM higher lvl start char?!?!?
  231.             ARPlayer np = new ARPlayer();
  232.             np.PlayerName = PlayerName;
  233.             np.Experience = 0;
  234.             np.Level = 0;
  235.  
  236.             // Possibly update database here to include new player
  237.             // --------------------- //
  238.  
  239.             // return configured player
  240.             return np;
  241.         }
  242.  
  243.         // Grab all the needed player info from db here
  244.         // If the player is not found we will return null
  245.         public ARPlayer DB_GetPlayer(string PlayerName)
  246.         {
  247.             // Player was not found - return empty (null)
  248.             return null;
  249.         }
  250.  
  251.         // Update the databse with new player stats
  252.         public void DB_UpdatePlayerInfo(ARPlayer rpg_Player)
  253.         {
  254.             // Update Player Info on DB
  255.         }
  256.  
  257.         // ---------------------------------- MISC FUNCTIONS ---------------------------------- //
  258.         // Join chat command for bot to send
  259.         string JoinChatCommand = "?chat=battledev";
  260.         public void ReJoinChatCheck()
  261.         {
  262.             // Safety chat rejoin in case bot lags out - NOT a crash
  263.             if ((DateTime.Now - ts_RejoinChats).TotalMinutes > delay_RejoinChats)
  264.             {
  265.                 // Update time stamp
  266.                 ts_RejoinChats = DateTime.Now;
  267.                 msg.SCPubCommand(JoinChatCommand);
  268.             }
  269.         }
  270.  
  271.         //Dices
  272.         // Our custom random method
  273.         public int DiceRoll(int MinValue, int MaxValue)
  274.         {
  275.             int[] intbag = new int[30];
  276.  
  277.             for (int i = 0; i < 30; i++)
  278.             {
  279.                 intbag[i] = random.Next(MinValue,MaxValue);
  280.             }
  281.             return intbag[random.Next(0,30)];
  282.         }
  283.  
  284.         public void InitializeBot()
  285.         {
  286.             //Configure and start main game timer
  287.             GameTimer.Elapsed += new ElapsedEventHandler(TimerMethod);
  288.             GameTimer.Interval = 10;
  289.             GameTimer.Start();
  290.  
  291.             //update chat with bot initialized msg
  292.             msg.SCChanMessage(1, BotName + " initialized.");
  293.         }
  294.  
  295.         public override void Dispose()
  296.         {
  297.             throw new NotImplementedException();
  298.         }
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment