示例#1
0
        private void HandleMessage(byte[] msg, IPEndPoint connection)
        {
            packet = new Packet(msg);
            int uID;
            switch (packet.packet_type)
            {
                //DEBUG MESSAGE RECIEVED
                case PacketType.ServerMsg:
                    NetData tmp = packet.Pop_nData();
                    StringBuilder sb = new StringBuilder("ServerDebugMsg: ");
                    while (tmp != null)
                    {
                        sb.Append(tmp.To_String());
                        sb.Append(" ");
                        tmp = packet.Pop_nData();
                    }
                    Console.WriteLine(sb);
                    sb = null;
                    tmp = null;
                    break;
                case PacketType.Login:

                    //TODO: CLEANUP
                    string CryptUsername = packet.Pop_nData().To_String();
                    string CryptPassword = packet.Pop_nData().To_String();
                    Console.WriteLine("Login Request Received. <" + connection.Address + ">");
                    //Console.WriteLine("Password: "******"PosUpdate: uID[{0}]: Pos{1}", OnlinePlayers[playerIndex].UID, OnlinePlayers[playerIndex].PlayerPosition.ToString()));
                        Packet posUp = new Packet(PacketType.PositionUpdate);
                        posUp.Push_nData(new NetData(OnlinePlayers[playerIndex].UID));
                        posUp.Push_nData(new NetData(OnlinePlayers[playerIndex].PlayerPosition.x));
                        posUp.Push_nData(new NetData(OnlinePlayers[playerIndex].PlayerPosition.y));
                        posUp.Push_nData(new NetData(OnlinePlayers[playerIndex].PlayerPosition.z));

                        Send_All(OnlinePlayers[playerIndex].CurrentMap,posUp);
                    }
                    catch (ArgumentNullException e)
                    {
                        Console.WriteLine("Couldn't find player in list!");
                    }
                break;
                case PacketType.ChangeMap:
                    try
                    {
                        uID = packet.Pop_nData().To_Int();
                        int playerIndex = OnlinePlayers.FindIndex(ply => ply.UID == uID);
                        int mapID = packet.Pop_nData().To_Int();
                        OnlinePlayers[playerIndex].CurrentMap = mapID;
                        Console.WriteLine(string.Format("uID[{0}]: Switching to MapID<{1}>", mapID));

                    }
                    catch (ArgumentNullException e)
                    {
                        Console.WriteLine("Couldn't find player in list!");
                    }
                    break;
                case PacketType.HitboxCreate:
                    //Hitbox Spawned
                    uID = packet.Pop_nData().To_Int();
                    int pIndex = OnlinePlayers.FindIndex(ply => ply.UID == uID);
                    Vector3 pos = new Vector3(packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float());
                    //Console.WriteLine(string.Format("HitboxCreated: uID[{0}] at Pos{1}", uID, pos));

                    //Inform rest of players in map
                    Packet hitboxPacket = new Packet(PacketType.HitboxCreate);
                    hitboxPacket.Push_nData(new NetData(uID));
                    hitboxPacket.Push_nData(new NetData(pos.x));
                    hitboxPacket.Push_nData(new NetData(pos.y));
                    hitboxPacket.Push_nData(new NetData(pos.z));
                    hitboxPacket.Push_nData(new NetData(packet.Pop_nData().To_Float()));
                    hitboxPacket.Push_nData(new NetData(packet.Pop_nData().To_Float()));

                    Send_All(OnlinePlayers[pIndex].CurrentMap, hitboxPacket);
                    break;
                case PacketType.ProjectileCreate:
                    //Hitbox Spawned
                    //Inform rest of players in map
                    uID = packet.Pop_nData().To_Int();
                    int pProjIndex = OnlinePlayers.FindIndex(ply => ply.UID == uID);
                    Vector3 proj_pos = new Vector3(packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float());
                    Vector3 proj_dir = new Vector3(packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float());
                    //Console.WriteLine(string.Format("ProjectileCreated: uID[{0}] at Pos{1} Direction{2}", uID, proj_pos, proj_dir));


                    //Inform rest of players in map
                    Packet projPacket = new Packet(PacketType.ProjectileCreate);
                    projPacket.Push_nData(new NetData(uID));
                    projPacket.Push_nData(new NetData(proj_pos.x));
                    projPacket.Push_nData(new NetData(proj_pos.y));
                    projPacket.Push_nData(new NetData(proj_pos.z));
                    projPacket.Push_nData(new NetData(proj_dir.x));
                    projPacket.Push_nData(new NetData(proj_dir.y));
                    projPacket.Push_nData(new NetData(proj_dir.z));
                    projPacket.Push_nData(new NetData(packet.Pop_nData().To_Int()));
                    projPacket.Push_nData(new NetData(packet.Pop_nData().To_Float()));
                    projPacket.Push_nData(new NetData(packet.Pop_nData().To_Float()));

                    Send_All(OnlinePlayers[pProjIndex].CurrentMap, projPacket);
                    break;
                case PacketType.Damage:
                    //Inform rest of players in map
                    break;
                case PacketType.CrowdControl:
                    //Inform rest of players in map
                    break;
                case PacketType.Death:
                    //Inform rest of players in map
                    break;
                case PacketType.ItemAdd:
                    //Query server database
                    break;
                case PacketType.ItemRemove:
                    //Query server database
                    break;
                case PacketType.GetEquipment:
                    //Query server database
                    uID = packet.Pop_nData().To_Int();
                    Packet equipQuery = new Packet(PacketType.SERVER_QueryEquip);
                    equipQuery.Push_nData(new NetData(connection.Address.ToString()));
                    equipQuery.Push_nData(new NetData(uID));
                    BackEnd.Send_ToBackend(equipQuery);
                    break;
                case PacketType.SetEquipment:
                    break;
                case PacketType.GetInventory:
                    //Query server database
                    break;
                case PacketType.CharacterInfo:
                    uID = packet.Pop_nData().To_Int();
                    Packet charQuery = new Packet(PacketType.SERVER_QueryEquip);
                    charQuery.Push_nData(new NetData(uID));
                    BackEnd.Send_ToBackend(charQuery);
                    break;
            }
        }
