using System;
using System.Net.Sockets;
using System.Collections;
using System.Data.Odbc;
using System.Net.NetworkInformation;

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

    class CP_WELCOME_PACKET : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            Client.setMac(Packet.getBlockByIndex(2));
            if (Program.getBannedMacs.Contains(Client.getMac()))
            {
                Client.Send(new SP_SHOW_MSG("Sorry your IP is banned!"));
                System.Threading.Thread.Sleep(500);
                Client.Disconnect();
            }
            else
            {
                //if (Client.getAccesslevel() > 2)
                //{
                    Client.Send(new SP_WELCOME_PACKET());
                    Program.SQL.conn.ExecuteQuery("UPDATE warock.account SET mac='" + Client.getMac() + "' WHERE userID='" + Client.getUserID() + "'");

                //}
                //else
                //{
                //    Client.Send(new SP_SHOW_MSG("We're updating only for GMs Sorry!"));
                //    System.Threading.Thread.Sleep(500);
                //    Client.Disconnect();
                //}
            }

        }
    }

    class CP_GET_DINAR : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            Client.Send(new SP_GET_DINAR(Client));
        }
    }

    class CP_LEAVE_SERVER : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            new cLog(C.MSG_INFO, "Client: " + Client.getNickname() + " left the Server! #IP : " + Client.getNetworkIP());
            Program.SQL.conn.ExecuteQuery("UPDATE account SET online = '0' WHERE userID = '" + Client.getUserID().ToString() + "'");
            Client.Send(new SP_LEAVE_SERVER());

            Client.Disconnect();
            
        }
    }
    class CP_BUYCOSTUME : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            int Subtype = int.Parse(Packet.getBlock());
            Packet.getBlock(); Packet.getBlock();
            int Price = int.Parse(Packet.getBlock());
            string CostumeCode = Packet.getBlock();

            int Class = 0;

            if(CostumeCode == "BA06" )
            {
                Class = 0;
            }
            else if (CostumeCode == "BA10")
            {
                Class = 1;
            }
            else if (CostumeCode == "BA08")
            {
                Class = 2;
            }
            else if (CostumeCode == "BA0E")
            {
                Class = 3;
            }
            else if (CostumeCode == "BA0F")
            {
                Class = 4;
            }

            //Client.setCash(Client.getCash() - Price); 

            switch ( Class )
            {
                case 0: Program.SQL.conn.ExecuteQuery("UPDATE costumes SET class_0='" + CostumeCode + "' WHERE userID='"+ Client.getUserID().ToString() + "'"); break;
                case 1: Program.SQL.conn.ExecuteQuery("UPDATE costumes SET class_1='" + CostumeCode + "' WHERE userID='"+ Client.getUserID().ToString() + "'"); break;
                case 2: Program.SQL.conn.ExecuteQuery("UPDATE costumes SET class_2='" + CostumeCode + "' WHERE userID='" + Client.getUserID().ToString() + "'"); break;
                case 3: Program.SQL.conn.ExecuteQuery("UPDATE costumes SET class_3='" + CostumeCode + "' WHERE userID='" + Client.getUserID().ToString() + "'"); break;
                case 4: Program.SQL.conn.ExecuteQuery("UPDATE costumes SET class_4='" + CostumeCode + "' WHERE userID='" + Client.getUserID().ToString() + "'"); break;
            } // wait

            Client.Send(new SP_CHARACTER_INFO(Client));//i remember u done it on old core...do it too cause we need it..


        }
    }

    class CP_CHANGE_COSTUME : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            Packet.getBlock();
            int Class = int.Parse(Packet.getBlock());
            Packet.getBlock(); Packet.getBlock();
            string CostumeCode = Packet.getBlock();


            switch (Class)
            {
                case 0: Program.SQL.conn.ExecuteQuery("UPDATE costumes SET class_0='" + CostumeCode + "' WHERE userID='" + Client.getUserID().ToString() + "'"); break;
                case 1: Program.SQL.conn.ExecuteQuery("UPDATE costumes SET class_1='" + CostumeCode + "' WHERE userID='" + Client.getUserID().ToString() + "'"); break;
                case 2: Program.SQL.conn.ExecuteQuery("UPDATE costumes SET class_2='" + CostumeCode + "' WHERE userID='" + Client.getUserID().ToString() + "'"); break;
                case 3: Program.SQL.conn.ExecuteQuery("UPDATE costumes SET class_3='" + CostumeCode + "' WHERE userID='" + Client.getUserID().ToString() + "'"); break;
                case 4: Program.SQL.conn.ExecuteQuery("UPDATE costumes SET class_4='" + CostumeCode + "' WHERE userID='" + Client.getUserID().ToString() + "'"); break;
            } // wait

            Client.Send(new SP_CHARACTER_INFO(Client));//i remember u done it on old core...do it too cause we need it..


        }
    }
    class CP_CHARACTER_INFO : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            Client.setUserID(long.Parse(Packet.getBlock()));
            Client.getUserData();
            Client.loadItems();
            //Client.GetAllWeapon();

            Client.uniqIDisCRC = int.Parse(Packet.getBlockByIndex(6));

            string Passport = Packet.getBlockByIndex(14);
            if (Passport == Client.Password)
            {

                if (Client.getAccesslevel() == 4)
                {
                    Client.setNickname("[GM]i" + Client.getNickname());
                    Client.setExp(37757475);
                    Client.setPremium(3);// remove it... What setexp setlevel and setpremium because is bugged...some gm can test from lvl 1. wait
                }
                else if (Client.getAccesslevel() == 3)
                {
                    Client.setNickname("[GM]" + Client.getNickname());
                    Client.setPremium(3);
                }
                else if (Client.getAccesslevel() == 2)
                {
                    Client.setNickname("[MOD]" + Client.getNickname());
                    Client.setPremium(3);
                }
                else if (Client.getAccesslevel() == 1)
                {
                    Client.setNickname("[I*]" + Client.getNickname()); // i ma
                    Client.setPremium(2);
                }
                Client.Send(new SP_CHARACTER_INFO(Client));
                Program.SQL.conn.ExecuteQuery("UPDATE account SET online = '1' WHERE userID = '" + Client.getUserID().ToString() + "'");
               

                Client.Send(new SP_ROOM_LIST(Client, Client.getRoomPage()));
               // Client.Send(new SP_SHOW_MSG("Welcome on the Insomania Server,is Back!                Visit www.insomniaproject.tk                                   Our Teamspeak IP is: InsomniaProject.no-ip.org"));
                Client.Send(new SP_SHOW_MSG("Some Development is still to do!                       AI Will get added tommorw!                  Urban Ops Added! Vehicles Added!"));

                Client.Send(new SP_PREMIUM_LEFT(Client));

                Client.Send(new SP_MESSAGE_PLAYER(Client, "James >> ", "Hello " + Client.getNickname() + ""));
                Client.Send(new SP_MESSAGE_PLAYER(Client, "James >> ", "This Server is Running in Debug Mode! "));
                Client.Send(new SP_MESSAGE_PLAYER(Client, "James >> ", "[ " + Program.WRServer.getAllPlayers().Count + " ] Players Are Online! "));


                if (Program.uCollcetor.getAllPlayers().Count > 12)
                {
                    Client.Send(new SP_SHOW_MSG("Server is too full choose another!"));
                    System.Threading.Thread.Sleep(600);
                    Client.Disconnect();
                }
                //if (Client.getAccesslevel() > 2)
                //{ }
                //else { Client.Send(new SP_SHOW_MSG("Only for GMS for debugging!")); Client.Disconnect(); }
                Client.isLoggedin = true;
                Client.pingOK = true;

            }
            else
            {
                Client.Send(new SP_SHOW_MSG("Please log in using normal Client"));
                Client.Disconnect();

            }
        }
    }

    class CP_CHECKSUM : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            string checksum = Packet.getBlockByIndex(6);

            Console.Write(checksum);
            //try
            //{
            //    if (String.Equals(checksum, "534657442d5743415534373234363530373631444146353031"))
            //    {
            //        Client.Disconnect();
            //    }
            //    else { }
            //}
            //catch (Exception ex)
            //{
            //    new cLog(C.MSG_ERROR, ex.Message);
            //}
        }
    }
    class CP_SPECTACE_ROOM : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            int roomID = int.Parse(Packet.getBlockByIndex(1));
            cWRRoom WRRoom = Program.rCollector.getRoomByID(roomID, Client.getChannelID());

            new cLog(C.MSG_DEBUG, Client.getNickname() + " is trying to spectace room a room");

            if (WRRoom is cWRRoom && WRRoom.ID >= 0)
            {
                if (Client.getAccesslevel() > 2)
                {
                        Client.setRoom(WRRoom);
                        Client.Send(new SP_JOIN_ROOM(Client, WRRoom));
                        new cLog(C.MSG_INFO, Client.getNickname() + " is spectacing room " + Client.getRoom());

                        //Send Playerlist in room(@WORK):

                        ArrayList PlayerList = new ArrayList();
                }
                else
                {
                    Client.Send(new SP_JOIN_ROOM(SP_JOIN_ROOM.ErrorCodes.BadLevel));
                }

            }
            else
            {
                Client.Send(new SP_JOIN_ROOM(SP_JOIN_ROOM.ErrorCodes.GenericError));
            }
        }
    }
   
    class CP_CHANGE_CHANNEL : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            try
            {
                if (Client.isRegistered)
                {
                    int Channel = int.Parse(Packet.getBlock());

                    if (Channel == 1 || Channel == 2)
                    {
                       
                        Client.setChannelID(Channel);
                    }else{
                        if (Client.getAccesslevel() > 2)
                        {
                            Client.setChannelID(Channel);
                        }
                        else
                        {
                            Client.setChannelID(1);
                            Client.Send(new SP_SHOW_MSG("Channel blocked by GM!"));
                        }
                      }

                    Client.Send(new SP_CHANGE_CHANNEL(Client));
                    Client.Send(new SP_ROOM_LIST(Client, 0));
                }
                else
                {
                    Client.Disconnect();
                }
            }
            catch (Exception ex)
            {
                new cLog(C.MSG_ERROR, ex.Message);
            }
        }
    }

 

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

            Client.Send(new SP_LUCKY_SHOT());

        }
    }
    class CP_LUCKY_SHOT_WIN : cClientPacketHandler
    {
        public string[] WeaponsOnLoose = new string[] { "DF35", "DC93", "DF65", "DC33", "DB33", "DU01", "BA0E", "BA0F", "BA08", "BA09", "BA10", "DB16", "DB08", "DB09", "DA04", "DA06", "DQ02", "DQ03", "DV01" };
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            int AlreadyHave = 0;
            //3777520 30258 0 DA08 3000 3 
            Packet.getBlock();
            string Item = Packet.getBlock();
            int Dinar = int.Parse(Packet.getBlock());
            int Bets = int.Parse(Packet.getBlock());
            Random random = new Random();
            int win = random.Next(2);
           
            if (win == 0)
            {
                int weapon2 = random.Next(20);
                Item = WeaponsOnLoose[weapon2];
            }
            foreach (cWRClient.tItem pItem in Client.getItems())
            {
                if (pItem.itemCode == Item)
                {
                    AlreadyHave = 1;
                }
            }
            if (AlreadyHave != 1)
            {

                Client.Send(new SP_LUCKY_SHOT(Client, Bets, Item, win));
                Client.setDinar(Client.getDinar() - (1200 * Bets));
                Program.SQL.conn.ExecuteQuery("UPDATE account_detail SET dinar = '" + Client.getDinar() + "' WHERE userID = '" + Client.getUserID() + "'");
                Program.SQL.conn.ExecuteQuery("INSERT INVENTORY_TBL (userID, itemCode, expireDate, class_0, class_1, class_2, class_3, class_4) VALUES (" + Client.getUserID().ToString() + ", '" + Item + "',22020609, -1, -1, -1, -1, -1)");
                Client.loadItems();
                Client.getDinar();
            }
            else
            {
                Client.Send(new SP_LUCKY_SHOT(Client, Bets, Item, win));
            }
                
            
        }
    }
    class CP_DELETE_WEAPON : cClientPacketHandler
    {
         public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            string WeaponCode = Packet.getBlock();
            int DaysRemaining = int.Parse(Packet.getBlock());

            Program.SQL.conn.ExecuteQuery("DELETE FROM `warock`.`inventory_tbl` WHERE `inventory_tbl`.`userID`='" + Client.getUserID() + "' AND `inventory_tbl`.`itemCode` = \'" + WeaponCode + "\'");
            //Client.Send(new SP_DELETE_WEAPON(Client,WeaponCode));
            Client.loadItems();
        }
    }

    class CP_PLANT_BOMB : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            string[] Blocks = Packet.getAllBlocks();
            cWRRoom ActualRoom = Client.getRoom();

            int Type = int.Parse(Packet.getBlock());
            bool isPlant = true;

            if (Type == 4)
            {
                isPlant = false;
            }

            if (isPlant == true)
            {
                Client.iPoints += 20;

                ActualRoom.RoundTimeLeft = ActualRoom.BombTimeLeft;
            }
            else
            {
                Client.iPoints += 35;
                Client.getRoom().cNIUPoints++;

                if (Client.getRoom().cNIUPoints < 9)
                {
                    foreach (cWRClient c in Client.getRoom().Players)
                    {
                        if (Client.getRoom().getSide(c) == 0)
                        {
                            c.Send(new SP_CHANGE_ROOM_DATA(c, 1));
                        }
                        else
                        {
                            c.Send(new SP_CHANGE_ROOM_DATA(c, 2));
                        }
                    }
                }else{
                    foreach (cWRClient c in Client.getRoom().Players)
                    {
                        if (Client.getRoom().getSide(c) == 0)
                        {
                            c.Send(new SP_END_OF_GAME(c, 1, 0));
                        }
                        else
                        {
                            c.Send(new SP_END_OF_GAME(c, 2, 0));
                        }
                    }
                }
               
            }


            ActualRoom.send(new SP_PLANT_BOMB(Blocks));
        }
    }
    class CP_ZOMBIE_HANDLE2 : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {

           

        }
    }

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

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


    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();
           // 25602778 29696 1 2 [GM]UnknownHype 3 0 NULL [GM]UnknownHype>>hi  

            bool toSend = true;

            
            if (Client.getAccesslevel() > 2)
            {
                if (Message.Contains("/not"))
                {
                    Message = Message.Remove(0, (Client.getNickname().Length + 4 + 4));
                    ChatType = 30;
                    toSend = false;
                    foreach (cWRClient _Client in Program.cCollector.getAllPlayers())
                    {
                        _Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Notice1, "NOTICE:" + Message, 200, TargetName));
                    }
                    new cLog(C.MSG_INFO, Client.getNickname() + " sent this notice on the server: " + Message);
                }
                else if (Message.Contains("/test"))
                {
                    foreach (cWRClient c in Client.getRoom().Players)
                    {
                        c.Send(new SP_SHOW_MSG(Client.getNickname() + " ended tha Game!"));
                        c.Send(new SP_GAME_END(Client.getRoom(),c));
                    }
                }
                else if (Message.Contains("/shutdown"))
                {
                    foreach (cWRClient c in Program.WRServer.getAllPlayers())
                    {
                        c.Send(new SP_SHOW_MSG("Server is going offline for maintance in 5 Minutes!"));
                    }
                    System.Threading.Thread.Sleep(60000 * 5);
                    Environment.Exit(0);
                }
                else if (Message.Contains("/kk"))
                {
                    Client.iKills++;
                }
                else if (Message.Contains("/removeR"))
                {
                    int roomID = int.Parse(Message.Remove(0, (Client.getNickname().Length + 4 + 7)));
                    cWRRoom _Room = Program.cCollector.getRoomByID(roomID, Client.getChannelID());

                    if (_Room is cWRRoom)
                    {
                        foreach (cWRClient c in _Room.Players)
                        {
                            c.Disconnect();
                        }
                        Program.rCollector.RemoveRoom(_Room, _Room.Channel);
                        _Room = null;
                    }
                }
                else if (Message.Contains("/msg"))
                {
                    string MSG = Message.Remove(0, (Client.getNickname().Length + 4 + 4));
                    ChatType = 30;
                    toSend = false;
                    foreach (cWRClient c in Program.cCollector.getAllPlayers())
                    {
                        if (c is cWRClient)
                        {
                            c.Send(new SP_SHOW_MSG(MSG)); //SENDS MESSAGE BOX TO PLAYER
                        }
                    }
                }
                else if (Message.Contains("BANMAC"))
                {
                    string Player = Message.Remove(0, (Client.getNickname().Length + 4 + 6));
                    Program.getBannedMacs += Player;
                }
                else if (Message.Contains("KICKALL"))
                {
                    foreach (cWRClient c in Program.cCollector.getAllPlayers())
                    {

                        c.Send(new SP_SHOW_MSG("Server Kick by " + Client.getNickname()));
                        c.Disconnect();

                    }
                }
                else if (Message.Contains("/userinfo"))
                {
                    string Player = Message.Remove(0, (Client.getNickname().Length + 4 + 11));
                    cWRClient c = Program.cCollector.getPlayerByID(Client.getID(Player));
                    if (c is cWRClient)
                    {
                        Client.Send(new SP_SHOW_MSG("Nickname : " + Player + Environment.NewLine + "Mac : " + c.getMac() + Environment.NewLine + "Acceslevel : " + c.getAccesslevel()));
                    }
                }
                else if (Message.Contains("/kick"))
                {
                    string Player = Message.Remove(0, (Client.getNickname().Length + 4 + 5));
                    foreach (cWRClient _C in Program.cCollector.getAllPlayers())
                    {
                        if (_C.getNickname().Equals(Player) || _C.getNickname() == Player)
                        {
                            _C.Disconnect();
                        }
                    }
                }
                else if (Message.Contains("/godmode"))
                {
                    Client.Life = 999999999;
                }
                else if (Message.Contains("/add"))
                {
                    string toAdd = Message.Remove(0, (Client.getNickname().Length + 4 + 4));
                    ChatType = 30;
                    toSend = false;
                    foreach (cWRClient c in Program.cCollector.getAllPlayers())
                    {
                        if (c.getNickname() == toAdd)
                        {
                            //Program.SQL.conn.ExecuteQuery("INSERT INTO friends(userID1,userID2) VALUES('" + Client.getUserID() + "','" + c.getUserID() + "')");
                            c.Send(new SP_SHOW_MSG(Client.getNickname() + "added you as Friend !"));
                        }

                    }
                }
            }
            else
            {
                if (Message.Contains("ADDFRIEND"))
                {
                    string toAdd = Message.Remove(0, (Client.getNickname().Length + 4 + 9));
                    ChatType = 30;
                    toSend = false;
                    foreach (cWRClient c in Program.cCollector.getAllPlayers())
                    {
                        if (c is cWRClient)
                        {
                            if (c.getNickname().Equals(toAdd))
                            {
                                Program.SQL.conn.ExecuteQuery("INSERT INTO friends(userID1,userID2) VALUES('" + Client.getUserID() + "','" + c.getUserID() + "')");
                                c.Send(new SP_SHOW_MSG(Client.getNickname() + "added you as Friend !"));
                            }
                        }
                    }
                }
            }
            if (Message.Contains("/fin"))
            {
                Client.Send(new SP_END_OF_GAME(Client, Client.iKills, Client.iDeaths));
            }

            string isCommand = "NO";
            if (toSend == false)
            {
                isCommand = "YES";
            }
           // Program.SQL.conn.ExecuteQuery("INSERT INTO `warock`.`log_chat` (`nickname`, `message`, `isCommand`) VALUES ('" + Client.getNickname() + "', '" + Message + "', '" + isCommand + "');");

            switch (ChatType)
            {

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


                    foreach (cWRClient _Client in Program.cCollector.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.Players)
                    {
                        _Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Room_ToAll, Message, TargetID, TargetName));
                    }

                    break;
                case 5:
                    Client.Send(new SP_SHOW_MSG("Team Chat is not implented yet!"));
                    break;
                case 6:
                    cWRClient TargetClient = Program.cCollector.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
                    {
                        if (toSend)
                        {
                            Client.Send(new SP_CHAT(SP_CHAT.ErrorCodes.ErrorUser, TargetName + Convert.ToChar(0x1D)));
                        }
                    }
                    break;
                case 8:
                    foreach (cWRClient _Client in Program.cCollector.getPlayerInChannel(Client.getChannelID()))
                    {
                        if (toSend)
                        {
                            _Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Room_ToAll, Message, TargetID, TargetName));
                        }
                    }
                    break;
                default:
                    Client.Send(new SP_SHOW_MSG("This Chat option is not implented!"));
                    break;
            }
        }
    }
    class CP_UNKNOW : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            ArrayList tPlayer = new ArrayList();
            tPlayer = Client.getRoom().Players;

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

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

            int Vorhanden = 0;
            string WeaponToBuy = Packet.getBlock();
            string WeaponCode = string.Empty;
           // DF05-1-0-12050209-0-0-0-0-0,DB12-1-0-12042717-0-0-0-0-0,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^,^
            WeaponCode = WeaponToBuy;

            foreach (cWRClient.tItem pItem in Client.getItems())
            {
                if (pItem.itemCode == WeaponToBuy)
                {
                   // Nothing
                    Vorhanden = 1; 
                }
                else
                {
                    
                }
            }
            if (Vorhanden != 1)
            {
                Client.setCoupons(Client.getCoupons() - 5);
                Program.SQL.conn.ExecuteQuery("INSERT INTO inventory_tbl (userID, itemCode, expireDate, class_0, class_1, class_2, class_3, class_4) VALUES (" + Client.getUserID().ToString() + ", '" + WeaponToBuy + "', " + Client.generateTimeStamp(3) + ", -1, -1, -1, -1, -1)");
                Program.SQL.conn.ExecuteQuery("UPDATE account_detail SET `Coupons`='"+Client.getCoupons()+"'");
                Client.loadItems();
                Client.Send(new SP_COUPON_BUY(WeaponCode, Client));
            }
         
           
                
            
        }
    }
    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 
            Packet.getBlock();
            int Time = int.Parse(Packet.getBlock());//3 
            int Days = 0;
            int Dinars = int.Parse(Packet.getBlock());
            int Error = 1;
            int Dinar = 0;
            string Items = "";
            int DTime = 0;
            int Vorhanden = 0;

            switch (Time)
            {
                case 1:
                    Days = 7;
                    break;
                case 2:
                    Days = 15;
                    break;
                case 3:
                    Days = 30;
                    break;
                default:
                    Days = 0;
                    break;
            }


            if (Error == 1)
            {


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

                        Vorhanden = 1;

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


                }
            }
            if (Vorhanden == 1)
            {
                Error = 97020;
            }
            else
            {
                if (Error == 1)
                {
                    if (Weapon.Contains("CC"))
                    {
                        //IS PACKAGE
                    }
                    Program.SQL.conn.ExecuteQuery("INSERT INTO inventory_tbl (userID, itemCode, expireDate, class_0, class_1, class_2, class_3, class_4) VALUES (" + Client.getUserID().ToString() + ", '" + Weapon + "', " + Client.generateTimeStamp(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.generateTimeStamp(DTime) + " -0-0-0-0-0," + Items;

            if ((Client.getDinar() - Dinars) > 0)
            {
                if (Error == 1)
                {
                    Client.Send(new SP_BUY_WEAPON(Client, 1, (Client.getDinar() - Dinars), Weaponstring));
                    Client.setDinar(Client.getDinar() - Dinar);
                    Program.SQL.conn.ExecuteQuery("UPDATE account_detail SET dinar='" + Client.getDinar() + "' WHERE userID='" + Client.getUserID() + "'");
                              
                }
                else
                {
                    Client.Send(new SP_BUY_WEAPON(Client, Error, 0, ""));
                }
            }
            else
            {
                Error = 97040;
                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());
            Client.pingOK = true;

            if (Client.Second != DateTime.Now.Second)
            {
                try
                {
                    byte[] buffer = new byte[32];
                    System.Net.NetworkInformation.PingOptions pingOptions = new System.Net.NetworkInformation.PingOptions(128, true);
                    System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
                    System.Net.NetworkInformation.PingReply pingReply = ping.Send(((System.Net.IPEndPoint)Client.cSocket.RemoteEndPoint).Address, 75, buffer, pingOptions);
                    Client.Second = DateTime.Now.Second;
                    if (pingReply != null)
                    {
                        switch (pingReply.Status)
                        {
                            case System.Net.NetworkInformation.IPStatus.Success:
                                Client.ClientPing = pingReply.RoundtripTime;
                                break;
                            default:
                                Client.ClientPing = 999;
                                break;
                        }
                    }
                }
                catch { Client.ClientPing = 999; }
            }
                Client.Send(new SP_PREMIUM_LEFT(Client));
            
           // 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.cCollector.getAllPlayers();
            }
            else if (Operation == 1) // by Name
            {
                string Nickname = Packet.getBlock();
                cWRClient tPlayer = null;
                foreach (cWRClient _Client in Program.cCollector.getAllPlayers())
                {
                    long ID = Client.getID(Nickname);
                    if (_Client.getUserID() == ID)
                    {
                        tPlayer = _Client;
                    }
                }
                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.cCollector.getAllPlayers())
                {
                    if (_Client.getLevel() >= Range1 && _Client.getLevel() <= Range2)
                    {
                        PlayerList.Add(_Client);
                    }
                }

            }

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

    class CP_CHANGE_WEAPON : cClientPacketHandler
    {
        public string CalculateWeaponCode(string Weapon,cWRClient Client)
        {
            string newCode = "";

            if (Client.hasPX("D101"))
            {
                if (Weapon.Contains("-"))
                {
                //DS01 = I021
                //DS03 = I020
                //DV01 = I014
                //DU01 = I022

                    string[] Codes = Weapon.Split(new char[] { '-' });
                    if (Codes[0] == "DS01") { Codes[0] = "I021"; } else if (Codes[0] == "DS03") { Codes[0] = "I020"; } else if (Codes[0] == "DV01") { Codes[0] = "I014"; } else if (Codes[0] == "DU01") { Codes[0] = "I022"; }
                    if (Codes[1] == "DS01") { Codes[1] = "I021"; } else if (Codes[1] == "DS03") { Codes[1] = "I020"; } else if (Codes[1] == "DV01") { Codes[1] = "I014"; } else if (Codes[1] == "DU01") { Codes[1] = "I022"; }

                    newCode = Codes[0] + "-" + Codes[1];
                }
            }

            return newCode;
        }
        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.SQL.conn.ExecuteQuery("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;
                    case 6: Client.slot7E = pItem.itemCode; break;
                    case 7: Client.slot8E = 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;
                    case 6: Client.slot7M = pItem.itemCode; break;
                    case 7: Client.slot8M = 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;
                    case 6: Client.slot7S = pItem.itemCode; break;
                    case 7: Client.slot8S = 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;
                    case 6: Client.slot7A = pItem.itemCode; break;
                    case 7: Client.slot8A = 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;
                    case 6: Client.slot7H = pItem.itemCode; break;
                    case 7: Client.slot8H = pItem.itemCode; break;

                }

            }

            if (Slot == 5 && Client.hasPX("D101") && WeaponCode.Contains("-"))
            {
                Unequip = 0;
                WeaponCode = CalculateWeaponCode(WeaponCode, Client);
            }

            if (Unequip == 0)
                Program.SQL.conn.ExecuteQuery("UPDATE INVENTORY_TBL SET class_" + _Class.ToString() + " = '" + Slot.ToString() + "' WHERE userID = '" + Client.getUserID().ToString() + "' AND itemCode = '" + WeaponCode + "'");
            else
                Program.SQL.conn.ExecuteQuery("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;
                        case 7: if (Unequip == 0) Client.slot8E = WeaponCode; else Client.slot8E = "^"; 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;
                        case 7: if (Unequip == 0) Client.slot8M = WeaponCode; else Client.slot8M = "^"; 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;
                        case 7: if (Unequip == 0) Client.slot8S = WeaponCode; else Client.slot8S = "^"; 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;
                        case 7: if (Unequip == 0) Client.slot8A = WeaponCode; else Client.slot8A = "^"; 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;
                        case 7: if (Unequip == 0) Client.slot8H = WeaponCode; else Client.slot8H = "^"; 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 = "^";
            if (Client.slot3M == Client.slot5M) Client.slot5M = "^";
            if (Client.slot3S == Client.slot5S) Client.slot5S = "^";
            if (Client.slot3A == Client.slot5A) Client.slot5A = "^";
            if (Client.slot3H == Client.slot5H) Client.slot5H = "^";

            Client.Send(new SP_CHANGE_WEAPON(_Class, WeaponString)); 
        }
    }

    class CP_CREATE_ROOM : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            
                string Items = "";

                foreach (cWRClient.tItem pItem in Client.getItems())
                {
                    Items += pItem.itemCode + "-3-0-" + pItem.expireDate.ToString() + "-0-0-0-0-0-9999-9999,";
                }

                for (int i = 0; i < (31 - Client.getItems().Count); i++)
                {
                    Items += "^,";
                }

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

                cWRRoom WRRoom = new cWRRoom();


                
                WRRoom.ID = Program.rCollector.getOpenID(Client.getChannelID());
                WRRoom.Channel = Client.getChannelID();
                WRRoom.Name = Packet.getBlock();

                if (Client.getChannelID() == 4)
                {
                    Packet.getBlock();
                    WRRoom.Password = Packet.getBlock();
                    WRRoom.MaxPlayers = 4;
                    WRRoom.MapID = int.Parse(Packet.getBlock());
                    WRRoom.RoomMode = 7;

                    Packet.getBlock(); Packet.getBlock();

                    WRRoom.RoomType = int.Parse(Packet.getBlock());
                    WRRoom.LevelLimit = (int.Parse(Packet.getBlock()));
                    WRRoom.PremiumOnly = (int.Parse(Packet.getBlock()));
                    WRRoom.VoteKick = (int.Parse(Packet.getBlock()));


                    if (WRRoom.joinUser(Client))
                    {
                        Program.rCollector.AddRoom(WRRoom, WRRoom.Channel);
                        Client.Send(new SP_CREATE_ROOM(WRRoom));


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

                        }
                    }
                }else if (WRRoom.Name.Contains(".Training."))
                {
                   
                   // WRRoom.setPassword(int.Parse(Packet.getBlock()), Packet.getBlock());
                    Packet.getBlock();
                    WRRoom.Password = Packet.getBlock();
                    WRRoom.MaxPlayers = 8 * (int.Parse(Packet.getBlock()) + 1);
                    WRRoom.MapID = int.Parse(Packet.getBlock());
                    
                      WRRoom.RoomMode = 0;
                    
                   
                    

                    Packet.getBlock(); Packet.getBlock();

                    WRRoom.RoomType = int.Parse(Packet.getBlock());
                    WRRoom.LevelLimit = (int.Parse(Packet.getBlock()));
                    WRRoom.PremiumOnly = (int.Parse(Packet.getBlock()));
                    WRRoom.VoteKick = (int.Parse(Packet.getBlock()));

                    WRRoom.ID = (999);
                    
                        //WRRoom.Supermaster = 1;


                       

                    if (WRRoom.joinUser(Client))
                    {
                        WRRoom.Channel = 9;
                        Program.rCollector.AddRoom(WRRoom, 9);
                        WRRoom.Name = (Client.getNickname() + "'s training Room");
                        Client.Send(new SP_CREATE_ROOM(WRRoom));
                    }
                }
                else
                {
                    //WRRoom.setPassword(int.Parse(Packet.getBlock()), Packet.getBlock());

                    Packet.getBlock();
                    WRRoom.Password = Packet.getBlock();
                    if (WRRoom.Channel == 4)
                    {
                        WRRoom.MaxPlayers = (8 * (int.Parse(Packet.getBlock()) + 1));
                        WRRoom.MapID = (int.Parse(Packet.getBlock()));
                        WRRoom.RoomMode = (6);
                    }
                    else
                    {
                        WRRoom.MaxPlayers = (8 * (int.Parse(Packet.getBlock()) + 1));
                        WRRoom.MapID = (int.Parse(Packet.getBlock()));
                        if (WRRoom.Channel == 1)
                        {
                            WRRoom.RoomMode = (0);
                        }
                        else
                        {
                            WRRoom.RoomMode = (2);
                        }
                    }

                    Packet.getBlock(); Packet.getBlock();

                    WRRoom.RoomType = (int.Parse(Packet.getBlock()));
                    WRRoom.LevelLimit = (int.Parse(Packet.getBlock()));
                    WRRoom.PremiumOnly = (int.Parse(Packet.getBlock()));
                    WRRoom.VoteKick = (int.Parse(Packet.getBlock()));
                 

                    if (Items.Contains("CC02"))
                    {
                       // WRRoom.Supermaster = 1;
                    }
                    else
                    {
                      //  WRRoom.Supermaster = 0;
                    }

                   
                    if (WRRoom.joinUser(Client))
                    {
                        Client.setRoom(WRRoom);
                        Program.rCollector.AddRoom(WRRoom, WRRoom.Channel);
                        Client.Send(new SP_CREATE_ROOM(WRRoom));

                        foreach (cWRClient _Client in Program.cCollector.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 = Client.RoomSlot;
                int newMaster = -1;
                WRRoom.removeUser(Client.RoomSlot);
                Client.setRoom(null);

                Client.iKills = 0;
                Client.iDeaths = 0;
                Client.iPoints = 0;
                Client.iFlags = 0;

                if (WRRoom.isSpawned)
                {
                    ArrayList Deberan = new ArrayList();
                    ArrayList NIU = new ArrayList();
                    foreach (cWRClient _Client in WRRoom.Players)
                    {
                        if (WRRoom.getSide(_Client) == 0)
                        {
                            Deberan.Add(_Client);
                        }
                        else
                        {
                            NIU.Add(_Client);
                        }
                    }
                    if (Deberan.Count == 1 && NIU.Count == 0 || NIU.Count == 1 && Deberan.Count == 0)
                    {
                        Client.Send(new SP_END_OF_GAME(Client, Client.iKills, Client.iDeaths));
                    }
                }

                if (WRRoom.PlayerCount == 0 || WRRoom.isSpawned && WRRoom.PlayerCount == 1)
                {
                    Program.rCollector.RemoveRoom(WRRoom, WRRoom.Channel);
                }
                else
                {
                    foreach (cWRClient _Client in WRRoom.Players)
                    {
                        newMaster = _Client.RoomSlot;
                        WRRoom.RoomMaster = _Client.RoomSlot;
                        break;
                    }
                }

                foreach (cWRClient _Client in WRRoom.Players)
                {
                    _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.cCollector.getPlayerInChannel(Client.getChannelID()))
                {
                    _Client.Send(new SP_ROOM_LIST(_Client, _Client.getRoomPage()));
                }
            
            
           
        }
    }

    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.rCollector.getRoomByID(roomID, Client.getChannelID());

            if (WRRoom is cWRRoom && WRRoom.ID >= 0)
            {
                if (WRRoom.Password != Password)
                {
                    Client.Send(new SP_JOIN_ROOM(SP_JOIN_ROOM.ErrorCodes.InvalidPassword));
                }
                else if (WRRoom.PremiumOnly == 1 && Client.getPremium() <= 0)
                {
                    Client.Send(new SP_JOIN_ROOM(SP_JOIN_ROOM.ErrorCodes.OnlyPremium));
                }
                else
                {
                    if (Client.ClientPing > 100 && WRRoom.Ping == 0)
                    {
                        Client.Send(new SP_SHOW_MSG("Your Ping is over the Ping Limit!"));
                        Client.Send(new SP_JOIN_ROOM(SP_JOIN_ROOM.ErrorCodes.GenericError));
                    }
                    else if (Client.ClientPing > 300 && WRRoom.Ping == 1)
                    {
                        Client.Send(new SP_SHOW_MSG("Your Ping is over the Ping Limit!"));
                        Client.Send(new SP_JOIN_ROOM(SP_JOIN_ROOM.ErrorCodes.GenericError));
                    }else{
                    if (WRRoom.joinUser(Client))
                    {
                        Client.setRoom(WRRoom);
                        Client.Send(new SP_JOIN_ROOM(Client, WRRoom));

                        //hypers BOOOOOOSSSLIKE stuff
                        ArrayList PlayerList = new ArrayList();

                        foreach (cWRClient _Client in WRRoom.Players)
                        {
                            PlayerList.Add(_Client);
                            WRRoom.send(new SP_PLAYER_LIST(_Client, PlayerList));
                            PlayerList.Clear();
                        }

                        foreach (cWRClient _Client in WRRoom.Players)
                        {
                            string Team= "DEBERAN";
                            if (WRRoom.getSide(Client) == 1)
                            {
                                Team = "NIU";
                            }

                            _Client.Send(new SP_MESSAGE_PLAYER(_Client, "", "<" + Client.getNickname() + "> entred as <" + Team + "> team."));
                        }
                    }
                    }
                    
                   


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

    class CP_CHANGE_ROOM_DATA : cClientPacketHandler
    {
        int GetDamageForWeapon(string WeaponCode)
        {
            int Damage = 0;
            if (WeaponCode.Contains("DA") && WeaponCode.Contains("DA04"))// Knuckles (1nd Slot)
            {
                Damage = 1000;
            }
            else if (WeaponCode.Contains("DA"))
            {
                Damage = 200;
            }
            else if (WeaponCode.Contains("DB"))// 2nd Slots
            {
                Damage = 150;
            }
            else if (WeaponCode.Contains("DF"))//Medic / Engineer
            {
                Damage = 250;
            }
            else if (WeaponCode.Contains("DJ"))//Rockets
            {
                Damage = 1000;
            }
            else if (WeaponCode.Contains("DM"))//Nade-k400
            {
                Damage = 1000;
            }
            else if (WeaponCode.Contains("DG"))//Sniper
            {
                Damage = 750;
            }
            else if (WeaponCode.Contains("DC"))//Assault
            {
                Damage = 250;
            }
            else if (WeaponCode.Contains("DH"))
            {
                Damage = 300;
            }
            else if (WeaponCode.Contains("DI"))
            {
                Damage = 600;
            }
            else if (WeaponCode.Contains("DD"))
            {
                Damage = 350;
            }
            else if (WeaponCode.Contains("DN"))
            {
                Damage = 1000;
            }
            else
            {
                Damage = 10;
                Console.WriteLine(WeaponCode);
            }

            return Damage;
        }
        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));
            int Zombie1 = 5;
            int[] Spawn = new int[Packet.Blocks.Length];

            object[] ArrSorted = new object[1];
   

            switch (tType)
            {
                case 1:
                    if (WRRoom.Players.Count > 1)
                    {
                            tType += 3;
                            tValue = WRRoom.MapID;
                            WRRoom.RoomStatus = 2;
                        
                    }
                    else
                    {
                        if (Client.getAccesslevel() > 2)
                        {
                            tType += 3;
                            tValue = WRRoom.MapID;
                            WRRoom.RoomStatus = 2;
                        }
                        else
                        {
                            Client.Send(new SP_CHAT("You can't start room alone!"));
                        }
                    }
                    break;
                case 7:
                    System.Threading.Thread.Sleep(2000);
                    tType = 5;
                    break;
                case 155:
                    {
                        Client.CurrentWeapon = int.Parse(Packet.getBlockByIndex(7));
                    }
                    break;
                case 156:
                    {
                        if (WRRoom.Flag[Value] == -1)
                        {
                            Value = Place1 = WRRoom.Flag[Value] = WRRoom.getSide(Client);
                            Client.iFlags++;
                            Client.iPoints += 5;
                        }
                        else
                        {
                            Place1 = WRRoom.Flag[Value] = -1;
                            Value = 0;
                        }
                    }
                    break;
                case 900:
                    {
                        ArrSorted = Packet.getAllBlocks();
                    }
                    break;
                case 500:
                    {
                        Place2 = (Client.Life-100);
                    }
                    break;
                case 54:
                    {
                        WRRoom.RoomTimeLeft = tValue * 10;
                        //TimeLimitChange
                    }
                    break;
                case 400:
                    {
                    //    Packet.getAllBlocks()[8] = Convert.ToString(WRRoom.Plants);
                    //    ArrSorted = Packet.getAllBlocks();
                    //    WRRoom.Plants++;
                        Client.Send(new SP_SHOW_MSG("Plant PX Items don't work yet!"));
                    } break;
                case 101:
                    {
                        if (Client.CurrentWeapon == 94 || Client.CurrentWeapon == 95 || Client.CurrentWeapon == 96)
                        {

                            if (Client.Life != 1000 && Client.Life > 0 && Client.isLiving)
                            {
                                Client.Life += 350;
                                if (Client.Life > 1000)
                                {
                                    Client.Life = 1000;
                                }
                            }
                            else
                            {
                                Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Whisper, "SYSTEM >> Your Life is full You Cant Heal!", Client.getSessionID(), Client.getNickname()));
                                Client.Life = 1000;
                            }
                        }
                        Place1 = Client.Life;

                    }
                    break;
                case 102:
                    {
                       // Client.Send(new SP_CHAT(Client, SP_CHAT.ChatType.Room_ToAll, "Server >> Repairing: " + tValue + " Other Value: " + Value, 999, "Server"));
                    }
                    break;
                case 402:
                   tType += 1;
                    tValue = 3;
                    Place1 = 882;
                    Value = 1;

                    WRRoom.isSpawned = true;

                    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.MapID = (tValue);
                    break;
                case 52: // Room Mode(Explosive, etc)
                    WRRoom.RoomMode = (tValue);
                    if (WRRoom.RoomMode == 2)
                    {
                        WRRoom.cNIUPoints = WRRoom.getRealRounds();
                        WRRoom.cDeberanPoints = WRRoom.getRealRounds();
                    }
                    //Client.Send(new SP_SHOW_MSG("NEW MODE: " + tValue));
                    break;
                case 53: // Rounds/Kills
                    WRRoom.Rounds = (tValue);
                    if (WRRoom.RoomMode == 2)
                    {
                        WRRoom.cNIUPoints = WRRoom.getRealRounds();
                        WRRoom.cDeberanPoints = WRRoom.getRealRounds();
                    }
                    break;
                case 59: // ping
                    WRRoom.Ping = (tValue);
                    break;
                case 62: // Autostart
                    break;
                case 56: // Change Side
                    Place1 = WRRoom.switchSide(Client);
                    Client.RoomSlot = Place1;
                    Place2 = WRRoom.RoomMasterSlot;
                    Client.Team = WRRoom.getSide(Client);
                    break;
                

                case 150:
             
                    if (!Client.isLiving)
                    {
                        Client.FuckPacket = 1000;
                        Client.FuckPacket2 = 165000;
                        Client.isLiving = true;
                        Client.Life = 1000;
                        Client.chooseClass = Packet.getAllBlocks()[26];
                    }
            

                    break;
                case 200:
                    Client.Send(new SP_SHOW_MSG("The use of Vehicles is still bugged!"));
                   ArrSorted = new string[26] { Packet.getBlockByIndex(0), Packet.getBlockByIndex(1), Packet.getBlockByIndex(2), Packet.getBlockByIndex(4), Packet.getBlockByIndex(5), Packet.getBlockByIndex(6), Packet.getBlockByIndex(7), "2700","2700", "999", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "$" };
                    //        ElseIf tType = 200 Then 'Car Join

                    break;
                case 202:
                    ArrSorted = new string[13] { Packet.getBlockByIndex(0), Packet.getBlockByIndex(1), Packet.getBlockByIndex(2), Packet.getBlockByIndex(4), Packet.getBlockByIndex(5), Packet.getBlockByIndex(6), Packet.getBlockByIndex(7), "0", "0", "0", "1", "0", "0" };

                    break;
                default:
                    //new cLog(C.MSG_INFO, tType.ToString());
                    break;

            }


            ArrayList Players = WRRoom.Players;
            if (tType != 403 && tType != 103 && tType != 104 && tType != 400 && tType != 150 && tType != 200 && tType != 202 && tType != 900)
            {
                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 if (tType == 104)
            {
              // 30000 0 0 2 104 0 1 1 11 0 11 0 43 2 0 1 100 0 0 3782.3088 218.2119 3739.2190 0 0 13.0000 5.0000 0 DF051299 
              // 30000 0 0 2 104 0 1 1 11 0 11 0 43 2 0 1 100 550 450 3782.3088 218.2119 3739.2190 0 0 13.0000 5.0000 0 DF05 
                foreach (cWRClient _Client in WRRoom.Players)
                {
                    try
                    {

                        _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)),
                            int.Parse(Packet.getBlockByIndex(6)),
                            int.Parse(Packet.getBlockByIndex(7)),
                            int.Parse(Packet.getBlockByIndex(8)),
                            int.Parse(Packet.getBlockByIndex(9)),
                            int.Parse(Packet.getBlockByIndex(10)),
                            int.Parse(Packet.getBlockByIndex(11)),
                            int.Parse(Packet.getBlockByIndex(12)),
                            int.Parse(Packet.getBlockByIndex(13)),
                            int.Parse(Packet.getBlockByIndex(14)),
                            int.Parse(Packet.getBlockByIndex(15)),
                            int.Parse(Packet.getBlockByIndex(16)),
                            int.Parse(Packet.getBlockByIndex(17)),
                            Packet.getBlockByIndex(18), // X
                            Packet.getBlockByIndex(19), // Z
                            Packet.getBlockByIndex(20), // Y
                            Packet.getBlockByIndex(21), // 2.0000
                            int.Parse(Packet.getBlockByIndex(22)),
                            Packet.getBlockByIndex(23), // 93.0000
                            Packet.getBlockByIndex(24), // 1.0000
                            Packet.getBlockByIndex(25), // 0
                           Packet.getBlockByIndex(26)// ITEM
                                                    ));
                    }
                    catch (Exception ex)
                    {
                        new cLog(C.MSG_ERROR, ex.Message);
                    }
                }
            } else if (tType == 103)
            {
               int Selection = int.Parse(Packet.getBlockByIndex(7));
                    int Damage = GetDamageForWeapon(Packet.getBlockByIndex(26));
                    cWRClient SelectedUser = null;
                    foreach (cWRClient _Client in WRRoom.Players)
                    {
                        if (_Client.RoomSlot == Selection)
                        {
                            SelectedUser = _Client;
                        }
                    }

                    int isHeadshot = 0;
                    int HeadshotCalculation = int.Parse(Packet.getBlockByIndex(15));
                    if (HeadshotCalculation == (1237 + Client.getSessionID()))
                    {
                        isHeadshot = 1;
                    }
                    else { isHeadshot = 0; }

                    string block21 = Packet.getBlockByIndex(21);
                    if (isHeadshot == 1)
                    {
                        block21 =  "99.0000"; 
                        Damage+= 300;
                    }

                    SelectedUser.Life = (SelectedUser.Life - Damage);
                   
                    if (SelectedUser.Life > 10)
                    {
                        Client.getRoom().send(new SP_CHANGE_ROOM_DATA(103,
                                                 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)),
                                                 int.Parse(Packet.getBlockByIndex(6)),
                                                 int.Parse(Packet.getBlockByIndex(7)),
                                                 int.Parse(Packet.getBlockByIndex(8)),
                                                 int.Parse(Packet.getBlockByIndex(9)),
                                                 int.Parse(Packet.getBlockByIndex(10)),
                                                 int.Parse(Packet.getBlockByIndex(11)),
                                                 int.Parse(Packet.getBlockByIndex(12)),
                                                 int.Parse(Packet.getBlockByIndex(13)),
                                                 int.Parse(Packet.getBlockByIndex(14)),
                                                 int.Parse(Packet.getBlockByIndex(15)),
                                                 SelectedUser.Life,
                                                 (SelectedUser.Life + Damage),
                                                 Packet.getBlockByIndex(18), // X
                                                 Packet.getBlockByIndex(19), // Z
                                                 Packet.getBlockByIndex(20), // Y
                                                 block21,
                                                 int.Parse(Packet.getBlockByIndex(22)),
                                                 Packet.getBlockByIndex(23), // 93.0000
                                                 Packet.getBlockByIndex(24), // 1.0000
                                                 Packet.getBlockByIndex(25), // 0
                                                Packet.getBlockByIndex(26)// ITEM
                                                                 ));
                    }
                    else
                    {
                        Client.getRoom().send(new SP_CHANGE_ROOM_DATA(152,
                                                 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)),
                                                 int.Parse(Packet.getBlockByIndex(6)),
                                                 int.Parse(Packet.getBlockByIndex(7)),
                                            int.Parse(Packet.getBlockByIndex(8)),
                                                 int.Parse(Packet.getBlockByIndex(9)),
                                                 int.Parse(Packet.getBlockByIndex(10)),
                                                 int.Parse(Packet.getBlockByIndex(11)),
                                                 int.Parse(Packet.getBlockByIndex(12)),
                                                 int.Parse(Packet.getBlockByIndex(13)),
                                                 int.Parse(Packet.getBlockByIndex(14)),
                                                 int.Parse(Packet.getBlockByIndex(15)),
                                                 SelectedUser.Life,
                                                 (SelectedUser.Life + Damage),
                                                 Packet.getBlockByIndex(18), // X
                                                 Packet.getBlockByIndex(19), // Z
                                                 Packet.getBlockByIndex(20), // Y
                                                 block21, // 2.0000
                                                 int.Parse(Packet.getBlockByIndex(22)),
                                                 Packet.getBlockByIndex(23), // 93.0000
                                                 Packet.getBlockByIndex(24), // 1.0000
                                                 Packet.getBlockByIndex(25), // 0
                                                Packet.getBlockByIndex(26)// ITEM
                                                                 ));

                        if (SelectedUser.RoomSlot != Client.RoomSlot)
                        {


                            SelectedUser.DoubleKill = 0;
                            SelectedUser.TrippleKill = 0;
                            SelectedUser.iDeaths++;
                            SelectedUser.Life = 1000;
                            SelectedUser.isLiving = false;
                            SelectedUser = null;
                            ArrayList Points = new ArrayList();
                            foreach (cWRClient _Client in WRRoom.Players)
                            {
                                Points.Add(_Client.iKills);
                            }

                            if (Convert.ToInt32(Program.WRServer.GetMaxValue(Points)) == 0)
                            {
                                Client.Send(new SP_SEND_BADGE());
                                Client.iPoints += 15;
                            }
                            Client.iKills++;
                            Client.DoubleKill++;
                            Client.TrippleKill++;
                            Client.iPoints += 6;

                            if (Client.DoubleKill == 2)
                            {
                                Client.Send(new SP_SEND_DOUBLE());
                                Client.DoubleKill = 0;
                                Client.iPoints += 5;
                            }

                            if (Client.TrippleKill == 3)
                            {
                                Client.Send(new SP_SEND_TRIPPLE());
                                Client.TrippleKill = 0;
                                Client.iPoints += 10;
                            }

                              if (WRRoom.RoomMode == 0)
                        {
                            ArrayList tempNIU = WRRoom.getLivingTeamPlayer(0);
                            ArrayList tempDeberan = WRRoom.getLivingTeamPlayer(1);

                            if (tempNIU.Count == 0)
                            {
                                WRRoom.cNIUPoints++;
                                WRRoom.newRound(0);
                            }
                            else if (tempDeberan.Count == 0)
                            {
                                WRRoom.cDeberanPoints++;
                                WRRoom.newRound(1);
                            }
                        }
                        else
                        {
                            if (WRRoom.cNIUPoints == 0 || WRRoom.cDeberanPoints ==0)
                            {
                                WRRoom.EndGame(0);
                            }
                            else
                            {
                                if (WRRoom.getSide(SelectedUser) == 0)
                                {
                                    WRRoom.cDeberanPoints--;
                                }
                                else
                                {
                                    WRRoom.cNIUPoints--;
                                }
                            }

                        }
                    

                        }

                    }

                }
                 
       
            
         
            else if (tType == 500)
            {
                foreach (cWRClient _Client in WRRoom.Players)
                {
                    try
                    {
                        //18466636 30000 1 2 0 2 500 0 1 1 2 2 629 1191580 629 371 0 0 0 0 0 0 0 0 0 0 0 0 0 $ 
                        int Damage = int.Parse(Packet.getBlockByIndex(9));
                        Client.Life = (Client.Life - Damage);
                        //F1 F3 F4 F6 F1 E3 x x x x x x x x x E3 C9
                        _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(5)),
                        int.Parse(Packet.getBlockByIndex(6)),
                         int.Parse(Packet.getBlockByIndex(7)),
                        int.Parse(Packet.getBlockByIndex(8)),
                        int.Parse(Packet.getBlockByIndex(9)),
                        int.Parse(Packet.getBlockByIndex(10)),
                        int.Parse(Packet.getBlockByIndex(11)),
                        int.Parse(Packet.getBlockByIndex(12)),
                        Client.Life,
                        int.Parse(Packet.getBlockByIndex(14)),
                        int.Parse(Packet.getBlockByIndex(15)),
                        int.Parse(Packet.getBlockByIndex(16)),
                        int.Parse(Packet.getBlockByIndex(17)),
                        int.Parse(Packet.getBlockByIndex(18)),
                        int.Parse(Packet.getBlockByIndex(19)),
                        int.Parse(Packet.getBlockByIndex(20)),
                        int.Parse(Packet.getBlockByIndex(21)),
                        int.Parse(Packet.getBlockByIndex(22)),
                        int.Parse(Packet.getBlockByIndex(23)),
                        int.Parse(Packet.getBlockByIndex(24)),
                        int.Parse(Packet.getBlockByIndex(25)),
                        int.Parse(Packet.getBlockByIndex(26))
                                                ));
                    }
                    catch
                    {

                    }
                }
            }
            else if ( tType == 200 || tType == 202 || tType == 900 )
            {
                foreach (cWRClient _Client in WRRoom.Players)
                {
                    try
                    {
                        if (tType == 900)
                        {
                            _Client.Send(new SP_CHANGE_ROOM_DATA(1000, ArrSorted));
                        }
                        else
                        {
                            _Client.Send(new SP_CHANGE_ROOM_DATA(tType, ArrSorted));
                        }
                    }
                    catch (Exception Ex)
                    {
                        _Client.Disconnect();
                    }
                }
            }
            else if (tType == 101)
            {
                foreach (cWRClient _Client in WRRoom.Players)
                {
                    // 30000 0 0 2 101 0 0 0 0 0 0 0 0 0 0 
                    // 30000 0 0 2 101 0 0 0 1000 0 0 0 0 0 0 
                    try
                    {
                        Client.Life = (Client.Life + 310);
                        if (Client.Life > 999)
                        {
                            Client.Life = 1000;
                        }
                        _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)),
                            int.Parse(Packet.getBlockByIndex(6)),
                            Client.Life,
                            int.Parse(Packet.getBlockByIndex(8)),
                            int.Parse(Packet.getBlockByIndex(9)),
                            int.Parse(Packet.getBlockByIndex(10)),
                            int.Parse(Packet.getBlockByIndex(11)),
                            int.Parse(Packet.getBlockByIndex(12)),
                            int.Parse(Packet.getBlockByIndex(13))
                                                    ));
                    }
                    catch
                    {

                    }
                }
            }
            else if (tType == 150)
            {
                // 30000  2 0 2 150 0 1 7 3 2 0 2 4 5 0 0 0 0 0 0 0 0 0 0 0 0 0 ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ  
                foreach (cWRClient _Client in WRRoom.Players)
                {
                    try
                    {

                        _Client.Send(new SP_CHANGE_ROOM_DATA(150,
                                    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)),
                                    int.Parse(Packet.getBlockByIndex(6)),
                                    int.Parse(Packet.getBlockByIndex(7)),
                                    int.Parse(Packet.getBlockByIndex(8)),
                                    int.Parse(Packet.getBlockByIndex(9)),
                                    int.Parse(Packet.getBlockByIndex(10)),
                                    int.Parse(Packet.getBlockByIndex(11)),
                                    int.Parse(Packet.getBlockByIndex(12)),
                                    int.Parse(Packet.getBlockByIndex(13)),
                                    int.Parse(Packet.getBlockByIndex(14)),
                                    int.Parse(Packet.getBlockByIndex(15)),
                                    int.Parse(Packet.getBlockByIndex(16)),
                                    int.Parse(Packet.getBlockByIndex(17)),
                                   int.Parse(Packet.getBlockByIndex(18)),
                                   int.Parse(Packet.getBlockByIndex(19)),
                                   int.Parse(Packet.getBlockByIndex(20)),
                                   int.Parse(Packet.getBlockByIndex(21)),
                                   int.Parse(Packet.getBlockByIndex(22)),
                                   int.Parse(Packet.getBlockByIndex(23)),
                                   int.Parse(Packet.getBlockByIndex(24)),
                                   int.Parse(Packet.getBlockByIndex(25)),
                                   Packet.getBlockByIndex(26)//trenncode WICHTIG

                                   ));

                        Client.isLiving = true;
                    }
                    catch
                    {

                    }
                }
            }
           
            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))
                        ));

            }
        
        }
    }
    class CP_CREDITS : cClientPacketHandler
    {
        public override void HandlePacket(cClientPacketHandler Packet, cWRClient Client)
        {
            int ErrCode = int.Parse(Packet.getBlock());
            if (ErrCode == 1113)
            {
                Client.Send(new SP_ITEM_SHOP(Client));
            }
            else if (ErrCode == 1111)
            {
                string nickname = Program.SQL.conn.Stripslash(Packet.getBlockByIndex(4));

                try
                {
                    new cLog(C.MSG_INFO, "Client " + Client.getNickname() + "has changed his nickname to " + nickname + "");
                    Client.Send(new SP_CHG_NICK(Client, nickname));
                    Program.SQL.conn.ExecuteQuery("UPDATE account SET nickname = '" + nickname + "' WHERE userID = '" + Client.getUserID().ToString() + "'");
                    Program.SQL.conn.ExecuteQuery("DELETE FROM INVENTORY_TBL WHERE userID = '" + Client.getUserID() + "' AND itemCode = 'CB01'");
                    Client.getUserData();
                    Client.Send(new SP_CHARACTER_INFO(Client));
                    Client.Send(new SP_CHANGE_CHANNEL(Client));
                }
                catch (Exception ex)
                {
                    new cLog(C.MSG_ERROR, "MySQL Error: " + ex.Message);
                }

                Client.Send(new SP_CHARACTER_INFO(Client));

            }
            else if (ErrCode == 1110)
            {
                //[8/19/2012 3:27:49 PM] [CP_CREDITS.HandlePacket] » New Cash ErrCode : 1110
                //[8/19/2012 3:27:49 PM] [CP_CREDITS.HandlePacket] » 1110 33 3 2700 DC33
                Packet.getBlock();
                int Time = int.Parse(Packet.getBlock());
                int Cash = int.Parse(Packet.getBlock());
                string ItemCode = Packet.getBlock();

                int Days = 0;
                switch (Time)
                {
                    case 1:
                        Days = 7;
                        break;
                    case 2:
                        Days = 15;
                        break;
                    case 3:
                        Days = 30;
                        break;
                    default:
                        Days = 0;
                        break;
                }

                if (Days != 0)
                {
                    if (Client.Cash - Cash > 0)
                    {
                        Client.Cash = (Client.Cash - Cash);
                    }
                    else
                    {
                        return;
                    }


                    int invenotoryContains = 0;

                    foreach (cWRClient.tItem pItem in Client.getItems())
                    {
                        if (pItem.itemCode == ItemCode) { }
                        else
                        {
                            if (ItemCode.Contains("CM08") || ItemCode.Contains("CM07"))
                            {
                                if (ItemCode.Contains("CM08"))
                                {
                                    Client.setDinar(Client.getDinar() + 20000);
                                    Program.SQL.conn.ExecuteQuery("UPDATE account_detail SET dinar='" + Client.getDinar() + "' WHERE userID='"+ Client.getUserID() +"'");
                                }
                                else if(ItemCode.Contains("CM07"))
                                {
                                    Client.setDinar(Client.getDinar() + 10000);
                                    Program.SQL.conn.ExecuteQuery("UPDATE account_detail SET dinar='" + Client.getDinar() + "' WHERE userID='" + Client.getUserID() + "'");
                                }
                            }
                            else
                            {
                                Program.SQL.conn.ExecuteQuery("INSERT INTO inventory_tbl (userID, itemCode, expireDate, class_0, class_1, class_2, class_3, class_4) VALUES (" + Client.getUserID().ToString() + ", '" + ItemCode + "', " + Client.generateTimeStamp(Days) + ", -1, -1, -1, -1, -1)");
                                Client.loadItems();

                            }
                            Program.SQL.conn.ExecuteQuery("UPDATE account_detail SET iscash='" + Client.Cash + "' WHERE userID='" + Client.getUserID() + "'");

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


                }
            }
            else
            {
                Logger.Notification("New Cash ErrCode : " + ErrCode);
                Logger.Notification(String.Join(" ", Packet.getAllBlocks()));
            }
        }
    }
    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.cCollector.getPlayerInChannel(Client.getChannelID());

                foreach (cWRClient _Client in Players)
                {
                    if (_Client.getChannelID() == Client.getChannelID())
                    {
                        iPlayer = _Client;
                        break;
                    }
                }
            }
            else
            {
                cWRClient tPlayer = Program.cCollector.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().getPlayer(Place);

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


