]> git.deb.at Git - rhonda/impostor.git/commitdiff
Add create game api (#241)
authorjs6pak <kubastaron@hotmail.com>
Thu, 4 Mar 2021 09:11:50 +0000 (09:11 +0000)
committerGitHub <noreply@github.com>
Thu, 4 Mar 2021 09:11:50 +0000 (09:11 +0000)
src/Impostor.Api/Games/Extensions/GameManagerExtensions.cs
src/Impostor.Api/Games/IGame.cs
src/Impostor.Api/Games/Managers/IGameManager.cs
src/Impostor.Api/Innersloth/GameOptionsData.cs
src/Impostor.Api/Innersloth/MapTypes.cs
src/Impostor.Api/Net/Messages/S2C/Message16GetGameListS2C.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.Api.cs
src/Impostor.Server/Net/State/Game.cs

index 9a5a2b4f1eb29b646bf0b4541d0d02c4b57c9957..6a95b3d4a5de26b69d55a0a93e2070bc17db125b 100644 (file)
@@ -8,7 +8,7 @@ namespace Impostor.Api.Games
     {
         public static int GetGameCount(this IGameManager manager, MapFlags map)
         {
-            return manager.Games.Count(game => map.HasFlag((MapFlags)(1 << game.Options.MapId)));
+            return manager.Games.Count(game => map.HasFlag((MapFlags)(1 << (byte)game.Options.Map)));
         }
     }
 }
\ No newline at end of file
index 7c6140c09591cd6769d8a94cad7ff7c511454af0..57c3fcd2551ca23935aebe441d36dac5d7974f62 100644 (file)
@@ -24,10 +24,15 @@ namespace Impostor.Api.Games
 
         int PlayerCount { get; }
 
-        IClientPlayer Host { get; }
+        IClientPlayer? Host { get; }
 
         bool IsPublic { get; }
 
+        /// <summary>
+        /// Gets or sets display name on game list.
+        /// </summary>
+        string? DisplayName { get; set; }
+
         IDictionary<object, object> Items { get; }
 
         int HostId { get; }
@@ -55,6 +60,13 @@ namespace Impostor.Api.Games
         /// <returns>A <see cref="ValueTask"/> representing the asynchronous operation.</returns>
         ValueTask SyncSettingsAsync();
 
+        /// <summary>
+        ///     Sets game's privacy.
+        /// </summary>
+        /// <param name="isPublic">Privacy to set.</param>
+        /// <returns>A <see cref="ValueTask"/> representing the asynchronous operation.</returns>
+        ValueTask SetPrivacyAsync(bool isPublic);
+
         /// <summary>
         ///     Send the message to all players.
         /// </summary>
index 10a05f0773cbe74c41c7f2283c747cc2f36ce4e4..7ee403fd28da5f44265e83b073c03d995da16eff 100644 (file)
@@ -1,4 +1,6 @@
 using System.Collections.Generic;
+using System.Threading.Tasks;
+using Impostor.Api.Innersloth;
 
 namespace Impostor.Api.Games.Managers
 {
@@ -7,5 +9,7 @@ namespace Impostor.Api.Games.Managers
         IEnumerable<IGame> Games { get; }
 
         IGame? Find(GameCode code);
+
+        ValueTask<IGame> CreateAsync(GameOptionsData options);
     }
 }
\ No newline at end of file
index 7f7e0c5efcad96b1fda51a4fbd2f8ae74f6eae8b..3d648445e478a6b7a5943300a995003ca9bbea83 100644 (file)
@@ -14,112 +14,97 @@ namespace Impostor.Api.Innersloth
         /// <summary>
         /// Gets or sets host's version of the game.
         /// </summary>
-        public byte Version { get; set; }
+        public byte Version { get; set; } = LatestVersion;
 
         /// <summary>
         /// Gets or sets the maximum amount of players for this lobby.
         /// </summary>
-        public byte MaxPlayers { get; set; }
+        public byte MaxPlayers { get; set; } = 10;
 
         /// <summary>
         /// Gets or sets the language of the lobby as per <see cref="GameKeywords"/> enum.
         /// </summary>
