context: /*
  	Copyright 2011 MCForge
  		
  	Dual-licensed under the	Educational Community License, Version 2.0 and
  	the GNU General Public License, Version 3 (the "Licenses"); you may
  	not use this file except in compliance with the Licenses. You may
  	obtain a copy of the Licenses at
  	
  	http://www.opensource.org/licenses/ecl2.php
  	http://www.gnu.org/licenses/gpl-3.0.html
  	
  	Unless required by applicable law or agreed to in writing,
  	software distributed under the Licenses are distributed on an "AS IS"
  	BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  	or implied. See the Licenses for the specific language governing
  	permissions and limitations under the Licenses.
  */
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading;
  namespace MCForge
  {
      public sealed class CountdownGame
      {
          public static List<Player> players = new List<Player>();
          public static List<Player> playersleftlist = new List<Player>();
          public static List<string> squaresleft = new List<string>();
          public static Level mapon;
  
          public static int playersleft;
          public static int speed;
  
          public static bool freezemode = false;
          public static bool cancel = false;
  
          public static string speedtype;
  
          public static CountdownGameStatus gamestatus = CountdownGameStatus.Disabled;
  
          //private static ushort[] x; // this is useless?
  
          public static void GameStart(Player p)
          {
              switch (gamestatus)
              {
                  case CountdownGameStatus.Disabled:
                      Player.SendMessage(p, "Please enable Countdown first!!");
                      return;
  
                  case CountdownGameStatus.AboutToStart:
                      Player.SendMessage(p, "Game is about to start");
                      return;
  
                  case CountdownGameStatus.InProgress:
                      Player.SendMessage(p, "Game is already in progress");
                      return;
  
                  case CountdownGameStatus.Finished:
                      Player.SendMessage(p, "Game has finished");
                      return;
  
                  case CountdownGameStatus.Enabled:
                      gamestatus = CountdownGameStatus.AboutToStart;
                      Thread.Sleep(2000);
                      break;
              }
              {
                  {
                      mapon.Blockchange(15, 27, 16, Block.glass);
                      mapon.Blockchange(16, 27, 15, Block.glass);
                      mapon.Blockchange(16, 27, 16, Block.glass);
                      mapon.Blockchange(15, 27, 15, Block.glass);
                  }
                  {
                      {
                          mapon.Blockchange(15, 18, 14, Block.glass);
                          mapon.Blockchange(16, 18, 14, Block.glass);
                          mapon.Blockchange(15, 17, 14, Block.glass);
                          mapon.Blockchange(16, 17, 14, Block.glass);
                      }
                      {
                          mapon.Blockchange(14, 17, 15, Block.glass);
                          mapon.Blockchange(14, 18, 16, Block.glass);
                          mapon.Blockchange(14, 17, 16, Block.glass);
                          mapon.Blockchange(14, 18, 15, Block.glass);
                      }
                      {
                          mapon.Blockchange(15, 17, 17, Block.glass);
                          mapon.Blockchange(16, 18, 17, Block.glass);
                          mapon.Blockchange(15, 18, 17, Block.glass);
                          mapon.Blockchange(16, 17, 17, Block.glass);
                      }
                      {
                          mapon.Blockchange(17, 17, 16, Block.glass);
                          mapon.Blockchange(17, 18, 15, Block.glass);
                          mapon.Blockchange(17, 18, 16, Block.glass);
                          mapon.Blockchange(17, 17, 15, Block.glass);
                      }
                  }
                  {
                      mapon.Blockchange(16, 16, 15, Block.glass);
                      mapon.Blockchange(15, 16, 16, Block.glass);
                      mapon.Blockchange(15, 16, 15, Block.glass);
                      mapon.Blockchange(16, 16, 16, Block.glass);
                  }
              }
              mapon.ChatLevel("Countdown is about to start!!");
              mapon.permissionbuild = LevelPermission.Nobody;
              ushort x1 = (ushort)((15.5) * 32);
              ushort y1 = (ushort)((30) * 32);
              ushort z1 = (ushort)((15.5) * 32);
              foreach (Player player in players)
              {
                  if (player.level != mapon)
                  {
                      player.SendMessage("Sending you to the correct map.");
                      Command.all.Find("goto").Use(player, mapon.name);
                      Thread.Sleep(1000);
                      // Sleep for a bit while they load
                      while (player.Loading) { Thread.Sleep(250); }
                  }
                  unchecked
                  {
                      player.SendSpawn((byte)-1, player.name, x1, y1, z1, (byte)0, (byte)0);
                  }
              }
              {
                  CountdownGame.squaresleft.Clear();
                  PopulateSquaresLeft();
                  if (freezemode == true)
                  {
                      mapon.ChatLevel("Countdown starting with difficulty " + speedtype + " and mode freeze in:");
                  }
                  else
                  {
                      mapon.ChatLevel("Countdown starting with difficulty " + speedtype + " and mode normal in:");
                  }
                  Thread.Sleep(2000);
                  mapon.ChatLevel("-----&b5" + Server.DefaultColor + "-----");
                  {
                      mapon.Blockchange(16, 16, 15, Block.air);
                      mapon.Blockchange(15, 16, 16, Block.air);
                      mapon.Blockchange(15, 16, 15, Block.air);
                      mapon.Blockchange(16, 16, 16, Block.air);
                  }
                  Thread.Sleep(1000);
                  mapon.ChatLevel("-----&b4" + Server.DefaultColor + "-----");
                  Thread.Sleep(1000);
                  mapon.ChatLevel("-----&b3" + Server.DefaultColor + "-----");
                  Thread.Sleep(1000);
                  {
                      mapon.Blockchange(15, 27, 16, Block.air);
                      mapon.Blockchange(16, 27, 15, Block.air);
                      mapon.Blockchange(16, 27, 16, Block.air);
                      mapon.Blockchange(15, 27, 15, Block.air);
                  }
                  mapon.ChatLevel("-----&b2" + Server.DefaultColor + "-----");
                  Thread.Sleep(1000);
                  mapon.ChatLevel("-----&b1" + Server.DefaultColor + "-----");
                  Thread.Sleep(1000);
                  mapon.ChatLevel("GO!!!!!!!");
              }
              {
                  playersleft = players.Count();
                  playersleftlist = players;
                  foreach (Player plya in players)
                  {
                      plya.incountdown = true;
                  }
              }
              AfterStart();
              Play();
          }
  
          public static void Play()
          {
              if (freezemode == false)
              {
                  while (squaresleft.Any() && playersleft != 0 && (gamestatus == CountdownGameStatus.InProgress || gamestatus == CountdownGameStatus.Finished))
                  {
                      Random number = new Random();
                      int randnum = number.Next(squaresleft.Count);
                      string nextsquare = squaresleft.ElementAt(randnum);
                      squaresleft.Remove(nextsquare);
                      RemoveSquare(nextsquare);
                      if (squaresleft.Count % 10 == 0 && gamestatus != CountdownGameStatus.Finished)
                      {
                          mapon.ChatLevel(squaresleft.Count + " Squares Left and " + playersleft.ToString() + " Players left!!");
                      }
                      if (cancel == true)
                      {
                          End(null);
                      }
                  }
                  return;
              }
              else
              {
                  {//Find yo places stuff (15 seconds)
                      Thread.Sleep(500);
                      MessagePlayers("Welcome to Freeze Mode of countdown");
                      MessagePlayers("You have 15 seconds to stand on a square");
                      Thread.Sleep(500);
                      MessagePlayers("-----&b15" + Server.DefaultColor + "-----");
                      Thread.Sleep(500);
                      MessagePlayers("Once the countdown is up, you are stuck on your square");
                      Thread.Sleep(500);
                      MessagePlayers("-----&b14" + Server.DefaultColor + "-----");
                      Thread.Sleep(500);
                      MessagePlayers("The squares then start to dissapear");
                      Thread.Sleep(500);
                      MessagePlayers("-----&b13" + Server.DefaultColor + "-----");
                      Thread.Sleep(500);
                      MessagePlayers("Whoever is last out wins!!");
                      Thread.Sleep(500);
                      MessagePlayers("-----&b12" + Server.DefaultColor + "-----");
                      Thread.Sleep(1000);
                      MessagePlayers("-----&b11" + Server.DefaultColor + "-----");
                      Thread.Sleep(1000);
                      MessagePlayers("-----&b10" + Server.DefaultColor + "-----");
                      MessagePlayers("Only 10 Seconds left to pick your places!!");
                      Thread.Sleep(1000);
                      MessagePlayers("-----&b9" + Server.DefaultColor + "-----");
                      Thread.Sleep(1000);
                      MessagePlayers("-----&b8" + Server.DefaultColor + "-----");
                      Thread.Sleep(1000);
                      MessagePlayers("-----&b7" + Server.DefaultColor + "-----");
                      Thread.Sleep(1000);
                      MessagePlayers("-----&b6" + Server.DefaultColor + "-----");
                      Thread.Sleep(1000);
                      MessagePlayers("-----&b5" + Server.DefaultColor + "-----");
                      MessagePlayers("5 Seconds left to pick your places!!");
                      Thread.Sleep(1000);
                      MessagePlayers("-----&b4" + Server.DefaultColor + "-----");
                      Thread.Sleep(1000);
                      MessagePlayers("-----&b3" + Server.DefaultColor + "-----");
                      Thread.Sleep(1000);
                      MessagePlayers("-----&b2" + Server.DefaultColor + "-----");
                      Thread.Sleep(1000);
  
gt:                     MessagePlayers("-----&b1" + Server.DefaultColor + "-----");