void GetAnimations(MMOMessage M)
        {
            if (multiplayerPlayer.IsLocal)
            {
                return;
            }
            if (!MultiplayerClient.singleton.OtherPlayers.ContainsKey(M.Sender))
            {
                return;
            }
            if (MultiplayerClient.singleton.OtherPlayers[M.Sender] != transform)
            {
                return;
            }

            Dictionary <string, string>[] Variables = JsonConvert.DeserializeObject <Dictionary <string, string>[]>(M.Message);

            Dictionary <string, string> intsVariables   = Variables[0];
            Dictionary <string, string> boolsVariables  = Variables[1];
            Dictionary <string, string> floatsVariables = Variables[2];

            foreach (var i in intsVariables.Keys)
            {
                animator.SetInteger(i, int.Parse(intsVariables[i]));
            }
            foreach (var b in boolsVariables.Keys)
            {
                animator.SetBool(b, boolsVariables[b] == "1" ? true : false);
            }
            foreach (var f in floatsVariables.Keys)
            {
                animator.SetFloat(f, float.Parse(floatsVariables[f]));
            }
        }
 public void PlayerDisconnected(MMOMessage M)
 {
     if (otherPlayers.ContainsKey(M.Sender))
     {
         Destroy(otherPlayers[M.Sender].gameObject);
         otherPlayers.Remove(M.Sender);
     }
 }
 public void CreateAPlayer(MMOMessage M, bool isLocal)
 {
     player = Instantiate(playerPrefab).transform;
     player.SendMessage(isLocal?nameof(MultiplayerPlayer.SetLocal):nameof(MultiplayerPlayer.SetNonLocal), SendMessageOptions.DontRequireReceiver);
     if (!isLocal)
     {
         otherPlayers.Add(M.Sender, player);
     }
 }
Ejemplo n.º 4
0
        protected void SendMessage(string type, string sender, string reciever, string message, TcpClient c)
        {
            var m = new MMOMessage();

            m.Sender   = sender;
            m.Type     = type;
            m.Reciever = reciever;
            m.Message  = message;
            SendMessage(JsonConvert.SerializeObject(m), c);
        }
Ejemplo n.º 5
0
        protected void BroadcastMessage(string type, string sender, string reciever, string message)
        {
            var m = new MMOMessage();

            m.Sender   = sender;
            m.Type     = type;
            m.Reciever = reciever;
            m.Message  = message;
            BroadcastMessage(JsonConvert.SerializeObject(m));
        }
Ejemplo n.º 6
0
        public void SendData(string type, string sender, string reciever, string message)
        {
            var MessageToSend = new MMOMessage();

            MessageToSend.Type     = type;
            MessageToSend.Sender   = sender;
            MessageToSend.Reciever = reciever;
            MessageToSend.Message  = message;

            string output = JsonConvert.SerializeObject(MessageToSend);

            SendData(output);
        }
 public void OnMovement(MMOMessage M)
 {
     if (M.Sender == id.ToString())
     {
         return;
     }
     else
     {
         if (!otherPlayers.ContainsKey(M.Sender))
         {
             //Create and move
             CreateAPlayer(M, false);
         }
         var t = JsonConvert.DeserializeObject <MMOTransform>(M.Message);
         otherPlayers[M.Sender].SendMessage(nameof(MultiplayerPlayer.GetUpdatePosition), t);
     }
 }
Ejemplo n.º 8
0
        public void Connect(Action <MMOMessage> onConnect, Action <MMOMessage> onDisconnect)
        {
            AddCallback("OnConnect", onConnect);
            AddCallback("OnDisconnect", onDisconnect);

            stayConnected = true;
            this.StartCoroutineAsync(StartServer());
            recievedDataCallback += (message) =>
            {
                try
                {
                    MMOMessage recivedMessage = JsonConvert.DeserializeObject <MMOMessage>(message);
                    if (Callbacks.ContainsKey(recivedMessage.Type))
                    {
                        Callbacks[recivedMessage.Type].Invoke(recivedMessage);
                    }
                }
                catch (Exception ex)
                {
                    Debug.LogError("Error ocurred trying to read the message " + message + " the error was " + ex);
                }
            };
        }
Ejemplo n.º 9
0
        protected override void ClientManagement(object o)
        {
            int       id     = (int)o;
            TcpClient client = clientsList[id];

            bool isConnected = true;

            string nickname = "";

            while (isConnected)
            {
                NetworkStream stream = client.GetStream();
                byte[]        buffer = new byte[1024];
                try
                {
                    int byte_count = stream.Read(buffer, 0, buffer.Length);

                    if (byte_count == 0)
                    {
                        Console.WriteLine($"Client {id} disconnected");
                        BroadcastMessage("Disconnection", nickname, "All", "");
                        isConnected = false;

                        if (loggedClientsList.ContainsKey(nickname))
                        {
                            loggedClientsList.Remove(nickname);
                        }

                        break;
                    }

                    string     data           = Encoding.ASCII.GetString(buffer, 0, byte_count);
                    MMOMessage recivedMessage = JsonConvert.DeserializeObject <MMOMessage>(data);

                    if (recivedMessage.Reciever != "Server")
                    {
                        if (!string.IsNullOrEmpty(nickname))
                        {
                            recivedMessage.Sender = nickname;
                            BroadcastMessage(JsonConvert.SerializeObject(recivedMessage));
                            Console.WriteLine(data);
                        }
                    }
                    else
                    {
                        if (recivedMessage.Type == "Login")
                        {
                            //Comprobate
                            Console.WriteLine($"{0} is trying to start a connection");
                            MMOLogin logData = JsonConvert.DeserializeObject <MMOLogin>(recivedMessage.Message);
                            if (Login(logData.nickname, logData.password))
                            {
                                nickname = logData.nickname;
                                loggedClientsList.Add(nickname, client);
                                SendMessage("ConnectionAproved", "Server", nickname, "", client);
                            }
                            else
                            {
                                SendMessage("Error", "Server", "", "Incorrect login data", client);
                            }
                        }
                        else if (recivedMessage.Type == "Register")
                        {
                            MMOLogin logData = JsonConvert.DeserializeObject <MMOLogin>(recivedMessage.Message);
                            if (Register(logData.nickname, logData.password))
                            {
                                SendMessage("Alert", "Server", id.ToString(), "Succedfull register", client);
                            }
                            else
                            {
                                SendMessage("Error", "Server", id.ToString(), "Error ocurred on the register", client);
                            }
                        }
                    }
                }
                catch
                {
                    Console.WriteLine($"Client {id} disconnected in with an error");
                    BroadcastMessage("Disconnection", nickname, "All", "");
                    isConnected = false;

                    if (loggedClientsList.ContainsKey(nickname))
                    {
                        loggedClientsList.Remove(nickname);
                    }
                    break;
                }
            }

            clientsList.Remove(id);
            client.Client.Shutdown(SocketShutdown.Both);
            client.Close();
        }