Skip to content

Commit

Permalink
Update example plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak committed Apr 6, 2021
1 parent 304b4bb commit f03e722
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 36 deletions.
5 changes: 4 additions & 1 deletion src/Impostor.Api/Events/Game/IGamePlayerJoinedEvent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace Impostor.Api.Events
using Impostor.Api.Net;

namespace Impostor.Api.Events
{
public interface IGamePlayerJoinedEvent : IGameEvent
{
IClientPlayer Player { get; }
}
}
7 changes: 6 additions & 1 deletion src/Impostor.Api/Events/Game/IGamePlayerLeftEvent.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
30 changes: 15 additions & 15 deletions src/Impostor.Plugins.Example/Handlers/GameEventListener.cs
Original file line number Diff line number Diff line change
@@ -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<GameEventListener> _logger;

public GameEventListener(ILogger<GameEventListener> 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);
}
}
}
15 changes: 11 additions & 4 deletions src/Impostor.Plugins.Example/Handlers/MeetingEventListener.cs
Original file line number Diff line number Diff line change
@@ -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<MeetingEventListener> _logger;

public MeetingEventListener(ILogger<MeetingEventListener> 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");
}
}
}
33 changes: 18 additions & 15 deletions src/Impostor.Plugins.Example/Handlers/PlayerEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayerEventListener> _logger;

Expand All @@ -22,48 +22,45 @@ public PlayerEventListener(ILogger<PlayerEventListener> 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")
{
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit f03e722

Please sign in to comment.