-        public GameKeywords Keywords { get; set; }
+        public GameKeywords Keywords { get; set; } = GameKeywords.English;
 
         /// <summary>
-        /// Gets or sets the MapId selected for this lobby
+        /// Gets or sets the Map selected for this lobby.
         /// </summary>
-        /// <remarks>
-        /// Skeld = 0, MiraHQ = 1, Polus = 2.
-        /// </remarks>
-        internal byte MapId { get; set; }
-
-        /// <summary>
-        /// Gets or sets the map selected for this lobby
-        /// </summary>
-        public MapTypes Map
-        {
-            get => (MapTypes)MapId;
-            set => MapId = (byte)value;
-        }
+        public MapTypes Map { get; set; } = MapTypes.Skeld;
 
         /// <summary>
         /// Gets or sets the Player speed modifier.
         /// </summary>
-        public float PlayerSpeedMod { get; set; }
+        public float PlayerSpeedMod { get; set; } = 1f;
 
         /// <summary>
         /// Gets or sets the Light modifier for the players that are members of the crew as a multiplier value.
         /// </summary>
-        public float CrewLightMod { get; set; }
+        public float CrewLightMod { get; set; } = 1f;
 
         /// <summary>
         /// Gets or sets the Light modifier for the players that are Impostors as a multiplier value.
         /// </summary>
-        public float ImpostorLightMod { get; set; }
+        public float ImpostorLightMod { get; set; } = 1f;
 
         /// <summary>
         /// Gets or sets the Impostor cooldown to kill in seconds.
         /// </summary>
-        public float KillCooldown { get; set; }
+        public float KillCooldown { get; set; } = 15f;
 
         /// <summary>
         /// Gets or sets the number of common tasks.
         /// </summary>
-        public int NumCommonTasks { get; set; }
+        public int NumCommonTasks { get; set; } = 1;
 
         /// <summary>
         /// Gets or sets the number of long tasks.
         /// </summary>
-        public int NumLongTasks { get; set; }
+        public int NumLongTasks { get; set; } = 1;
 
         /// <summary>
         /// Gets or sets the number of short tasks.
         /// </summary>
-        public int NumShortTasks { get; set; }
+        public int NumShortTasks { get; set; } = 2;
 
         /// <summary>
         /// Gets or sets the maximum amount of emergency meetings each player can call during the game in seconds.
         /// </summary>
-        public int NumEmergencyMeetings { get; set; }
+        public int NumEmergencyMeetings { get; set; } = 1;
 
         /// <summary>
         /// Gets or sets the cooldown between each time any player can call an emergency meeting in seconds.
         /// </summary>
-        public int EmergencyCooldown { get; set; }
+        public int EmergencyCooldown { get; set; } = 15;
 
         /// <summary>
         /// Gets or sets the number of impostors for this lobby.
         /// </summary>
-        public int NumImpostors { get; set; }
+        public int NumImpostors { get; set; } = 1;
 
         /// <summary>
         /// Gets or sets a value indicating whether ghosts (dead crew members) can do tasks.
         /// </summary>
-        public bool GhostsDoTasks { get; set; }
+        public bool GhostsDoTasks { get; set; } = true;
 
         /// <summary>
         /// Gets or sets the Kill as per values in <see cref="KillDistances"/>.
         /// </summary>
-        /// <remarks>
-        /// Short = 0, Normal = 1, Long = 2.
-        /// </remarks>
-        public KillDistances KillDistance { get; set; }
+        public KillDistances KillDistance { get; set; } = KillDistances.Normal;
 
         /// <summary>
         /// Gets or sets the time for discussion before voting time in seconds.
         /// </summary>
-        public int DiscussionTime { get; set; }
+        public int DiscussionTime { get; set; } = 15;
 
         /// <summary>
         /// Gets or sets the time for voting in seconds.
         /// </summary>
-        public int VotingTime { get; set; }
+        public int VotingTime { get; set; } = 120;
 
         /// <summary>
         /// Gets or sets a value indicating whether an ejected player is an impostor or not.
         /// </summary>
