Advertisement
HabboGame

NIMG Magnets Server Code

May 5th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using PlayerIO.GameLibrary;
  7.  
  8. namespace NIMGMagnets {
  9.     public class Magnet {
  10.         public char Character { get; }
  11.         public int X { get; private set; }
  12.         public int Y { get; private set; }
  13.         public Magnet (char character, int x, int y) {
  14.             Character = character;
  15.             X = x;
  16.             Y = y;
  17.         }
  18.         public void SetLocation (int x, int y) {
  19.             X = x;
  20.             Y = y;
  21.         }
  22.     }
  23.     public class MagnetSet {
  24.         public Magnet[] Magnets { get; }
  25.         public byte Color { get; }
  26.         public MagnetSet (string characters, byte color, int y) {
  27.             Color = color;
  28.             char[] charactersArray = characters.ToCharArray ();
  29.             Magnets = new Magnet[charactersArray.Length];
  30.             for (int i = 0; i < charactersArray.Length; i++) {
  31.                 Magnets[i] = new Magnet (charactersArray[i], (i * 16) + 4, (y * 16) + 4);
  32.             }
  33.         }
  34.     }
  35.     public class Player : BasePlayer {
  36.         public string Username { get; set; }
  37.         public bool Inited { get; set; }
  38.     }
  39.     [RoomType ("Game1")]
  40.     public class Room : Game<Player> {
  41.         public string characterSet;
  42.         public string GetColorString (MagnetSet magnetSet) {
  43.             switch (magnetSet.Color) {
  44.                 case 0:
  45.                     return "default";
  46.                 case 1:
  47.                     return "blue";
  48.                 case 2:
  49.                     return "red";
  50.                 case 3:
  51.                     return "green";
  52.                 case 4:
  53.                     return "yellow";
  54.                 case 5:
  55.                     return "magenta";
  56.                 case 6:
  57.                     return "orange";
  58.                 case 7:
  59.                     return "cyan";
  60.                 case 8:
  61.                     return "pink";
  62.                 case 9:
  63.                     return "brown";
  64.                 case 10:
  65.                     return "gray";
  66.                 default: // To prevent compile-time error
  67.                     return string.Empty;
  68.             }
  69.         }
  70.         public Message Init () {
  71.             Message message = Message.Create ("init", characterSet);
  72.             foreach (MagnetSet magnetSet in magnetSets) {
  73.                 message.Add (GetColorString (magnetSet));
  74.                 foreach (Magnet magnet in magnetSet.Magnets) {
  75.                     message.Add (magnet.X, magnet.Y);
  76.                 }
  77.             }
  78.             return message;
  79.         }
  80.         public bool created = false;
  81.         public MagnetSet[] magnetSets;
  82.         public override void GameStarted () {
  83.             System.Timers.Timer timer = new System.Timers.Timer ();
  84.             timer.Elapsed += (s, e) => {
  85.                 if (!created) {
  86.                     Broadcast ("error", "timeout");
  87.                     Visible = false;
  88.                     ForEachPlayer ((pl) => { pl.Disconnect (); });
  89.                     timer.Stop ();
  90.                 }
  91.             };
  92.             timer.Interval = 10000;
  93.             timer.Start ();
  94.         }
  95.         public override void GotMessage (Player player, Message message) {
  96.             switch (message.Type) {
  97.                 case "create":
  98.                     if (!created) try {
  99.                             byte[] colorByteArray = message.GetByteArray (1);
  100.                             foreach (byte color in colorByteArray) if (color > 10) throw new Exception ();
  101.                             magnetSets = new MagnetSet[colorByteArray.Length];
  102.                             Array.Sort (colorByteArray);
  103.                             string selection = message.GetString (0);
  104.                             string magnetSetCreationString;
  105.                             switch (selection) {
  106.                                 case "A":
  107.                                     magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  108.                                     break;
  109.                                 case "Aa":
  110.                                     magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  111.                                     break;
  112.                                 case "A!":
  113.                                     magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&@/()[]{}+-*^=.,:;~";
  114.                                     break;
  115.                                 case "A0":
  116.                                     magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  117.                                     break;
  118.                                 case "a":
  119.                                     magnetSetCreationString = "abcdefghijklmnopqrstuvwxyz";
  120.                                     break;
  121.                                 case "a!":
  122.                                     magnetSetCreationString = "abcdefghijklmnopqrstuvwxyz!\"#$%&@/()[]{}+-*^=.,:;~";
  123.                                     break;
  124.                                 case "a0":
  125.                                     magnetSetCreationString = "abcdefghijklmnopqrstuvwxyz0123456789";
  126.                                     break;
  127.                                 case "!":
  128.                                     magnetSetCreationString = "!\"#$%&@/()[]{}+-*^=.,:;~";
  129.                                     break;
  130.                                 case "!0":
  131.                                     magnetSetCreationString = "!\"#$%&@/()[]{}+-*^=.,:;~0123456789";
  132.                                     break;
  133.                                 case "0":
  134.                                     magnetSetCreationString = "0123456789";
  135.                                     break;
  136.                                 case "Aa!":
  137.                                     magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$%&@/()[]{}+-*^=.,:;~";
  138.                                     break;
  139.                                 case "Aa0":
  140.                                     magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  141.                                     break;
  142.                                 case "A!0":
  143.                                     magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&@/()[]{}+-*^=.,:;~0123456789";
  144.                                     break;
  145.                                 case "a!0":
  146.                                     magnetSetCreationString = "abcdefghijklmnopqrstuvwxyz!\"#$%&@/()[]{}+-*^=.,:;~0123456789";
  147.                                     break;
  148.                                 case "Aa!0":
  149.                                     magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$%&@/()[]{}+-*^=.,:;~0123456789";
  150.                                     break;
  151.                                 default:
  152.                                     throw new Exception ("invalid character option");
  153.                             }
  154.                             characterSet = magnetSetCreationString;
  155.                             for (int i = 0; i < magnetSets.Length; i++) for (int j = 0; j < magnetSets[i].Magnets.Length; j++) magnetSets[i].Magnets[j].SetLocation (i * 12 + 3, j * 12 + 3);
  156.                             Broadcast (Init ());
  157.                             ForEachPlayer ((pl) => { pl.Inited = true; });
  158.                             created = true;
  159.                         } catch {
  160.                             created = false;
  161.                             Broadcast ("error", "invalid");
  162.                             Visible = false;
  163.                             ForEachPlayer ((pl) => { pl.Disconnect (); });
  164.                         }
  165.                     break;
  166.                 case "init":
  167.                     if (created && !player.Inited) {
  168.                         player.Send (Init ());
  169.                         player.Inited = true;
  170.                     } else if (!created) {
  171.                         player.Send ("error", "not created");
  172.                     } else if (player.Inited) player.Send ("error", "already inited");
  173.                     break;
  174.             }
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement