Skip to content

Commit

Permalink
Start getting actual basic plugin working
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 2, 2024
1 parent 8289f8d commit d7ab48a
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Commands/CommandCollection.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Microsoft.Extensions.DependencyInjection;
using GangsAPI.Extensions;
using GangsAPI.Services;
using GangsAPI.Services.Commands;

namespace Commands;

public static class CommandCollection {
public static void RegisterCommands(this IServiceCollection provider) {
provider.AddPluginBehavior<GangCommand>();
provider.AddPluginBehavior<ICommandManager, CommandManager>();
}
}
35 changes: 35 additions & 0 deletions Commands/CommandManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using GangsAPI;
using GangsAPI.Data;
using GangsAPI.Data.Command;
using GangsAPI.Services;
using GangsAPI.Services.Commands;
using Mock;

namespace Commands;

public class CommandManager(IGangManager gangMgr)
: MockCommandManager, IPluginBehavior {
private BasePlugin plugin = null!;

public void Start(BasePlugin? basePlugin, bool hotReload) {
ArgumentNullException.ThrowIfNull(basePlugin, nameof(basePlugin));
plugin = basePlugin;

RegisterCommand(new GangCommand(gangMgr));
}

public override bool RegisterCommand(ICommand command) {
base.RegisterCommand(command);
plugin.AddCommand(command.Name, command.Description ?? string.Empty,
(player, info) => {
var wrapper = player == null ? null : new PlayerWrapper(player);
var args = info.GetCommandString.Split(" ");
Server.NextFrameAsync(async () => {
await ProcessCommand(wrapper, args);
});
});
return true;
}
}
10 changes: 5 additions & 5 deletions Commands/GangCommand.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
using Commands.gang;
using GangsAPI.Data;
using GangsAPI.Data.Command;
using GangsAPI.Services;
using GangsAPI.Services.Commands;
using Mock;

namespace Commands;

public class GangCommand : ICommand {
public class GangCommand(IGangManager gangMgr) : ICommand {
public string Name => "css_gang";
public string? Description => "Master command for gangs";

private Dictionary<string, ICommand> sub = new() {
// ["create"] = new CreateGangCommand(),
private readonly Dictionary<string, ICommand> sub = new() {
// ["delete"] = new DeleteGangCommand(),
// ["invite"] = new InviteGangCommand(),
// ["kick"] = new KickGangCommand(),
// ["leave"] = new LeaveGangCommand(),
// ["leave"] = new LeaveGangCommand(),ggG
// ["list"] = new ListGangCommand(),
// ["promote"] = new PromoteGangCommand(),
// ["demote"] = new DemoteGangCommand(),
// ["info"] = new InfoGangCommand()
["help"] = new HelpCommand()
["create"] = new CreateCommand(gangMgr), ["help"] = new HelpCommand()
};

public async Task<CommandResult> Execute(PlayerWrapper? executor,
Expand Down
6 changes: 3 additions & 3 deletions Commands/gang/CreateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public async Task<CommandResult> Execute(PlayerWrapper? executor,
if (executor == null) { return CommandResult.PLAYER_ONLY; }

if (info.ArgCount < 2) {
info.ReplyToCommandSync("Please provide a name for the gang");
info.ReplySync("Please provide a name for the gang");
return CommandResult.FAILURE;
}

var name = string.Join(' ', info.ArgString.Split(" ").Skip(1));

if (await gang.GetGang(executor.Steam) != null) {
info.ReplyToCommandSync("You are already in a gang");
info.ReplySync("You are already in a gang");
return CommandResult.FAILURE;
}

if ((await gang.GetGangs()).Any(g => g.Name == name)) {
info.ReplyToCommandSync($"Gang '{name}' already exists");
info.ReplySync($"Gang '{name}' already exists");
return CommandResult.FAILURE;
}

Expand Down
2 changes: 1 addition & 1 deletion Commands/gang/HelpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class HelpCommand : ICommand {

public Task<CommandResult> Execute(PlayerWrapper? executor,
CommandInfoWrapper info) {
info.ReplyToCommandSync("create [name] - Creates a new gang");
info.ReplySync("create [name] - Creates a new gang");
return Task.FromResult(CommandResult.SUCCESS);
}
}
10 changes: 10 additions & 0 deletions Core/GangServiceCollection.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
using Commands;
using CounterStrikeSharp.API.Core;
using GangsAPI;
using GangsAPI.Extensions;
using GangsAPI.Services;
using Microsoft.Extensions.DependencyInjection;
using Mock;

namespace GangsImpl;

public class GangServiceCollection : IPluginServiceCollection<IGangPlugin> {
public void ConfigureServices(IServiceCollection serviceCollection) {
serviceCollection.AddPluginBehavior<IGangManager, MockGangManager>();
serviceCollection.AddPluginBehavior<IPlayerManager, MockPlayerManager>();
serviceCollection.AddPluginBehavior<IStatManager, MockStatManager>();
serviceCollection
.AddPluginBehavior<IGangStatManager, MockInstanceStatManager>();
serviceCollection
.AddPluginBehavior<IPlayerStatManager, MockInstanceStatManager>();
serviceCollection.RegisterCommands();
}
}
2 changes: 1 addition & 1 deletion GangsAPI/Data/Command/CommandInfoWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public string ArgString

public string GetCommandString => string.Join(' ', args.Skip(offset));

public void ReplyToCommandSync(string message) {
public void ReplySync(string message) {
if (CallingPlayer == null) {
Console.WriteLine(message);
return;
Expand Down
2 changes: 1 addition & 1 deletion GangsAPI/Services/IPlayerStatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace GangsAPI.Services;

public interface IPlayerStatManager : ICacher {
public interface IPlayerStatManager : IPluginBehavior, ICacher {
Task<IPlayerStat<V>?> GetForPlayer<V>(ulong key, string statId);

Task<bool> PushToPlayer<V>(ulong key, IPlayerStat<V> value) {
Expand Down
2 changes: 1 addition & 1 deletion GangsImpl/Mock/MockCommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Mock;
public class MockCommandManager : ICommandManager {
private readonly Dictionary<string, ICommand> commands = new();

public bool RegisterCommand(ICommand command) {
public virtual bool RegisterCommand(ICommand command) {
return commands.TryAdd(command.Name, command);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Commands;
using GangsAPI.Data.Command;
using GangsAPI.Services;
using GangsAPI.Services.Commands;

namespace GangsTest.Commands;

public class GangCommandTestses(ICommandManager commands)
: CommandTests(commands, new GangCommand()) {
public class GangCommandTests(ICommandManager commands, IGangManager gangMgr)
: CommandTests(commands, new GangCommand(gangMgr)) {
[Fact]
public async Task Gang_TestBase() {
Assert.Equal("css_gang", Command.Name);
Expand Down

0 comments on commit d7ab48a

Please sign in to comment.