Advertisement
Guest User

Untitled

a guest
Aug 15th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.47 KB | None | 0 0
  1. using DarkRift;
  2. using DarkRift.Server;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. namespace Chains_of_Atlas_Plugin
  8. {
  9.     /// <summary>
  10.     ///     Manages the players in the server.
  11.     /// </summary>
  12.     public class CoaPlayerManager : Plugin
  13.     {
  14.  
  15.         /// <summary>
  16.         ///     The version number of the plugin in SemVer form.
  17.         /// </summary>
  18.         public override Version Version => new Version(1, 0, 0);
  19.  
  20.         /// <summary>
  21.         ///     Indicates that this plugin is thread safe and DarkRift can invoke events from
  22.         ///     multiple threads simultaneously.
  23.         /// </summary>
  24.         public override bool ThreadSafe => true;
  25.  
  26.         /// <summary>
  27.         ///     The player in the server.
  28.         /// </summary>
  29.         Dictionary<IClient, Player> players = new Dictionary<IClient, Player>();
  30.  
  31.         public CoaPlayerManager(PluginLoadData pluginLoadData) : base(pluginLoadData)
  32.         {
  33.             //Subscribe for notification when a new client connects
  34.             ClientManager.ClientConnected += ClientManager_ClientConnected;
  35.  
  36.             //Subscribe for notifications when a new client disconnects
  37.             ClientManager.ClientDisconnected += ClientManager_ClientDisconnected;
  38.         }
  39.  
  40.         /// <summary>
  41.         ///     Invoked when a new client connects.
  42.         /// </summary>
  43.         /// <param name="sender">The client manager.</param>
  44.         /// <param name="e">The event arguments.</param>
  45.         void ClientManager_ClientConnected(object sender, ClientConnectedEventArgs e)
  46.         {
  47.             //Spawn our new player on all other players
  48.             Player player = new Player(new Vec3(0, 0, 0), new Vec3(0, 0, 0), 1, e.Client.ID);
  49.             using (Message message = Message.Create(CoaTags.SpawnPlayer, player))
  50.             {
  51.                 foreach (IClient client in ClientManager.GetAllClients())
  52.                 {
  53.                     if (client != e.Client)
  54.                         client.SendMessage(message, SendMode.Reliable);
  55.                 }
  56.             }
  57.  
  58.             lock (players)
  59.                 players.Add(e.Client, player);
  60.  
  61.             //Spawn all other players on our new player
  62.             foreach (IClient client in ClientManager.GetAllClients())
  63.             {
  64.                 Player p;
  65.                 lock (players)
  66.                     p = players[client];
  67.  
  68.                 using (Message message = Message.Create(CoaTags.SpawnPlayer, p))
  69.                     e.Client.SendMessage(message, SendMode.Reliable);
  70.             }
  71.  
  72.             //Subscribe to when this client sends messages
  73.             e.Client.MessageReceived += Client_PlayerEvent;
  74.             e.Client.MessageReceived += Client_AnimationTriggerEvent;
  75.         }
  76.  
  77.         /// <summary>
  78.         ///     Invoked when a new client connects.
  79.         /// </summary>
  80.         /// <param name="sender">The client manager.</param>
  81.         /// <param name="e">The event arguments.</param>
  82.         private void ClientManager_ClientDisconnected(object sender, ClientDisconnectedEventArgs e)
  83.         {
  84.             players.Remove(e.Client);
  85.  
  86.             using (DarkRiftWriter writer = DarkRiftWriter.Create())
  87.             {
  88.                 writer.Write(e.Client.ID);
  89.  
  90.                 using (Message message = Message.Create(CoaTags.DespawnPlayer, writer))
  91.                 {
  92.                     foreach (IClient client in ClientManager.GetAllClients())
  93.                         client.SendMessage(message, SendMode.Reliable);
  94.                 }
  95.             }
  96.         }
  97.  
  98.         /// <summary>
  99.         ///     Invoked when the client sends a Player message.
  100.         /// </summary>
  101.         /// <param name="sender">The client.</param>
  102.         /// <param name="e">The event arguments.</param>
  103.         void Client_PlayerEvent(object sender, MessageReceivedEventArgs e)
  104.         {
  105.             using (Message message = e.GetMessage() as Message)
  106.             {
  107.                 //Check it's a movement message
  108.                 if (message != null && message.Tag == CoaTags.Movement)
  109.                 {
  110.                     //Get the player in question
  111.                     Player player;
  112.                     lock (players)
  113.                         player = players[e.Client];
  114.  
  115.                     //Deserialize the new position
  116.                     using (DarkRiftReader reader = message.GetReader())
  117.                     {
  118.                         Vec3 newPosition = reader.ReadSerializable<Vec3>();
  119.                         Vec3 newRotation = reader.ReadSerializable<Vec3>();
  120.                         ushort newSpeed = reader.ReadUInt16();
  121.  
  122.                         lock (player)
  123.                         {
  124.                             //Update the player
  125.                             player.Position = newPosition;
  126.                             player.Rotation = newRotation;
  127.                             player.Speed = newSpeed;
  128.                             //Serialize the whole player to the message so that we also include the ID
  129.                             message.Serialize(player);
  130.                         }
  131.  
  132.                         //Send to everyone else
  133.                         foreach (IClient sendTo in ClientManager.GetAllClients().Except(new IClient[] { e.Client }))
  134.                             sendTo.SendMessage(message, SendMode.Reliable);
  135.                     }
  136.                 }
  137.             }
  138.         }
  139.  
  140.         /// <summary>
  141.         ///     Invoked when the client sends a animation event.
  142.         /// </summary>
  143.         /// <param name="sender">The client.</param>
  144.         /// <param name="e">The event arguments.</param>
  145.         void Client_AnimationTriggerEvent(object sender, MessageReceivedEventArgs e)
  146.         {
  147.             using (Message message = e.GetMessage() as Message)
  148.             {
  149.                 //Check its tag
  150.                 if (message != null && message.Tag == CoaTags.AnimationTrigger)
  151.                 {
  152.                     using (DarkRiftReader reader = message.GetReader())
  153.                     {
  154.                         string newTriggerEvent = reader.ReadString();
  155.  
  156.                         AnimationEvent ev = new AnimationEvent(newTriggerEvent, e.Client.ID);
  157.                         message.Serialize(ev);
  158.  
  159.                         //Send to everyone else
  160.                         foreach (IClient sendTo in ClientManager.GetAllClients().Except(new IClient[] { e.Client }))
  161.                             sendTo.SendMessage(message, SendMode.Reliable);
  162.                     }
  163.                 }
  164.             }
  165.         }
  166.  
  167.  
  168.  
  169.  
  170.  
  171.         private class AnimationEvent : IDarkRiftSerializable
  172.         {
  173.             public string TriggerEvent { get; set; }
  174.             public ushort ID { get; set; }
  175.  
  176.             public AnimationEvent()
  177.             {
  178.  
  179.             }
  180.  
  181.             public AnimationEvent(string triggerEvent, ushort ID)
  182.             {
  183.                 this.TriggerEvent = triggerEvent;
  184.                 this.ID = ID;
  185.             }
  186.  
  187.             public void Deserialize(DeserializeEvent e)
  188.             {
  189.                 this.TriggerEvent = e.Reader.ReadString();
  190.                 this.ID = e.Reader.ReadUInt16();
  191.             }
  192.  
  193.             public void Serialize(SerializeEvent e)
  194.             {
  195.                 e.Writer.Write(TriggerEvent);
  196.                 e.Writer.Write(ID);
  197.             }
  198.         }
  199.         /// <summary>
  200.         ///     Holds serializable data about a player.
  201.         /// </summary>
  202.         private class Player : IDarkRiftSerializable
  203.         {
  204.             public Vec3 Position { get; set; }
  205.             public Vec3 Rotation { get; set; }
  206.             public ushort Speed;
  207.             public ushort ID { get; set; }
  208.  
  209.             public Player()
  210.             {
  211.  
  212.             }
  213.  
  214.             public Player(Vec3 position, Vec3 rotation,ushort speed, ushort ID)
  215.             {
  216.                 this.Position = position;
  217.                 this.Rotation = rotation;
  218.                 this.Speed = speed;
  219.                 this.ID = ID;
  220.             }
  221.  
  222.             public void Deserialize(DeserializeEvent e)
  223.             {
  224.                 this.Position = e.Reader.ReadSerializable<Vec3>();
  225.                 this.Rotation = e.Reader.ReadSerializable<Vec3>();
  226.                 this.Speed = e.Reader.ReadUInt16();
  227.                 this.ID = e.Reader.ReadUInt16();
  228.             }
  229.  
  230.             public void Serialize(SerializeEvent e)
  231.             {
  232.                 e.Writer.Write(Position);
  233.                 e.Writer.Write(Rotation);
  234.                 e.Writer.Write(Speed);
  235.                 e.Writer.Write(ID);
  236.             }
  237.         }
  238.  
  239.         /// <summary>
  240.         ///     A primative 3 axis vector.
  241.         /// </summary>
  242.         private class Vec3 : IDarkRiftSerializable
  243.         {
  244.             public float X { get; set; }
  245.             public float Y { get; set; }
  246.             public float Z { get; set; }
  247.  
  248.             public Vec3()
  249.             {
  250.  
  251.             }
  252.  
  253.             public Vec3(float x, float y, float z)
  254.             {
  255.                 this.X = x;
  256.                 this.Y = y;
  257.                 this.Z = z;
  258.             }
  259.  
  260.             public void Deserialize(DeserializeEvent e)
  261.             {
  262.                 this.X = e.Reader.ReadSingle();
  263.                 this.Y = e.Reader.ReadSingle();
  264.                 this.Z = e.Reader.ReadSingle();
  265.             }
  266.  
  267.             public void Serialize(SerializeEvent e)
  268.             {
  269.                 e.Writer.Write(X);
  270.                 e.Writer.Write(Y);
  271.                 e.Writer.Write(Z);
  272.             }
  273.         }
  274.     }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement