]> git.deb.at Git - rhonda/impostor.git/commitdiff
Add sync options method
authorAeonLucid <aeonlucid@outlook.com>
Sun, 25 Oct 2020 00:16:19 +0000 (02:16 +0200)
committerAeonLucid <aeonlucid@outlook.com>
Sun, 25 Oct 2020 00:16:19 +0000 (02:16 +0200)
src/Impostor.Api/Games/IGame.cs
src/Impostor.Plugins.Example/Handlers/PlayerEventListener.cs
src/Impostor.Server/Net/State/Game.Api.cs

index 17f67f1b84a6ef6e609d1ad54ede74b5f3bbcf0c..1460adcece9d3305a46e2c2f2b76484d1e43cde0 100644 (file)
@@ -34,6 +34,13 @@ namespace Impostor.Api.Games
 
         IClientPlayer GetClientPlayer(int clientId);
 
+        /// <summary>
+        ///     Syncs the internal <see cref="GameOptionsData"/> to all players.
+        ///     Necessary to do if you modified it, otherwise it won't be used.
+        /// </summary>
+        /// <returns></returns>
+        ValueTask SyncSettingsAsync();
+
         /// <summary>
         ///     Send the message to all players.
         /// </summary>
index 9d162299dcd9564ef370a00dbae23a2649a5e008..eaaf83320169b6917a042dc980c3308db0faff76 100644 (file)
@@ -24,6 +24,15 @@ namespace Impostor.Plugins.Example.Handlers
         {
             Console.WriteLine(e.PlayerControl.PlayerInfo.PlayerName + " said " + e.Message);
 
+            if (e.Message == "test")
+            {
+                e.Game.Options.KillCooldown = 0;
+                e.Game.Options.NumImpostors = 2;
+                e.Game.Options.PlayerSpeedMod = 5;
+
+                await e.Game.SyncSettingsAsync();
+            }
+
             await e.PlayerControl.SetNameAsync(e.Message);
             await e.PlayerControl.SendChatAsync(e.Message);
         }
index 0a764e37a7d0d606c90ae66a3ac7d3e6851393bc..2a5ec4a2b3e10c8ec129596d26caf77f22e6c4ef 100644 (file)
@@ -1,13 +1,36 @@
-using Impostor.Api.Games;
+using System.IO;
+using System.Threading.Tasks;
+using Impostor.Api.Games;
+using Impostor.Api.Innersloth;
 using Impostor.Api.Net;
 using Impostor.Api.Net.Inner;
+using Impostor.Server.Net.Inner;
 
 namespace Impostor.Server.Net.State
 {
-    internal partial class Game
+    internal partial class Game : IGame
     {
         IClientPlayer IGame.Host => Host;
 
         IGameNet IGame.GameNet => GameNet;
+
+        public async ValueTask SyncSettingsAsync()
+        {
+            using (var writer = StartRpc(Host.Character.NetId, RpcCalls.SyncSettings))
+            {
+                // Someone will probably forget to do this, so we include it here.
+                // If this is not done, the host will overwrite changes later with the defaults.
+                Options.IsDefaults = false;
+
+                await using (var memory = new MemoryStream())
+                await using (var writerBin = new BinaryWriter(memory))
+                {
+                    Options.Serialize(writerBin, GameOptionsData.LatestVersion);
+                    writer.WriteBytesAndSize(memory.ToArray());
+                }
+
+                await FinishRpcAsync(writer);
+            }
+        }
     }
 }
\ No newline at end of file