/// <summary>
 /// Gets called when the Header of Receive is "login"
 /// </summary>
 /// <param name="connectionid">The connection id of the Telepathy.Message</param>
 /// <param name="login">The Helpers.TcpLogin</param>
 static void OnUserLogin(int connectionid, Helpers.TcpLogin login)
 {
     Logging.Info($"[Server] User {login.Data.GetValue("username")} ({connectionid}) tries to login to the server");
     if (Users.Count >= MaxPlayers)
     {
         Logging.Info($"[Server] User {connectionid} tries login but max users reached");
         Send(connectionid, new Helpers.TcpResponse("login_response", "max_players"));
     }
     else if (Password != (string)login.Data.GetValue("password"))
     {
         Logging.Info($"[Server] User {connectionid} tries login with password {(string)login.Data.GetValue("password")} but pass is {Password}");
         Send(connectionid, new Helpers.TcpResponse("login_response", "wrong_password"));
     }
     else if (Users.Count < MaxPlayers && Password == (string)login.Data.GetValue("password"))
     {
         Send(connectionid, new Helpers.TcpResponse("login_response", "ok"));
         Users.Add(new Helpers.User()
         {
             ID       = connectionid,
             Role     = Users.Count < 1 ? Helpers.UserRole.Host : Helpers.UserRole.Client,
             UniqueID = (string)login.Data.GetValue("uniqueid"),
             Username = (string)login.Data.GetValue("username")
         });
         Logging.Info($"[Server] User {(string)login.Data.GetValue("username")} logged in!");
         Send(new Helpers.TcpServerChat($"{(string)login.Data.GetValue("username")} has joined the server.", Helpers.TcpServerChatType.Info));
         return;
     }
     else
     {
         Logging.Warn($"[Server] Invalid TcpLogin data!");
     }
     Logging.Warn("[Server] User didn't login, check client for details");
 }
        /// <summary>
        /// Gets called whenever a message (other than connect or disconnect) gets received
        /// </summary>
        /// <param name="msg">The Telepathy.Message sent by the server.getNextMessage() function</param>
        static void Receive(Telepathy.Message msg)
        {
            //string datastr = Encoding.UTF8.GetString(msg.data);
            //Logging.Info($"[Server] From Connection {msg.connectionId}: " + datastr);
            Logging.Info($"[Server] Data from Connection {msg.connectionId}: {msg.data.Length} bytes");

            //Handle TCPLogin
            //Helpers.TcpLogin tcplogin = XML.From<Helpers.TcpLogin>(datastr);
            Helpers.TcpLogin tcplogin = Helpers.TcpLogin.Deserialize(msg.data);
            if (tcplogin != null && tcplogin.Header == "login")
            {
                OnUserLogin(msg.connectionId, tcplogin);
            }

            //Handle TCPChat
            //Helpers.TcpChat tcpchat = XML.From<Helpers.TcpChat>(datastr);
            Helpers.TcpChat tcpchat = Helpers.TcpChat.Deserialize(msg.data);
            if (tcpchat != null && tcpchat.Header == "chat")
            {
                OnUserChat(msg.connectionId, tcpchat);
            }

            //Handle TCPRequests
            //Helpers.TcpRequest tcprequest = XML.From<Helpers.TcpRequest>(datastr);
            Helpers.TcpRequest tcprequest = Helpers.TcpRequest.Deserialize(msg.data);
            if (tcprequest != null && tcprequest.Header == "request")
            {
                string req = (string)tcprequest.Data.GetValue("request");
                if (req == "gameworld")
                {
                    OnRequestGameWorld(msg.connectionId);
                }
                else if (req == "userlist")
                {
                    OnRequestUserList(msg.connectionId);
                }
            }

            Helpers.TcpGamespeed tcpspeed = Helpers.TcpGamespeed.Deserialize(msg.data);
            if (tcpspeed != null && tcpspeed.Header == "gamespeed")
            {
                OnGamespeedChange(msg.connectionId, tcpspeed);
            }
        }
Example #3
0
 public static void Send(Helpers.TcpLogin login)
 {
     Logging.Info("[Client] Sending login message");
     client.Send(login.Serialize());
 }