]> git.deb.at Git - rhonda/impostor.git/commitdiff
Add more API methods to InnerPlayerControl
authorAeonLucid <aeonlucid@outlook.com>
Mon, 26 Oct 2020 19:31:05 +0000 (20:31 +0100)
committerAeonLucid <aeonlucid@outlook.com>
Mon, 26 Oct 2020 19:31:05 +0000 (20:31 +0100)
src/Impostor.Api/Net/Inner/Objects/IInnerPlayerControl.cs
src/Impostor.Plugins.Example/Handlers/PlayerEventListener.cs
src/Impostor.Server/Net/Hazel/HazelConnection.cs
src/Impostor.Server/Net/Inner/Objects/InnerPlayerControl.Api.cs

index 4d53d2c52c2a8d49efd0494afa12b7e7152763a0..e86214745c6c7622035a8630c11adeb8797d6558 100644 (file)
@@ -4,10 +4,58 @@ namespace Impostor.Api.Net.Inner.Objects
 {
     public interface IInnerPlayerControl
     {
+        /// <summary>
+        ///     Gets the <see cref="IInnerPlayerInfo"/> of the <see cref="IInnerPlayerControl"/>.
+        ///     Contains metadata about the player.
+        /// </summary>
         IInnerPlayerInfo PlayerInfo { get; }
 
+        /// <summary>
+        ///     Sets the name of the current <see cref="IInnerPlayerControl"/>.
+        ///     Visible to all players.
+        /// </summary>
+        /// <param name="name">A name for the player.</param>
+        /// <returns>Task that must be awaited.</returns>
         ValueTask SetNameAsync(string name);
 
+        /// <summary>
+        ///     Sets the color of the current <see cref="IInnerPlayerControl"/>.
+        ///     Visible to all players.
+        /// </summary>
+        /// <param name="colorId">A color for the player.</param>
+        /// <returns>Task that must be awaited.</returns>
+        ValueTask SetColorAsync(byte colorId);
+
+        /// <summary>
+        ///     Sets the hat of the current <see cref="IInnerPlayerControl"/>.
+        ///     Visible to all players.
+        /// </summary>
+        /// <param name="hatId">An hat for the player.</param>
+        /// <returns>Task that must be awaited.</returns>
+        ValueTask SetHatAsync(uint hatId);
+
+        /// <summary>
+        ///     Sets the pet of the current <see cref="IInnerPlayerControl"/>.
+        ///     Visible to all players.
+        /// </summary>
+        /// <param name="petId">An hat for the player.</param>
+        /// <returns>Task that must be awaited.</returns>
+        ValueTask SetPetAsync(uint petId);
+
+        /// <summary>
+        ///     Sets the skin of the current <see cref="IInnerPlayerControl"/>.
+        ///     Visible to all players.
+        /// </summary>
+        /// <param name="skinId">An hat for the player.</param>
+        /// <returns>Task that must be awaited.</returns>
+        ValueTask SetSkinAsync(uint skinId);
+
+        /// <summary>
+        ///     Send a chat message as the current <see cref="IInnerPlayerControl"/>.
+        ///     Visible to all players.
+        /// </summary>
+        /// <param name="text">The message to send.</param>
+        /// <returns>Task that must be awaited.</returns>
         ValueTask SendChatAsync(string text);
     }
-}
\ No newline at end of file
+}
index 2b1b13014593caf13b457c0cdeb9952458fc5d95..f3b8dfb09917c4e47f6922f1bd417b935cd0b009 100644 (file)
@@ -7,10 +7,39 @@ namespace Impostor.Plugins.Example.Handlers
 {
     public class PlayerEventListener : IEventListener
     {
+        private static readonly Random Random = new Random();
+
         [EventListener]
         public void OnPlayerSpawned(IPlayerSpawnedEvent e)
         {
             Console.WriteLine(e.PlayerControl.PlayerInfo.PlayerName + " spawned");
+
+            // Need to make a local copy because it might be possible that
+            // the event gets changed after being handled.
+            var clientPlayer = e.ClientPlayer;
+            var playerControl = e.PlayerControl;
+
+            Task.Run(async () =>
+            {
+                Console.WriteLine("Starting player task.");
+
+                // Give the player time to load.
+                await Task.Delay(TimeSpan.FromSeconds(3));
+
+                while (clientPlayer.Client.Connection != null &&
+                       clientPlayer.Client.Connection.IsConnected)
+                {
+                    // Modify player properties.
+                    await playerControl.SetColorAsync((byte) Random.Next(1, 9));
+                    await playerControl.SetHatAsync((uint) Random.Next(1, 9));
+                    await playerControl.SetSkinAsync((uint) Random.Next(1, 9));
+                    await playerControl.SetPetAsync((uint) Random.Next(1, 9));
+
+                    await Task.Delay(TimeSpan.FromMilliseconds(500));
+                }
+
+                Console.WriteLine("Stopping player task.");
+            });
         }
 
         [EventListener]
index 9a9a07140d98112df8e1da373f2d20fdeeba39b4..1d6f35c475a4044257b05ac0279f73f15c8eb5bf 100644 (file)
@@ -54,7 +54,6 @@ namespace Impostor.Server.Net.Hazel
         {
             if (Client == null)
             {
-                _logger.LogWarning("Client was null.");
                 return;
             }
 
@@ -69,4 +68,4 @@ namespace Impostor.Server.Net.Hazel
             }
         }
     }
