From 4cf4ef321d03e23b46658d9f44e254b8f2871686 Mon Sep 17 00:00:00 2001 From: "Alex 'Braker' R." Date: Mon, 30 Sep 2024 21:40:52 +0200 Subject: [PATCH] Get max player for scoreboard dynamically - Use GameModeUiModule to hide default scoreboard - Simplify code --- .../Controllers/ScoreboardEventController.cs | 23 +++--- .../Services/ScoreboardService.cs | 75 +++++++------------ .../ScoreboardModule/Templates/Scoreboard.mt | 7 +- 3 files changed, 41 insertions(+), 64 deletions(-) diff --git a/src/Modules/ScoreboardModule/Controllers/ScoreboardEventController.cs b/src/Modules/ScoreboardModule/Controllers/ScoreboardEventController.cs index 2f488bb01..f916bc08a 100644 --- a/src/Modules/ScoreboardModule/Controllers/ScoreboardEventController.cs +++ b/src/Modules/ScoreboardModule/Controllers/ScoreboardEventController.cs @@ -10,25 +10,24 @@ namespace EvoSC.Modules.Official.ScoreboardModule.Controllers; [Controller] -public class ScoreboardEventController(IScoreboardNicknamesService nicknamesService) +public class ScoreboardEventController( + IScoreboardService scoreboardService, + IScoreboardNicknamesService nicknamesService +) : EvoScController { + [Subscribe(GbxRemoteEvent.PlayerConnect)] + public Task OnPlayerConnectAsync(object sender, PlayerGbxEventArgs args) => + nicknamesService.AddNicknameByLoginAsync(args.Login); + [Subscribe(ModeScriptEvent.EndMapEnd)] - public async Task OnEndMapAsync(object sender, MapEventArgs args) - { - await nicknamesService.ClearNicknamesAsync(); - } + public Task OnEndMapAsync(object sender, MapEventArgs args) => + nicknamesService.ClearNicknamesAsync(); [Subscribe(GbxRemoteEvent.BeginMap)] public async Task OnBeginMapAsync(object sender, MapGbxEventArgs args) { await nicknamesService.LoadNicknamesAsync(); - await nicknamesService.SendNicknamesManialinkAsync(); - } - - [Subscribe(GbxRemoteEvent.PlayerConnect)] - public async Task OnPlayerConnectAsync(object sender, PlayerGbxEventArgs args) - { - await nicknamesService.AddNicknameByLoginAsync(args.Login); + await scoreboardService.SendScoreboardAsync(); } } diff --git a/src/Modules/ScoreboardModule/Services/ScoreboardService.cs b/src/Modules/ScoreboardModule/Services/ScoreboardService.cs index c1af2d882..f9e04ac4b 100644 --- a/src/Modules/ScoreboardModule/Services/ScoreboardService.cs +++ b/src/Modules/ScoreboardModule/Services/ScoreboardService.cs @@ -3,6 +3,8 @@ using EvoSC.Common.Services.Attributes; using EvoSC.Common.Services.Models; using EvoSC.Manialinks.Interfaces; +using EvoSC.Modules.Official.GameModeUiModule.Enums; +using EvoSC.Modules.Official.GameModeUiModule.Interfaces; using EvoSC.Modules.Official.ScoreboardModule.Config; using EvoSC.Modules.Official.ScoreboardModule.Interfaces; @@ -14,68 +16,41 @@ public class ScoreboardService( IServerClient server, IScoreboardNicknamesService nicknamesService, IThemeManager themes, - IScoreboardSettings settings + IScoreboardSettings settings, + IGameModeUiModuleService gameModeUiModuleService ) : IScoreboardService { - public static readonly string ScoreboardTemplate = "ScoreboardModule.Scoreboard"; - + private static readonly string ScoreboardTemplate = "ScoreboardModule.Scoreboard"; + public async Task SendScoreboardAsync() { await manialinks.SendPersistentManialinkAsync(ScoreboardTemplate, await GetDataAsync()); await nicknamesService.SendNicknamesManialinkAsync(); } - private Task GetDataAsync() - { - return Task.FromResult(new - { - settings, - MaxPlayers = 64, //TODO: make dynamic - PositionColors = new Dictionary - { - { 1, themes.Theme.Gold }, { 2, themes.Theme.Silver }, { 3, themes.Theme.Bronze } - }, - }); - } - - public async Task HideNadeoScoreboardAsync() + private async Task GetDataAsync() { - var hudSettings = new List - { - @"{ - ""uimodules"": [ - { - ""id"": ""Race_ScoresTable"", - ""position"": [-50,0], - ""scale"": 1, - ""visible"": false, - ""visible_update"": true - } - ] -}" - }; + var maxPlayers = await server.Remote.GetMaxPlayersAsync(); - await server.Remote.TriggerModeScriptEventArrayAsync("Common.UIModules.SetProperties", hudSettings.ToArray()); + return new { settings, MaxPlayers = maxPlayers.CurrentValue }; } - public async Task ShowNadeoScoreboardAsync() - { - var hudSettings = new List - { - @"{ - ""uimodules"": [ - { - ""id"": ""Race_ScoresTable"", - ""position"": [-50,0], - ""scale"": 1, - ""visible"": true, - ""visible_update"": true - } - ] -}" - }; + public Task HideNadeoScoreboardAsync() => + gameModeUiModuleService.ApplyComponentSettingsAsync( + GameModeUiComponents.ScoresTable, + false, + 0.0, + 0.0, + 1.0 + ); - await server.Remote.TriggerModeScriptEventArrayAsync("Common.UIModules.SetProperties", hudSettings.ToArray()); - } + public Task ShowNadeoScoreboardAsync() => + gameModeUiModuleService.ApplyComponentSettingsAsync( + GameModeUiComponents.ScoresTable, + true, + -50.0, + 0.0, + 1.0 + ); } diff --git a/src/Modules/ScoreboardModule/Templates/Scoreboard.mt b/src/Modules/ScoreboardModule/Templates/Scoreboard.mt index e0cdb4165..edf8dc379 100644 --- a/src/Modules/ScoreboardModule/Templates/Scoreboard.mt +++ b/src/Modules/ScoreboardModule/Templates/Scoreboard.mt @@ -10,7 +10,6 @@ - @@ -488,7 +487,11 @@ PlayerRowsFilled = -1; CurrentScoreMode = -1; - {! string.Join("\n", PositionColors.Select(pc => $"PositionColors[{pc.Key}] = \"{pc.Value}\";")) !} + PositionColors = [ + 1 => "{{ Theme.Gold }}", + 2 => "{{ Theme.Silver }}", + 3 => "{{ Theme.Bronze }}" + ]; *** *** OnLoop ***