From: AeonLucid Date: Mon, 19 Dec 2022 01:39:03 +0000 (+0100) Subject: Add FilterOptions to Game and use in FindListings X-Git-Tag: v1.8.0~2^2~10 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=5a1f1c9d86f4ef324965f8445fde7daac17b3ae7;p=rhonda%2Fimpostor.git Add FilterOptions to Game and use in FindListings --- diff --git a/src/Impostor.Api/Games/IGame.cs b/src/Impostor.Api/Games/IGame.cs index a10da63..54c6bde 100644 --- a/src/Impostor.Api/Games/IGame.cs +++ b/src/Impostor.Api/Games/IGame.cs @@ -12,6 +12,8 @@ namespace Impostor.Api.Games { IGameOptions Options { get; } + GameFilterOptions FilterOptions { get; } + GameCode Code { get; } GameStates GameState { get; } diff --git a/src/Impostor.Api/Games/Managers/IGameManager.cs b/src/Impostor.Api/Games/Managers/IGameManager.cs index c418b72..a9572e4 100644 --- a/src/Impostor.Api/Games/Managers/IGameManager.cs +++ b/src/Impostor.Api/Games/Managers/IGameManager.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Threading.Tasks; +using Impostor.Api.Innersloth; using Impostor.Api.Innersloth.GameOptions; namespace Impostor.Api.Games.Managers @@ -14,8 +15,9 @@ namespace Impostor.Api.Games.Managers /// Creates a new game. /// /// Game options. + /// Filter options. /// Created game or null if creation was cancelled by a plugin. /// Thrown when game creation failed. - ValueTask CreateAsync(IGameOptions options); + ValueTask CreateAsync(IGameOptions options, GameFilterOptions filterOptions); } } diff --git a/src/Impostor.Api/Innersloth/GameFilterOptions.cs b/src/Impostor.Api/Innersloth/GameFilterOptions.cs index e84cb52..e560c00 100644 --- a/src/Impostor.Api/Innersloth/GameFilterOptions.cs +++ b/src/Impostor.Api/Innersloth/GameFilterOptions.cs @@ -6,6 +6,17 @@ public class GameFilterOptions { public HashSet FilterTags { get; } = new HashSet(); + public static GameFilterOptions CreateDefault() + { + return new GameFilterOptions + { + FilterTags = + { + "Beginner", + }, + }; + } + public static GameFilterOptions Deserialize(IMessageReader reader) { var options = new GameFilterOptions(); diff --git a/src/Impostor.Client.App/Program.cs b/src/Impostor.Client.App/Program.cs index eeb328e..554dccd 100644 --- a/src/Impostor.Client.App/Program.cs +++ b/src/Impostor.Client.App/Program.cs @@ -32,7 +32,7 @@ namespace Impostor.Client.App { MaxPlayers = 4, NumImpostors = 2, - }, CrossplayFlags.All, new GameFilterOptions()); + }, CrossplayFlags.All, GameFilterOptions.CreateDefault()); // TODO: ObjectPool for MessageReaders using (var connection = new UdpClientConnection(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 22023), null)) diff --git a/src/Impostor.Plugins.Example/ExamplePlugin.cs b/src/Impostor.Plugins.Example/ExamplePlugin.cs index e5c91b0..c0f377f 100644 --- a/src/Impostor.Plugins.Example/ExamplePlugin.cs +++ b/src/Impostor.Plugins.Example/ExamplePlugin.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using Impostor.Api.Games.Managers; +using Impostor.Api.Innersloth; using Impostor.Api.Innersloth.GameOptions; using Impostor.Api.Plugins; using Microsoft.Extensions.Logging; @@ -22,7 +23,7 @@ namespace Impostor.Plugins.Example { _logger.LogInformation("Example is being enabled."); - var game = await _gameManager.CreateAsync(new NormalGameOptions()); + var game = await _gameManager.CreateAsync(new NormalGameOptions(), GameFilterOptions.CreateDefault()); if (game == null) { _logger.LogWarning("Example game creation was cancelled"); diff --git a/src/Impostor.Server/Net/Client.cs b/src/Impostor.Server/Net/Client.cs index b6c056f..af477be 100644 --- a/src/Impostor.Server/Net/Client.cs +++ b/src/Impostor.Server/Net/Client.cs @@ -69,7 +69,7 @@ namespace Impostor.Server.Net Message00HostGameC2S.Deserialize(reader, out var gameOptions, out _, out var gameFilterOptions); // Create game. - var game = await _gameManager.CreateAsync(this, gameOptions); + var game = await _gameManager.CreateAsync(this, gameOptions, gameFilterOptions); if (game == null) { @@ -260,8 +260,8 @@ namespace Impostor.Server.Net case MessageFlags.GetGameListV2: { - Message16GetGameListC2S.Deserialize(reader, out var options, out _, out _, out _); - await OnRequestGameListAsync(options); + Message16GetGameListC2S.Deserialize(reader, out var options, out _, out _, out var filterOptions); + await OnRequestGameListAsync(options, filterOptions); break; } @@ -359,11 +359,11 @@ namespace Impostor.Server.Net /// All options given. /// At this moment, the client can only specify the map, impostor count and chat language. /// - private ValueTask OnRequestGameListAsync(IGameOptions options) + private ValueTask OnRequestGameListAsync(IGameOptions options, GameFilterOptions filterOptions) { using var message = MessageWriter.Get(MessageType.Reliable); - var games = _gameManager.FindListings((MapFlags)options.Map, options.NumImpostors, options.Keywords, this.GameVersion); + var games = _gameManager.FindListings((MapFlags)options.Map, options.NumImpostors, options.Keywords, this.GameVersion, filterOptions.FilterTags); Message16GetGameListS2C.Serialize(message, games); diff --git a/src/Impostor.Server/Net/Manager/GameManager.cs b/src/Impostor.Server/Net/Manager/GameManager.cs index a4bf36a..1a95058 100644 --- a/src/Impostor.Server/Net/Manager/GameManager.cs +++ b/src/Impostor.Server/Net/Manager/GameManager.cs @@ -51,7 +51,7 @@ namespace Impostor.Server.Net.Manager return game; } - public IEnumerable FindListings(MapFlags map, int impostorCount, GameKeywords language, int gameVersion, int count = 10) + public IEnumerable FindListings(MapFlags map, int impostorCount, GameKeywords language, int gameVersion, HashSet filterTags, int count = 10) { var results = 0; @@ -78,6 +78,11 @@ namespace Impostor.Server.Net.Manager continue; } + if (!game.FilterOptions.FilterTags.SetEquals(filterTags)) + { + continue; + } + // Add to result. yield return game; @@ -111,7 +116,7 @@ namespace Impostor.Server.Net.Manager await _eventManager.CallAsync(new GameDestroyedEvent(game)); } - public async ValueTask CreateAsync(IClient? owner, IGameOptions options) + public async ValueTask CreateAsync(IClient? owner, IGameOptions options, GameFilterOptions filterOptions) { var @event = new GameCreationEvent(this, owner); await _eventManager.CallAsync(@event); @@ -121,11 +126,11 @@ namespace Impostor.Server.Net.Manager return null; } - var (success, game) = await TryCreateAsync(options, @event.GameCode); + var (success, game) = await TryCreateAsync(options, filterOptions, @event.GameCode); for (var i = 0; i < 10 && !success; i++) { - (success, game) = await TryCreateAsync(options); + (success, game) = await TryCreateAsync(options, filterOptions); } if (!success || game == null) @@ -136,15 +141,15 @@ namespace Impostor.Server.Net.Manager return game; } - public ValueTask CreateAsync(IGameOptions options) + public ValueTask CreateAsync(IGameOptions options, GameFilterOptions filterOptions) { - return CreateAsync(null, options); + return CreateAsync(null, options, filterOptions); } - private async ValueTask<(bool Success, Game? Game)> TryCreateAsync(IGameOptions options, GameCode? desiredGameCode = null) + private async ValueTask<(bool Success, Game? Game)> TryCreateAsync(IGameOptions options, GameFilterOptions filterOptions, GameCode? desiredGameCode = null) { var gameCode = desiredGameCode ?? _gameCodeFactory.Create(); - var game = ActivatorUtilities.CreateInstance(_serviceProvider, _publicIp, gameCode, options); + var game = ActivatorUtilities.CreateInstance(_serviceProvider, _publicIp, gameCode, options, filterOptions); if (!_games.TryAdd(gameCode, game)) { diff --git a/src/Impostor.Server/Net/State/Game.cs b/src/Impostor.Server/Net/State/Game.cs index 2f800e7..d364dda 100644 --- a/src/Impostor.Server/Net/State/Game.cs +++ b/src/Impostor.Server/Net/State/Game.cs @@ -38,6 +38,7 @@ namespace Impostor.Server.Net.State IPEndPoint publicIp, GameCode code, IGameOptions options, + GameFilterOptions filterOptions, ClientManager clientManager, IEventManager eventManager, IOptions compatibilityConfig) @@ -54,6 +55,7 @@ namespace Impostor.Server.Net.State GameState = GameStates.NotStarted; GameNet = new GameNet(); Options = options; + FilterOptions = filterOptions; _clientManager = clientManager; _eventManager = eventManager; _compatibilityConfig = compatibilityConfig.Value; @@ -74,6 +76,8 @@ namespace Impostor.Server.Net.State public IGameOptions Options { get; } + public GameFilterOptions FilterOptions { get; } + public IDictionary Items { get; } public int PlayerCount => _players.Count; diff --git a/src/Impostor.Tools.ServerReplay/Program.cs b/src/Impostor.Tools.ServerReplay/Program.cs index 990de64..7adeb14 100644 --- a/src/Impostor.Tools.ServerReplay/Program.cs +++ b/src/Impostor.Tools.ServerReplay/Program.cs @@ -214,7 +214,7 @@ namespace Impostor.Tools.ServerReplay case RecordedPacketType.GameCreated: _gameCodeFactory.Result = GameCode.From(reader.ReadString()); - await _gameManager.CreateAsync(Connections[clientId].Client, GameOptions[clientId]); + await _gameManager.CreateAsync(Connections[clientId].Client, GameOptions[clientId], GameFilterOptions.CreateDefault()); GameOptions.Remove(clientId); break;