Skip to content

Commit

Permalink
More tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 18, 2024
1 parent 0532794 commit 6f35c45
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 52 deletions.
4 changes: 2 additions & 2 deletions mod/Gangs.BaseImpl/BasePerk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

namespace Gangs.BaseImpl;

public abstract class BasePerk(IServiceProvider provider) : BaseStat, IPerk {
public abstract class BasePerk : BaseStat, IPerk {
public abstract Task<int?> GetCost(IGangPlayer player);
public abstract Task OnPurchase(IGangPlayer player);
public abstract Task<IMenu?> GetMenu(IGangPlayer player);
}

public abstract class BasePerk<TV>(IServiceProvider provider)
: BasePerk(provider), IPerk, IStat<TV> {
: BasePerk(), IPerk, IStat<TV> {
protected IServiceProvider Provider { get; } = provider;
public override Type ValueType => typeof(TV);
public abstract TV Value { get; set; }
Expand Down
5 changes: 0 additions & 5 deletions mod/Gangs.BaseImpl/GangCommandManager.cs

This file was deleted.

2 changes: 1 addition & 1 deletion mod/Gangs.BombIconPerk/BombIconCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,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(provider, gang.GangId);
var menu = new BombIconMenu(provider, data);
await menus.OpenMenu(executor, menu);
return CommandResult.SUCCESS;
}
Expand Down
69 changes: 32 additions & 37 deletions mod/Gangs.BombIconPerk/BombIconMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,46 @@
using GangsAPI.Extensions;
using GangsAPI.Services.Gang;
using GangsAPI.Services.Menu;
using Menu;
using Microsoft.Extensions.DependencyInjection;

namespace Gangs.BombIconPerk;

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 class BombIconMenu(IServiceProvider provider, BombPerkData data)
: AbstractPagedMenu<BombIcon>(provider, NativeSenders.Chat, 7) {
// Method to sort bomb icons
private int CompareBombIcons(BombIcon a, BombIcon b) {
// If the icon is equipped, it should be first
if (a == data.Equipped) return -1;

// If icon is unlocked, it should be next
// If both are unlocked, sort by cost (highest first)
if (a == data.Unlocked) {
if (b == data.Equipped) return 1;
return b == data.Unlocked ? a.GetCost().CompareTo(b.GetCost()) : -1;
}

// If both are locked, sort by cost (lowest first)
if (b == data.Equipped) return 1;
return b == data.Unlocked ? 1 : a.GetCost().CompareTo(b.GetCost());
}

public Task Close(PlayerWrapper player) { return Task.CompletedTask; }
override protected Task<List<BombIcon>> GetItems(PlayerWrapper player) {
var list = Enum.GetValues<BombIcon>().ToList();
list.Sort(CompareBombIcons);
return Task.FromResult(list);
}

public Task AcceptInput(PlayerWrapper player, int input) {
override protected Task HandleItemSelection(PlayerWrapper player,
List<BombIcon> items, int selectedIndex) {
player.PrintToChat("Selected " + items[selectedIndex]);
return Task.CompletedTask;
}

override protected Task<string> FormatItem(PlayerWrapper player, int index,
BombIcon item) {
if (item == data.Equipped) return Task.FromResult($"{item} (Equipped)");
if (item == data.Unlocked) return Task.FromResult($"{item} (Unlocked)");
return Task.FromResult($"{item} ({item.GetCost()})");
}
}
16 changes: 13 additions & 3 deletions mod/Gangs.BombIconPerk/BombPerk.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Diagnostics;
using Gangs.BaseImpl;
using GangsAPI.Data.Gang;
using GangsAPI.Services.Gang;
using GangsAPI.Services.Menu;
using Microsoft.Extensions.DependencyInjection;

namespace Gangs.BombIconPerk;

Expand All @@ -11,6 +13,9 @@ public class BombPerk(IServiceProvider provider)
public override string StatId => STAT_ID;
public override string Name => "Bomb Icon";

private readonly IGangStatManager gangStats =
provider.GetRequiredService<IGangStatManager>();

public override string? Description
=> "Customize the icon that is shown when you bomb a CT";

Expand All @@ -24,10 +29,15 @@ public override Task OnPurchase(IGangPlayer player) {
throw new NotImplementedException();
}

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

var (success, data) =
await gangStats.GetForGang<BombPerkData>(player.GangId.Value, STAT_ID);

if (!success || data == null) data = new BombPerkData();

return new BombIconMenu(Provider, data);
}
}

Expand Down
4 changes: 0 additions & 4 deletions mod/Gangs.Boostrap/GangsServiceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ public static void AddGangs(this IServiceCollection services) {

public class GangsInit : IPluginBehavior {
public void Start(BasePlugin basePlugin) {
basePlugin.Logger.LogInformation("Timer fired");
var services = API.Gangs?.Services;
basePlugin.Logger.LogInformation(
"Found services: {services} (is null: {null}) ({Gangs}) (null: {isNull})",
services, services == null, API.Gangs, API.Gangs == null);
if (services == null) return;

_ = new BombIconBootstrap(services, basePlugin);
Expand Down
Binary file modified public/Jailbreak.Public/Mixin/GangsAPI.dll
Binary file not shown.

0 comments on commit 6f35c45

Please sign in to comment.