From fbd9f12f9e352a96821fe8e73c1c7e49b04d8961 Mon Sep 17 00:00:00 2001 From: 112batman <53775078+112batman@users.noreply.github.com> Date: Mon, 12 Apr 2021 22:53:14 +0200 Subject: [PATCH] Add GameHostChange event (#277) --- .../Events/Game/IGameHostChangedEvent.cs | 11 ++++++++++ .../Handlers/GameEventListener.cs | 11 ++++++++++ .../Events/Game/GameHostChangedEvent.cs | 22 +++++++++++++++++++ src/Impostor.Server/Net/State/Game.State.cs | 16 +++++++------- src/Impostor.Server/Net/State/Game.cs | 5 ----- 5 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 src/Impostor.Api/Events/Game/IGameHostChangedEvent.cs create mode 100644 src/Impostor.Server/Events/Game/GameHostChangedEvent.cs diff --git a/src/Impostor.Api/Events/Game/IGameHostChangedEvent.cs b/src/Impostor.Api/Events/Game/IGameHostChangedEvent.cs new file mode 100644 index 000000000..44709783a --- /dev/null +++ b/src/Impostor.Api/Events/Game/IGameHostChangedEvent.cs @@ -0,0 +1,11 @@ +using Impostor.Api.Net; + +namespace Impostor.Api.Events +{ + public interface IGameHostChangedEvent : IGameEvent + { + IClientPlayer PreviousHost { get; } + + IClientPlayer? NewHost { get; } + } +} diff --git a/src/Impostor.Plugins.Example/Handlers/GameEventListener.cs b/src/Impostor.Plugins.Example/Handlers/GameEventListener.cs index eabbdf149..0125a19a1 100644 --- a/src/Impostor.Plugins.Example/Handlers/GameEventListener.cs +++ b/src/Impostor.Plugins.Example/Handlers/GameEventListener.cs @@ -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) { diff --git a/src/Impostor.Server/Events/Game/GameHostChangedEvent.cs b/src/Impostor.Server/Events/Game/GameHostChangedEvent.cs new file mode 100644 index 000000000..3aa00f225 --- /dev/null +++ b/src/Impostor.Server/Events/Game/GameHostChangedEvent.cs @@ -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; } + } +} diff --git a/src/Impostor.Server/Net/State/Game.State.cs b/src/Impostor.Server/Net/State/Game.State.cs index 9f9d90473..a2105f146 100644 --- a/src/Impostor.Server/Net/State/Game.State.cs +++ b/src/Impostor.Server/Net/State/Game.State.cs @@ -50,8 +50,15 @@ private async ValueTask 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; @@ -60,12 +67,6 @@ private async ValueTask PlayerRemove(int playerId, bool isBan = false) return true; } - // Host migration. - if (HostId == playerId) - { - await MigrateHost(); - } - if (isBan) { BanIp(player.Client.Connection.EndPoint.Address); @@ -97,7 +98,6 @@ private async ValueTask MigrateHost() if (host == null) { - await EndAsync(); return; } diff --git a/src/Impostor.Server/Net/State/Game.cs b/src/Impostor.Server/Net/State/Game.cs index 20f6c01b2..2d28eeadd 100644 --- a/src/Impostor.Server/Net/State/Game.cs +++ b/src/Impostor.Server/Net/State/Game.cs @@ -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)