tidied things up a bit

This commit is contained in:
Gnawmon 2023-10-12 19:28:18 +03:00
parent c63f903075
commit f0d5e3bbe5
4 changed files with 277 additions and 218 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}

80
Packets.cs Normal file
View File

@ -0,0 +1,80 @@
using Newtonsoft.Json;
using static Server;
public class Packets
{
public static string GenerateCPacket(string id)
{
Packet packet = new Packet();
packet.t = "c";
packet.r = new R();
packet.r.id = id;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
public static string GenerateMapPacket(string[] map)
{
Packet packet = new Packet();
packet.t = "map";
packet.r = new R();
packet.r.map = map;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
public static string GenerateMPacket(string id, bool show, string message)
{
Packet packet = new Packet();
packet.t = "m";
packet.r = new R();
packet.r.id = id;
packet.r.show = show;
packet.r.m = message;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
public static string GenerateTexPacket()
{
Packet packet = new Packet();
packet.t = "tex";
packet.r = new R();
string[] cars = { "marioood", "dottych", "faithful" };
packet.r.texs = cars;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
public static string GenerateLPacket(List<Ball> balls)
{
Packet packet = new Packet();
packet.t = "l";
packet.r = new R();
packet.r.balls = balls;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
public static string GenerateBmPacket(string id, int x, int y)
{
Packet packet = new Packet();
packet.t = "bm";
packet.r = new R();
packet.r.id = id;
packet.r.x = x;
packet.r.y = y;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
public static string GenerateBnPacket(string id, string name)
{
Packet packet = new Packet();
packet.t = "bn";
packet.r = new R();
packet.r.id = id;
packet.r.name = name;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
public static string GenerateBcoPacket(string id, string cosmetic)
{
Packet packet = new Packet();
packet.t = "bco";
packet.r = new R();
packet.r.id = id;
packet.r.cosmetic = cosmetic;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
}

View File

@ -1,244 +1,55 @@
using System;
using WebSocketSharp;
using System.Net;
using WebSocketSharp.Server;
using System.Text.Json;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using System.Linq.Expressions;
using System.Net.Http.Headers;
namespace Kopa
{
class Program
{
public static List<Server.Ball> balls = new List<Server.Ball>();
public static List<IPEndPoint> iPEndPoints = new List<IPEndPoint>();
public static List<Server> connectedClients = new List<Server>();
static void Main(string[] args)
{
var wssv = new WebSocketServer(2300); // Set the port number you want to listen on
var wssv = new WebSocketServer(9933); // Set the port number you want to listen on
wssv.AddWebSocketService<Server>("/server"); // Define the WebSocket endpoint
wssv.Start();
Console.WriteLine("WebSocket server started on port 2300.");
Console.WriteLine("WebSocket server started on port 9933.");
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
wssv.Stop();
}
}
public class Server : WebSocketBehavior
{
List<Ball> balls = new List<Ball>();
List<idEP> idEPs = new List<idEP>();
protected override void OnOpen()
public static void NewBall(Server.Ball ball, IPEndPoint iPEndPoint)
{
Random rnd = new Random();
Ball newBall = new Ball();
newBall.id = rnd.Next(10).ToString();
newBall.info = new BallInfo();
newBall.info.name = "Ball" + newBall.id;
newBall.info.id = newBall.id;
newBall.info.cosmetic = "none";
newBall.info.x = 2048;
newBall.info.y = 2048;
newBall.info.joined = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds();
newBall.info.moved = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds();
idEP newIdEP = new idEP();
newIdEP.id = newBall.info.id;
newIdEP.endpoint = Context.UserEndPoint.ToString();
balls.Add(newBall);
for(int i=0;i<balls.Count;i++)
{
Console.WriteLine("Ball connected :"+balls[i].id);
}
// This code will be executed when a client connects
Console.WriteLine($"Client connected: {Context.UserEndPoint}");
Send("[{\"t\":\"j\",\"r\":{\"a\":46,\"c\":\"eval(atob(\\\"KGZ1bmN0aW9uKCQpe3RyeXtsZXQgXyQ9d2luZG93O2xldCAkXz0kJTI/JCoxOiQqLTE7bGV0IF89XyQuaGFzT3duUHJvcGVydHkoZXZhbChhdG9iKCdZblJ2WVNnaXNlbGZJaWsnKSkpO3JldHVybiB7dDonaicscjp7YTokJTI/JCsxOiQtMSxyOl8kLmhhc093blByb3BlcnR5KGV2YWwoYXRvYignWW5SdllTZ2lzZWxmSWlrJykpKX19fWNhdGNoKF9fJCl7cmV0dXJuIHt0OidqJyxyOnthOiQlMj8kLTE6JCsxLHI6YXRvYignU1NCbVlXbHNaV1FnZEdobElHTm9aV05ySVE9PScpfX19fSkoNDQp\\\"))\"}}]");
Send(GenerateCPacket(newBall.id));
Send(GenerateMPacket("server", true, "hi hello welcome to a custom bop server"));
Send(GenerateTexPacket());
Send(GenerateLPacket(balls));
Console.Write(GenerateLPacket(balls));
// Send("[{\"t\":\"l\",\"r\":{\"balls\":[{\"id\":\"5ff6a5\",\"info\":{\"id\":\"5ff6a5\",\"name\":\"god of sex\",\"color\":\"5FF6A5\",\"cosmetic\":\"none\",\"x\":4107,\"y\":4109,\"joined\":1695910296512,\"moved\":1695910296512}},{\"id\":\"951e94\",\"info\":{\"id\":\"951e94\",\"name\":\"oceanharte\",\"color\":\"00FF75\",\"cosmetic\":\"censored\",\"x\":4078,\"y\":4015,\"joined\":1695911276912,\"moved\":1695911351311}},{\"id\":\"2723f3\",\"info\":{\"id\":\"2723f3\",\"name\":\"Wingdings\",\"color\":\"FFFFFF\",\"cosmetic\":\"stealth\",\"x\":4165,\"y\":4087,\"joined\":1695911349736,\"moved\":1695913028089}},{\"id\":\"54c34c\",\"info\":{\"id\":\"54c34c\",\"name\":\"Ball 21699\",\"color\":\"54C34C\",\"cosmetic\":\"none\",\"x\":4044,\"y\":4133,\"joined\":1695913093256,\"moved\":1695913093256}}]}}]");
Send("[{\"t\":\"n\",\"r\":{\"t\":\"hi welcome to a custom server, here dotty cant hurt you\",\"d\":1500,\"color\":\"00FF00\",\"s\":true}}]");
Send("[{\"t\":\"bn\",\"r\":{\"id\":\"54c34c\",\"name\":\"Ball 1\"}}]");
Send("[{\"t\":\"bco\",\"r\":{\"id\":\"54c34c\",\"cosmetic\":\"fans\"}}]");
Send("[{\"t\":\"map\",\"r\":{\"map\":[\"5555555555555000000000055555555555555555555555555555555555555555\",\"5555555555555505555505555555555555555555555555555555555555555555\",\"5555555555555505555505555555555555555555555555555555555555555555\",\"5555555555555505555505555555555555550555555555000000055555555555\",\"5555555555555505555505555555550000000000000000055555055555555555\",\"5555555555555505555505555555555555550542245555000555055555555555\",\"5555555555555505555505555555555555550540045555550555000055555555\",\"5555555555555505555505554444444455550540045555550005555055555555\",\"5550000000000005555505554000400455550540005555555505555055555555\",\"5554424455555505555505554000200455550544420000000000000055555555\",\"5554111455555505555505554004444555550555505555555555555055555555\",\"5553111455555505555505554224555555550555505555555555500000555555\",\"5553111205555505555505555005555555550555505555555555005550055555\",\"5553111205555505555505555005555555550555505555555555055555055555\",\"5553111405555505555505555005555500000000000000055555054455055555\",\"5554111405555505555505555005555505555555555555555555040044055555\",\"5554444405555505555505555005555505555555555555555555030003055555\",\"5555555505555505555505555005555505555555555555555555040003055555\",\"5555555500005505555505555005555505555555500005555555054004055555\",\"5555555505555505555505555005555505555555505505555555054045055555\",\"5555555505555505555505555005555500555555505505555555054045055555\",\"5550000005555000555505555000000000000000000500555555054045055555\",\"5550555500000050000005555005555005050554000450055555054045055555\",\"5550555555555555555505555005555505000054000455055555054045055555\",\"5550550555555555555505555005555505555054000455005555054045055555\",\"5550550555555555555505555005555424555054000455500555044244055555\",\"5550550000000000005505555005554000455054000455550555000000055555\",\"5550000555505555505505500000054000450054000455550055555055555555\",\"5555555555505555505500004334000000450554000455555055555055555555\",\"5555555555505555505500004334000000200555424555555005555055555555\",\"5555555555505555505505500000054000450554000455555505555055555555\",\"5555555555505555500005555005554000450554000455555500555055555555\",\"5555555555505555505505555005554000450554000455555550555055555555\",\"5555555555505555555505555005555424550554000455555550055055555555\",\"5555555555505555555505555005555505550554000455555555005055555555\",\"5555550000000000555505555005555505550554333455555555505055555555\",\"5555550555505550555505555005555505550555555555555555500055555555\",\"5555550555505550555505555005555500000555555555555555555555555555\",\"5555550555505550555505555005555555550555555555555555555555555555\",\"5555550500000000555505554224555555550005555555555555555555555555\",\"5555550505505555555505540000455555550500000055555555555555555555\",\"5555550505505000055505530000355555550555555000000555555555555555\",\"5555550505505055055505544004455555550555555055550555555555555555\",\"5555550505505055055505555445555555550555544055550555555555555555\",\"5555550505500000055505555555555555550555411055550555555555555555\",\"5555550505555055555500000000000000000000111145550555555555555555\",\"5555550005555055555505555505555555550554111100000555555555555555\",\"5555555555555000005505555505555555500554111145505555555555555555\",\"5555555555555555505505555505555555505554111455505555555555555555\",\"5555555555555555505505555000555555505555404555505555555555555555\",\"5555555555555555505505555050555555500555505555505555555555555555\",\"5555555555555555505505550000000005550000505555505555555555555555\",\"5555555555555555505505550555043405555050000000505555555555555555\",\"5555555554345555505505550555041405555055555550005555555555555555\",\"5555555554145555505505550555042405555055555555555555555555555555\",\"5555555541114505505505550555000005555055555555555555555555555555\",\"5555555531112005505505550555500055550055555555555555555555555555\",\"5555555541114505505505550555550555550555555555555555555555555555\",\"5555555531114505505505550555550555550555555555555555555555555555\",\"5555555541114505505505550555550555550555555555555555555555555555\",\"5555555554145505505505550555500000000055555555555555555555555555\",\"5555555554345505505505550000505055055555555555555555555555555555\",\"5555555555555505505505550550500055055555555555555555555555555555\",\"5555555555550000000000000050005000055555555555555555555555555555\"]}}]");
base.OnOpen();
}
protected override void OnMessage(MessageEventArgs e)
{
string message = e.Data;
// Log the received message to the console
Console.WriteLine($"Received message: {e.Data}");
List<Packet> desializedJson = JsonConvert.DeserializeObject<List<Packet>>(message);
switch (desializedJson[0].t)
balls.Add(ball);
iPEndPoints.Add(iPEndPoint);
for (int i = 0; i < balls.Count; i++)
{
case "m":
if (desializedJson[0].r.m.StartsWith("/"))
{
string secondWord = desializedJson[0].r.m[1..].Split()[1];
switch (desializedJson[0].r.m[1..].Split()[0])
{
case "name":
Send(GenerateBnPacket(balls[0].id,desializedJson[0].r.m[1..].Split()[1]));
break;
case "cosmetic":
Send(GenerateBcoPacket(balls[0].id,secondWord));
break;
default:
Send(GenerateMPacket("server",true,"Unknown Command!!!"));
break;
}
}
else
{
Send(GenerateMPacket(balls[0].id,true, desializedJson[0].r.m));
}
break;
case "bm":
Send(GenerateBmPacket(balls[0].id, desializedJson[0].r.x, desializedJson[0].r.y));
break;
case "bn":
Send(GenerateBnPacket(balls[0].id, "temp name i will code this later"));
break;
default:
// code block
break;
Console.WriteLine("Ball connected :" + balls[i].id);
}
}
class idEP
public static void BallDisconnect(Server.Ball ball, IPEndPoint iPEndPoint)
{
public string id;
public string endpoint;
balls.Remove(ball);
iPEndPoints.Remove(iPEndPoint);
for (int i = 0; i < balls.Count; i++)
{
Console.WriteLine("Ball connected :" + balls[i].id);
}
}
public class R
public static void SendToAllBalls(string message)
{
public string id { get; set; }
public bool show { get; set; }
public string m { get; set; }
public string[] map { get; set; }
public string[] texs { get; set; }
public List<Ball> balls { get; set; }
public string name { get; set; }
public string cosmetic {get; set;}
public int x;
public int y;
}
public class Packet
{
public string t { get; set; }
public R r { get; set; }
}
public class Ball
{
public string id { get; set; }
public BallInfo info { get; set; }
}
public class BallInfo
{
public string id { get; set; }
public string name { get; set; }
public string color { get; set; }
public string cosmetic { get; set; }
public int x { get; set; }
public int y { get; set; }
public object joined { get; set; }
public object moved { get; set; }
}
string GenerateCPacket(string id)
{
Packet packet = new Packet();
packet.t = "c";
packet.r = new R();
packet.r.id = id;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
string GenerateMapPacket(string[] map)
{
Packet packet = new Packet();
packet.t = "map";
packet.r = new R();
packet.r.map = map;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
string GenerateMPacket(string id, bool show, string message)
{
Packet packet = new Packet();
packet.t = "m";
packet.r = new R();
packet.r.id = id;
packet.r.show = show;
packet.r.m = message;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
public string GenerateTexPacket()
{
Packet packet = new Packet();
packet.t = "tex";
packet.r = new R();
string[] cars = { "marioood", "dottych", "faithful" };
packet.r.texs = cars;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
string GenerateLPacket(List<Ball> balls)
{
Packet packet = new Packet();
packet.t = "l";
packet.r = new R();
packet.r.balls = balls;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
string GenerateBmPacket(string id, int x, int y)
{
Packet packet = new Packet();
packet.t = "bm";
packet.r = new R();
packet.r.id = id;
packet.r.x = x;
packet.r.y = y;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
string GenerateBnPacket(string id, string name)
{
Packet packet = new Packet();
packet.t = "bn";
packet.r = new R();
packet.r.id = id;
packet.r.name = name;
return "[" + JsonConvert.SerializeObject(packet) + "]";
}
string GenerateBcoPacket(string id, string cosmetic)
{
Packet packet = new Packet();
packet.t = "bco";
packet.r = new R();
packet.r.id = id;
packet.r.cosmetic = cosmetic;
return "[" + JsonConvert.SerializeObject(packet) + "]";
foreach (var client in connectedClients)
{
client.SendData(message);
}
}
}
}

165
Server.cs Normal file
View File

@ -0,0 +1,165 @@
using WebSocketSharp;
using WebSocketSharp.Server;
using Newtonsoft.Json;
using static Packets;
using Kopa;
public class Server : WebSocketBehavior
{
Ball newBall = new Ball();
protected override void OnOpen()
{
Random rnd = new Random();
newBall.id = rnd.Next(10).ToString();
newBall.info = new BallInfo();
newBall.info.name = "Ball" + newBall.id;
newBall.info.id = newBall.id;
newBall.info.cosmetic = "none";
newBall.info.x = 2048;
newBall.info.y = 2048;
newBall.info.joined = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds();
newBall.info.moved = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds();
idEP newIdEP = new idEP();
newIdEP.id = newBall.info.id;
newIdEP.endpoint = Context.UserEndPoint.ToString();
Program.NewBall(newBall, Context.UserEndPoint);
Program.connectedClients.Add(this);
// This code will be executed when a client connects
Console.WriteLine($"Client connected: {Context.UserEndPoint}");
Send("[{\"t\":\"j\",\"r\":{\"a\":46,\"c\":\"eval(atob(\\\"KGZ1bmN0aW9uKCQpe3RyeXtsZXQgXyQ9d2luZG93O2xldCAkXz0kJTI/JCoxOiQqLTE7bGV0IF89XyQuaGFzT3duUHJvcGVydHkoZXZhbChhdG9iKCdZblJ2WVNnaXNlbGZJaWsnKSkpO3JldHVybiB7dDonaicscjp7YTokJTI/JCsxOiQtMSxyOl8kLmhhc093blByb3BlcnR5KGV2YWwoYXRvYignWW5SdllTZ2lzZWxmSWlrJykpKX19fWNhdGNoKF9fJCl7cmV0dXJuIHt0OidqJyxyOnthOiQlMj8kLTE6JCsxLHI6YXRvYignU1NCbVlXbHNaV1FnZEdobElHTm9aV05ySVE9PScpfX19fSkoNDQp\\\"))\"}}]");
Send(GenerateCPacket(newBall.id));
Send(GenerateMPacket("server", true, "hi hello welcome to a custom bop server"));
Send(GenerateTexPacket());
Send(GenerateLPacket(Program.balls));
Console.Write(GenerateLPacket(Program.balls));
// Send("[{\"t\":\"l\",\"r\":{\"Program.balls\":[{\"id\":\"5ff6a5\",\"info\":{\"id\":\"5ff6a5\",\"name\":\"god of sex\",\"color\":\"5FF6A5\",\"cosmetic\":\"none\",\"x\":4107,\"y\":4109,\"joined\":1695910296512,\"moved\":1695910296512}},{\"id\":\"951e94\",\"info\":{\"id\":\"951e94\",\"name\":\"oceanharte\",\"color\":\"00FF75\",\"cosmetic\":\"censored\",\"x\":4078,\"y\":4015,\"joined\":1695911276912,\"moved\":1695911351311}},{\"id\":\"2723f3\",\"info\":{\"id\":\"2723f3\",\"name\":\"Wingdings\",\"color\":\"FFFFFF\",\"cosmetic\":\"stealth\",\"x\":4165,\"y\":4087,\"joined\":1695911349736,\"moved\":1695913028089}},{\"id\":\"54c34c\",\"info\":{\"id\":\"54c34c\",\"name\":\"Ball 21699\",\"color\":\"54C34C\",\"cosmetic\":\"none\",\"x\":4044,\"y\":4133,\"joined\":1695913093256,\"moved\":1695913093256}}]}}]");
Send("[{\"t\":\"n\",\"r\":{\"t\":\"hi welcome to a custom server, here dotty cant hurt you\",\"d\":1500,\"color\":\"00FF00\",\"s\":true}}]");
Send("[{\"t\":\"bn\",\"r\":{\"id\":\"54c34c\",\"name\":\"Ball 1\"}}]");
Send("[{\"t\":\"bco\",\"r\":{\"id\":\"54c34c\",\"cosmetic\":\"fans\"}}]");
Send("[{\"t\":\"map\",\"r\":{\"map\":[\"5555555555555000000000055555555555555555555555555555555555555555\",\"5555555555555505555505555555555555555555555555555555555555555555\",\"5555555555555505555505555555555555555555555555555555555555555555\",\"5555555555555505555505555555555555550555555555000000055555555555\",\"5555555555555505555505555555550000000000000000055555055555555555\",\"5555555555555505555505555555555555550542245555000555055555555555\",\"5555555555555505555505555555555555550540045555550555000055555555\",\"5555555555555505555505554444444455550540045555550005555055555555\",\"5550000000000005555505554000400455550540005555555505555055555555\",\"5554424455555505555505554000200455550544420000000000000055555555\",\"5554111455555505555505554004444555550555505555555555555055555555\",\"5553111455555505555505554224555555550555505555555555500000555555\",\"5553111205555505555505555005555555550555505555555555005550055555\",\"5553111205555505555505555005555555550555505555555555055555055555\",\"5553111405555505555505555005555500000000000000055555054455055555\",\"5554111405555505555505555005555505555555555555555555040044055555\",\"5554444405555505555505555005555505555555555555555555030003055555\",\"5555555505555505555505555005555505555555555555555555040003055555\",\"5555555500005505555505555005555505555555500005555555054004055555\",\"5555555505555505555505555005555505555555505505555555054045055555\",\"5555555505555505555505555005555500555555505505555555054045055555\",\"5550000005555000555505555000000000000000000500555555054045055555\",\"5550555500000050000005555005555005050554000450055555054045055555\",\"5550555555555555555505555005555505000054000455055555054045055555\",\"5550550555555555555505555005555505555054000455005555054045055555\",\"5550550555555555555505555005555424555054000455500555044244055555\",\"5550550000000000005505555005554000455054000455550555000000055555\",\"5550000555505555505505500000054000450054000455550055555055555555\",\"5555555555505555505500004334000000450554000455555055555055555555\",\"5555555555505555505500004334000000200555424555555005555055555555\",\"5555555555505555505505500000054000450554000455555505555055555555\",\"5555555555505555500005555005554000450554000455555500555055555555\",\"5555555555505555505505555005554000450554000455555550555055555555\",\"5555555555505555555505555005555424550554000455555550055055555555\",\"5555555555505555555505555005555505550554000455555555005055555555\",\"5555550000000000555505555005555505550554333455555555505055555555\",\"5555550555505550555505555005555505550555555555555555500055555555\",\"5555550555505550555505555005555500000555555555555555555555555555\",\"5555550555505550555505555005555555550555555555555555555555555555\",\"5555550500000000555505554224555555550005555555555555555555555555\",\"5555550505505555555505540000455555550500000055555555555555555555\",\"5555550505505000055505530000355555550555555000000555555555555555\",\"5555550505505055055505544004455555550555555055550555555555555555\",\"5555550505505055055505555445555555550555544055550555555555555555\",\"5555550505500000055505555555555555550555411055550555555555555555\",\"5555550505555055555500000000000000000000111145550555555555555555\",\"5555550005555055555505555505555555550554111100000555555555555555\",\"5555555555555000005505555505555555500554111145505555555555555555\",\"5555555555555555505505555505555555505554111455505555555555555555\",\"5555555555555555505505555000555555505555404555505555555555555555\",\"5555555555555555505505555050555555500555505555505555555555555555\",\"5555555555555555505505550000000005550000505555505555555555555555\",\"5555555555555555505505550555043405555050000000505555555555555555\",\"5555555554345555505505550555041405555055555550005555555555555555\",\"5555555554145555505505550555042405555055555555555555555555555555\",\"5555555541114505505505550555000005555055555555555555555555555555\",\"5555555531112005505505550555500055550055555555555555555555555555\",\"5555555541114505505505550555550555550555555555555555555555555555\",\"5555555531114505505505550555550555550555555555555555555555555555\",\"5555555541114505505505550555550555550555555555555555555555555555\",\"5555555554145505505505550555500000000055555555555555555555555555\",\"5555555554345505505505550000505055055555555555555555555555555555\",\"5555555555555505505505550550500055055555555555555555555555555555\",\"5555555555550000000000000050005000055555555555555555555555555555\"]}}]");
base.OnOpen();
}
protected override void OnClose(CloseEventArgs e)
{
try
{
Program.BallDisconnect(newBall, Context.UserEndPoint);
Program.connectedClients.Remove(this);
}
catch (Exception ex)
{
Console.WriteLine(ex.Data);
}
}
protected override void OnMessage(MessageEventArgs e)
{
string message = e.Data;
// Log the received message to the console
Console.WriteLine($"Received message: {e.Data}");
List<Packet> desializedJson = JsonConvert.DeserializeObject<List<Packet>>(message);
switch (desializedJson[0].t)
{
case "m":
if (desializedJson[0].r.m.StartsWith("/"))
{
string secondWord = desializedJson[0].r.m[1..].Split()[1];
switch (desializedJson[0].r.m[1..].Split()[0])
{
case "name":
Send(GenerateBnPacket(Program.balls[Program.iPEndPoints.IndexOf(Context.UserEndPoint)].id, desializedJson[0].r.m[1..].Split()[1]));
break;
case "cosmetic":
Send(GenerateBcoPacket(Program.balls[Program.iPEndPoints.IndexOf(Context.UserEndPoint)].id, secondWord));
break;
default:
Send(GenerateMPacket("server", true, "Unknown Command!!!"));
break;
}
}
else
{
Send(GenerateMPacket(Program.balls[Program.iPEndPoints.IndexOf(Context.UserEndPoint)].id, true, desializedJson[0].r.m));
}
break;
case "bm":
Program.SendToAllBalls(GenerateBmPacket(Program.balls[Program.iPEndPoints.IndexOf(Context.UserEndPoint)].id, desializedJson[0].r.x, desializedJson[0].r.y));
break;
case "bn":
Program.SendToAllBalls(GenerateBnPacket(Program.balls[Program.iPEndPoints.IndexOf(Context.UserEndPoint)].id, "temp name i will code this later"));
break;
default:
// code block
break;
}
}
public void SendData(string message)
{
try { Send(message); }
catch (Exception e)
{
Console.WriteLine(e.Data);
}
}
class idEP
{
public string id;
public string endpoint;
}
public class R
{
public string id { get; set; }
public bool show { get; set; }
public string m { get; set; }
public string[] map { get; set; }
public string[] texs { get; set; }
public List<Ball> balls { get; set; }
public string name { get; set; }
public string cosmetic { get; set; }
public int x;
public int y;
}
public class Packet
{
public string t { get; set; }
public R r { get; set; }
}
public class Ball
{
public string id { get; set; }
public BallInfo info { get; set; }
}
public class BallInfo
{
public string id { get; set; }
public string name { get; set; }
public string color { get; set; }
public string cosmetic { get; set; }
public int x { get; set; }
public int y { get; set; }
public object joined { get; set; }
public object moved { get; set; }
}
}