-}
\ No newline at end of file
+}
index 6a9fa45fa093c21909a083362b719da5f482ede8..d3e8d8070b090b8c5171ba0d252137237fbfffee 100644 (file)
@@ -9,20 +9,54 @@ namespace Impostor.Server.Net.Inner.Objects
 
         public async ValueTask SetNameAsync(string name)
         {
-            using (var writer = _game.StartRpc(NetId, RpcCalls.SetName))
-            {
-                writer.Write(name);
-                await _game.FinishRpcAsync(writer);
-            }
+            PlayerInfo.PlayerName = name;
+
+            using var writer = _game.StartRpc(NetId, RpcCalls.SetName);
+            writer.Write(name);
+            await _game.FinishRpcAsync(writer);
+        }
+
+        public async ValueTask SetColorAsync(byte colorId)
+        {
+            PlayerInfo.ColorId = colorId;
+
+            using var writer = _game.StartRpc(NetId, RpcCalls.SetColor);
+            writer.Write(colorId);
+            await _game.FinishRpcAsync(writer);
+        }
+
+        public async ValueTask SetHatAsync(uint hatId)
+        {
+            PlayerInfo.HatId = hatId;
+
+            using var writer = _game.StartRpc(NetId, RpcCalls.SetHat);
+            writer.WritePacked(hatId);
+            await _game.FinishRpcAsync(writer);
+        }
+
+        public async ValueTask SetPetAsync(uint petId)
+        {
+            PlayerInfo.PetId = petId;
+
+            using var writer = _game.StartRpc(NetId, RpcCalls.SetPet);
+            writer.WritePacked(petId);
+            await _game.FinishRpcAsync(writer);
+        }
+
+        public async ValueTask SetSkinAsync(uint skinId)
+        {
+            PlayerInfo.SkinId = skinId;
+
+            using var writer = _game.StartRpc(NetId, RpcCalls.SetSkin);
+            writer.WritePacked(skinId);
+            await _game.FinishRpcAsync(writer);
         }
 
         public async ValueTask SendChatAsync(string text)
         {
-            using (var writer = _game.StartRpc(NetId, RpcCalls.SendChat))
-            {
-                writer.Write(text);
-                await _game.FinishRpcAsync(writer);
-            }
+            using var writer = _game.StartRpc(NetId, RpcCalls.SendChat);
+            writer.Write(text);
+            await _game.FinishRpcAsync(writer);
         }
     }
-}
\ No newline at end of file
+}