From cbc136583636227f7bb0dd94fa3926f977213a4d Mon Sep 17 00:00:00 2001 From: faketuna Date: Wed, 7 May 2025 15:54:54 +0900 Subject: [PATCH] modify: now server can spcify what coundown ui should be use --- .../MapVote/Countdown/Chat/McsChatCountdownUi.cs | 8 +++++++- .../MapVote/Countdown/McsCountdownUiController.cs | 9 ++++++--- .../PluginConfig/Interfaces/IMcsVoteConfig.cs | 3 +++ .../Modules/PluginConfig/McsPluginConfigParser.cs | 15 +++++++++++++-- .../Modules/PluginConfig/Models/McsVoteConfig.cs | 4 +++- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/MapChooserSharp/Modules/MapVote/Countdown/Chat/McsChatCountdownUi.cs b/MapChooserSharp/Modules/MapVote/Countdown/Chat/McsChatCountdownUi.cs index e0a7830..def4e75 100644 --- a/MapChooserSharp/Modules/MapVote/Countdown/Chat/McsChatCountdownUi.cs +++ b/MapChooserSharp/Modules/MapVote/Countdown/Chat/McsChatCountdownUi.cs @@ -9,10 +9,16 @@ namespace MapChooserSharp.Modules.MapVote.Countdown.Chat; public class McsChatCountdownUi(IServiceProvider provider): IMcsCountdownUi { private readonly TncssPluginBase _plugin = provider.GetRequiredService(); + + private bool _isFirstNotificationNotified = false; public void ShowCountdownToPlayer(CCSPlayerController player, int secondsLeft) { - player.PrintToChat(_plugin.LocalizeStringForPlayer(player, "MapVote.Broadcast.Countdown", secondsLeft)); + if (!_isFirstNotificationNotified || secondsLeft <= 10) + { + player.PrintToChat(_plugin.LocalizeStringForPlayer(player, "MapVote.Broadcast.Countdown", secondsLeft)); + _isFirstNotificationNotified = true; + } } public void Close(CCSPlayerController player){} diff --git a/MapChooserSharp/Modules/MapVote/Countdown/McsCountdownUiController.cs b/MapChooserSharp/Modules/MapVote/Countdown/McsCountdownUiController.cs index 72c1330..6d83e0c 100644 --- a/MapChooserSharp/Modules/MapVote/Countdown/McsCountdownUiController.cs +++ b/MapChooserSharp/Modules/MapVote/Countdown/McsCountdownUiController.cs @@ -5,6 +5,7 @@ using MapChooserSharp.Modules.MapVote.Countdown.CenterHud; using MapChooserSharp.Modules.MapVote.Countdown.Chat; using MapChooserSharp.Modules.MapVote.Countdown.Interfaces; +using MapChooserSharp.Modules.PluginConfig.Interfaces; using Microsoft.Extensions.DependencyInjection; using TNCSSPluginFoundation.Models.Plugin; @@ -22,7 +23,7 @@ internal sealed class McsCountdownUiController(IServiceProvider serviceProvider, private readonly Dictionary _countdownUis = new(); - + private IMcsPluginConfigProvider _mcsPluginConfigProvider = null!; public override void RegisterServices(IServiceCollection services) { @@ -37,6 +38,8 @@ protected override void OnInitialize() _countdownUis[McsCountdownType.CenterHtml] = new McsCenterHtmlCountdownUi(ServiceProvider); _countdownUis[McsCountdownType.Chat] = new McsChatCountdownUi(ServiceProvider); + _mcsPluginConfigProvider = ServiceProvider.GetRequiredService(); + if (hotReload) { foreach (CCSPlayerController player in Utilities.GetPlayers()) @@ -68,9 +71,9 @@ private HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventI private void PlayerConnectFull(CCSPlayerController player) { + McsCountdownType type = _mcsPluginConfigProvider.PluginConfig.VoteConfig.CurrentCountdownType; + // TODO() Player preference from DB - - McsCountdownType type = McsCountdownType.CenterHtml; UpdateCountdownType(player, type); } diff --git a/MapChooserSharp/Modules/PluginConfig/Interfaces/IMcsVoteConfig.cs b/MapChooserSharp/Modules/PluginConfig/Interfaces/IMcsVoteConfig.cs index eab331c..bda345f 100644 --- a/MapChooserSharp/Modules/PluginConfig/Interfaces/IMcsVoteConfig.cs +++ b/MapChooserSharp/Modules/PluginConfig/Interfaces/IMcsVoteConfig.cs @@ -1,4 +1,5 @@ using MapChooserSharp.Models; +using MapChooserSharp.Modules.MapVote.Countdown; using MapChooserSharp.Modules.McsMenu; namespace MapChooserSharp.Modules.PluginConfig.Interfaces; @@ -9,6 +10,8 @@ internal interface IMcsVoteConfig internal McsSupportedMenuType CurrentMenuType { get; } + internal McsCountdownType CurrentCountdownType { get; } + internal int MaxMenuElements { get; } internal bool ShouldPrintVoteToChat { get; } diff --git a/MapChooserSharp/Modules/PluginConfig/McsPluginConfigParser.cs b/MapChooserSharp/Modules/PluginConfig/McsPluginConfigParser.cs index dad6fe5..dccb6c8 100644 --- a/MapChooserSharp/Modules/PluginConfig/McsPluginConfigParser.cs +++ b/MapChooserSharp/Modules/PluginConfig/McsPluginConfigParser.cs @@ -1,5 +1,6 @@ using CounterStrikeSharp.API; using MapChooserSharp.Models; +using MapChooserSharp.Modules.MapVote.Countdown; using MapChooserSharp.Modules.McsDatabase; using MapChooserSharp.Modules.McsMenu; using MapChooserSharp.Modules.PluginConfig.Interfaces; @@ -178,6 +179,11 @@ private IMcsVoteConfig ParseVoteConfig(TomlTable tomlModel) { throw new InvalidOperationException("MapVote.MenuType is not found or invalid"); } + + if (!voteTable.TryGetValue("CountdownUiType", out var countdownUiTypeObj) || countdownUiTypeObj is not string countdownUiType) + { + throw new InvalidOperationException("MapVote.MenCountdownUiTypeuType is not found or invalid"); + } if (!voteTable.TryGetValue("MaxVoteElements", out var maxVoteElementsObj) || maxVoteElementsObj is not long maxVoteElementsLong) @@ -192,11 +198,16 @@ private IMcsVoteConfig ParseVoteConfig(TomlTable tomlModel) var availableMenus = _avaiableMenuTypes; - var currentMenuType = DecideMenuType(menuTypeStr, availableMenus); + var currentMenuType = DecideMenuType(countdownUiType, availableMenus); var soundConfig = ParseVoteSoundConfig(voteTable); - return new McsVoteConfig(availableMenus, currentMenuType, (int)maxVoteElementsLong, shouldPrintVoteToChatBool, soundConfig); + if (!Enum.TryParse(countdownUiType, true, out McsCountdownType countdownType)) + { + throw new InvalidOperationException("MapVote.MenCountdownUiTypeuType is invalid"); + } + + return new McsVoteConfig(availableMenus, currentMenuType, (int)maxVoteElementsLong, shouldPrintVoteToChatBool, soundConfig, countdownType); } private IMcsVoteSoundConfig ParseVoteSoundConfig(TomlTable tomlModel) diff --git a/MapChooserSharp/Modules/PluginConfig/Models/McsVoteConfig.cs b/MapChooserSharp/Modules/PluginConfig/Models/McsVoteConfig.cs index 1d6622f..6c24e04 100644 --- a/MapChooserSharp/Modules/PluginConfig/Models/McsVoteConfig.cs +++ b/MapChooserSharp/Modules/PluginConfig/Models/McsVoteConfig.cs @@ -1,14 +1,16 @@ using MapChooserSharp.Models; +using MapChooserSharp.Modules.MapVote.Countdown; using MapChooserSharp.Modules.McsMenu; using MapChooserSharp.Modules.PluginConfig.Interfaces; namespace MapChooserSharp.Modules.PluginConfig.Models; -internal class McsVoteConfig(List availableVoteMenuTypes, McsSupportedMenuType currentMcsVoteMenuType, int maxMenuElements, bool shouldPrintVoteToChat, IMcsVoteSoundConfig voteSoundConfig) +internal class McsVoteConfig(List availableVoteMenuTypes, McsSupportedMenuType currentMcsVoteMenuType, int maxMenuElements, bool shouldPrintVoteToChat, IMcsVoteSoundConfig voteSoundConfig, McsCountdownType currentCountdownType) : IMcsVoteConfig { public List AvailableMenuTypes { get; } = availableVoteMenuTypes; public McsSupportedMenuType CurrentMenuType { get; } = currentMcsVoteMenuType; + public McsCountdownType CurrentCountdownType { get; } = currentCountdownType; public int MaxMenuElements { get; } = maxMenuElements; public bool ShouldPrintVoteToChat { get; } = shouldPrintVoteToChat; public IMcsVoteSoundConfig VoteSoundConfig { get; } = voteSoundConfig;