From 05327941a87f6cf546cede5a4a850fd983668362 Mon Sep 17 00:00:00 2001 From: MSWS Date: Tue, 17 Sep 2024 23:29:39 -0700 Subject: [PATCH] Improve menu --- mod/Gangs.BombIconPerk/BombIconCommand.cs | 11 +----- mod/Gangs.BombIconPerk/BombIconMenu.cs | 47 ++++++++++++++++++++--- mod/Gangs.BombIconPerk/BombPerk.cs | 7 +++- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/mod/Gangs.BombIconPerk/BombIconCommand.cs b/mod/Gangs.BombIconPerk/BombIconCommand.cs index 522fc5b2..26160ddc 100644 --- a/mod/Gangs.BombIconPerk/BombIconCommand.cs +++ b/mod/Gangs.BombIconPerk/BombIconCommand.cs @@ -50,17 +50,10 @@ public class BombIconCommand(IServiceProvider provider, BasePlugin plugin) public string Name => "css_bombicon"; public string[] Usage => [""]; - public void Start() { - plugin.AddCommand("css_bombicon", "", (player, info) => { - var playerWrapper = player == null ? null : new PlayerWrapper(player); - var infoWrapper = new CommandInfoWrapper(info); - Task.Run(async () => await Execute(playerWrapper, infoWrapper)); - }); - } + public void Start() { commands.RegisterCommand(this); } public async Task Execute(PlayerWrapper? executor, CommandInfoWrapper info) { - info.ReplySync("Test"); if (executor == null) return CommandResult.PLAYER_ONLY; var player = await players.GetPlayer(executor.Steam) ?? throw new PlayerNotFoundException(executor.Steam); @@ -78,7 +71,7 @@ public async Task Execute(PlayerWrapper? executor, if (!success || data == null) data = new BombPerkData(); if (info.ArgCount == 1) { - var menu = new BombIconMenu(); + var menu = new BombIconMenu(provider, gang.GangId); await menus.OpenMenu(executor, menu); return CommandResult.SUCCESS; } diff --git a/mod/Gangs.BombIconPerk/BombIconMenu.cs b/mod/Gangs.BombIconPerk/BombIconMenu.cs index 712a9803..5860f3cc 100644 --- a/mod/Gangs.BombIconPerk/BombIconMenu.cs +++ b/mod/Gangs.BombIconPerk/BombIconMenu.cs @@ -1,12 +1,49 @@ -using GangsAPI.Data; +using CounterStrikeSharp.API.Modules.Utils; +using GangsAPI.Data; +using GangsAPI.Exceptions; +using GangsAPI.Extensions; +using GangsAPI.Services.Gang; using GangsAPI.Services.Menu; +using Microsoft.Extensions.DependencyInjection; namespace Gangs.BombIconPerk; -public class BombIconMenu : IMenu { - public Task Open(PlayerWrapper player) { - player.PrintToChat("Bomb Icon Menu"); - return Task.CompletedTask; +public class BombIconMenu(IServiceProvider provider, int gangId) : IMenu { + private readonly IGangStatManager gangStats = + provider.GetRequiredService(); + + private readonly IGangManager gangs = + provider.GetRequiredService(); + + public async Task Open(PlayerWrapper player) { + player.PrintToChat(ChatColors.DarkBlue + "Gang Perk: " + + ChatColors.LightBlue + "Bomb Icon"); + + var (success, data) = + await gangStats.GetForGang(gangId, BombPerk.STAT_ID); + if (!success || data == null) data = new BombPerkData(); + + var unlocked = data.Unlocked; + var equipped = data.Equipped; + + var index = 0; + foreach (var icon in Enum.GetValues()) { + index++; + if (unlocked.HasFlag(icon)) { + player.PrintToChat( + $"{index}. {ChatColors.LightBlue}{icon.ToString().ToTitleCase()} {ChatColors.LightRed}{icon.GetCost()}"); + continue; + } + + if (equipped == icon) { + player.PrintToChat( + $"{index}. {ChatColors.LightBlue}{icon.ToString().ToTitleCase()} {ChatColors.Green}(Equipped)"); + continue; + } + + player.PrintToChat( + $"{index}. {ChatColors.LightBlue}{icon.ToString().ToTitleCase()} {ChatColors.LightYellow}(Select)"); + } } public Task Close(PlayerWrapper player) { return Task.CompletedTask; } diff --git a/mod/Gangs.BombIconPerk/BombPerk.cs b/mod/Gangs.BombIconPerk/BombPerk.cs index 5620f70c..d1dfb754 100644 --- a/mod/Gangs.BombIconPerk/BombPerk.cs +++ b/mod/Gangs.BombIconPerk/BombPerk.cs @@ -1,4 +1,5 @@ -using Gangs.BaseImpl; +using System.Diagnostics; +using Gangs.BaseImpl; using GangsAPI.Data.Gang; using GangsAPI.Services.Menu; @@ -24,7 +25,9 @@ public override Task OnPurchase(IGangPlayer player) { } public override Task GetMenu(IGangPlayer player) { - return Task.FromResult(new BombIconMenu()); + Debug.Assert(player.GangId != null, "player.GangId != null"); + return Task.FromResult(new BombIconMenu(Provider, + player.GangId.Value)); } }