Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using PlayerIO.GameLibrary;
- namespace NIMGMagnets {
- public class Magnet {
- public char Character { get; }
- public int X { get; private set; }
- public int Y { get; private set; }
- public Magnet (char character, int x, int y) {
- Character = character;
- X = x;
- Y = y;
- }
- public void SetLocation (int x, int y) {
- X = x;
- Y = y;
- }
- }
- public class MagnetSet {
- public Magnet[] Magnets { get; }
- public byte Color { get; }
- public MagnetSet (string characters, byte color, int y) {
- Color = color;
- char[] charactersArray = characters.ToCharArray ();
- Magnets = new Magnet[charactersArray.Length];
- for (int i = 0; i < charactersArray.Length; i++) {
- Magnets[i] = new Magnet (charactersArray[i], (i * 16) + 4, (y * 16) + 4);
- }
- }
- }
- public class Player : BasePlayer {
- public string Username { get; set; }
- public bool Inited { get; set; }
- }
- [RoomType ("Game1")]
- public class Room : Game<Player> {
- public string characterSet;
- public string GetColorString (MagnetSet magnetSet) {
- switch (magnetSet.Color) {
- case 0:
- return "default";
- case 1:
- return "blue";
- case 2:
- return "red";
- case 3:
- return "green";
- case 4:
- return "yellow";
- case 5:
- return "magenta";
- case 6:
- return "orange";
- case 7:
- return "cyan";
- case 8:
- return "pink";
- case 9:
- return "brown";
- case 10:
- return "gray";
- default: // To prevent compile-time error
- return string.Empty;
- }
- }
- public Message Init () {
- Message message = Message.Create ("init", characterSet);
- foreach (MagnetSet magnetSet in magnetSets) {
- message.Add (GetColorString (magnetSet));
- foreach (Magnet magnet in magnetSet.Magnets) {
- message.Add (magnet.X, magnet.Y);
- }
- }
- return message;
- }
- public bool created = false;
- public MagnetSet[] magnetSets;
- public override void GameStarted () {
- System.Timers.Timer timer = new System.Timers.Timer ();
- timer.Elapsed += (s, e) => {
- if (!created) {
- Broadcast ("error", "timeout");
- Visible = false;
- ForEachPlayer ((pl) => { pl.Disconnect (); });
- timer.Stop ();
- }
- };
- timer.Interval = 10000;
- timer.Start ();
- }
- public override void GotMessage (Player player, Message message) {
- switch (message.Type) {
- case "create":
- if (!created) try {
- byte[] colorByteArray = message.GetByteArray (1);
- foreach (byte color in colorByteArray) if (color > 10) throw new Exception ();
- magnetSets = new MagnetSet[colorByteArray.Length];
- Array.Sort (colorByteArray);
- string selection = message.GetString (0);
- string magnetSetCreationString;
- switch (selection) {
- case "A":
- magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- break;
- case "Aa":
- magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- break;
- case "A!":
- magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&@/()[]{}+-*^=.,:;~";
- break;
- case "A0":
- magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- break;
- case "a":
- magnetSetCreationString = "abcdefghijklmnopqrstuvwxyz";
- break;
- case "a!":
- magnetSetCreationString = "abcdefghijklmnopqrstuvwxyz!\"#$%&@/()[]{}+-*^=.,:;~";
- break;
- case "a0":
- magnetSetCreationString = "abcdefghijklmnopqrstuvwxyz0123456789";
- break;
- case "!":
- magnetSetCreationString = "!\"#$%&@/()[]{}+-*^=.,:;~";
- break;
- case "!0":
- magnetSetCreationString = "!\"#$%&@/()[]{}+-*^=.,:;~0123456789";
- break;
- case "0":
- magnetSetCreationString = "0123456789";
- break;
- case "Aa!":
- magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$%&@/()[]{}+-*^=.,:;~";
- break;
- case "Aa0":
- magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- break;
- case "A!0":
- magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&@/()[]{}+-*^=.,:;~0123456789";
- break;
- case "a!0":
- magnetSetCreationString = "abcdefghijklmnopqrstuvwxyz!\"#$%&@/()[]{}+-*^=.,:;~0123456789";
- break;
- case "Aa!0":
- magnetSetCreationString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$%&@/()[]{}+-*^=.,:;~0123456789";
- break;
- default:
- throw new Exception ("invalid character option");
- }
- characterSet = magnetSetCreationString;
- 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);
- Broadcast (Init ());
- ForEachPlayer ((pl) => { pl.Inited = true; });
- created = true;
- } catch {
- created = false;
- Broadcast ("error", "invalid");
- Visible = false;
- ForEachPlayer ((pl) => { pl.Disconnect (); });
- }
- break;
- case "init":
- if (created && !player.Inited) {
- player.Send (Init ());
- player.Inited = true;
- } else if (!created) {
- player.Send ("error", "not created");
- } else if (player.Inited) player.Send ("error", "already inited");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement