diff options
| author | Michael <[email protected]> | 2017-01-08 09:57:10 -0500 |
|---|---|---|
| committer | Michael <[email protected]> | 2017-01-08 09:57:10 -0500 |
| commit | f30dcf5ef41d54c588d7b42c48be8d941abba72e (patch) | |
| tree | 7705f99b965673b1c034ac2b1c56e65072c827df /ShiftOS_TheReturn/ServerManager.cs | |
| parent | 69dfad54724d4176dfce238a8d7e73970e6eef24 (diff) | |
| download | shiftos_thereturn-f30dcf5ef41d54c588d7b42c48be8d941abba72e.tar.gz shiftos_thereturn-f30dcf5ef41d54c588d7b42c48be8d941abba72e.tar.bz2 shiftos_thereturn-f30dcf5ef41d54c588d7b42c48be8d941abba72e.zip | |
Initial upload
Diffstat (limited to 'ShiftOS_TheReturn/ServerManager.cs')
| -rw-r--r-- | ShiftOS_TheReturn/ServerManager.cs | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/ShiftOS_TheReturn/ServerManager.cs b/ShiftOS_TheReturn/ServerManager.cs new file mode 100644 index 0000000..3d654e8 --- /dev/null +++ b/ShiftOS_TheReturn/ServerManager.cs @@ -0,0 +1,177 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShiftOS.Objects; +using NetSockets; +using System.Windows.Forms; +using System.Threading; +using ShiftOS; +using static ShiftOS.Engine.SaveSystem; +using Newtonsoft.Json; + +namespace ShiftOS.Engine +{ + public static class ServerManager + { + public static void PrintDiagnostics() + { + Console.WriteLine($@"{{CLIENT_DIAGNOSTICS}} + +{{GUID}}: {thisGuid} +{{CLIENT_DATA}}: + +{JsonConvert.SerializeObject(client, Formatting.Indented)}"); + } + + private static Guid thisGuid { get; set; } + private static NetObjectClient client { get; set; } + + public static void Disconnect() + { + if (client != null) + { + client.Disconnect(); + } + Disconnected?.Invoke(); + + } + + public static event EmptyEventHandler Disconnected; + + public static void InitiateMUDHack() + { + MessageReceived += ServerManager_MessageReceived; + SendMessage("mudhack_init", ""); + } + + public static event Action<string> ServerPasswordGenerated; + public static event EmptyEventHandler ServerAccessGranted; + public static event EmptyEventHandler ServerAccessDenied; + public static event Action<string> GUIDReceived; + public static event Action<List<OnlineUser>> UsersReceived; + + private static void ServerManager_MessageReceived(ServerMessage msg) + { + switch(msg.Name) + { + case "mudhack_users": + UsersReceived?.Invoke(JsonConvert.DeserializeObject<List<OnlineUser>>(msg.Contents)); + break; + case "mudhack_init": + ServerPasswordGenerated?.Invoke(msg.Contents); + break; + case "mudhack_denied": + ServerAccessDenied?.Invoke(); + break; + case "mudhack_granted": + ServerAccessGranted?.Invoke(); + break; + case "getguid_fromserver": + if(SaveSystem.CurrentSave.Username == msg.Contents) + { + client.Send(new NetObject("yes_i_am", new ServerMessage + { + Name = "getguid_reply", + GUID = msg.GUID, + Contents = thisGuid.ToString(), + })); + } + break; + case "getguid_reply": + GUIDReceived?.Invoke(msg.Contents); + break; + } + } + + public static void Detach_ServerManager_MessageReceived() + { + MessageReceived -= new ServerMessageReceived(ServerManager_MessageReceived); + } + + public static void Initiate(string mud_address, int port) + { + client = new NetObjectClient(); + + client.OnReceived += (o, a) => + { + var msg = a.Data.Object as ServerMessage; + if (msg.Name == "Welcome") + { + thisGuid = new Guid(msg.Contents); + GUIDReceived?.Invoke(msg.Contents); + } + else if(msg.Name =="broadcast") + { + Console.WriteLine(msg.Contents); + } + else if (msg.Name == "Error") + { + var ex = JsonConvert.DeserializeObject<Exception>(msg.Contents); + TerminalBackend.PrefixEnabled = true; + Console.WriteLine($@"{{MUD_ERROR}}: {ex.Message}"); + TerminalBackend.PrefixEnabled = true; + Console.Write($"{SaveSystem.CurrentSave.Username}@{CurrentSave.SystemName}:~$ "); + } + else + { + MessageReceived?.Invoke(msg); + } + }; + + client.Connect(mud_address, port); + + } + + public static void SendMessage(string name, string contents) + { + var sMsg = new ServerMessage + { + Name = name, + Contents = contents, + GUID = thisGuid.ToString(), + }; + + client.Send(new NetObject("msg", sMsg)); + + } + + private static bool singleplayer = false; + public static bool IsSingleplayer { get { return singleplayer; } } + + public static void StartLANServer() + { + singleplayer = true; + ShiftOS.Server.Program.ServerStarted += (address) => + { + Console.WriteLine($"Connecting to {address}..."); + Initiate(address, 13370); + }; + Disconnected += () => + { + ShiftOS.Server.Program.Stop(); + }; + ShiftOS.Server.Program.Main(new[] { "" }); + + + } + + + public static event ServerMessageReceived MessageReceived; + + } + + public delegate void ServerMessageReceived(ServerMessage msg); + + public class MultiplayerOnlyAttribute : Attribute + { + /// <summary> + /// Marks this application as a multiplayer-only application. + /// </summary> + public MultiplayerOnlyAttribute() + { + + } + } +} |
