Skip to content

Commit

Permalink
Add GameHostChange event (Impostor#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
112batman authored Apr 12, 2021
1 parent a56eed2 commit fbd9f12
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
11 changes: 11 additions & 0 deletions src/Impostor.Api/Events/Game/IGameHostChangedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Impostor.Api.Net;

namespace Impostor.Api.Events
{
public interface IGameHostChangedEvent : IGameEvent
{
IClientPlayer PreviousHost { get; }

IClientPlayer? NewHost { get; }
}
}
11 changes: 11 additions & 0 deletions src/Impostor.Plugins.Example/Handlers/GameEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public void OnGameDestroyed(IGameDestroyedEvent e)
_logger.LogInformation("Game {code} > destroyed", e.Game.Code);
}

[EventListener]
public void OnGameHostChanged(IGameHostChangedEvent e)
{
_logger.LogInformation(
"Game {code} > changed host from {previous} to {new}",
e.Game.Code,
e.PreviousHost.Character?.PlayerInfo.PlayerName,
e.NewHost != null ? e.NewHost.Character?.PlayerInfo.PlayerName : "none"
);
}

[EventListener]
public void OnPlayerJoined(IGamePlayerJoinedEvent e)
{
Expand Down
22 changes: 22 additions & 0 deletions src/Impostor.Server/Events/Game/GameHostChangedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Impostor.Api.Events;
using Impostor.Api.Games;
using Impostor.Api.Net;

namespace Impostor.Server.Events
{
public class GameHostChangedEvent : IGameHostChangedEvent
{
public GameHostChangedEvent(IGame game, IClientPlayer previousHost, IClientPlayer? newHost)
{
Game = game;
PreviousHost = previousHost;
NewHost = newHost;
}

public IGame Game { get; }

public IClientPlayer PreviousHost { get; }

public IClientPlayer? NewHost { get; }
}
}
16 changes: 8 additions & 8 deletions src/Impostor.Server/Net/State/Game.State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ private async ValueTask<bool> PlayerRemove(int playerId, bool isBan = false)

player.Client.Player = null;

// Host migration.
if (HostId == playerId)
{
await MigrateHost();
await _eventManager.CallAsync(new GameHostChangedEvent(this, player, Host));
}

// Game is empty, remove it.
if (_players.IsEmpty)
if (_players.IsEmpty || Host == null)
{
GameState = GameStates.Destroyed;

Expand All @@ -60,12 +67,6 @@ private async ValueTask<bool> PlayerRemove(int playerId, bool isBan = false)
return true;
}

// Host migration.
if (HostId == playerId)
{
await MigrateHost();
}

if (isBan)
{
BanIp(player.Client.Connection.EndPoint.Address);
Expand Down Expand Up @@ -97,7 +98,6 @@ private async ValueTask MigrateHost()

if (host == null)
{
await EndAsync();
return;
}

Expand Down
5 changes: 0 additions & 5 deletions src/Impostor.Server/Net/State/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ public bool TryGetPlayer(int id, [MaybeNullWhen(false)] out ClientPlayer player)
return _players.TryGetValue(clientId, out var clientPlayer) ? clientPlayer : null;
}

public ValueTask EndAsync()
{
return _gameManager.RemoveAsync(Code);
}

internal async ValueTask StartedAsync()
{
if (GameState == GameStates.Starting)
Expand Down

0 comments on commit fbd9f12

Please sign in to comment.