Example #1
0
        private void HandlePacket(NetIncomingMessage msg)
        {
            PacketType type = (PacketType)msg.ReadByte();
            switch (type)
            {
                case PacketType.UsernameKeywordCombo:
                    UsernameKeywordComboPacket unkwPacket = new UsernameKeywordComboPacket();
                    unkwPacket.Unpack(msg);
                    if (gameManager.PlayerSaves.ContainsKey(unkwPacket.username))
                    {
                        LoginVerificationPacket packet = new LoginVerificationPacket();
                        packet.message = "You are already logged in.";
                        ServerConsoleMessage(unkwPacket.username + " login error: Already logged in.");
                        SendUnconnectedMessage(packet, msg.SenderEndpoint);
                        //server.Connections.Remove(server.GetConnection(msg.SenderEndpoint));  // try
                    }
                    else
                    {
                        PlayerSave save = FileGrabber.getPlayerSave(isLocalGame, unkwPacket.username, unkwPacket.keyword);
                        if (!unkwPacket.keyword.Equals(save.Keyword))
                        {
                            LoginVerificationPacket packet = new LoginVerificationPacket();
                            packet.message = "Keyword does not match.";
                            ServerConsoleMessage(unkwPacket.username + " login error: Bad keyword.");
                            SendUnconnectedMessage(packet, msg.SenderEndpoint);
                            //server.Connections.Remove(server.GetConnection(msg.SenderEndpoint));  // try
                        }
                        else
                        {
                            LoginVerificationPacket packet = new LoginVerificationPacket();
                            packet.message = "verified";
                            gameManager.PlayerSaves.Add(unkwPacket.username, save);
                            gameManager.createPlayerEntityFromSave(unkwPacket.username);
                            gameManager.RUIDUsernames.Add(unkwPacket.username, msg.SenderConnection.RemoteUniqueIdentifier);
                            SendUnconnectedMessage(packet, msg.SenderEndpoint);
                            ServerConsoleMessage(unkwPacket.username + " has logged into the game.");
                            if (!isLocalGame)
                            {
                                ChatMessageQueue.Enqueue(new ChatMessage("SERVER", unkwPacket.username + " has connected."));
                            }
                        }
                    }
                    break;

                case PacketType.ChatMessage:
                    ChatMessagePacket chatPacket = new ChatMessagePacket();
                    chatPacket.Unpack(msg);
                    string username = gameManager.RUIDUsernames.GetValue(msg.SenderConnection.RemoteUniqueIdentifier);
                    if (!username.Equals(""))
                    {
                        ChatMessage cmsg = new ChatMessage(username, chatPacket.message);
                        ChatMessageQueue.Enqueue(cmsg);
                        ServerConsoleMessage(cmsg.getChatString());
                    }
                    break;

                case PacketType.InputsPacket:
                    InputsPacket inputsPacket = new InputsPacket();
                    inputsPacket.Unpack(msg);
                    string un = gameManager.RUIDUsernames.GetValue(msg.SenderConnection.RemoteUniqueIdentifier);
                    if (!un.Equals(""))
                    {
                        gameManager.HandlePlayerMoving(un, inputsPacket);
                    }
                    break;
            }
        }
 private void HandleMessage(NetIncomingMessage msg)
 {
     PacketType type = (PacketType)msg.ReadByte();
     switch (type)
     {
         case PacketType.ChatMessage:
             ChatMessagePacket chatPacket = new ChatMessagePacket();
             chatPacket.Unpack(msg);
             ChatManager.Instance.addMessage(chatPacket.username, chatPacket.message);
             break;
         case PacketType.PlayerPosition:
             PlayerPositionPacket posPacket = new PlayerPositionPacket();
             posPacket.Unpack(msg);
             ClientGameManager.Instance.HandleNewPlayerPosition(posPacket);
             break;
         case PacketType.Disconnect:
             DisconnectPacket disPacket = new DisconnectPacket();
             disPacket.Unpack(msg);
             ClientGameManager.Instance.HandlePlayerDisconnect(disPacket.username);
             break;
         case PacketType.MobPosition:
             MobPositionPacket mpPacket = new MobPositionPacket();
             mpPacket.Unpack(msg);
             ClientGameManager.Instance.HandleMobMovement(mpPacket);
             break;
     }
 }