]> git.deb.at Git - rhonda/impostor.git/commitdiff
Add FilterOptions to Game and use in FindListings
authorAeonLucid <aeonlucid@gmail.com>
Mon, 19 Dec 2022 01:39:03 +0000 (02:39 +0100)
committerAeonLucid <aeonlucid@gmail.com>
Mon, 19 Dec 2022 01:39:03 +0000 (02:39 +0100)
src/Impostor.Api/Games/IGame.cs
src/Impostor.Api/Games/Managers/IGameManager.cs
src/Impostor.Api/Innersloth/GameFilterOptions.cs
src/Impostor.Client.App/Program.cs
src/Impostor.Plugins.Example/ExamplePlugin.cs
src/Impostor.Server/Net/Client.cs
src/Impostor.Server/Net/Manager/GameManager.cs
src/Impostor.Server/Net/State/Game.cs
src/Impostor.Tools.ServerReplay/Program.cs

index a10da63e161d372715a2b00b2533f36ded211b1a..54c6bdeb4a5804d8200dc957d4dc793bfe5bd52f 100644 (file)
@@ -12,6 +12,8 @@ namespace Impostor.Api.Games
     {
         IGameOptions Options { get; }
 
+        GameFilterOptions FilterOptions { get; }
+
         GameCode Code { get; }
 
         GameStates GameState { get; }
index c418b729d1fbb32a68f9b8957e426505c09d9525..a9572e40a80f68c4c6906b240e8be2d443ed4c93 100644 (file)
@@ -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.
         /// </summary>
         /// <param name="options">Game options.</param>
+        /// <param name="filterOptions">Filter options.</param>
         /// <returns>Created game or null if creation was cancelled by a plugin.</returns>
         /// <exception cref="ImpostorException">Thrown when game creation failed.</exception>
-        ValueTask<IGame?> CreateAsync(IGameOptions options);
+        ValueTask<IGame?> CreateAsync(IGameOptions options, GameFilterOptions filterOptions);
     }
 }
index e84cb526ecdb196e715dc25d1df4acd7041c8a01..e560c002d768110e61f33ea887563f4f1d693132 100644 (file)
@@ -6,6 +6,17 @@ public class GameFilterOptions
 {
     public HashSet<string> FilterTags { get; } = new HashSet<string>();
 
+    public static GameFilterOptions CreateDefault()
+    {
+        return new GameFilterOptions
+        {
+            FilterTags =
+            {
+                "Beginner",
+            },
+        };
+    }
+
     public static GameFilterOptions Deserialize(IMessageReader reader)
     {
         var options = new GameFilterOptions();
index eeb328e4b00950d8efeec8d1d09291f83eb66665..554dccd8753543d02b74458119529895c61efc87 100644 (file)
@@ -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))
index e5c91b0f626b0ce54b3bf500f1e9987f2a72f69d..c0f377f15d451d11e6d88daec2093292b4f29a5e 100644 (file)
@@ -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");
index b6c056f69360de0a2f5d91460dd13048b99b9d67..af477be858fec0c44054ea80b13ccfa2c3d6fbae 100644 (file)
@@ -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.
         /// </param>
-        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);
 
index a4bf36afe8bfe07bbc19d85e032e7daaac601ba1..1a9505834691aa4d0237fb39eed2140ad3536d07 100644 (file)
@@ -51,7 +51,7 @@ namespace Impostor.Server.Net.Manager
             return game;
         }
 
-        public IEnumerable<Game> FindListings(MapFlags map, int impostorCount, GameKeywords language, int gameVersion, int count = 10)
+        public IEnumerable<Game> FindListings(MapFlags map, int impostorCount, GameKeywords language, int gameVersion, HashSet<string> 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<IGame?> CreateAsync(IClient? owner, IGameOptions options)
+        public async ValueTask<IGame?> 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<IGame?> CreateAsync(IGameOptions options)
+        public ValueTask<IGame?> 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<Game>(_serviceProvider, _publicIp, gameCode, options);
+            var game = ActivatorUtilities.CreateInstance<Game>(_serviceProvider, _publicIp, gameCode, options, filterOptions);
 
             if (!_games.TryAdd(gameCode, game))
             {
index 2f800e7a190802a95c3393c89ddb6e1907be0465..d364ddad75aebede7eeb7fcd2c24c7fc612f8c1d 100644 (file)
@@ -38,6 +38,7 @@ namespace Impostor.Server.Net.State
             IPEndPoint publicIp,
             GameCode code,
             IGameOptions options,
+            GameFilterOptions filterOptions,
             ClientManager clientManager,
             IEventManager eventManager,
             IOptions<CompatibilityConfig> 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<object, object> Items { get; }
 
         public int PlayerCount => _players.Count;
index 990de641670c6fc2fcb89c742a636b72663034d0..7adeb145bbc8112c07c592f5ed009378c29d146f 100644 (file)
@@ -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;