-        public bool ConfirmImpostor { get; set; }
+        public bool ConfirmImpostor { get; set; } = true;
 
         /// <summary>
         /// Gets or sets a value indicating whether players are able to see tasks being performed by other players.
@@ -127,7 +112,7 @@ namespace Impostor.Api.Innersloth
         /// <remarks>
         /// By being set to true, tasks such as Empty Garbage, Submit Scan, Clear asteroids, Prime shields execution will be visible to other players.
         /// </remarks>
-        public bool VisualTasks { get; set; }
+        public bool VisualTasks { get; set; } = true;
 
         /// <summary>
         /// Gets or sets a value indicating whether the vote is anonymous.
@@ -137,12 +122,12 @@ namespace Impostor.Api.Innersloth
         /// <summary>
         /// Gets or sets the task bar update mode as per values in <see cref="Innersloth.TaskBarUpdate"/>.
         /// </summary>
-        public TaskBarUpdate TaskBarUpdate { get; set; }
+        public TaskBarUpdate TaskBarUpdate { get; set; } = TaskBarUpdate.Always;
 
         /// <summary>
         /// Gets or sets a value indicating whether the GameOptions are the default ones.
         /// </summary>
-        public bool IsDefaults { get; set; }
+        public bool IsDefaults { get; set; } = true;
 
         /// <summary>
         /// Deserialize a packet/message to a new GameOptionsData object.
@@ -166,7 +151,7 @@ namespace Impostor.Api.Innersloth
             writer.Write((byte)version);
             writer.Write((byte)MaxPlayers);
             writer.Write((uint)Keywords);
-            writer.Write((byte)MapId);
+            writer.Write((byte)Map);
             writer.Write((float)PlayerSpeedMod);
             writer.Write((float)CrewLightMod);
             writer.Write((float)ImpostorLightMod);
@@ -223,7 +208,7 @@ namespace Impostor.Api.Innersloth
             Version = bytes.ReadByte();
             MaxPlayers = bytes.ReadByte();
             Keywords = (GameKeywords)bytes.ReadUInt32();
-            MapId = bytes.ReadByte();
+            Map = (MapTypes)bytes.ReadByte();
             PlayerSpeedMod = bytes.ReadSingle();
 
             CrewLightMod = bytes.ReadSingle();
index 8dc07b5d17c3b8c5ca3e10b64b51b51298bbd09e..2b5ede98d3416d7ce5fd5ea977e863a8f28cee50 100644 (file)
@@ -1,6 +1,6 @@
 namespace Impostor.Api.Innersloth
 {
-    public enum MapTypes
+    public enum MapTypes : byte
     {
         Skeld = 0,
         MiraHQ = 1,
index 93386d7640b1dcbfad6fca2ab0eeb3d80c12e151..0d742edc71481692ac8cd95763428fa6dfc70e1e 100644 (file)
@@ -25,10 +25,10 @@ namespace Impostor.Api.Net.Messages.S2C
                 writer.Write(game.PublicIp.Address);
                 writer.Write((ushort)game.PublicIp.Port);
                 writer.Write(game.Code);
-                writer.Write(game.Host.Client.Name);
+                writer.Write(game.DisplayName ?? game.Host?.Client.Name ?? string.Empty);
                 writer.Write((byte)game.PlayerCount);
                 writer.WritePacked(1); // TODO: What does Age do?
-                writer.Write((byte)game.Options.MapId);
+                writer.Write((byte)game.Options.Map);
                 writer.Write((byte)game.Options.NumImpostors);
                 writer.Write((byte)game.Options.MaxPlayers);
                 writer.EndMessage();
@@ -43,4 +43,4 @@ namespace Impostor.Api.Net.Messages.S2C
             throw new System.NotImplementedException();
         }
     }
-}
\ No newline at end of file
+}
index dafba7c478aea803b453d886c073d6fdc4c0c41a..139e42c04ca7c859fed6be3a58032ac2123686b1 100644 (file)
@@ -1,4 +1,6 @@
 using System.Threading.Tasks;
