Skip to content

Commit

Permalink
Improve menu
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 18, 2024
1 parent b6df10f commit 0532794
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
11 changes: 2 additions & 9 deletions mod/Gangs.BombIconPerk/BombIconCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,10 @@ public class BombIconCommand(IServiceProvider provider, BasePlugin plugin)
public string Name => "css_bombicon";
public string[] Usage => ["<icon>"];

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<CommandResult> 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);
Expand All @@ -78,7 +71,7 @@ public async Task<CommandResult> 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;
}
Expand Down
47 changes: 42 additions & 5 deletions mod/Gangs.BombIconPerk/BombIconMenu.cs
Original file line number Diff line number Diff line change
@@ -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<IGangStatManager>();

private readonly IGangManager gangs =
provider.GetRequiredService<IGangManager>();

public async Task Open(PlayerWrapper player) {
player.PrintToChat(ChatColors.DarkBlue + "Gang Perk: "
+ ChatColors.LightBlue + "Bomb Icon");

var (success, data) =
await gangStats.GetForGang<BombPerkData>(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<BombIcon>()) {
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; }
Expand Down
7 changes: 5 additions & 2 deletions mod/Gangs.BombIconPerk/BombPerk.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Gangs.BaseImpl;
using System.Diagnostics;
using Gangs.BaseImpl;
using GangsAPI.Data.Gang;
using GangsAPI.Services.Menu;

Expand All @@ -24,7 +25,9 @@ public override Task OnPurchase(IGangPlayer player) {
}

public override Task<IMenu?> GetMenu(IGangPlayer player) {
return Task.FromResult<IMenu?>(new BombIconMenu());
Debug.Assert(player.GangId != null, "player.GangId != null");
return Task.FromResult<IMenu?>(new BombIconMenu(Provider,
player.GangId.Value));
}
}

Expand Down

0 comments on commit 0532794

Please sign in to comment.