From 7321b95821a22daa8ad83e476d66c132d33f59c6 Mon Sep 17 00:00:00 2001 From: Robert Paciorek Date: Mon, 12 Aug 2024 10:12:42 +0000 Subject: [PATCH] room alerts configuration via appsettings.json thanks to HaiFire for alert config for SS --- src/Core/Configuration.cs | 1 + src/Core/Room.cs | 10 +++++++--- src/Server.cs | 17 +++++++++++++---- src/appsettings.json | 9 +++++++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Core/Configuration.cs b/src/Core/Configuration.cs index 20fb473..727e7dd 100644 --- a/src/Core/Configuration.cs +++ b/src/Core/Configuration.cs @@ -32,6 +32,7 @@ internal sealed class ServerConfiguration { public string EventName { get; set; } = "ScoutAttack"; public int EventTimer { get; set; } = 30; public int FirstEventTimer { get; set; } = 10; + public Dictionary RoomAlerts { get; set; } = new(); public int RacingMaxPlayers { get; set; } = 6; public int RacingMinPlayers { get; set; } = 2; public int RacingMainLobbyTimer { get; set; } = 15; diff --git a/src/Core/Room.cs b/src/Core/Room.cs index b733f0f..31f7b7a 100644 --- a/src/Core/Room.cs +++ b/src/Core/Room.cs @@ -174,14 +174,18 @@ public class Room { private void StartAlert(AlertInfo alert, Client? specificClient = null) { if (specificClient != null) return; // Disables joining ongoing alerts. - alert.songId = random.Next(0, alert.songs); + NetworkArray NewRoomVariables = new(); NewRoomVariables.Add(NetworkArray.VlElement(REDALERT_START, alertId++, isPersistent: true)); NewRoomVariables.Add(NetworkArray.VlElement(REDALERT_TYPE, alert.type, isPersistent: true)); double duration = (alert.endTime - DateTime.Now).TotalSeconds; NewRoomVariables.Add(NetworkArray.VlElement(REDALERT_LENGTH, alert.type == "1" ? alert.redAlertDuration : duration, isPersistent: true)); - if (alert.type == "1") NewRoomVariables.Add(NetworkArray.VlElement(REDALERT_TIMEOUT, duration, isPersistent: true)); - if (alert.type == "3") NewRoomVariables.Add(NetworkArray.VlElement(REDALERT_SONG, (double)alert.songId, isPersistent: true)); + if (alert.type == "1") { + NewRoomVariables.Add(NetworkArray.VlElement(REDALERT_TIMEOUT, duration, isPersistent: true)); + } else if (alert.type == "3") { + alert.songId = random.Next(0, alert.songs); + NewRoomVariables.Add(NetworkArray.VlElement(REDALERT_SONG, (double)alert.songId, isPersistent: true)); + } NetworkPacket packet = Utils.VlNetworkPacket(NewRoomVariables, Id); if (specificClient is null) { RoomVariables = NewRoomVariables; diff --git a/src/Server.cs b/src/Server.cs index 9a46352..1340baa 100644 --- a/src/Server.cs +++ b/src/Server.cs @@ -28,10 +28,19 @@ public class Server { if (IPv6AndIPv4) listener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0); listener.Bind(new IPEndPoint(ipAddress, port)); - Room.GetOrAdd("LoungeInt").AddAlert(new Room.AlertInfo("3")); // FIXME use config for this - Room.GetOrAdd("Spaceport").AddAlert(new Room.AlertInfo("1", 20.0, 300, 300)); - Room.GetOrAdd("Spaceport").AddAlert(new Room.AlertInfo("2", 120.0, 1800, 3600)); - Room.GetOrAdd("Academy").AddAlert(new Room.AlertInfo("1", 20.0, 300, 300)); + + foreach (var room in Configuration.ServerConfiguration.RoomAlerts) { + foreach (var alert in room.Value) { + Console.WriteLine($"Setup alert \"{alert[0]}\" for {room.Key}"); + Room.GetOrAdd(room.Key).AddAlert(new Room.AlertInfo( + alert[0], // type + float.Parse(alert[1], System.Globalization.CultureInfo.InvariantCulture.NumberFormat), // duration + Int32.Parse(alert[2]), Int32.Parse(alert[3]), // start min - max for random start time + Int32.Parse(alert[4]), Int32.Parse(alert[5]) // extra parameters for specific alarm types + )); + } + } + await Listen(listener); } diff --git a/src/appsettings.json b/src/appsettings.json index 7a3cd19..b4b10bf 100644 --- a/src/appsettings.json +++ b/src/appsettings.json @@ -21,6 +21,15 @@ "// 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')", + "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] ], + }, + "// RacingMaxPlayers": "maximum players allowed in Thunder Run Racing (no more than 6)", "RacingMaxPlayers": 6,