]> git.deb.at Git - rhonda/impostor.git/commitdiff
Add Host property to IGameCreatedEvent
authorminiduikboot <mini@duikbo.at>
Fri, 12 Apr 2024 20:42:27 +0000 (22:42 +0200)
committerminiduikboot <mini@duikbo.at>
Fri, 12 Apr 2024 20:53:01 +0000 (22:53 +0200)
When a game has just been created, it does not yet have any players in
them, and as a result you can't get the host or its connection id when
this event is called. This makes it annoying to link between this event
and the IGameCreationEvent that came just before it.

Having this link is useful for the usecase of a game code allocation
plugin that allocates codes from a pool, and needs to return codes to
a pool if these codes weren't picked.

src/Impostor.Api/Events/Game/IGameCreatedEvent.cs
src/Impostor.Server/Events/Game/GameCreatedEvent.cs
src/Impostor.Server/Net/Manager/GameManager.cs

index 48c8d7ccadec055ea727e313739966ee31c18ca2..089ee9c8a667ac4fc6e5cb4714e5a3ae5626358b 100644 (file)
@@ -1,11 +1,24 @@
 using Impostor.Api.Games;
+using Impostor.Api.Net;
 
 namespace Impostor.Api.Events
 {
     /// <summary>
     ///     Called whenever a new <see cref="IGame" /> is created.
     /// </summary>
+    /// <remarks>
+    ///     Note that the game just has been created, so no players have joined
+    ///     it yet. If you want to know the future host of this game, use the
+    ///     <see cref="Host"/> property.
+    /// </remarks>
     public interface IGameCreatedEvent : IGameEvent
     {
+        /// <summary>
+        ///     Gets the client that requested creation of the game.
+        /// </summary>
+        /// <remarks>
+        ///     Will be null if game creation was requested by a plugin.
+        /// </remarks>
+        IClient? Host { get; }
     }
 }
index 57e7a20e03d828e8a9c2612828a8657e79d9155d..d3691608013a922533a34921287a6fbe9ceb911d 100644 (file)
@@ -1,15 +1,19 @@
 using Impostor.Api.Events;
 using Impostor.Api.Games;
+using Impostor.Api.Net;
 
 namespace Impostor.Server.Events
 {
     public class GameCreatedEvent : IGameCreatedEvent
     {
-        public GameCreatedEvent(IGame game)
+        public GameCreatedEvent(IGame game, IClient? host)
         {
             Game = game;
+            Host = host;
         }
 
         public IGame Game { get; }
+
+        public IClient? Host { get; }
     }
 }
index c8371d55512481454092039733ae439d95d5bd93..4e985214223bb87c11952ae5a3cfb1f495d90872 100644 (file)
@@ -93,11 +93,11 @@ namespace Impostor.Server.Net.Manager
                 return null;
             }
 
-            var (success, game) = await TryCreateAsync(options, filterOptions, @event.GameCode);
+            var (success, game) = await TryCreateAsync(options, filterOptions, owner, @event.GameCode);
 
             for (var i = 0; i < 10 && !success; i++)
             {
-                (success, game) = await TryCreateAsync(options, filterOptions);
+                (success, game) = await TryCreateAsync(options, filterOptions, owner);
             }
 
             if (!success || game == null)
@@ -113,7 +113,7 @@ namespace Impostor.Server.Net.Manager
             return CreateAsync(null, options, filterOptions);
         }
 
-        private async ValueTask<(bool Success, Game? Game)> TryCreateAsync(IGameOptions options, GameFilterOptions filterOptions, GameCode? desiredGameCode = null)
+        private async ValueTask<(bool Success, Game? Game)> TryCreateAsync(IGameOptions options, GameFilterOptions filterOptions, IClient? owner, GameCode? desiredGameCode = null)
         {
             var gameCode = desiredGameCode ?? _gameCodeFactory.Create();
             var game = ActivatorUtilities.CreateInstance<Game>(_serviceProvider, _publicIp, gameCode, options, filterOptions);
@@ -125,7 +125,7 @@ namespace Impostor.Server.Net.Manager
 
             _logger.LogDebug("Created game with code {0}.", game.Code);
 
-            await _eventManager.CallAsync(new GameCreatedEvent(game));
+            await _eventManager.CallAsync(new GameCreatedEvent(game, owner));
 
             return (true, game);
         }