From bb6d40451886b4c5259bea98a63b3caaa0900ca8 Mon Sep 17 00:00:00 2001 From: DasIschBims Date: Thu, 17 Aug 2023 20:10:21 +0200 Subject: [PATCH] i brok it --- CommandHandler.cs | 38 +++++++++ Commands.cs | 82 ++++++++++++++++++ CommunityServerAPI.csproj | 1 + Enums/ActionEnum.cs | 16 ++++ Program.cs | 171 +++++--------------------------------- Utils.cs | 19 +++++ 6 files changed, 179 insertions(+), 148 deletions(-) create mode 100644 CommandHandler.cs create mode 100644 Commands.cs create mode 100644 Enums/ActionEnum.cs create mode 100644 Utils.cs diff --git a/CommandHandler.cs b/CommandHandler.cs new file mode 100644 index 0000000..1958ef2 --- /dev/null +++ b/CommandHandler.cs @@ -0,0 +1,38 @@ +using BattleBitAPI; +using CommunityServerAPI.Enums; + +namespace CommunityServerAPI; + +public class CommandHandler +{ + public async Task handleCommand(MyPlayer player, List players, Command cmd) + { + switch (cmd.Action) + { + case ActionType.Kill: + { + var target = players.Find(p => p.SteamID == cmd.SteamId); + if (target == null) + { + player.Message("Player not found!"); + return; + } + + target.Kill(); + player.Message("Player killed!"); + break; + } + case ActionType.Start: + { + player.Message("Starting game!"); + player.GameServer.RoundSettings.PlayersToStart = 0; + break; + } + default: + { + player.Message("Unknown command!"); + break; + } + } + } +} \ No newline at end of file diff --git a/Commands.cs b/Commands.cs new file mode 100644 index 0000000..6f4839f --- /dev/null +++ b/Commands.cs @@ -0,0 +1,82 @@ +using BattleBitAPI.Common; +using System.Numerics; +using BattleBitAPI; +using BattleBitAPI.Server; +using CommunityServerAPI.Enums; + +namespace CommunityServerAPI; + +public abstract class ApiCommand +{ + public string CommandString; + public string HelpString; + public string[] Aliases; + public bool AdminOnly; + + public virtual Command ChatCommand(MyPlayer player, List players, ChatChannel channel, string msg) + { + return null; + } + + public Player FindPlayerByIdentifier(string identifier, List players) + { + var player = players.Find(p => p.SteamID.ToString() == identifier || p.Name == identifier); + return player; + } +} + +public class KillCommand : ApiCommand +{ + public KillCommand() + { + CommandString = "/kill"; + HelpString = "Kills a player by name or steamid"; + Aliases = new string[] { "/k" }; + AdminOnly = true; + } + + public override Command ChatCommand(MyPlayer player, List players, ChatChannel channel, string msg) + { + var words = msg.Split(" "); + var target = FindPlayerByIdentifier(words[1], players); + + if (target == null) + { + return new Command + { + Action = ActionType.Kill, + Executor = player.Name, + Error = true, + }; + } + + return new Command + { + Action = ActionType.Kill, + Executor = player.Name, + SteamId = target.SteamID, + Error = false, + }; + } +} + +public class StartCommand : ApiCommand +{ + public StartCommand() + { + CommandString = "/start"; + HelpString = "Starts the game"; + Aliases = new string[] { "/s" }; + AdminOnly = true; + } + + public override Command ChatCommand(MyPlayer player, List players, ChatChannel channel, string msg) + { + return new Command + { + Action = ActionType.Start, + Executor = player.Name, + Error = false, + }; + } +} \ No newline at end of file diff --git a/CommunityServerAPI.csproj b/CommunityServerAPI.csproj index 2fb5483..4d26240 100644 --- a/CommunityServerAPI.csproj +++ b/CommunityServerAPI.csproj @@ -8,4 +8,5 @@ True + diff --git a/Enums/ActionEnum.cs b/Enums/ActionEnum.cs new file mode 100644 index 0000000..ed7a741 --- /dev/null +++ b/Enums/ActionEnum.cs @@ -0,0 +1,16 @@ +namespace CommunityServerAPI.Enums; + +public enum ActionType +{ + Heal, + Kill, + Kick, + Ban, + Teleport, + Speed, + Help, + Start, + Stop, + Op, + DeOp, +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 903f429..99801de 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,7 @@ using BattleBitAPI; using BattleBitAPI.Common; using BattleBitAPI.Server; +using CommunityServerAPI; class Program { @@ -15,20 +16,29 @@ class Program } } -class MyPlayer : Player +public class MyPlayer : Player { + public bool IsAdmin = true; public int Kills; public int Deaths; - public List players; + public List Players; } class MyGameServer : GameServer { - private List players; + private readonly List mApiCommands = new() + { + new KillCommand(), + }; + + private CommandHandler handler = new CommandHandler(); + + public List Players; public override async Task OnConnected() { Console.WriteLine($"Gameserver connected! {this.GameIP}:{this.GamePort}"); + Console.Write(mApiCommands.Count); ServerSettings.BleedingEnabled = false; ServerSettings.SpectatorEnabled = false; @@ -67,158 +77,23 @@ class MyGameServer : GameServer await Console.Out.WriteLineAsync("Disconnected: " + player); } - public override async Task OnTick() - { - var top5 = players.OrderByDescending(x => x.Kills / x.Deaths).Take(5).ToList(); - var topPlayersInfo = top5.Select((p, index) => $"{index + 1}. {p.Name} - {p.Kills / p.Deaths}"); - var announcement = $"--- Top 5 Players ---\n{string.Join("\n", topPlayersInfo)}"; - - AnnounceShort(announcement); - } - - private MyPlayer FindPlayerByIdentifier(string identifier) - { - ulong.TryParse(identifier, out ulong steamid); - return players.FirstOrDefault(x => x.SteamID == steamid || x.Name == identifier); - } - public override async Task OnPlayerTypedMessage(MyPlayer player, ChatChannel channel, string msg) { - if (player.SteamID != 76561198395073327 || !msg.StartsWith("/")) return true; - - var words = msg.Split(" "); - - switch (words[0]) + if (player.IsAdmin) { - case "/tp": - // /tp or /tp (tp executor to target) - var tpTarget = FindPlayerByIdentifier(words[1]); - var tpDestination = FindPlayerByIdentifier(words.Length > 2 ? words[2] : player.Name); - - if (tpTarget == null || tpDestination == null) + var splits = msg.Split(" "); + var cmd = splits[0].ToLower(); + foreach(var apiCommand in mApiCommands) + { + if (apiCommand.CommandString == cmd) { - player.Message("Player not found!"); + var command = apiCommand.ChatCommand(player, Players, channel, msg); // stops here, async issue? + await handler.handleCommand(player, Players, command); return false; } - - tpTarget.Teleport(tpDestination.Position); - break; - - case "/kill": - // /kill - var killTarget = FindPlayerByIdentifier(words[1]); - - if (killTarget == null) - { - player.Message("Player not found!"); - return false; - } - - killTarget.Kill(); - break; - - case "/heal": - // /heal - var healTarget = FindPlayerByIdentifier(words[1]); - - if (healTarget == null) - { - player.Message("Player not found!"); - return false; - } - - var healAmount = words.Length > 2 ? int.Parse(words[2]) : 100; - healTarget.Heal(healAmount); - break; - - case "/kick": - // /kick - var kickTarget = FindPlayerByIdentifier(words[1]); - - if (kickTarget == null) - { - player.Message("Player not found!"); - return false; - } - - var kickReason = words.Length > 2 ? string.Join(" ", words.Skip(2)) : "No reason provided"; - - kickTarget.Kick(kickReason); - break; - default: - player.Message("Unknown command!"); - break; + } } - return false; - } - - public override async Task OnRoundStarted() - { - players = AllPlayers.ToList(); - - foreach (var player in players) - { - player.Kills = 0; - player.Deaths = 0; - } - } - - - public override async Task OnPlayerConnected(MyPlayer player) - { - player.Kills = 0; - player.Deaths = 0; - } - - public override Task OnPlayerJoiningToServer(ulong steamID, PlayerJoiningArguments args) - { - var stats = new PlayerStats(); - - stats.Progress.Rank = 200; - stats.Progress.Prestige = 10; - stats.Progress.EXP = uint.MaxValue; - if (steamID == 76561198395073327) - { - stats.Roles = Roles.Admin; - } - - return Task.FromResult(stats); - } - - public override async Task OnAPlayerDownedAnotherPlayer(OnPlayerKillArguments args) - { - args.Victim.Kill(); - args.Killer.Heal(100); - args.Killer.Kills++; - args.Victim.Deaths++; - } - - public override async Task OnPlayerRequestingToChangeRole(MyPlayer player, GameRole role) - { - if (role == GameRole.Assault) - return true; - - player.GameServer.AnnounceShort("You can only play as Assault!"); - return false; - } - - public override async Task OnPlayerSpawning(MyPlayer player, OnPlayerSpawnArguments request) - { - request.Loadout.PrimaryWeapon = default; - request.Loadout.SecondaryWeapon = default; - request.Loadout.LightGadget = null; - request.Loadout.HeavyGadget = Gadgets.AdvancedBinoculars; - request.Loadout.Throwable = null; - request.Loadout.FirstAid = null; - request.Loadout.PrimaryExtraMagazines = 5; - request.Loadout.SecondaryExtraMagazines = 5; - - return request; - } - - public override async Task OnPlayerSpawned(MyPlayer player) - { - player.SetGiveDamageMultiplier(0.25f); + return true; } } \ No newline at end of file diff --git a/Utils.cs b/Utils.cs new file mode 100644 index 0000000..2b2b42c --- /dev/null +++ b/Utils.cs @@ -0,0 +1,19 @@ +using System.Numerics; +using CommunityServerAPI.Enums; + +namespace CommunityServerAPI; + +public class Command +{ + public ActionType Action { get; set; } + public ulong SteamId { get; set; } + public string Executor { get; set; } + public string Target { get; set; } + public ulong TargetSteamId { get; set; } + public string Message { get; set; } + public Vector3 Location { get; set; } + public int Amount { get; set; } + public int Duration { get; set; } + public string Reason { get; set; } + public bool Error { get; set; } +} \ No newline at end of file