From f03e72206e7ba239571a28e1901b3a12af8d7bd5 Mon Sep 17 00:00:00 2001 From: js6pak Date: Tue, 6 Apr 2021 20:24:20 +0200 Subject: [PATCH] Update example plugin --- .../Events/Game/IGamePlayerJoinedEvent.cs | 5 ++- .../Events/Game/IGamePlayerLeftEvent.cs | 7 +++- .../Handlers/GameEventListener.cs | 30 ++++++++--------- .../Handlers/MeetingEventListener.cs | 15 ++++++--- .../Handlers/PlayerEventListener.cs | 33 ++++++++++--------- 5 files changed, 54 insertions(+), 36 deletions(-) diff --git a/src/Impostor.Api/Events/Game/IGamePlayerJoinedEvent.cs b/src/Impostor.Api/Events/Game/IGamePlayerJoinedEvent.cs index 921568e5f..280d397d9 100644 --- a/src/Impostor.Api/Events/Game/IGamePlayerJoinedEvent.cs +++ b/src/Impostor.Api/Events/Game/IGamePlayerJoinedEvent.cs @@ -1,6 +1,9 @@ -namespace Impostor.Api.Events +using Impostor.Api.Net; + +namespace Impostor.Api.Events { public interface IGamePlayerJoinedEvent : IGameEvent { + IClientPlayer Player { get; } } } diff --git a/src/Impostor.Api/Events/Game/IGamePlayerLeftEvent.cs b/src/Impostor.Api/Events/Game/IGamePlayerLeftEvent.cs index 21d8b7c22..4568a602c 100644 --- a/src/Impostor.Api/Events/Game/IGamePlayerLeftEvent.cs +++ b/src/Impostor.Api/Events/Game/IGamePlayerLeftEvent.cs @@ -1,6 +1,11 @@ -namespace Impostor.Api.Events +using Impostor.Api.Net; + +namespace Impostor.Api.Events { public interface IGamePlayerLeftEvent : IGameEvent { + IClientPlayer Player { get; } + + bool IsBan { get; } } } diff --git a/src/Impostor.Plugins.Example/Handlers/GameEventListener.cs b/src/Impostor.Plugins.Example/Handlers/GameEventListener.cs index be2d0f3ff..eabbdf149 100644 --- a/src/Impostor.Plugins.Example/Handlers/GameEventListener.cs +++ b/src/Impostor.Plugins.Example/Handlers/GameEventListener.cs @@ -1,64 +1,64 @@ -using System; -using Impostor.Api.Events; +using Impostor.Api.Events; +using Microsoft.Extensions.Logging; namespace Impostor.Plugins.Example.Handlers { public class GameEventListener : IEventListener { - [EventListener(EventPriority.Monitor)] - public void OnGame(IGameEvent e) + private readonly ILogger _logger; + + public GameEventListener(ILogger logger) { - Console.WriteLine(e.GetType().Name + " triggered"); + _logger = logger; } [EventListener] public void OnGameCreated(IGameCreatedEvent e) { - Console.WriteLine("Game > created"); + _logger.LogInformation("Game {code} > created", e.Game.Code); } [EventListener] public void OnGameStarting(IGameStartingEvent e) { - Console.WriteLine("Game > starting"); + _logger.LogInformation("Game {code} > starting", e.Game.Code); } [EventListener] public void OnGameStarted(IGameStartedEvent e) { - Console.WriteLine("Game > started"); + _logger.LogInformation("Game {code} > started", e.Game.Code); foreach (var player in e.Game.Players) { - var info = player.Character.PlayerInfo; + var info = player.Character!.PlayerInfo; - Console.WriteLine($"- {info.PlayerName} {info.IsImpostor}"); + _logger.LogInformation("- {player} is {role}", info.PlayerName, info.IsImpostor ? "an impostor" : "a crewmate"); } } [EventListener] public void OnGameEnded(IGameEndedEvent e) { - Console.WriteLine("Game > ended"); - Console.WriteLine("- Reason: " + e.GameOverReason); + _logger.LogInformation("Game {code} > ended because {reason}", e.Game.Code, e.GameOverReason); } [EventListener] public void OnGameDestroyed(IGameDestroyedEvent e) { - Console.WriteLine("Game > destroyed"); + _logger.LogInformation("Game {code} > destroyed", e.Game.Code); } [EventListener] public void OnPlayerJoined(IGamePlayerJoinedEvent e) { - Console.WriteLine("Player joined a game."); + _logger.LogInformation("Game {code} > {player} joined", e.Game.Code, e.Player.Client.Name); } [EventListener] public void OnPlayerLeftGame(IGamePlayerLeftEvent e) { - Console.WriteLine("Player left a game."); + _logger.LogInformation("Game {code} > {player} left", e.Game.Code, e.Player.Client.Name); } } } diff --git a/src/Impostor.Plugins.Example/Handlers/MeetingEventListener.cs b/src/Impostor.Plugins.Example/Handlers/MeetingEventListener.cs index 847532c50..84948a71c 100644 --- a/src/Impostor.Plugins.Example/Handlers/MeetingEventListener.cs +++ b/src/Impostor.Plugins.Example/Handlers/MeetingEventListener.cs @@ -1,21 +1,28 @@ -using System; -using Impostor.Api.Events; +using Impostor.Api.Events; using Impostor.Api.Events.Meeting; +using Microsoft.Extensions.Logging; namespace Impostor.Plugins.Example.Handlers { public class MeetingEventListener : IEventListener { + private readonly ILogger _logger; + + public MeetingEventListener(ILogger logger) + { + _logger = logger; + } + [EventListener] public void OnMeetingStarted(IMeetingStartedEvent e) { - Console.WriteLine("Meeting > started"); + _logger.LogInformation("Meeting > started"); } [EventListener] public void OnMeetingEnded(IMeetingEndedEvent e) { - Console.WriteLine("Meeting > ended"); + _logger.LogInformation("Meeting > ended"); } } } diff --git a/src/Impostor.Plugins.Example/Handlers/PlayerEventListener.cs b/src/Impostor.Plugins.Example/Handlers/PlayerEventListener.cs index c62433c64..a052fd712 100644 --- a/src/Impostor.Plugins.Example/Handlers/PlayerEventListener.cs +++ b/src/Impostor.Plugins.Example/Handlers/PlayerEventListener.cs @@ -10,7 +10,7 @@ namespace Impostor.Plugins.Example.Handlers { public class PlayerEventListener : IEventListener { - private static readonly Random Random = new Random(); + private readonly Random _random = new Random(); private readonly ILogger _logger; @@ -22,48 +22,45 @@ public PlayerEventListener(ILogger logger) [EventListener] public void OnPlayerSpawned(IPlayerSpawnedEvent e) { - _logger.LogDebug(e.PlayerControl.PlayerInfo.PlayerName + " spawned"); + _logger.LogInformation("Player {player} > spawned", e.PlayerControl.PlayerInfo.PlayerName); // Need to make a local copy because it might be possible that // the event gets changed after being handled. var clientPlayer = e.ClientPlayer; var playerControl = e.PlayerControl; - /* Task.Run(async () => { - Console.WriteLine("Starting player task."); + _logger.LogDebug("Starting player task"); // Give the player time to load. await Task.Delay(TimeSpan.FromSeconds(3)); - while (clientPlayer.Client.Connection != null && - clientPlayer.Client.Connection.IsConnected) + while (clientPlayer.Client.Connection != null && clientPlayer.Client.Connection.IsConnected) { // Modify player properties. - await playerControl.SetColorAsync((byte) Random.Next(1, 9)); - await playerControl.SetHatAsync((uint) Random.Next(1, 9)); - await playerControl.SetSkinAsync((uint) Random.Next(1, 9)); - await playerControl.SetPetAsync((uint) Random.Next(1, 9)); + await playerControl.SetColorAsync((ColorType)_random.Next(1, 9)); + await playerControl.SetHatAsync((HatType)_random.Next(1, 9)); + await playerControl.SetSkinAsync((SkinType)_random.Next(1, 9)); + await playerControl.SetPetAsync((PetType)_random.Next(1, 9)); await Task.Delay(TimeSpan.FromMilliseconds(5000)); } - _logger.LogDebug("Stopping player task."); + _logger.LogDebug("Stopping player task"); }); - */ } [EventListener] public void OnPlayerDestroyed(IPlayerDestroyedEvent e) { - _logger.LogDebug(e.PlayerControl.PlayerInfo.PlayerName + " destroyed"); + _logger.LogInformation("Player {player} > destroyed", e.PlayerControl.PlayerInfo.PlayerName); } [EventListener] public async ValueTask OnPlayerChat(IPlayerChatEvent e) { - _logger.LogDebug(e.PlayerControl.PlayerInfo.PlayerName + " said " + e.Message); + _logger.LogInformation("Player {player} > said {message}", e.PlayerControl.PlayerInfo.PlayerName, e.Message); if (e.Message == "test") { @@ -94,7 +91,13 @@ public async ValueTask OnPlayerChat(IPlayerChatEvent e) [EventListener] public void OnPlayerStartMeetingEvent(IPlayerStartMeetingEvent e) { - _logger.LogDebug($"Player {e.PlayerControl.PlayerInfo.PlayerName} start meeting, reason: " + (e.Body == null ? "Emergency call button" : "Found the body of the player " + e.Body.PlayerInfo.PlayerName)); + _logger.LogInformation("Player {player} > started meeting, reason: {reason}", e.PlayerControl.PlayerInfo.PlayerName, e.Body == null ? "Emergency call button" : "Found the body of the player " + e.Body.PlayerInfo.PlayerName); + } + + [EventListener] + public void OnPlayerVentEvent(IPlayerVentEvent e) + { + _logger.LogInformation("Player {player} " + (e.VentEnter ? "entered" : "exit") + " the vent in {vent}", e.PlayerControl.PlayerInfo.PlayerName, e.Vent.Name); } } }