Advertisement
PsyOps

BaseDuel.cs

May 16th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.17 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 Devastation.BaseDuel
  12. {
  13.     class BaseDuel
  14.     {
  15.         public BaseDuel(BaseManager bm)
  16.         {
  17.             this.m_GameMode = GameMode.AhmadMode;
  18.             this.msg = new ShortChat();
  19.             this.m_BaseManager = bm;
  20.             this.m_BlockedList = new List<string>();
  21.             this.m_GameStatus = GameStatus.GameIdle;
  22.             this.m_CountDownSeconds = 10;
  23.             this.m_CountDownShow = 3;
  24.             this.m_GameQEvents = new Queue<EventArgs>();
  25.             this.m_CountDown = new Timer();
  26.             this.m_TeamOutTimer = new Timer();
  27.             this.m_TeamOutTimerSetting = 5;
  28.  
  29.             this.m_CurrentGame = new BaseGame();
  30.             this.m_AlphaWaitList = new List<string>();
  31.             this.m_BravoWaitList = new List<string>();
  32.             this.m_AlphaFreq = 887;
  33.             this.m_BravoFreq = 889;
  34.         }
  35.  
  36.         private ShortChat msg;                      // My module to make sending messages easier
  37.         private GameMode m_GameMode;                // Think ill use this for preset settings
  38.         private BaseManager m_BaseManager;          // Controls base loading
  39.         private List<string> m_BlockedList;         // A way to prevent a player from interacting with baseduel
  40.         private GameStatus m_GameStatus;            // Current status of game
  41.         private Queue<EventArgs> m_GameQEvents;     // Any chat or commands that need to be sent using timer
  42.         private Timer m_CountDown;                  // Count Down Timer
  43.         private int m_CountDownSeconds;             // Seconds before game starts
  44.         private int m_CountDownSeconds_Elapsed;     // Used to count down - no need to set
  45.         private int m_CountDownShow;                // what part of the countdown you want to show. If set to (3) =>  3..2..1
  46.         private Timer m_TeamOutTimer;               // TeamOut timer
  47.         private int m_TeamOutTimerSetting;          // Seconds before TeamOut activates
  48.  
  49.         private BaseGame m_CurrentGame;
  50.         private ushort m_AlphaFreq, m_BravoFreq;
  51.         private List<string> m_AlphaWaitList, m_BravoWaitList;
  52.  
  53.         public EventArgs BaseTimer()
  54.         {
  55.             // Send next message/event in q
  56.             if (m_GameQEvents.Count > 0)
  57.             {
  58.                 return m_GameQEvents.Dequeue();
  59.             }
  60.             return null;
  61.         }
  62.  
  63.         // Shuffle the wait list.
  64.         public void Shuffle(ChatEvent c)
  65.         {
  66.             // Make sure the start timer isnt already running
  67.             if (m_CountDown.Enabled)
  68.             {
  69.                 m_GameQEvents.Enqueue(msg.pm(c.PlayerName, "Game Start Timer has started. You can not use this command at this time."));
  70.                 return;
  71.             }
  72.            
  73.             // Make sure command is used only before game
  74.             if (m_GameStatus != GameStatus.GameIdle)
  75.             {
  76.                 m_GameQEvents.Enqueue(msg.pm(c.PlayerName, "Game has started. You can not use this command at this time."));
  77.                 return;
  78.             }
  79.  
  80.             // Make sure there is at least 3 Players
  81.             if (m_AlphaWaitList.Count + m_BravoWaitList.Count > 2)
  82.             {
  83.                 ShuffleTeams();
  84.             }
  85.             else
  86.             {  
  87.                 m_GameQEvents.Enqueue(msg.pm(c.PlayerName, "You do not have enough players to use this command."));    
  88.             }
  89.         }
  90.  
  91.         private void StartGame()
  92.         {
  93.             if (m_GameStatus == GameStatus.GameIdle)
  94.             {
  95.                 m_CurrentGame = new BaseGame();
  96.                 m_CurrentGame.CreateFreqs(m_AlphaFreq, m_BravoFreq);
  97.                 m_CurrentGame.CreateTeams(m_AlphaWaitList, m_BravoWaitList);
  98.                
  99.             }
  100.             else if (m_GameStatus == GameStatus.GameIntermission)
  101.             {
  102.             }
  103.  
  104.             m_CurrentGame.WarpPlayersToStart(m_BaseManager.LoadedBase, m_GameQEvents);
  105.  
  106.             // Turn game status to inprogress
  107.             m_GameStatus = GameStatus.GameInProgress;
  108.         }
  109.  
  110.         private void EndGame(bool Alpha, WinType winType)
  111.         {
  112.             if (winType != WinType.NoCount)
  113.             {
  114.                 m_BaseManager.LoadNextBase();
  115.                 m_GameQEvents.Enqueue(msg.macro(("[ " + (Alpha ? "AlphaTeam" : "Bravo Team") + " ] wins [ " + winType + " ]. Game PrintOut here -")));
  116.                 m_CurrentGame.MatchOver(Alpha, winType);
  117.             }
  118.             else
  119.             {
  120.                 m_GameQEvents.Enqueue(msg.macro(("[ No Count ]. Game PrintOut here -")));
  121.             }
  122.  
  123.             m_GameStatus = GameStatus.GameIntermission;
  124.             m_GameQEvents.Enqueue(msg.macro("Match will begin in " + m_CountDownSeconds + " seconds."));
  125.             GameStartTimer();
  126.         }
  127.  
  128.         public Queue<EventArgs> GetTeamList(string PlayerName)
  129.         {
  130.             Queue<EventArgs> reply = new Queue<EventArgs>();
  131.  
  132.             reply.Enqueue(msg.arena("Alpha Wait List -------"));
  133.             for (int i = 0; i < m_AlphaWaitList.Count; i++)
  134.             { reply.Enqueue(msg.arena("Player [ "+m_AlphaWaitList[i]+" ]")); }
  135.  
  136.             reply.Enqueue(msg.arena("Bravo Wait List -------"));
  137.             for (int i = 0; i < m_BravoWaitList.Count; i++)
  138.             { reply.Enqueue(msg.arena("Player [ " + m_BravoWaitList[i] + " ]")); }
  139.  
  140.             return reply;
  141.         }
  142.  
  143.         public Queue<EventArgs> GetGameInfo(string PlayerName)
  144.         {
  145.             Queue<EventArgs> reply = new Queue<EventArgs>();
  146.             reply.Enqueue(msg.pm(PlayerName, "Game Settings -------------------------------"));
  147.             reply.Enqueue(msg.pm(PlayerName, "Current Game Status:".PadRight(20) + m_GameStatus.ToString().PadLeft(25)));
  148.             reply.Enqueue(msg.pm(PlayerName, "Setting/Mode:".PadRight(20) + m_GameMode.ToString().PadLeft(25)));
  149.             reply.Enqueue(msg.pm(PlayerName, "Alpha Team Freq:".PadRight(20) + m_AlphaFreq.ToString().PadLeft(25)));
  150.             reply.Enqueue(msg.pm(PlayerName, "Bravo Team Freq:".PadRight(20) + m_BravoFreq.ToString().PadLeft(25)));
  151.             reply.Enqueue(msg.pm(PlayerName, "Match Start Timer:".PadRight(20) + (m_CountDownSeconds.ToString() + " (seconds)").PadLeft(25)));
  152.             reply.Enqueue(msg.pm(PlayerName, "Start Timer Show:".PadRight(20) + (m_CountDownShow).ToString().PadLeft(25)));
  153.             reply.Enqueue(msg.pm(PlayerName, "All Out timer:".PadRight(20) + (m_TeamOutTimerSetting + " (seconds)").PadLeft(25)));
  154.             return reply;
  155.         }
  156.  
  157.         private void ShuffleTeams()
  158.         {
  159.             List<string> players = new List<string>();
  160.  
  161.             for (int i = 0; i < m_AlphaWaitList.Count; i++)
  162.             {   players.Add(m_AlphaWaitList[i]);   }
  163.             for (int i = 0; i < m_BravoWaitList.Count; i++)
  164.             {   players.Add(m_BravoWaitList[i]);   }
  165.  
  166.             Random ran = new Random();
  167.            
  168.             for (int h = 0; h < 10; h++ )
  169.             {
  170.                 for (int i = 0; i < players.Count; i++ )
  171.                 {
  172.                     string temp = players[i];
  173.                     int r = ran.Next(i, players.Count);
  174.                     players[i] = players[r];
  175.                     players[r] = temp;
  176.                 }
  177.             }
  178.  
  179.             m_AlphaWaitList = new List<string>();
  180.             m_BravoWaitList = new List<string>();
  181.  
  182.             while (players.Count > 0)
  183.             {
  184.                 m_GameQEvents.Enqueue(msg.pm(players[0], "?|setfreq 0|prize fullcharge"));
  185.                 players.RemoveAt(0);
  186.             }
  187.         }
  188.  
  189.         private void SendToWaitList(string PlayerName)
  190.         {
  191.             if (m_AlphaWaitList.Count <= m_BravoWaitList.Count)
  192.             {
  193.                 m_AlphaWaitList.Add(PlayerName);
  194.                 m_GameQEvents.Enqueue(msg.pm(PlayerName, "*setfreq " + m_AlphaFreq));
  195.             }
  196.             else
  197.             {
  198.                 m_BravoWaitList.Add(PlayerName);
  199.                 m_GameQEvents.Enqueue(msg.pm(PlayerName, "*setfreq " + m_BravoFreq));
  200.             }
  201.             m_GameQEvents.Enqueue(msg.pm(PlayerName, "?|shipreset"));
  202.         }
  203.         //----------------------------------------------------------------------//
  204.         //                         Events                                       //
  205.         //----------------------------------------------------------------------//
  206.         public void PlayerPositionUpdate(DevaPlayer d)
  207.         {
  208.             ushort pX = d.Position.MapPositionX;
  209.             ushort pY = d.Position.MapPositionY;
  210.  
  211.             // Ignore if on block list
  212.             if (m_BlockedList.Contains(d.PlayerName)) return;
  213.  
  214.             if (m_GameStatus == GameStatus.GameIdle)
  215.             {
  216.                 // Player is inside lobby area
  217.                 if (InRegion(pX, pY, m_BaseManager.Lobby.BaseDimension))
  218.                 {
  219.                     if (!InTeamFreq(d.Frequency))
  220.                     {
  221.                         if (!InWaitList(d.PlayerName))
  222.                         {
  223.                             // May need to move this - some tasks may not need warp timestamp
  224.                             if ((DateTime.Now - d.WarpTimeStamp).TotalMilliseconds <= 1500) return;
  225.  
  226.                             d.WarpTimeStamp = DateTime.Now;
  227.                             SendToWaitList(d.PlayerName);
  228.  
  229.                             return;
  230.                         }
  231.                     }
  232.                 }
  233.                 return;
  234.             }
  235.  
  236.             if (m_GameStatus == GameStatus.GameInProgress)
  237.             {
  238.                 bool alpha;
  239.                 BasePlayer b = m_CurrentGame.GetPlayer(d.PlayerName, out alpha);
  240.  
  241.                 if (b == null) return;
  242.  
  243.                 // In base area
  244.                 if (InRegion(pX, pY, m_BaseManager.LoadedBase.BaseDimension))
  245.                 {
  246.                     b.InLobby = false;
  247.  
  248.                     if ((alpha && InRegion(pX, pY, m_BaseManager.LoadedBase.BravoSafe) && d.Position.ShipState.IsSafe) ||
  249.                         (!alpha && InRegion(pX, pY, m_BaseManager.LoadedBase.AlphaSafe) && d.Position.ShipState.IsSafe))
  250.                     { EndGame(alpha,WinType.SafeWin); }
  251.                 }
  252.                 else
  253.                 {
  254.                     b.InLobby = true;
  255.                    
  256.                     if (!m_TeamOutTimer.Enabled)
  257.                         StartTeamOutTimer();
  258.                 }
  259.                 return;
  260.             }
  261.         }
  262.  
  263.         public Queue<EventArgs> PlayerLeftEvent(DevaPlayer b)
  264.         {
  265.             Queue<EventArgs> reply = new Queue<EventArgs>();
  266.  
  267.             m_AlphaWaitList.Remove(b.PlayerName);
  268.             m_BravoWaitList.Remove(b.PlayerName);
  269.  
  270.             return reply;
  271.         }
  272.  
  273.         public Queue<EventArgs> PlayerShipChange(DevaPlayer b)
  274.         {
  275.             Queue<EventArgs> reply = new Queue<EventArgs>();
  276.  
  277.             if (b.Ship == ShipTypes.Spectator)
  278.             {
  279.                 m_AlphaWaitList.Remove(b.PlayerName);
  280.                 m_BravoWaitList.Remove(b.PlayerName);
  281.             }
  282.  
  283.             return reply;
  284.         }
  285.  
  286.         public Queue<EventArgs> PlayerFreqChange(DevaPlayer b)
  287.         {
  288.             Queue<EventArgs> reply = new Queue<EventArgs>();
  289.  
  290.             if (InTeamFreq(b.OldFrequency))
  291.             {
  292.                 m_AlphaWaitList.Remove(b.PlayerName);
  293.                 m_BravoWaitList.Remove(b.PlayerName);
  294.             }
  295.  
  296.             return reply;
  297.         }
  298.         //----------------------------------------------------------------------//
  299.         //                         Timers                                       //
  300.         //----------------------------------------------------------------------//
  301.         private void StartTeamOutTimer()
  302.         {
  303.             m_TeamOutTimer = new Timer();
  304.             m_TeamOutTimer.Elapsed += new ElapsedEventHandler(TeamOutTimer);
  305.             m_TeamOutTimer.Interval = m_TeamOutTimerSetting * 1000;
  306.             m_TeamOutTimer.Start();
  307.         }
  308.  
  309.         private void TeamOutTimer(object source, ElapsedEventArgs e)
  310.         {
  311.             m_TeamOutTimer.Stop();
  312.             bool alphaOut;
  313.             bool bravoOut;
  314.  
  315.             if (m_CurrentGame.TeamIsOut(out alphaOut, out bravoOut))
  316.             {
  317.                 if (alphaOut != bravoOut) EndGame(!alphaOut, WinType.AllOut);
  318.                 else EndGame(true,WinType.NoCount);
  319.             }
  320.         }
  321.  
  322.         public void StartBDCommand()
  323.         {
  324.             // Command can only be used before a game gets started
  325.             if (m_GameStatus != GameStatus.GameIdle) return;
  326.             m_GameQEvents.Enqueue(msg.macro("Game will begin in " + m_CountDownSeconds + " seconds."));
  327.             GameStartTimer();
  328.         }
  329.  
  330.         private void GameStartTimer()
  331.         {
  332.             m_CountDownSeconds_Elapsed = m_CountDownSeconds;
  333.             m_CountDown = new Timer();
  334.             m_CountDown.Elapsed += new ElapsedEventHandler(CountDownTimer);
  335.             m_CountDown.Interval = 1000;
  336.             m_CountDown.Start();
  337.         }
  338.  
  339.         private void CountDownTimer(object source, ElapsedEventArgs e)
  340.         {
  341.             m_CountDownSeconds_Elapsed--;
  342.             if (m_CountDownSeconds_Elapsed <= m_CountDownShow && m_CountDownSeconds_Elapsed != 0)
  343.             {
  344.                 m_GameQEvents.Enqueue(msg.macro("- " + m_CountDownSeconds_Elapsed + " -"));
  345.                 m_GameQEvents.Enqueue(msg.arena("", SoundCodes.MessageAlarm));
  346.             }
  347.  
  348.             if (m_CountDownSeconds_Elapsed > 0) return;
  349.  
  350.             m_GameQEvents.Enqueue(msg.arena("-Go Go Go-", SoundCodes.Goal));
  351.             m_CountDown.Stop();
  352.             StartGame();
  353.         }
  354.  
  355.         //----------------------------------------------------------------------//
  356.         //                         Misc                                         //
  357.         //----------------------------------------------------------------------//
  358.         private bool InRegion(ushort x, ushort y, ushort[] region)
  359.         {
  360.             return (x >= region[0] && x <= region[2] && y >= region[1] && y <= region[3]);
  361.         }
  362.  
  363.         private bool InTeamFreq(ushort freq)
  364.         {
  365.             return freq == m_AlphaFreq || freq == m_BravoFreq;
  366.         }
  367.  
  368.         private bool InWaitList(string PlayerName)
  369.         {
  370.             return m_AlphaWaitList.Contains(PlayerName) || m_AlphaWaitList.Contains(PlayerName);
  371.         }
  372.  
  373.         //private BasePlayer GetBasePlayer(string PlayerName, out bool Alpha)
  374.         //{
  375.         //    BasePlayer p = m_AlphaTeam.TeamList.Find(item => item.PlayerName == PlayerName);
  376.  
  377.         //    if (p != null)
  378.         //    {
  379.         //        Alpha = true;
  380.         //        return p;
  381.         //    }
  382.  
  383.         //    Alpha = false;
  384.         //    return m_BravoTeam.TeamList.Find(item => item.PlayerName == PlayerName);
  385.         //}
  386.  
  387.         //private void TeamOutCheck()
  388.         //{
  389.         //    bool A_allOut = m_AlphaTeam.AllOut;
  390.         //    bool B_allOut = m_BravoTeam.AllOut;
  391.  
  392.         //    if ((A_allOut || B_allOut) && !m_TeamOutTimer.Enabled)
  393.         //    { StartTeamOutTimer(); }
  394.         //}
  395.     }
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement