From 13e9723626a08aee260ed79075b43968597e1e33 Mon Sep 17 00:00:00 2001 From: AlanMoonbase Date: Mon, 17 Mar 2025 14:01:03 -0700 Subject: [PATCH] implement custom ``SUE`` command for handling api --- src/CommandHandlers/LoginHandler.cs | 1 + src/CommandHandlers/SendUserEventHandler.cs | 32 ++++++ src/Server.cs | 5 + src/appsettings.json | 107 ++++++++++---------- 4 files changed, 93 insertions(+), 52 deletions(-) create mode 100644 src/CommandHandlers/SendUserEventHandler.cs diff --git a/src/CommandHandlers/LoginHandler.cs b/src/CommandHandlers/LoginHandler.cs index 0714226..07e57a6 100644 --- a/src/CommandHandlers/LoginHandler.cs +++ b/src/CommandHandlers/LoginHandler.cs @@ -90,6 +90,7 @@ class LoginHandler : CommandHandler } } catch (Exception ex) { Console.WriteLine($"Authentication exception IID: {client.ClientID} - {ex}"); + Console.WriteLine("This Can Be Ignored If API Is The One Logging In."); } return Configuration.ServerConfiguration.Authentication != AuthenticationMode.Required; // return true on auth err if not Required mode } diff --git a/src/CommandHandlers/SendUserEventHandler.cs b/src/CommandHandlers/SendUserEventHandler.cs new file mode 100644 index 0000000..e0dfb6d --- /dev/null +++ b/src/CommandHandlers/SendUserEventHandler.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using sodoffmmo.Attributes; +using sodoffmmo.Core; +using sodoffmmo.Data; + +namespace sodoffmmo.CommandHandlers +{ + [ExtensionCommandHandler("SUE")] + public class SendUserEventHandler : CommandHandler + { + public override Task Handle(Client client, NetworkObject receivedObject) + { + NetworkObject p = receivedObject.Get("p"); + + string userId = p.Get("UID"); + string cmd = p.Get("CMD"); + string[] arr = p.Get("ARR"); + + // find client in all clients list + Client? client1 = Server.AllClients.FirstOrDefault(e => e.PlayerData.Uid == userId); + + // send command + if (client1 != null) client1.Send(Utils.ArrNetworkPacket(arr, cmd)); + + return Task.CompletedTask; + } + } +} diff --git a/src/Server.cs b/src/Server.cs index 08b9d40..91f0c41 100644 --- a/src/Server.cs +++ b/src/Server.cs @@ -12,11 +12,14 @@ public class Server { readonly IPAddress ipAddress; readonly bool IPv6AndIPv4; ModuleManager moduleManager = new(); + public static List AllClients { get; private set; } public Server(IPAddress ipAdress, int port, bool IPv6AndIPv4) { this.ipAddress = ipAdress; this.port = port; this.IPv6AndIPv4 = IPv6AndIPv4; + + AllClients = new List(); } public async Task Run() { @@ -47,6 +50,7 @@ public class Server { private async Task HandleClient(Socket handler) { Client client = new(handler); + AllClients.Add(client); try { while (client.Connected) { await client.Receive(); @@ -59,6 +63,7 @@ public class Server { } finally { try { client.SetRoom(null); + AllClients.Remove(client); } catch (Exception) { } client.Disconnect(); Console.WriteLine("Socket disconnected IID: " + client.ClientID); diff --git a/src/appsettings.json b/src/appsettings.json index 98a03f2..6bb6874 100644 --- a/src/appsettings.json +++ b/src/appsettings.json @@ -1,72 +1,75 @@ { - "MMOServer": { - "// ListenIP": "Listening IP address for the MMO server, default is '*' which represents all IPv4 and IPv6 addresses", - "ListenIP": "*", + "MMOServer": { + "// ListenIP": "Listening IP address for the MMO server, default is '*' which represents all IPv4 and IPv6 addresses", + "ListenIP": "*", - "// Port": "Listening port number for the MMO server", - "Port": 9933, + "// Port": "Listening port number for the MMO server", + "Port": 9933, - "// PingDelay": "delay (in milliseconds) for PNG response", - "PingDelay": 17, + "// PingDelay": "delay (in milliseconds) for PNG response", + "PingDelay": 17, - "// EnableChat": "When true, in-game chat will be enabled", - "EnableChat": true, + "// EnableChat": "When true, in-game chat will be enabled", + "EnableChat": true, - "// EventName": "World event name send to client (can be used to select ship type after modding WorldEventScoutAttack in client)", - "EventName": "ScoutAttack", + "// EventName": "World event name send to client (can be used to select ship type after modding WorldEventScoutAttack in client)", + "EventName": "ScoutAttack", - "// FirstEventTimer": "time to start of first world event (battle ship event) after start MMO server", - "FirstEventTimer": 3, + "// FirstEventTimer": "time to start of first world event (battle ship event) after start MMO server", + "FirstEventTimer": 3, - "// EventTimer": "time between start of world events (battle ship events), set both timer values (EventTimer and FirstEventTimer) to 0 to disable events", - "EventTimer": 30, + "// EventTimer": "time between start of world events (battle ship events), set both timer values (EventTimer and FirstEventTimer) to 0 to disable events", + "EventTimer": 30, - "// RoomAlerts": "List of MMO rooms with alert function. Default empty (not used by SoD), bellow sample config for WoJS, MB and SS.", - "// alert parameters": "alert type, duration [s], minimum time to start [s], maximum time to start [s], redAlertDuration (used for type '1'), number of songs (used for type '3')", - "// alert types": "1 - Red Alert, 2 - Disco Alert, 3 - Dance Off", - "RoomAlerts": { - "LoungeInt" : [ ["3", 20.0, 30, 240, 0, 16] ], - "Spaceport": [ ["1", 20.0, 300, 300, 60, 0], ["2", 120.0, 1800, 3600, 60, 0] ], - "Academy": [ ["1", 20.0, 300, 300, 60, 0] ], - "ClubSSInt" : [ ["3", 20.0, 30, 240, 0, 16] ], - "JunkYardEMD": [ ["1", 20.0, 240, 300, 60, 0] ], - }, + "// RoomAlerts": "List of MMO rooms with alert function. Default empty (not used by SoD), bellow sample config for WoJS, MB and SS.", + "// alert parameters": "alert type, duration [s], minimum time to start [s], maximum time to start [s], redAlertDuration (used for type '1'), number of songs (used for type '3')", + "// alert types": "1 - Red Alert, 2 - Disco Alert, 3 - Dance Off", + "RoomAlerts": { + "LoungeInt": [ [ "3", 20.0, 30, 240, 0, 16 ] ], + "Spaceport": [ + [ "1", 20.0, 300, 300, 60, 0 ], + [ "2", 120.0, 1800, 3600, 60, 0 ] + ], + "Academy": [ [ "1", 20.0, 300, 300, 60, 0 ] ], + "ClubSSInt": [ [ "3", 20.0, 30, 240, 0, 16 ] ], + "JunkYardEMD": [ [ "1", 20.0, 240, 300, 60, 0 ] ] + }, - "// AmbassadorRooms": "Rooms with ambassadors (MB funzones).", - "AmbassadorRooms": ["Spaceport"], + "// AmbassadorRooms": "Rooms with ambassadors (MB funzones).", + "AmbassadorRooms": [ "Spaceport" ], - "// AmbassadorGaugeStart": "The starting value for all ambassador gauges (MB funzones).", - "AmbassadorGaugeStart": 75, + "// AmbassadorGaugeStart": "The starting value for all ambassador gauges (MB funzones).", + "AmbassadorGaugeStart": 75, - "// AmbassadorGaugeDecayRate": "Time in seconds before ambassador gauges decrease (MB funzones).", - "AmbassadorGaugeDecayRate": 60, + "// AmbassadorGaugeDecayRate": "Time in seconds before ambassador gauges decrease (MB funzones).", + "AmbassadorGaugeDecayRate": 60, - "// AmbassadorGaugeDecayOnlyWhenInRoom": "Only decrease ambassador gauges when there is at least one player in the room (MB funzones).", - "AmbassadorGaugeDecayOnlyWhenInRoom": true, + "// AmbassadorGaugeDecayOnlyWhenInRoom": "Only decrease ambassador gauges when there is at least one player in the room (MB funzones).", + "AmbassadorGaugeDecayOnlyWhenInRoom": true, - "// AmbassadorGaugePlayers": "Denominator for filling the ambassador gauges (MB funzones).", - "AmbassadorGaugePlayers": 0.5, + "// AmbassadorGaugePlayers": "Denominator for filling the ambassador gauges (MB funzones).", + "AmbassadorGaugePlayers": 0.5, - "// RacingMaxPlayers": "maximum players allowed in Thunder Run Racing (no more than 6)", - "RacingMaxPlayers": 6, + "// RacingMaxPlayers": "maximum players allowed in Thunder Run Racing (no more than 6)", + "RacingMaxPlayers": 6, - "// RacingMinPlayers": "minimum players to start Thunder Run Racing", - "RacingMinPlayers": 2, + "// RacingMinPlayers": "minimum players to start Thunder Run Racing", + "RacingMinPlayers": 2, - "// AllowChaos": "disable server side exploit protection", - "AllowChaos": false, + "// AllowChaos": "disable server side exploit protection", + "AllowChaos": false, - "// Authentication": "Player authentication mode: Disabled, Optional, RequiredForChat, Required", - "// Authentication Disabled": "authentication is disabled, anyone can connect to mmo", - "// Authentication Optional": "authentication is required only for moderation activities", - "// Authentication RequiredForChat": "authentication is required only for moderation activities and using chat (if chat is enabled)", - "// Authentication Required": "authentication is required to connect to mmo", - "Authentication": "Required", + "// Authentication": "Player authentication mode: Disabled, Optional, RequiredForChat, Required", + "// Authentication Disabled": "authentication is disabled, anyone can connect to mmo", + "// Authentication Optional": "authentication is required only for moderation activities", + "// Authentication RequiredForChat": "authentication is required only for moderation activities and using chat (if chat is enabled)", + "// Authentication Required": "authentication is required to connect to mmo", + "Authentication": "RequiredForChat", - "// ApiUrl": "SoDOff API server URL for authentication calls and other calls", - "ApiUrl": "http://localhost:5000", + "// ApiUrl": "SoDOff API server URL for authentication calls and other calls", + "ApiUrl": "http://localhost:5000", - "// BypassToken": "Token allowed to connect without authentication", - "BypassToken": "" - } + "// BypassToken": "Token allowed to connect without authentication", + "BypassToken": "" + } }