示例#2
0
    private void HandleMessage(byte[] msg)
    {
        //if (msg[0] == 0) return;
        int uID;
        Packet packet = new Packet(msg);
        switch (packet.packet_type)
        {
            case PacketType.ServerMsg:
                break;
            case PacketType.Login:
                //Login Successful
                //Change UI_State
                if (packet.Pop_nData().To_Bool())
                {
                    myID = packet.Pop_nData().To_Int();
                    //Tell UI to display successful login information
                    if (Manager.Instance.DEBUG_STATE == DebugState.DEBUG)
                        Debug.Log("Login Successful!");

                    //CreateConnection(NetworkOutboundDestination.MainServer);
                    //Send request for inventory information
                    //Send request for equipment information

                    //Generate Map
                    //Generate Player
                    Manager.Instance.PlayerManager.PlayerJoinMap(myID, Vector3.zero);
                }
                else
                {
                    //Tell UI to display unsuccessful login information
                }
                break;
            case PacketType.HitboxCreate:
                uID = packet.Pop_nData().To_Int();
                if (uID == myID) return;

                Vector3 hb_pos = new Vector3(packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float());

                if (Manager.Instance.DEBUG_STATE == DebugState.DEBUG)
                Debug.Log("Hitbox Created. Position:" + hb_pos.ToString());

                HelperMethods.SpawnDamageBox(uID, hb_pos, packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float());
                break;
            case PacketType.ProjectileCreate:
                uID = packet.Pop_nData().To_Int();
                if (uID == myID) return;

                Vector3 proj_pos = new Vector3(packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float());
                Vector3 proj_dir = new Vector3(packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float());

                if (Manager.Instance.DEBUG_STATE == DebugState.DEBUG)
                Debug.Log("Projectile Created. Position: " + proj_pos.ToString() + " Direction: " + proj_dir.ToString());

                    HelperMethods.SpawnProjectile(uID,proj_pos, proj_dir,packet.Pop_nData().To_Int(), packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float());
                break;
            case PacketType.ChangeMap:
                break;
            case PacketType.Damage:
                break;
            case PacketType.CrowdControl:
                break;
            case PacketType.Death:
                break;
            case PacketType.ItemAdd:
                break;
            case PacketType.ItemRemove:
                break;
            case PacketType.PositionUpdate:
                int playerID = packet.Pop_nData().To_Int();
                if (playerID == myID) break;

                Vector3 playerPosition = new Vector3(packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float(), packet.Pop_nData().To_Float());
                if (Manager.Instance.DEBUG_STATE == DebugState.DEBUG)
                    Debug.Log("Position Update Recieved: Player["+playerID+"]  pos: "+ playerPosition.ToString());

                Manager.Instance.PlayerManager.UpdatePlayerPosition(playerID, playerPosition);
                break;
            default:
                break;
        }

        Debug.Log("Handled packet of type " + packet.packet_type);
    }