From 46040964633d39d3ad8023ce9809efdc9f517f88 Mon Sep 17 00:00:00 2001 From: snixtho Date: Sat, 27 Jul 2024 06:17:26 +0100 Subject: [PATCH] Fixes (#249) --- .../Interfaces/Services/IAuditService.cs | 2 +- .../Services/IMatchSettingsService.cs | 13 ++++ src/EvoSC.Common/Services/AuditService.cs | 7 +- .../Services/MatchSettingsService.cs | 15 ++++- .../MatchSettings/DefaultModeScriptName.cs | 2 + .../Templates/Containers/Window.mt | 64 +------------------ src/EvoSC.Testing/EvoSC.Testing.csproj | 4 ++ src/EvoSC/InternalModules.cs | 6 +- .../ExampleModule/ExampleController.cs | 10 +-- .../Controllers/WidgetUpdateController.cs | 10 ++- .../Repository/LocalRecordRepository.cs | 2 +- .../Services/LocalRecordsService.cs | 2 +- .../Config/IOpenPlanetControlSettings.cs | 9 ++- .../OpenPlanetControlManialinkController.cs | 14 +++- .../OpenPlanetModule/Events/OpAuditEvents.cs | 12 ++++ .../Services/OpenPlanetControlService.cs | 20 +++++- .../OpenPlanetModule/Templates/DetectOP.mt | 7 +- .../Templates/WarningWindow.mt | 2 +- src/Modules/OpenPlanetModule/info.toml | 2 +- tests/EvoSC.CLI.Tests/EvoSC.CLI.Tests.csproj | 4 ++ .../EvoSC.Commands.Tests.csproj | 4 ++ .../EvoSC.Common.Tests.csproj | 4 ++ .../EvoSC.Manialinks.Tests.csproj | 4 ++ .../EvoSC.Testing.Tests.csproj | 4 ++ tests/EvoSC.Tests/EvoSC.Tests.csproj | 4 ++ .../ASayModule.Tests/ASayModule.Tests.csproj | 4 ++ .../CurrentMapModule.Tests.csproj | 4 ++ .../FastestCp.Tests/FastestCp.Tests.csproj | 4 ++ .../ForceTeamModule.Tests.csproj | 4 ++ .../LocalRecordsModule.Tests.csproj | 4 ++ .../MapListModule.Tests.csproj | 4 ++ .../MapQueueModuleTests.csproj | 4 ++ .../MapsModule.Tests/MapsModule.Tests.csproj | 4 ++ .../MatchManagerModule.Tests.csproj | 4 ++ .../MatchReadyModule.Tests.csproj | 4 ++ .../MatchTrackerModule.Tests.csproj | 4 ++ .../MotdModule.Tests/MotdModule.Tests.csproj | 4 ++ .../NextMapModule.Tests.csproj | 4 ++ ...enPlanetControlManialinkControllerTests.cs | 3 +- .../OpenPlanetModule.Tests.csproj | 4 ++ .../Services/OpenPlanetControlServiceTests.cs | 5 +- .../Modules/Player.Tests/Player.Tests.csproj | 4 ++ .../SetName.Tests/SetName.Tests.csproj | 4 ++ .../TeamSettingsModule.Tests.csproj | 4 ++ 44 files changed, 212 insertions(+), 91 deletions(-) create mode 100644 src/Modules/OpenPlanetModule/Events/OpAuditEvents.cs diff --git a/src/EvoSC.Common/Interfaces/Services/IAuditService.cs b/src/EvoSC.Common/Interfaces/Services/IAuditService.cs index e6748cb19..c65c54e7d 100644 --- a/src/EvoSC.Common/Interfaces/Services/IAuditService.cs +++ b/src/EvoSC.Common/Interfaces/Services/IAuditService.cs @@ -16,7 +16,7 @@ public interface IAuditService /// A comment or description of the event. /// Any properties or values associated with the event. /// - internal Task LogAsync(string eventName, AuditEventStatus status, IPlayer actor, string comment, + internal Task LogAsync(string eventName, AuditEventStatus status, IPlayer? actor, string comment, dynamic? properties); /// diff --git a/src/EvoSC.Common/Interfaces/Services/IMatchSettingsService.cs b/src/EvoSC.Common/Interfaces/Services/IMatchSettingsService.cs index a2232ca0e..9ad29a471 100644 --- a/src/EvoSC.Common/Interfaces/Services/IMatchSettingsService.cs +++ b/src/EvoSC.Common/Interfaces/Services/IMatchSettingsService.cs @@ -1,5 +1,6 @@ using EvoSC.Common.Interfaces.Models; using EvoSC.Common.Interfaces.Util; +using EvoSC.Common.Util.MatchSettings; using EvoSC.Common.Util.MatchSettings.Builders; using GbxRemoteNet.Exceptions; @@ -90,4 +91,16 @@ public interface IMatchSettingsService /// Thrown if the match settings file was not found. /// public Task DeleteMatchSettingsAsync(string name); + + /// + /// Get the name of the current mode script. + /// + /// + public Task GetCurrentScriptNameAsync(); + + /// + /// Get the mode of the current match settings. + /// + /// + public Task GetCurrentModeAsync(); } diff --git a/src/EvoSC.Common/Services/AuditService.cs b/src/EvoSC.Common/Services/AuditService.cs index ac049568b..385bfc352 100644 --- a/src/EvoSC.Common/Services/AuditService.cs +++ b/src/EvoSC.Common/Services/AuditService.cs @@ -6,6 +6,7 @@ using EvoSC.Common.Interfaces.Services; using EvoSC.Common.Interfaces.Util.Auditing; using EvoSC.Common.Models.Audit; +using EvoSC.Common.Util; using EvoSC.Common.Util.Auditing; using EvoSC.Common.Util.EnumIdentifier; using Microsoft.Extensions.Logging; @@ -37,7 +38,7 @@ private void LogLogger(DbAuditRecord auditRecord) ); } - async Task IAuditService.LogAsync(string eventName, AuditEventStatus status, IPlayer actor, string comment, + async Task IAuditService.LogAsync(string eventName, AuditEventStatus status, IPlayer? actor, string comment, dynamic? properties) { var serializedData = properties != null ? JsonSerializer.Serialize(properties) : null; @@ -45,9 +46,9 @@ async Task IAuditService.LogAsync(string eventName, AuditEventStatus status, IPl var auditRecord = new DbAuditRecord { Status = status, - Actor = new DbPlayer(actor), + Actor = actor == null ? null : new DbPlayer(actor), CreatedAt = DateTime.UtcNow, - ActorId = actor.Id, + ActorId = actor?.Id ?? 0, EventName = eventName, Comment = comment, Properties = serializedData diff --git a/src/EvoSC.Common/Services/MatchSettingsService.cs b/src/EvoSC.Common/Services/MatchSettingsService.cs index 083a0c001..d82751dbf 100644 --- a/src/EvoSC.Common/Services/MatchSettingsService.cs +++ b/src/EvoSC.Common/Services/MatchSettingsService.cs @@ -6,6 +6,7 @@ using EvoSC.Common.Interfaces.Util; using EvoSC.Common.Models.Maps; using EvoSC.Common.Util; +using EvoSC.Common.Util.EnumIdentifier; using EvoSC.Common.Util.MatchSettings; using EvoSC.Common.Util.MatchSettings.Builders; using GbxRemoteNet.Exceptions; @@ -108,7 +109,19 @@ public async Task DeleteMatchSettingsAsync(string name) var filePath = await GetFilePathAsync(name); File.Delete(filePath); } - + + public async Task GetCurrentScriptNameAsync() + { + var scriptInfo = await server.Remote.GetModeScriptInfoAsync(); + return scriptInfo.Name; + } + + public async Task GetCurrentModeAsync() + { + var scriptName = await GetCurrentScriptNameAsync(); + return scriptName.ToEnumValue() ?? DefaultModeScriptName.Unknown; + } + private async Task GetFilePathAsync(string name) { var mapsDir = await server.GetMapsDirectoryAsync(); diff --git a/src/EvoSC.Common/Util/MatchSettings/DefaultModeScriptName.cs b/src/EvoSC.Common/Util/MatchSettings/DefaultModeScriptName.cs index 7891a4eca..7d13d4e3a 100644 --- a/src/EvoSC.Common/Util/MatchSettings/DefaultModeScriptName.cs +++ b/src/EvoSC.Common/Util/MatchSettings/DefaultModeScriptName.cs @@ -24,9 +24,11 @@ public enum DefaultModeScriptName Laps, [Identifier(Name = "Trackmania/TM_Knockout_Online.Script.txt", NoPrefix = true)] + [Alias(Name = "ko")] Knockout, [Identifier(Name = "Trackmania/TM_TMWTTeams_Online.Script.txt", NoPrefix = true)] + [Alias(Name = "tmwt")] TmwtTeams, /// diff --git a/src/EvoSC.Manialinks/Templates/Containers/Window.mt b/src/EvoSC.Manialinks/Templates/Containers/Window.mt index 0bc290415..ff084a35e 100644 --- a/src/EvoSC.Manialinks/Templates/Containers/Window.mt +++ b/src/EvoSC.Manialinks/Templates/Containers/Window.mt @@ -109,6 +109,7 @@ The window is designed to only be used once per Manialink. y="-1.6" data="{{ id }}" size="small" + if="canClose" /> @@ -121,69 +122,6 @@ The window is designed to only be used once per Manialink. > - - - diff --git a/src/EvoSC.Testing/EvoSC.Testing.csproj b/src/EvoSC.Testing/EvoSC.Testing.csproj index 5adcb0a29..1c027c09c 100644 --- a/src/EvoSC.Testing/EvoSC.Testing.csproj +++ b/src/EvoSC.Testing/EvoSC.Testing.csproj @@ -7,6 +7,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + all diff --git a/src/EvoSC/InternalModules.cs b/src/EvoSC/InternalModules.cs index b083ebf6c..58bbe4458 100644 --- a/src/EvoSC/InternalModules.cs +++ b/src/EvoSC/InternalModules.cs @@ -31,9 +31,9 @@ namespace EvoSC; public static class InternalModules { public static readonly Type[] Modules = - { + [ + //typeof(ExampleModule), typeof(PlayerModule), - typeof(ExampleModule), typeof(MapsModule), typeof(WorldRecordModule), typeof(PlayerRecordsModule), @@ -56,7 +56,7 @@ public static class InternalModules typeof(LocalRecordsModule), typeof(ForceTeamModule), typeof(TeamSettingsModule) - }; + ]; /// /// Run any migrations from all the modules. diff --git a/src/Modules/ExampleModule/ExampleController.cs b/src/Modules/ExampleModule/ExampleController.cs index 33167c2d7..d420a19d4 100644 --- a/src/Modules/ExampleModule/ExampleController.cs +++ b/src/Modules/ExampleModule/ExampleController.cs @@ -77,13 +77,9 @@ public async Task RateMap(int rating) [ChatCommand("test", "Some testing.")] public async Task TestCommand() { - var mapList = await _server.Remote.GetMapListAsync(-1, 0); - - var maps = new List(); - foreach (var map in mapList) - { - maps.Add(await _mapService.GetMapByUidAsync(map.UId)); - } + var mode = await _matchSettings.GetCurrentModeAsync(); + + Console.WriteLine(mode); } [ChatCommand("rejoin", "Simulates the player joining the server.")] diff --git a/src/Modules/LocalRecordsModule/Controllers/WidgetUpdateController.cs b/src/Modules/LocalRecordsModule/Controllers/WidgetUpdateController.cs index 0eb819641..c9fe8ab0a 100644 --- a/src/Modules/LocalRecordsModule/Controllers/WidgetUpdateController.cs +++ b/src/Modules/LocalRecordsModule/Controllers/WidgetUpdateController.cs @@ -25,5 +25,13 @@ public async Task OnPlayerConnectAsync(object sender, PlayerConnectGbxEventArgs public Task OnBeginMapAsync(object sender, MapGbxEventArgs args) => localRecords.ShowWidgetToAllAsync(); [Subscribe(PlayerRecordsEvent.PbRecord)] - public Task OnPbAsync(object sender, PbRecordUpdateEventArgs args) => localRecords.UpdatePbAsync(args.Record); + public Task OnPbAsync(object sender, PbRecordUpdateEventArgs args) + { + if (args.Status == RecordUpdateStatus.NotUpdated) + { + return Task.CompletedTask; + } + + return localRecords.UpdatePbAsync(args.Record); + } } diff --git a/src/Modules/LocalRecordsModule/Database/Repository/LocalRecordRepository.cs b/src/Modules/LocalRecordsModule/Database/Repository/LocalRecordRepository.cs index f408a3c93..83dc9be9b 100644 --- a/src/Modules/LocalRecordsModule/Database/Repository/LocalRecordRepository.cs +++ b/src/Modules/LocalRecordsModule/Database/Repository/LocalRecordRepository.cs @@ -176,7 +176,7 @@ await NewLoadAll() public async Task GetRecordOfPlayerInMapAsync(IPlayer player, IMap map) => await NewLoadAll() - .FirstOrDefaultAsync(r => r.DbRecord.DbPlayer.Id == player.Id); + .FirstOrDefaultAsync(r => r.DbRecord.DbPlayer.Id == player.Id && r.DbMap.Id == map.Id); public async Task DeleteRecordAsync(IPlayer player, IMap map) { diff --git a/src/Modules/LocalRecordsModule/Services/LocalRecordsService.cs b/src/Modules/LocalRecordsModule/Services/LocalRecordsService.cs index 99cb5d163..dbf28ac3f 100644 --- a/src/Modules/LocalRecordsModule/Services/LocalRecordsService.cs +++ b/src/Modules/LocalRecordsModule/Services/LocalRecordsService.cs @@ -120,7 +120,7 @@ await server.InfoMessageAsync(new TextFormatter() .ToString()); await ShowWidgetToAllAsync(); } - else if (record.Score == oldRecord.Record.Score) + else if (record.Score == localRecord.Record.Score) { await server.InfoMessageAsync(new TextFormatter() .AddText(record.Player.NickName) diff --git a/src/Modules/OpenPlanetModule/Config/IOpenPlanetControlSettings.cs b/src/Modules/OpenPlanetModule/Config/IOpenPlanetControlSettings.cs index 0acc58cfd..033dfbe5d 100644 --- a/src/Modules/OpenPlanetModule/Config/IOpenPlanetControlSettings.cs +++ b/src/Modules/OpenPlanetModule/Config/IOpenPlanetControlSettings.cs @@ -2,6 +2,7 @@ using Config.Net; using EvoSC.Modules.Attributes; using EvoSC.Modules.Official.OpenPlanetModule.Models; +using LinqToDB.Common; namespace EvoSC.Modules.Official.OpenPlanetModule.Config; @@ -29,6 +30,12 @@ public interface IOpenPlanetControlSettings [Option(DefaultValue = true), Description("Allow the use of openplanet. If false, no signature mode is allowed.")] public bool AllowOpenplanet { get; set; } - [Option(DefaultValue = "1.25.45"), Description("The minimum required OpenPlanet version to play on this server.")] + [Option(DefaultValue = "1.26.25"), Description("The minimum required OpenPlanet version to play on this server.")] public Version MinimumRequiredVersion { get; set; } + + [Option(DefaultValue = true), Description("Enable auditing of all checks.")] + public bool AuditAllChecks { get; set; } + + [Option(DefaultValue = true), Description("Enable auditing of players that got jailed.")] + public bool AuditJails { get; set; } } diff --git a/src/Modules/OpenPlanetModule/Controllers/OpenPlanetControlManialinkController.cs b/src/Modules/OpenPlanetModule/Controllers/OpenPlanetControlManialinkController.cs index 58853a442..ea5044a9d 100644 --- a/src/Modules/OpenPlanetModule/Controllers/OpenPlanetControlManialinkController.cs +++ b/src/Modules/OpenPlanetModule/Controllers/OpenPlanetControlManialinkController.cs @@ -3,6 +3,8 @@ using EvoSC.Common.Util; using EvoSC.Manialinks; using EvoSC.Manialinks.Attributes; +using EvoSC.Modules.Official.OpenPlanetModule.Config; +using EvoSC.Modules.Official.OpenPlanetModule.Events; using EvoSC.Modules.Official.OpenPlanetModule.Interfaces; using EvoSC.Modules.Official.OpenPlanetModule.Interfaces.Models; @@ -11,7 +13,7 @@ namespace EvoSC.Modules.Official.OpenPlanetModule.Controllers; [Controller] [ManialinkRoute(Route = "OpenPlanetActions")] public class OpenPlanetControlManialinkController(IOpenPlanetControlService opControl, IServerClient server, - IOpenPlanetTrackerService trackerService) + IOpenPlanetTrackerService trackerService, IOpenPlanetScheduler scheduler) : ManialinkController { public async Task CheckAsync(IOpenPlanetInfo openPlanetInfo) @@ -20,5 +22,13 @@ public async Task CheckAsync(IOpenPlanetInfo openPlanetInfo) trackerService.AddOrUpdatePlayer(Context.Player, openPlanetInfo); } - public Task DisconnectAsync() => server.Remote.KickAsync(Context.Player.GetLogin()); + public async Task DisconnectAsync() + { + await server.Remote.KickAsync(Context.Player.GetLogin()); + + if (scheduler.PlayerIsScheduledForKick(Context.Player)) + { + scheduler.UnScheduleKickPlayer(Context.Player); + } + } } diff --git a/src/Modules/OpenPlanetModule/Events/OpAuditEvents.cs b/src/Modules/OpenPlanetModule/Events/OpAuditEvents.cs new file mode 100644 index 000000000..f99321028 --- /dev/null +++ b/src/Modules/OpenPlanetModule/Events/OpAuditEvents.cs @@ -0,0 +1,12 @@ +using EvoSC.Common.Util.EnumIdentifier; + +namespace EvoSC.Modules.Official.OpenPlanetModule.Events; + +public enum OpAuditEvents +{ + [Identifier(Name = "OpenPlanet:SignatureModeCheck")] + SignatureModeCheck, + + [Identifier(Name = "OpenPlanet:PlayerJailed")] + PlayerJailed +} diff --git a/src/Modules/OpenPlanetModule/Services/OpenPlanetControlService.cs b/src/Modules/OpenPlanetModule/Services/OpenPlanetControlService.cs index 9e6afbf95..3169c3a44 100644 --- a/src/Modules/OpenPlanetModule/Services/OpenPlanetControlService.cs +++ b/src/Modules/OpenPlanetModule/Services/OpenPlanetControlService.cs @@ -7,6 +7,7 @@ using EvoSC.Common.Util; using EvoSC.Manialinks.Interfaces; using EvoSC.Modules.Official.OpenPlanetModule.Config; +using EvoSC.Modules.Official.OpenPlanetModule.Events; using EvoSC.Modules.Official.OpenPlanetModule.Interfaces; using EvoSC.Modules.Official.OpenPlanetModule.Interfaces.Models; using EvoSC.Modules.Official.OpenPlanetModule.Models; @@ -18,7 +19,7 @@ namespace EvoSC.Modules.Official.OpenPlanetModule.Services; public class OpenPlanetControlService(ILogger logger, IPermissionManager permissions, IOpenPlanetControlSettings opcSettings, IManialinkManager manialinks, IServerClient server, IOpenPlanetScheduler scheduler, - Locale locale) + Locale locale, IAuditService auditService) : IOpenPlanetControlService { private readonly dynamic _locale = locale; @@ -27,6 +28,15 @@ public async Task VerifySignatureModeAsync(IPlayer player, IOpenPlanetInfo playe { logger.LogDebug("Verifying OpenPlanet for Player {Player}", player.AccountId); + if (opcSettings.AuditAllChecks) + { + await auditService.NewEvent(OpAuditEvents.SignatureModeCheck) + .HavingProperties(new { Player = player, OpenPlanetData = playerOpInfo }) + .Comment("OpenPlanet state checked for player.") + .Info() + .LogAsync(); + } + if (!playerOpInfo.IsOpenPlanet) { await ReleasePlayerAsync(player); @@ -74,6 +84,14 @@ public async Task VerifySignatureModeAsync(IPlayer player, IOpenPlanetInfo playe private async Task JailPlayerAsync(IPlayer player, OpJailReason reason) { + if (opcSettings.AuditJails) + { + await auditService.NewEvent(OpAuditEvents.PlayerJailed) + .HavingProperties(new { Player = player, Reason = reason }) + .Success() + .LogAsync("Player jailed."); + } + if (scheduler.PlayerIsScheduledForKick(player)) { return; diff --git a/src/Modules/OpenPlanetModule/Templates/DetectOP.mt b/src/Modules/OpenPlanetModule/Templates/DetectOP.mt index 572043386..0881914fb 100644 --- a/src/Modules/OpenPlanetModule/Templates/DetectOP.mt +++ b/src/Modules/OpenPlanetModule/Templates/DetectOP.mt @@ -31,8 +31,11 @@ *** OnLoop *** *** - if (lastTime + {{ config.CheckInterval }} <= Now && lastToolInfo != System.ExtraTool_Info) { - CheckOpenPlanet(); + if (lastTime + {{ config.CheckInterval }} <= Now) { + if (lastToolInfo != System.ExtraTool_Info) { + CheckOpenPlanet(); + } + lastTime = Now; lastToolInfo = System.ExtraTool_Info; } diff --git a/src/Modules/OpenPlanetModule/Templates/WarningWindow.mt b/src/Modules/OpenPlanetModule/Templates/WarningWindow.mt index 927d7dbfb..348036115 100644 --- a/src/Modules/OpenPlanetModule/Templates/WarningWindow.mt +++ b/src/Modules/OpenPlanetModule/Templates/WarningWindow.mt @@ -54,7 +54,7 @@