Skip to content

Commit

Permalink
Add LobbyCommands project.
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver4888 committed Nov 15, 2020
1 parent 5ab95ac commit 2c536e2
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
25 changes: 25 additions & 0 deletions LobbyCommands.sln
Original file line number Diff line number Diff line change
@@ -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
77 changes: 77 additions & 0 deletions LobbyCommands/GameEventListener.cs
Original file line number Diff line number Diff line change
@@ -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<LobbyCommandsPlugin> _logger;
readonly string[] _mapNames = Enum.GetNames(typeof(MapTypes));

public GameEventListener(ILogger<LobbyCommandsPlugin> 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<MapTypes>(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;
}
}
}
}
15 changes: 15 additions & 0 deletions LobbyCommands/LobbyCommands.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Impostor.Api" Version="1.2.2-ci.115" />
</ItemGroup>

</Project>
40 changes: 40 additions & 0 deletions LobbyCommands/LobbyCommandsPlugin.cs
Original file line number Diff line number Diff line change
@@ -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<LobbyCommandsPlugin> _logger;
readonly IEventManager _eventManager;
IDisposable _unregister;

public LobbyCommandsPlugin(ILogger<LobbyCommandsPlugin> 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;
}
}
}

0 comments on commit 2c536e2

Please sign in to comment.