diff --git a/LobbyCommands.sln b/LobbyCommands.sln new file mode 100644 index 0000000..2cb9ad0 --- /dev/null +++ b/LobbyCommands.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30711.63 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LobbyCommands", "LobbyCommands\LobbyCommands.csproj", "{9F979001-0C3F-4028-97ED-1B058E72CEA4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9F979001-0C3F-4028-97ED-1B058E72CEA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9F979001-0C3F-4028-97ED-1B058E72CEA4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9F979001-0C3F-4028-97ED-1B058E72CEA4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9F979001-0C3F-4028-97ED-1B058E72CEA4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {29697D01-9B19-43CD-9BDB-4620FFAE4B58} + EndGlobalSection +EndGlobal diff --git a/LobbyCommands/GameEventListener.cs b/LobbyCommands/GameEventListener.cs new file mode 100644 index 0000000..af9615e --- /dev/null +++ b/LobbyCommands/GameEventListener.cs @@ -0,0 +1,77 @@ +using System; +using System.Linq; +using System.Threading.Tasks; + +using Impostor.Api.Events; +using Impostor.Api.Innersloth; +using Impostor.Api.Events.Player; + +using Microsoft.Extensions.Logging; + +namespace LobbyCommands +{ + public class GameEventListener : IEventListener + { + readonly ILogger _logger; + readonly string[] _mapNames = Enum.GetNames(typeof(MapTypes)); + + public GameEventListener(ILogger logger) => _logger = logger; + + [EventListener] + public async ValueTask OnPlayerChat(IPlayerChatEvent e) + { + if (e.Game.GameState != GameStates.NotStarted || !e.Message.StartsWith("/") || !e.ClientPlayer.IsHost) + return; + + _logger.LogInformation($"Attempting to evaluate command from {e.PlayerControl.PlayerInfo.PlayerName} on {e.Game.Code.Code}. Message was: {e.Message}"); + + string[] parts = e.Message[1..].Split(" "); + + switch (parts[0]) + { + case "impostors": + if (parts.Length == 1) + { + await e.PlayerControl.SendChatAsync($"Please specify the number of impostors."); + return; + } + + if (int.TryParse(parts[1], out int num)) + { + num = Math.Clamp(num, 1, 3); + + await e.PlayerControl.SendChatAsync($"Setting the number of impostors to {num}"); + + e.Game.Options.NumImpostors = num; + await e.Game.SyncSettingsAsync(); + } + else + await e.PlayerControl.SendChatAsync($"Unable to convert '{parts[1]}' to a number!"); + break; + case "map": + if (parts.Length == 1) + { + await e.PlayerControl.SendChatAsync($"Please specify the map. Accepted values: {string.Join(", ", _mapNames)}"); + return; + } + + if (!_mapNames.Any(name => name.ToUpperInvariant() == parts[1].ToUpperInvariant())) + { + await e.PlayerControl.SendChatAsync($"Unknown map. Accepted values: {string.Join(", ", _mapNames)}"); + return; + } + + MapTypes map = Enum.Parse(parts[1], true); + + await e.PlayerControl.SendChatAsync($"Setting map to {map}"); + + e.Game.Options.Map = map; + await e.Game.SyncSettingsAsync(); + break; + default: + _logger.LogInformation($"Unknown command {parts[0]} from {e.PlayerControl.PlayerInfo.PlayerName} on {e.Game.Code.Code}."); + break; + } + } + } +} diff --git a/LobbyCommands/LobbyCommands.csproj b/LobbyCommands/LobbyCommands.csproj new file mode 100644 index 0000000..fc77a1a --- /dev/null +++ b/LobbyCommands/LobbyCommands.csproj @@ -0,0 +1,15 @@ + + + + netstandard2.1 + + + + + + + + + + + diff --git a/LobbyCommands/LobbyCommandsPlugin.cs b/LobbyCommands/LobbyCommandsPlugin.cs new file mode 100644 index 0000000..32f61f0 --- /dev/null +++ b/LobbyCommands/LobbyCommandsPlugin.cs @@ -0,0 +1,40 @@ +using System; +using System.Threading.Tasks; + +using Impostor.Api.Plugins; +using Impostor.Api.Events.Managers; + +using Microsoft.Extensions.Logging; + +namespace LobbyCommands +{ + [ImpostorPlugin( + package: "uk.ol48.lobbycommands", + name: "Lobby Commands", + author: "oliver4888", + version: "1.0.0")] + public class LobbyCommandsPlugin : PluginBase + { + readonly ILogger _logger; + readonly IEventManager _eventManager; + IDisposable _unregister; + + public LobbyCommandsPlugin(ILogger logger, IEventManager eventManager) + { + _logger = logger; + _eventManager = eventManager; + } + + public override ValueTask EnableAsync() + { + _unregister = _eventManager.RegisterListener(new GameEventListener(_logger)); + return default; + } + + public override ValueTask DisableAsync() + { + _unregister.Dispose(); + return default; + } + } +}