using System;
using System.Net.Sockets;
using System.Collections;

namespace GameServer
{
    class CP_ISC_LOGIN_PACKET : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            if (Convert.ToInt32(Packet.getBlock()) == 77)
            {
                Program.UDPClass.StartUDPServer();
            }
            else
            {
                Environment.Exit(0);
            }
        }
    }

    class CP_WELCOME_PACKET : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            Client.setMac(Packet.getBlockByIndex(2));
            Client.Send(new SP_WELCOME_PACKET());

        }
    }

    class CP_CHARACTER_INFO : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            Client.setUserID(long.Parse(Packet.getBlock()));
            Client.getUserData();
            Client.loadItems();
            Client.GetShopItems();
            Client.Send(new SP_CHARACTER_INFO(Client));
            Program.SQLClass.runSQL("UPDATE ACCOUNT_TBL SET online = '1' WHERE userID = '" + Client.getUserID().ToString() + "'");
           

            Client.Send(new SP_PREMIUM_LEFT(Client));
            Client.Send(new SP_ROOM_LIST(Client, Client.getRoomPage()));
            Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Notice1, "Welcome To WarRock PServer - Beta              Welcome To WarRock PServer - Beta                Welcome To WarRock PServer - Beta                              Welcome To WarRock PServer - Beta                    Welcome To WarRock PServer - Beta                    Welcome To WarRock PServer - Beta                          Welcome To WarRock PServer - Beta                              Welcome To WarRock PServer - Beta", 100, "PETER"));


        }
    }

    class CP_CHANGE_CHANNEL : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            Client.setChannelID(int.Parse(Packet.getBlock()));
            Client.Send(new SP_CHANGE_CHANNEL(Client));
            Client.Send(new SP_ROOM_LIST(Client, 0));
        }
    }

    class CP_CHAT : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            int ChatType = int.Parse(Packet.getBlock());
            int TargetID = int.Parse(Packet.getBlock());
            string TargetName = Packet.getBlock();
            string Message = Packet.getBlock();
            cWRRoom WRRoom = Client.getRoom();

            if (Message.EndsWith("!kickall") && Client.getAccesslevel() == 3)
            {
                foreach (cWRClient _Client in Program.WRServer.getAllPlayers())
                {
                    Program.WRServer.RemoveClient(_Client);
                    Program.SQLClass.runSQL("UPDATE ACCOUNT_TBL SET online = '0' WHERE userID = '" + _Client.getUserID().ToString() + "'");

                    Program.ConfigClass.CurrentPlayer--;


                }


            }

            if (Message.EndsWith("!RemoveRoom") && Client.getAccesslevel() == 3)
            {
                Message = Message.Remove(0, Client.getNickname().Length + 4);
                Message = Message.Remove(Message.Length - 11, 11);
                Program.WRServer.RemoveRoom(Program.WRServer.getRoomByID(Convert.ToInt32(Message), Client.getChannelID()), Client.getChannelID());

            }

            if (Message.EndsWith("!KickR") && Client.getAccesslevel() == 3)
            {
                Message = Message.Remove(0, Client.getNickname().Length + 4);
                Message = Message.Remove(Message.Length - 10, 10);

                cWRClient tPlayer = Program.WRServer.getPlayerByNickname(Message);

                if (tPlayer is cWRClient && tPlayer.getSessionID() > 0 && tPlayer.getAccesslevel() != 3)
                {

                    Client.getRoom().getIDOfPlayer(tPlayer);

                }
            }
            if (Message.EndsWith("!KickS") && Client.getAccesslevel() == 3)
            {
                Message = Message.Remove(0, Client.getNickname().Length + 4);
                Message = Message.Remove(Message.Length - 10, 10);

                cWRClient tPlayer = Program.WRServer.getPlayerByNickname(Message);

                if (tPlayer is cWRClient && tPlayer.getSessionID() > 0 && tPlayer.getAccesslevel() != 3)
                {

                    Program.WRServer.RemoveClient(tPlayer);
                    Program.SQLClass.runSQL("UPDATE ACCOUNT_TBL SET online = '0' WHERE userID = '" + tPlayer.getUserID().ToString() + "'");
                    Program.ConfigClass.CurrentPlayer--;

                    tPlayer = null;
                }
            }
            if (Message.EndsWith("!GetStats") && Client.getAccesslevel() == 3)
            {

                Message = Message.Remove(0, Client.getNickname().Length + 4);
                Message = Message.Remove(Message.Length - 9, 9);

                cWRClient tPlayer = Program.WRServer.getPlayerByNickname(Message);

                if (tPlayer is cWRClient && tPlayer.getSessionID() > 0 && tPlayer.getAccesslevel() != 3)
                {
                    Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Notice1, tPlayer.getPremium().ToString() +
                    tPlayer.getNetworkIP().ToString() +
                    tPlayer.getNickname() +
                    tPlayer.getUserID().ToString() +
                    tPlayer.getLevel().ToString() +
                    tPlayer.getKills().ToString() +
                    tPlayer.getDeaths().ToString() +
                    tPlayer.getDinar().ToString() +
                    tPlayer.getExp().ToString(), 100, "PETER"));


                }
            }

            if (Message.EndsWith("!BanPlayer") && Client.getAccesslevel() == 3)
            {
                Message = Message.Remove(0, Client.getNickname().Length + 4);
                Message = Message.Remove(Message.Length - 10, 10);

                cWRClient tPlayer = Program.WRServer.getPlayerByNickname(Message);

                if (tPlayer is cWRClient && tPlayer.getSessionID() > 0 && tPlayer.getAccesslevel() != 3)
                {

                    Program.WRServer.RemoveClient(tPlayer);
                    Program.SQLClass.runSQL("UPDATE ACCOUNT_TBL SET online = '0' WHERE userID = '" + tPlayer.getUserID().ToString() + "'");
                    Program.SQLClass.runSQL("UPDATE ACCOUNT_TBL SET accesslevel = '0' WHERE userID = '" + tPlayer.getUserID().ToString() + "'");
                    Program.ConfigClass.CurrentPlayer--;

                    tPlayer = null;
                }


            }


            if (Message.EndsWith("!Notic") && Client.getAccesslevel() == 3)
            {

                Message = Message.Remove(Message.Length - 6, 6);
                ChatType = 30;
                foreach (cWRClient _Client in Program.WRServer.getAllPlayers())
                {
                    _Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Notice1, Message, 100, TargetName));
                }
            }
            switch (ChatType)
            {

                case 3:
                    if (Client.getAccesslevel() > 1)
                    {
                        TargetID = 100;
                    }


                    foreach (cWRClient _Client in Program.WRServer.getPlayerInChannel(Client.getChannelID()))
                    {
                        if (_Client.getRoom() == null)
                        {
                            _Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Lobby_ToChannel, Message, TargetID, TargetName));
                        }
                    }

                    break;
                case 4:
                    TargetID = -1;

                    if (Client.getAccesslevel() > 1)
                    {
                        TargetID = 999; // Yellow GM Chat(Only in Rooms)
                    }


                    foreach (cWRClient _Client in WRRoom.getPlayers())
                    {
                        _Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Room_ToAll, Message, TargetID, TargetName));
                    }

                    break;
                case 5:

                    TargetID = -1;

                    if (Client.getAccesslevel() > 1)
                    {
                        TargetID = 999;
                    }

                    foreach (cWRClient _Client in WRRoom.getPlayersFromTeam(Client))
                    {


                        _Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Room_ToTeam, Message, TargetID, TargetName));


                    }

                    break;
                case 6:
                    cWRClient TargetClient = Program.WRServer.getPlayerByNickname(TargetName);
                    if (TargetClient is cWRClient && TargetClient.getSessionID() > 0)
                    {
                        Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Whisper, Message, TargetClient.getSessionID(), TargetClient.getNickname()));
                        if (!Client.Equals(TargetClient))
                        {
                            TargetClient.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Whisper, Message, TargetClient.getSessionID(), TargetClient.getNickname()));
                        }
                    }
                    else
                    {
                        Client.Send(new SP_CHAT(SP_CHAT.ErrorCodes.ErrorUser, TargetName + Convert.ToChar(0x1D)));
                    }
                    break;
                case 8:
                    foreach (cWRClient _Client in Program.WRServer.getPlayers(false))
                    {
                        _Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Room_ToAll, Message, TargetID, TargetName));
                    }
                    break;
            }
        }
    }
    class CP_UNKNOW : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {

            Client.Send(new SP_UNKNOW());
        }
    }

    class CP_BUY_WEAPON : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            Packet.getBlock(); //  1110   
            string Weapon = Packet.getBlock();//DC04 
            int WeaponID = int.Parse(Packet.getBlock());//22 
            int kp = int.Parse(Packet.getBlock());//-1 
            int Time = int.Parse(Packet.getBlock());//3 
            int Error = 1;
            long Dinar = 0;
            string Items = "";
            int DTime = 0;
            int Vorhanden = 0;

            foreach (cWRClient.bItem lItem in Client.getItemList())
            {
                if (lItem.WaffenName == Weapon)
                {

                    if (lItem.Premium == 1 && Client.getPremium() < 1) Error = 98010;
                    if (lItem.Level > Client.getLevel()) Error = 97020;
                    switch (Time)
                    {
                        case 0: if (lItem.Tage3 > Client.getDinar()) { Error = 97040; } else { Dinar = lItem.Tage3; DTime = 3; } break;//die sache ndann acuh
                        case 1: if (lItem.Tage7 > Client.getDinar()) { Error = 97040; } else { Dinar = lItem.Tage7; DTime = 7; } break;
                        case 2: if (lItem.Tage15 > Client.getDinar()) { Error = 97040; } else { Dinar = lItem.Tage15; DTime = 15; } break;
                        case 3: if (lItem.Tage30 > Client.getDinar()) { Error = 97040; } else { Dinar = lItem.Tage30; DTime = 30; } break;
                    }


                }
            }


            if (Error == 1)
            {


                foreach (cWRClient.tItem pItem in Client.getItems())
                {
                    if (pItem.itemCode == Weapon)
                    {

                        Vorhanden = 1;

                    }
                    else { Items += pItem.itemCode + "-1-1-" + pItem.expireDate.ToString() + "-0-0-0-0-0,"; }


                }
            }
            if (Vorhanden == 1)
            {
                Error = 97020;
            }
            else
            {
                if (Error == 1)
                {
                    Program.SQLClass.runSQL("INSERT INVENTORY_TBL (userID, itemCode, expireDate, class_0, class_1, class_2, class_3, class_4) VALUES (" + Client.getUserID().ToString() + ", '" + Weapon + "', " + Client.TimeAdd(DTime) + ", -1, -1, -1, -1, -1)");
                    Client.loadItems();
                }
            }

            for (int i = 0; i < (31 - (Client.getItems().Count + Vorhanden)); i++)
            {

                Items += "^,";

                Items = Items.Substring(0, Items.Length - 1);
            }


            string Weaponstring = Weapon + "-1-1-" + Client.TimeAdd(DTime) + "-0-0-0-0-0," + Items;


            if (Error == 1)
            {
                if (Client.getAccesslevel() == 3)
                {
                    Client.Send(new SP_BUY_WEAPON(Client, 1, (Client.getDinar()), Weaponstring));
                }
                else
                {
                    Client.Send(new SP_BUY_WEAPON(Client, 1, (Client.getDinar() - Dinar), Weaponstring));
                }
            }
            else
            {
                Client.Send(new SP_BUY_WEAPON(Client, Error, 0, ""));
            }
        }
    }


    class CP_PREMIUM_LEFT : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            Packet.getBlock();
            long iTime = long.Parse(Packet.getBlock());
            //kA ?

            
           // new cLog(C.MSG_SUCCESS, "SENDET" + Client.FuckPacket.ToString() + Client.FuckPacket2.ToString());
        }
    }

    class CP_USER_LIST : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            int Operation = int.Parse(Packet.getBlock());
            ArrayList PlayerList = new ArrayList();

            if (Operation == 0) // All
            {
                PlayerList = Program.WRServer.getAllPlayers();
            }
            else if (Operation == 1) // by Name
            {
                string Nickname = Packet.getBlock();
                cWRClient tPlayer = Program.WRServer.getPlayerByNickname(Nickname);
                if (tPlayer is cWRClient && tPlayer.getSessionID() > 0)
                {
                    PlayerList.Add(tPlayer);
                }
            }
            else if (Operation == 2) // by Clan
            {
                // Not ready
            }
            else if (Operation == 3) // by Level
            {
                string Range = Packet.getBlock();
                int Range1 = int.Parse(Range.Split(new char[] { '-' })[0]);
                int Range2 = int.Parse(Range.Split(new char[] { '-' })[1]);

                foreach (cWRClient _Client in Program.WRServer.getAllPlayers())
                {
                    if (_Client.getLevel() >= Range1 && _Client.getLevel() <= Range2)
                    {
                        PlayerList.Add(_Client);
                    }
                }

            }

            Client.Send(new SP_USER_LIST(PlayerList));
        }
    }

    class CP_CHANGE_WEAPON : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {

            int Unequip = int.Parse(Packet.getBlock());
            int _Class = int.Parse(Packet.getBlock());
            Packet.getBlock();
            Packet.getBlock();
            string WeaponCode = Packet.getBlock();
            int Slot = int.Parse(Packet.getBlock());

            string WeaponString = "";
            Program.SQLClass.runSQL("UPDATE INVENTORY_TBL SET class_" + _Class.ToString() + " = '-1' WHERE userID = '" + Client.getUserID().ToString() + "' AND class_" + _Class.ToString() + " = '" + Slot.ToString() + "'");
            Client.loadItems();
            foreach (cWRClient.tItem pItem in Client.getItems())
            {

                switch (pItem.SlotEngineer)
                {

                    case 0: Client.slot1E = pItem.itemCode; break;
                    case 1: Client.slot2E = pItem.itemCode; break;
                    case 2: Client.slot3E = pItem.itemCode; break;
                    case 3: Client.slot4E = pItem.itemCode; break;
                    case 4: Client.slot5E = pItem.itemCode; break;
                    case 5: Client.slot6E = pItem.itemCode; break;


                }
                switch (pItem.SlotMedic)
                {
                    case 0: Client.slot1M = pItem.itemCode; break;
                    case 1: Client.slot2M = pItem.itemCode; break;
                    case 2: Client.slot3M = pItem.itemCode; break;
                    case 3: Client.slot4M = pItem.itemCode; break;
                    case 4: Client.slot5M = pItem.itemCode; break;
                    case 5: Client.slot6M = pItem.itemCode; break;
                }
                switch (pItem.SlotSniper)
                {
                    case 0: Client.slot1S = pItem.itemCode; break;
                    case 1: Client.slot2S = pItem.itemCode; break;
                    case 2: Client.slot3S = pItem.itemCode; break;
                    case 3: Client.slot4S = pItem.itemCode; break;
                    case 4: Client.slot5S = pItem.itemCode; break;
                    case 5: Client.slot6S = pItem.itemCode; break;
                }
                switch (pItem.SlotAssault)
                {
                    case 0: Client.slot1A = pItem.itemCode; break;
                    case 1: Client.slot2A = pItem.itemCode; break;
                    case 2: Client.slot3A = pItem.itemCode; break;
                    case 3: Client.slot4A = pItem.itemCode; break;
                    case 4: Client.slot5A = pItem.itemCode; break;
                    case 5: Client.slot6A = pItem.itemCode; break;
                }
                switch (pItem.SlotHeavyTrooper)
                {
                    case 0: Client.slot1H = pItem.itemCode; break;
                    case 1: Client.slot2H = pItem.itemCode; break;
                    case 2: Client.slot3H = pItem.itemCode; break;
                    case 3: Client.slot4H = pItem.itemCode; break;
                    case 4: Client.slot5H = pItem.itemCode; break;
                    case 5: Client.slot6H = pItem.itemCode; break;
                }

            }


            if (Unequip == 0)
                Program.SQLClass.runSQL("UPDATE INVENTORY_TBL SET class_" + _Class.ToString() + " = '" + Slot.ToString() + "' WHERE userID = '" + Client.getUserID().ToString() + "' AND itemCode = '" + WeaponCode + "'");
            else
                Program.SQLClass.runSQL("UPDATE INVENTORY_TBL SET class_" + _Class.ToString() + " = '-1' WHERE userID = '" + Client.getUserID().ToString() + "' AND itemCode = '" + WeaponCode + "'");

            switch (_Class)
            {
                case 0:

                    switch (Slot)
                    {
                        case 0: if (Unequip == 0) Client.slot1E = WeaponCode; else Client.slot1E = "^"; break;
                        case 1: if (Unequip == 0) Client.slot2E = WeaponCode; else Client.slot2E = "^"; break;
                        case 2: if (Unequip == 0) Client.slot3E = WeaponCode; else Client.slot3E = "^"; break;
                        case 3: if (Unequip == 0) Client.slot4E = WeaponCode; else Client.slot4E = "^"; break;
                        case 4: if (Unequip == 0) Client.slot5E = WeaponCode; else Client.slot5E = "^"; break;
                        case 5: if (Unequip == 0) Client.slot6E = WeaponCode; else Client.slot6E = "^"; break;
                    }
                    if (Client.slot3E == Client.slot5E) Client.slot5E = "^";
                    WeaponString = Client.slot1E + "," + Client.slot2E + "," + Client.slot3E + "," + Client.slot4E + "," + Client.slot5E + "," + Client.slot6H + ",^,^";
                    break;
                case 1:

                    switch (Slot)
                    {
                        case 0: if (Unequip == 0) Client.slot1M = WeaponCode; else Client.slot1M = "^"; break;
                        case 1: if (Unequip == 0) Client.slot2M = WeaponCode; else Client.slot2M = "^"; break;
                        case 2: if (Unequip == 0) Client.slot3M = WeaponCode; else Client.slot3M = "^"; break;
                        case 3: if (Unequip == 0) Client.slot4M = WeaponCode; else Client.slot4M = "^"; break;
                        case 4: if (Unequip == 0) Client.slot5M = WeaponCode; else Client.slot5M = "^"; break;
                        case 5: if (Unequip == 0) Client.slot6M = WeaponCode; else Client.slot6M = "^"; break;
                    }
                    if (Client.slot3M == Client.slot5M) Client.slot5M = "^";
                    WeaponString = Client.slot1M + "," + Client.slot2M + "," + Client.slot3M + "," + Client.slot4M + "," + Client.slot5M + "," + Client.slot6M + ",^,^";
                    break;
                case 2:

                    switch (Slot)
                    {
                        case 0: if (Unequip == 0) Client.slot1S = WeaponCode; else Client.slot1S = "^"; break;
                        case 1: if (Unequip == 0) Client.slot2S = WeaponCode; else Client.slot2S = "^"; break;
                        case 2: if (Unequip == 0) Client.slot3S = WeaponCode; else Client.slot3S = "^"; break;
                        case 3: if (Unequip == 0) Client.slot4S = WeaponCode; else Client.slot4S = "^"; break;
                        case 4: if (Unequip == 0) Client.slot5S = WeaponCode; else Client.slot5S = "^"; break;
                        case 5: if (Unequip == 0) Client.slot6S = WeaponCode; else Client.slot6S = "^"; break;
                    }
                    if (Client.slot3S == Client.slot5S) Client.slot5S = "^";
                    WeaponString = Client.slot1S + "," + Client.slot2S + "," + Client.slot3S + "," + Client.slot4S + "," + Client.slot5S + "," + Client.slot6S + ",^,^";

                    break;
                case 3:

                    switch (Slot)
                    {
                        case 0: if (Unequip == 0) Client.slot1A = WeaponCode; else Client.slot1A = ""; break;
                        case 1: if (Unequip == 0) Client.slot2A = WeaponCode; else Client.slot2A = ""; break;
                        case 2: if (Unequip == 0) Client.slot3A = WeaponCode; else Client.slot3A = ""; break;
                        case 3: if (Unequip == 0) Client.slot4A = WeaponCode; else Client.slot4A = ""; break;
                        case 4: if (Unequip == 0) Client.slot5A = WeaponCode; else Client.slot5A = "^"; break;
                        case 5: if (Unequip == 0) Client.slot6A = WeaponCode; else Client.slot6A = "^"; break;
                    }
                    if (Client.slot3A == Client.slot5A) Client.slot5A = "^";
                    WeaponString = Client.slot1A + "," + Client.slot2A + "," + Client.slot3A + "," + Client.slot4A + "," + Client.slot5A + "," + Client.slot6A + ",^,^";
                    break;

                case 4:

                    switch (Slot)
                    {
                        case 0: if (Unequip == 0) Client.slot1H = WeaponCode; else Client.slot1H = "^"; break;
                        case 1: if (Unequip == 0) Client.slot2H = WeaponCode; else Client.slot2H = "^"; break;
                        case 2: if (Unequip == 0) Client.slot3H = WeaponCode; else Client.slot3H = "^"; break;
                        case 3: if (Unequip == 0) Client.slot4H = WeaponCode; else Client.slot4H = "^"; break;
                        case 4: if (Unequip == 0) Client.slot5H = WeaponCode; else Client.slot5H = "^"; break;
                        case 5: if (Unequip == 0) Client.slot6H = WeaponCode; else Client.slot6H = "^"; break;
                    }
                    if (Client.slot3H == Client.slot5H) Client.slot5H = "^";
                    WeaponString = Client.slot1H + "," + Client.slot2H + "," + Client.slot3H + "," + Client.slot4H + "," + Client.slot5H + "," + Client.slot6H + ",^,^";
                    break;
            }



            if (Client.slot3E == Client.slot5E) Client.slot5E = "^";
            Client.Send(new SP_CHANGE_WEAPON(_Class, WeaponString)); // Must adding Weapon shitt string
        }
    }

    class CP_CREATE_ROOM : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            cWRRoom WRRoom = new cWRRoom();

            WRRoom.setRoomID(Program.WRServer.getRoomsInChannel(Client.getChannelID()).Count);
            WRRoom.setRoomName(Packet.getBlock());
            WRRoom.setPassword(int.Parse(Packet.getBlock()), Packet.getBlock());
            WRRoom.setMaxPlayer(8 * (int.Parse(Packet.getBlock()) + 1));
            WRRoom.setMapID(int.Parse(Packet.getBlock()));
            Packet.getBlock();
            Packet.getBlock();
            WRRoom.setRoomType(int.Parse(Packet.getBlock()));
            WRRoom.setLevelLimit(int.Parse(Packet.getBlock()));
            WRRoom.setPremiumOnly(int.Parse(Packet.getBlock()));
            WRRoom.setVoteKick(int.Parse(Packet.getBlock()));
            WRRoom.setRoomMaster(Client);

            if (Client.getChannelID() == 1)
            {
                WRRoom.setRoomMode(0);
            }
            else
            {
                WRRoom.setRoomMode(2);
            }

            WRRoom.AddPlayer(Client);
            Client.setRoom(WRRoom);
            Program.WRServer.AddRoom(WRRoom, Client.getChannelID());
            Client.Send(new SP_CREATE_ROOM(WRRoom));

            foreach (cWRClient _Client in Program.WRServer.getPlayerInChannel(Client.getChannelID()))
            {
                _Client.Send(new SP_ROOM_LIST(_Client, _Client.getRoomPage()));
                       
            }
        }
    }

    class CP_LEAVE_ROOM : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            cWRRoom WRRoom = Client.getRoom();
            int oldPlace = WRRoom.getIDOfPlayer(Client);
            int newMaster = -1;
            WRRoom.RemovePlayer(Client);
            Client.setRoom(null);

            if (WRRoom.getPlayers().Count <= 0)
            {
                Program.WRServer.RemoveRoom(WRRoom, Client.getChannelID());
            }
            else
            {
                foreach (cWRClient _Client in WRRoom.getPlayers())
                {
                    newMaster = WRRoom.getIDOfPlayer(_Client);
                    WRRoom.setRoomMaster(_Client);
                    break;
                }
            }

            foreach (cWRClient _Client in WRRoom.getPlayers())
            {
                _Client.Send(new SP_LEAVE_ROOM(Client, WRRoom, oldPlace, newMaster));
            }

            Client.Send(new SP_LEAVE_ROOM(Client, WRRoom, oldPlace, newMaster));

            //Update roominfos to all Players(Das ist nur prowisorisch!!)
            foreach (cWRClient _Client in Program.WRServer.getPlayerInChannel(Client.getChannelID()))
            {
                _Client.Send(new SP_ROOM_LIST(_Client, _Client.getRoomPage()));
                //_Client.Send(new SP_ROOMSTATE2(WRRoom));
            }
           
        }
    }

    class CP_ROOM_LIST : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            int Page = int.Parse(Packet.getBlock());
            Client.setRoomPage(Page);
            Client.Send(new SP_ROOM_LIST(Client, Page));
        }
    }

    class CP_JOIN_ROOM : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            int roomID = int.Parse(Packet.getBlock());
            string Password = Packet.getBlock();
            cWRRoom WRRoom = Program.WRServer.getRoomByID(roomID, Client.getChannelID());

            if (WRRoom is cWRRoom && WRRoom.getRoomID() >= 0)
            {
                if (WRRoom.getPassword() != Password)
                {
                    Client.Send(new SP_JOIN_ROOM(SP_JOIN_ROOM.ErrorCodes.InvalidPassword));
                }
                else if (WRRoom.getPremiumOnly() == 1 && Client.getPremium() <= 0)
                {
                    Client.Send(new SP_JOIN_ROOM(SP_JOIN_ROOM.ErrorCodes.OnlyPremium));
                }
                else
                {
                    WRRoom.AddPlayer(Client);
                    Client.setRoom(WRRoom);
                    Client.Send(new SP_JOIN_ROOM(Client, WRRoom));

                    //Send Playerlist in room(@WORK):

                    ArrayList PlayerList = new ArrayList();

                    foreach (cWRClient _Client in WRRoom.getPlayers())
                    {
                        if (!_Client.Equals(Client))
                        {
                            PlayerList.Add(_Client);
                        }
                    }
                    Client.Send(new SP_PLAYER_LIST(PlayerList, WRRoom, false));

                    PlayerList.Clear();
                    PlayerList.Add(Client);

                    foreach (cWRClient _Client in WRRoom.getPlayers())
                    {
                        _Client.Send(new SP_PLAYER_LIST(PlayerList, WRRoom, true));
                    }


                    //Update Room Infos to all Clients in Channel(TODO)

                    ArrayList PlayerList2 = new ArrayList();

                    foreach (cWRClient _Client in Program.WRServer.getPlayerInChannel(Client.getChannelID()))
                    {
                        if (!_Client.Equals(Client))
                        {
                            PlayerList.Add(_Client);
                        }
                    }
                    Client.Send(new SP_PLAYER_LIST(PlayerList, WRRoom, false));

                    PlayerList.Clear();
                    PlayerList.Add(Client);

                    foreach (cWRClient _Client in Program.WRServer.getPlayerInChannel(Client.getChannelID()))
                    {
                        _Client.Send(new SP_PLAYER_LIST(PlayerList, WRRoom, true));
                    }



                }
            }
            else
            {
                Client.Send(new SP_JOIN_ROOM(SP_JOIN_ROOM.ErrorCodes.GenericError));
            }
        }
    }

    class CP_CHANGE_ROOM_DATA : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            cWRRoom WRRoom = Client.getRoom();
            int tType = int.Parse(Packet.getBlockByIndex(3));
            int tValue = int.Parse(Packet.getBlockByIndex(6));
            int Value = int.Parse(Packet.getBlockByIndex(9));
            int Place1 = int.Parse(Packet.getBlockByIndex(6));
            int Place2 = int.Parse(Packet.getBlockByIndex(7));


            switch (tType)
            {
                case 1:

                    tType += 3;
                    tValue = WRRoom.getMapID();
                    
                    break;

                case 402:
                   tType += 1;
                    tValue = 3;
                    Place1 = 882;
                    Value = 1;

                    break;

                case 50: // Ready State
                    if (Client.getReadyState() == 1)
                    {
                        Client.setReadyState(0);
                        tValue = 0;
                    }
                    else
                    {
                        Client.setReadyState(1);
                        tValue = 1;
                    }
                    break;
                case 51: // Map Change
                    WRRoom.setMapID(tValue);
                    break;
                case 52: // Room Mode(Explosive, etc)
                    WRRoom.setRoomMode(tValue);
                    break;
                case 53: // Rounds/Kills
                    WRRoom.setRounds(tValue);
                    break;
                case 59: // ping
                    WRRoom.setPing(tValue);
                    break;
                case 62: // Autostart
                    break;
                case 56: // Change Side
                    Place1 = WRRoom.ChangeSide(Client);
                    Place2 = WRRoom.getIDOfPlayer(WRRoom.getRoomMaster());
                    break;
                case 150:
                    Place1 = int.Parse(Packet.getBlockByIndex(7));
                    Place2 = int.Parse(Packet.getBlockByIndex(8));
                    break;
                default:
                    new cLog(C.MSG_INFO, tType.ToString());
                    break;

            }


            ArrayList Players = WRRoom.getPlayers();
            if (tType != 403)
            {
                foreach (cWRClient _Client in Players)
                {

                    _Client.Send(new SP_CHANGE_ROOM_DATA(tType,
                        int.Parse(Packet.getBlockByIndex(0)),
                        int.Parse(Packet.getBlockByIndex(1)),
                        int.Parse(Packet.getBlockByIndex(2)),
                        int.Parse(Packet.getBlockByIndex(4)),
                        int.Parse(Packet.getBlockByIndex(5)),
                        tValue, // Value
                        Place1, // New Place
                        Place2, // Master Place
                        Value,
                        int.Parse(Packet.getBlockByIndex(10)),
                        int.Parse(Packet.getBlockByIndex(11)),
                        int.Parse(Packet.getBlockByIndex(12)),
                        int.Parse(Packet.getBlockByIndex(13))
                        ));

                }
            }
            else
            {
                Client.Send(new SP_CHANGE_ROOM_DATA(tType, int.Parse(Packet.getBlockByIndex(0)),
                        int.Parse(Packet.getBlockByIndex(1)),
                        int.Parse(Packet.getBlockByIndex(2)),
                        int.Parse(Packet.getBlockByIndex(4)),
                        int.Parse(Packet.getBlockByIndex(5)),
                        tValue, // Value
                        Place1, // New Place
                        Place2, // Master Place
                        Value,
                        int.Parse(Packet.getBlockByIndex(10)),
                        int.Parse(Packet.getBlockByIndex(11)),
                        int.Parse(Packet.getBlockByIndex(12)),
                        int.Parse(Packet.getBlockByIndex(13))
                        ));
                Client.Send(new SP_PLAYER_NUMBER(Players,WRRoom));

            }
        
        }
    }

    class CP_INVITE : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            string TargetName = Packet.getBlock();
            string Message = Packet.getBlock();
            cWRClient iPlayer = (cWRClient)null;

            if (TargetName == "NULL")
            {
                ArrayList Players = Program.WRServer.getPlayers(false);

                foreach (cWRClient _Client in Players)
                {
                    if (_Client.getChannelID() == Client.getChannelID())
                    {
                        iPlayer = _Client;
                        break;
                    }
                }
            }
            else
            {
                cWRClient tPlayer = Program.WRServer.getPlayerByNickname(TargetName);
                if (tPlayer is cWRClient && tPlayer.getSessionID() > 0)
                {
                    iPlayer = tPlayer;
                }
                else
                {
                    Client.Send(new SP_INVITE(SP_INVITE.ErrorCodes.GenericError));
                }
            }

            if (iPlayer is cWRClient && iPlayer.getSessionID() > 0 && iPlayer.getRoom() == null)
            {
                iPlayer.Send(new SP_INVITE(Client, Message));
                Client.Send(new SP_INVITE(Client, Message));
            }
            else
            {
                Client.Send(new SP_INVITE(SP_INVITE.ErrorCodes.IsPlaying));
            }
        }
    }

    class CP_ROOM_KICK : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            int Place = int.Parse(Packet.getBlock());
            cWRClient tPlayer = Client.getRoom().getPlayerByID(Place);

            if (tPlayer is cWRClient && tPlayer.getSessionID() > 0 && tPlayer.getAccesslevel() != 3)
            {
                tPlayer.Send(new SP_ROOM_KICK(Place));
            }
        }
    }
}