+using Impostor.Api.Games.Managers;
+using Impostor.Api.Innersloth;
 using Impostor.Api.Plugins;
 using Microsoft.Extensions.Logging;
 
@@ -12,16 +14,23 @@ namespace Impostor.Plugins.Example
     public class ExamplePlugin : PluginBase
     {
         private readonly ILogger<ExamplePlugin> _logger;
+        private readonly IGameManager _gameManager;
 
-        public ExamplePlugin(ILogger<ExamplePlugin> logger)
+        public ExamplePlugin(ILogger<ExamplePlugin> logger, IGameManager gameManager)
         {
             _logger = logger;
+            _gameManager = gameManager;
         }
 
-        public override ValueTask EnableAsync()
+        public override async ValueTask EnableAsync()
         {
             _logger.LogInformation("Example is being enabled.");
-            return default;
+
+            var game = await _gameManager.CreateAsync(new GameOptionsData());
+            game.DisplayName = "Example game";
+            await game.SetPrivacyAsync(true);
+            
+            _logger.LogInformation("Created game {0}.", game.Code.Code);
         }
 
         public override ValueTask DisableAsync()
index 143a0d1032418f810772d404f4f58195f2d45d78..87fc56c3d99e56149e7e8a455e466772ab2de747 100644 (file)
@@ -332,7 +332,7 @@ namespace Impostor.Server.Net
         {
             using var message = MessageWriter.Get(MessageType.Reliable);
 
-            var games = _gameManager.FindListings((MapFlags)options.MapId, options.NumImpostors, options.Keywords);
+            var games = _gameManager.FindListings((MapFlags)options.Map, options.NumImpostors, options.Keywords);
 
             var skeldGameCount = _gameManager.GetGameCount(MapFlags.Skeld);
             var miraHqGameCount = _gameManager.GetGameCount(MapFlags.MiraHQ);
index a37829e585460dbceb9d05c420b74a9bc374fdce..8d883a41258d794d4a32e8890c839ed2fbb8f4b3 100644 (file)
@@ -98,7 +98,7 @@ namespace Impostor.Server.Net.Manager
                 x.Value.PlayerCount < x.Value.Options.MaxPlayers))
             {
                 // Check for options.
-                if (!map.HasFlag((MapFlags)(1 << game.Options.MapId)))
+                if (!map.HasFlag((MapFlags)(1 << (byte)game.Options.Map)))
                 {
                     continue;
                 }
index 1a623be9653a3c5a791a903c49dbcbabebe9fb24..a2996915f2f8bc44d4ae0a6c74c79e6a174e5d02 100644 (file)
@@ -6,13 +6,15 @@ using Impostor.Api.Games;
 using Impostor.Api.Innersloth;
 using Impostor.Api.Net;
 using Impostor.Api.Net.Inner;
+using Impostor.Api.Net.Messages;
+using Impostor.Hazel;
 using Impostor.Server.Net.Inner;
 
 namespace Impostor.Server.Net.State
 {
     internal partial class Game : IGame
     {
-        IClientPlayer IGame.Host => Host;
+        IClientPlayer? IGame.Host => Host;
 
         IGameNet IGame.GameNet => GameNet;
 
@@ -44,5 +46,17 @@ namespace Impostor.Server.Net.State
                 await FinishRpcAsync(writer);
             }
         }
+
+        public async ValueTask SetPrivacyAsync(bool isPublic)
+        {
+            IsPublic = isPublic;
+
+            using (var writer = MessageWriter.Get(MessageType.Reliable))
+            {
+                WriteAlterGameMessage(writer, false, IsPublic);
+
+                await SendToAllAsync(writer);
+            }
+        }
     }
 }
index a1aeb48a66b45849bd3e8f8dc10ae15039babf1f..6ca2fd6871d5432802f7490b9f209ebf9c32d47b 100644 (file)
@@ -60,6 +60,8 @@ namespace Impostor.Server.Net.State
 
         public bool IsPublic { get; private set; }
 
+        public string? DisplayName { get; set; }
+
         public int HostId { get; private set; }
 
         public GameStates GameState { get; private set; }