Merge pull request #589 from miniduikboot/feature/game-created-event-add-host

Add Host property to IGameCreatedEvent
This commit is contained in:
miniduikboot authored and GitHub committed 2024-04-21 21:44:24 +02:00
commit 155c3e69d9
3 files changed
+22 -5

No files matched your search

@@ -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; }
}
}
@@ -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; }
}
}
@@ -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);
}