public static void SendRequirement(Character character, TradeSkillSkillInfo tradeSkillSkillInfo)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(character.Id);
            packetWriter.PushInt(0x5E477770);
            packetWriter.PushIdentity(50000, character.Id);
            packetWriter.PushByte(0);
            packetWriter.PushInt(0xE3);
            packetWriter.PushInt(0);
            packetWriter.PushInt(0);
            packetWriter.PushInt(0);
            packetWriter.PushInt(tradeSkillSkillInfo.Skill);
            packetWriter.PushInt(tradeSkillSkillInfo.Requirement);
            packetWriter.PushByte(0);
            packetWriter.PushByte(0);
            byte[] packet = packetWriter.Finish();
            character.Client.SendCompressed(packet);
        }
        public static void Send(Character character, int type, int id)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(character.Id);
            packetWriter.PushInt(0x5e477770);
            packetWriter.PushInt(50000);
            packetWriter.PushInt(character.Id);
            packetWriter.PushByte(0);
            packetWriter.PushInt(0xcc);
            packetWriter.PushInt(0);
            packetWriter.PushInt(50000);
            packetWriter.PushInt(character.Id);
            packetWriter.PushInt(type);
            packetWriter.PushInt(id);
            packetWriter.PushShort(0);

            byte[] packet = packetWriter.Finish();
            character.Client.SendCompressed(packet);
        }
        public static void Send(Character talkingTo, NonPlayerCharacterClass talker)
        {
            Client client = talkingTo.Client;

            PacketWriter pw = new PacketWriter();

            pw.PushByte(0xdf);
            pw.PushByte(0xdf);
            pw.PushShort(10);
            pw.PushShort(1);
            pw.PushShort(0);
            pw.PushInt(3086);
            pw.PushInt(client.Character.Id);
            pw.PushInt(0x270a4c62);
            pw.PushIdentity(50000, talkingTo.Id);
            pw.PushByte(0);
            pw.PushShort(2);
            pw.PushIdentity(50000, talker.Id);
            pw.PushInt(5);
            pw.PushInt(0);

            byte[] packet = pw.Finish();
            client.SendCompressed(packet);

            // Closing KnuBot window from server side needs to clear KnuBot's variables
            talker.KnuBot.KnuBotCloseChatWindow(talkingTo);
        }
        public static void SendResult(Character character, int min, int max, int low, int high)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(character.Id);
            packetWriter.PushInt(0x5E477770);
            packetWriter.PushIdentity(50000, character.Id);
            packetWriter.PushByte(0);
            packetWriter.PushInt(0xE4);
            packetWriter.PushInt(0);
            packetWriter.PushInt(max);
            packetWriter.PushInt(high);
            packetWriter.PushInt(min);
            packetWriter.PushInt(low);
            packetWriter.PushByte(0);
            packetWriter.PushByte(0);
            byte[] pack = packetWriter.Finish();
            character.Client.SendCompressed(pack);
        }
        public static void DespawnPacket(int targetId)
        {
            PacketWriter packetWriter = new PacketWriter();
            packetWriter.PushByte(0xDF);
            packetWriter.PushByte(0xDF);
            packetWriter.PushShort(10);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0x1d);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(0);
            packetWriter.PushInt(0x36510078);
            packetWriter.PushIdentity(50000, targetId);
            packetWriter.PushByte(1);
            byte[] packet = packetWriter.Finish();

            Dynel dyn = FindDynel.FindDynelById(50000, targetId);
            if (dyn != null)
            {
                NonPlayerCharacterClass npc = dyn as NonPlayerCharacterClass;
                if (npc != null)
                {
                    npc.RemoveFromCache();
                }
                Announce.PlayfieldOthers(dyn.PlayField, packet);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="client"></param>
        public static void GetDynels(Client client)
        {
            foreach (Client clients in client.Server.Clients)
            {
                PacketWriter packetWriter;
                if ((clients.Character.PlayField == client.Character.PlayField)
                    && (clients.Character.Id != client.Character.Id))
                {
                    SimpleCharFullUpdate.SendToOne(clients.Character, client);

                    // Send CharacterInPlay packet 
                    // TODO: Move this out to own file
                    packetWriter = new PacketWriter();
                    packetWriter.PushByte(0xDF);
                    packetWriter.PushByte(0xDF);
                    packetWriter.PushShort(10);
                    packetWriter.PushShort(1);
                    packetWriter.PushShort(0);
                    packetWriter.PushInt(3086);
                    packetWriter.PushInt(client.Character.Id);
                    packetWriter.PushInt(0x570C2039);
                    packetWriter.PushIdentity(50000, clients.Character.Id);
                    packetWriter.PushByte(0);
                    byte[] reply2 = packetWriter.Finish();
                    client.SendCompressed(reply2);
                }
            }
        }
        public static void Send(Client cli, NonPlayerCharacterClass knubotTarget, string message)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(cli.Character.Id);
            packetWriter.PushInt(0x5d70532a);
            packetWriter.PushIdentity(cli.Character.Type, cli.Character.Id);
            packetWriter.PushByte(0);
            packetWriter.PushShort(2);
            packetWriter.PushIdentity(knubotTarget.Type, knubotTarget.Id);
            packetWriter.PushInt(0);
            packetWriter.PushInt(message.Length);
            packetWriter.PushString(message);
            packetWriter.PushInt(0);

            byte[] packet = packetWriter.Finish();

            cli.SendCompressed(packet);
        }
        public static void Send(Client client, string text, int color)
        {
            PacketWriter packetWriter = new PacketWriter();
            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushInt(0x206b4b73);
            packetWriter.PushInt(50000);
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushByte(1);
            packetWriter.PushInt(0);
            packetWriter.PushInt(0x557e26);
            packetWriter.PushInt(0x21212122);
            packetWriter.PushInt(0x3a212121);
            packetWriter.PushShort(0x293c);
            packetWriter.PushByte(0x73);
            packetWriter.PushByte((byte)text.Length);
            packetWriter.PushString(text);
            packetWriter.PushInt(0);

            byte[] packet = packetWriter.Finish();

            client.SendCompressed(packet);
        }
        public static void Send(Client cli, NonPlayerCharacterClass knubotTarget, string[] choices)
        {
            PacketWriter pw = new PacketWriter();

            pw.PushByte(0xdf);
            pw.PushByte(0xdf);
            pw.PushShort(0xa);
            pw.PushShort(1);
            pw.PushShort(0);
            pw.PushInt(3086);
            pw.PushInt(cli.Character.Id);
            pw.PushInt(0x55704d31);
            pw.PushIdentity(cli.Character.Type, cli.Character.Id);
            pw.PushByte(0);
            pw.PushShort(2);
            pw.PushIdentity(knubotTarget.Type, knubotTarget.Id);
            pw.PushInt(choices.Length);
            foreach (string choice in choices)
            {
                pw.PushInt(choice.Length);
                pw.PushString(choice);
            }

            byte[] packet = pw.Finish();

            cli.SendCompressed(packet);
        }
        public static void Send(Character character, InventoryEntries inventoryEntry)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(character.Id);
            packetWriter.PushInt(0x35505644);
            packetWriter.PushInt(50000);
            packetWriter.PushInt(character.Id);
            packetWriter.PushByte(0);
            packetWriter.PushInt(inventoryEntry.Item.LowID);
            packetWriter.PushInt(inventoryEntry.Item.HighID);
            packetWriter.PushInt(inventoryEntry.Item.Quality);
            packetWriter.PushInt(1); // Unknown
            packetWriter.PushInt(3); // Consume??
            packetWriter.PushInt(inventoryEntry.Container);
            packetWriter.PushInt(inventoryEntry.Placement);
            packetWriter.PushInt(0); // Unknown
            packetWriter.PushInt(0); // Unknown

            byte[] packet = packetWriter.Finish();
            character.Client.SendCompressed(packet);
        }
示例#11
0
        /// <summary>
        /// Set own stat (no announce)
        /// </summary>
        /// <param name="client">Affected client</param>
        /// <param name="stat">Stat</param>
        /// <param name="value">Value</param>
        /// <param name="announce">Let others on same playfield know?</param>
        public static uint Set(Client client, int stat, uint value, bool announce)
        {
            PacketWriter packetWriter = new PacketWriter();

            uint oldValue = (uint)client.Character.Stats.StatValueByName(stat);
            client.Character.Stats.SetStatValueByName(stat, value);

            packetWriter.PushBytes(new byte[] { 0xDF, 0xDF, });
            packetWriter.PushShort(10);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushInt(0x2B333D6E);
            packetWriter.PushIdentity(50000, client.Character.Id);
            packetWriter.PushByte(1);
            packetWriter.PushInt(1);
            packetWriter.PushInt(stat);
            packetWriter.PushUInt(value);

            byte[] packet = packetWriter.Finish();
            client.SendCompressed(packet);

            /* announce to playfield? */
            if (announce)
            {
                Announce.Playfield(client.Character.PlayField, packet);
            }

            return oldValue;
        }
        public static void Send(Character character, InventoryEntries inventoryEntries)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(character.Id);
            packetWriter.PushInt(0x52526858);
            packetWriter.PushInt(50000);
            packetWriter.PushInt(character.Id);
            packetWriter.PushByte(0);
            packetWriter.PushInt(1);
            packetWriter.PushInt(3);
            packetWriter.PushInt(3);
            packetWriter.PushInt(0);
            packetWriter.PushInt(50000);
            packetWriter.PushInt(character.Id);
            packetWriter.PushInt(inventoryEntries.Container);
            packetWriter.PushInt(inventoryEntries.Placement);
            byte[] pack = packetWriter.Finish();
            character.Client.SendCompressed(pack);
        }
        public static void Send(Client cli, NonPlayerCharacterClass knubotTarget, AOItem[] items)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(cli.Character.Id);
            packetWriter.PushInt(0x7864401d);
            packetWriter.PushIdentity(cli.Character.Type, cli.Character.Id);
            packetWriter.PushByte(0);
            packetWriter.PushShort(2);
            packetWriter.PushIdentity(knubotTarget.Type, knubotTarget.Id);
            packetWriter.PushInt(items.Length);
            foreach (AOItem item in items)
            {
                packetWriter.PushInt(item.LowID);
                packetWriter.PushInt(item.HighID);
                packetWriter.PushInt(item.Quality);
                packetWriter.PushInt(0x499602d2); // 1234567890  ???????
            }
            packetWriter.PushInt(0);

            byte[] packet = packetWriter.Finish();

            cli.SendCompressed(packet);
        }
        public static void Send(Client client, VendingMachine vendingMachine)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushInt(0x7f544905); // 20
            packetWriter.PushIdentity(vendingMachine.Type, vendingMachine.Id);
            packetWriter.PushByte(0);
            packetWriter.PushInt(0xb); // Counter??
            packetWriter.PushInt(0);
            packetWriter.PushInt(0); // 41
            packetWriter.PushCoord(vendingMachine.Coordinates);
            packetWriter.PushQuat(vendingMachine.Heading); // 69
            packetWriter.PushInt(vendingMachine.PlayField);
            packetWriter.PushInt(1000015);
            packetWriter.PushInt(0);
            packetWriter.PushShort(0x6f);
            packetWriter.PushInt(0x2379);
            packetWriter.PushInt(0); // 91
            packetWriter.PushByte(0x80);
            packetWriter.PushByte(2);
            packetWriter.PushShort(0x3603);
            packetWriter.PushInt(0x17);
            packetWriter.PushInt(vendingMachine.TemplateId);
            packetWriter.PushInt(0x2bd);
            packetWriter.PushInt(0); // 111
            packetWriter.PushInt(0x2be);
            packetWriter.PushInt(0);
            packetWriter.PushInt(0x2bf);
            packetWriter.PushInt(0);
            packetWriter.PushInt(0x19c); // 131
            packetWriter.PushInt(1);
            packetWriter.PushInt(0x1f5);
            packetWriter.PushInt(2);
            packetWriter.PushInt(0x1f4);
            packetWriter.PushInt(0);
            packetWriter.PushInt(0);
            packetWriter.PushInt(2);
            packetWriter.PushInt(0x32); // 147
            packetWriter.Push3F1Count(0);
            packetWriter.PushInt(3); // 155<

            byte[] packet = packetWriter.Finish();
            client.SendCompressed(packet);
        }
        public static void Send(Client client, int category, int instance, object[] args)
        {
            PacketWriter packetWriter = new PacketWriter();
            packetWriter.PushByte(0xDF);
            packetWriter.PushByte(0xDF);
            packetWriter.PushShort(10);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(0);
            packetWriter.PushInt(0x206B4B73);
            packetWriter.PushIdentity(50000, client.Character.Id);
            packetWriter.PushByte(1);
            packetWriter.PushInt(0);

            string message = "&~" + Encode85By4(category) + "&:" + Encode85By4(instance);

            foreach (object arg in args)
            {
                if (arg is Int32)
                {
                    message = message + "i" + Encode85By5((Int32)arg);
                }
                string stringArg = arg as string;
                if (stringArg != null)
                {
                    if (stringArg.Length > 255)
                    {
                        message = message + "S";
                        Int16 len = (Int16)stringArg.Length;
                        message = message + ShortToChar(len) + stringArg;
                    }
                    else
                    {
                        message = message + "s" + ByteToChar((byte)(stringArg.Length));
                    }
                }
            }

            Int16 mlen = (Int16)(message.Length);
            packetWriter.PushShort(mlen);
            packetWriter.PushString(message);
            packetWriter.PushInt(1);

            byte[] packet = packetWriter.Finish();
            client.SendCompressed(packet);
        }
 public static void Send(Client client)
 {
     PacketWriter packetWriter = new PacketWriter();
     packetWriter.PushByte(0xdf);
     packetWriter.PushByte(0xdf);
     packetWriter.PushShort(0xa);
     packetWriter.PushShort(1);
     packetWriter.PushShort(0); // Length
     packetWriter.PushInt(3086);
     packetWriter.PushInt(client.Character.Id);
     packetWriter.PushInt(0x343c287f);
     packetWriter.PushIdentity(50000, client.Character.Id);
     packetWriter.PushByte(1);
     packetWriter.PushInt(0);
     packetWriter.PushInt(0);
     packetWriter.PushInt(0);
     packetWriter.Push3F1Count(client.Character.Bank.Count);
     foreach (AOItem item in client.Character.Bank)
     {
         packetWriter.PushInt(item.Flags); // misused the flags for position in the bank
         short flags = 0;
         if (item.isInstanced())
         {
             flags |= 0xa0;
         }
         if (item.LowID == item.HighID)
         {
             flags |= 2;
         }
         else
         {
             flags |= 1;
         }
         // perhaps there are more flags...
         packetWriter.PushShort(flags);
         packetWriter.PushShort((short)item.MultipleCount);
         packetWriter.PushInt(item.Type);
         packetWriter.PushInt(item.Instance);
         packetWriter.PushInt(item.LowID);
         packetWriter.PushInt(item.HighID);
         packetWriter.PushInt(item.Quality);
         packetWriter.PushInt(0); // didnt encounter any other value
     }
     byte[] reply = packetWriter.Finish();
     client.SendCompressed(reply);
 }
        public static void OrgInfoPacket(Character character)
        {
            PacketWriter writer = new PacketWriter();

            writer.PushByte(0xDF);
            writer.PushByte(0xDF);
            writer.PushShort(10);
            writer.PushShort(1);
            writer.PushShort(0);
            writer.PushInt(3086);
            writer.PushInt(character.Id);
            writer.PushInt(0x2E2A4A6B);
            writer.PushIdentity(50000, character.Id);
            writer.PushByte(0);
            writer.PushShort((short)character.OrgName.Length);
            writer.PushBytes(Encoding.ASCII.GetBytes(character.OrgName));
            byte[] mReply = writer.Finish();
            Announce.Playfield(character.PlayField, mReply);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="client"></param>
        public static void Send(Client client)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushBytes(new byte[] { 0xDF, 0xDF });
            packetWriter.PushShort(10);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushInt(0x5F4B1A39);
            packetWriter.PushIdentity(40016, client.Character.PlayField);
            packetWriter.PushByte(0);

            packetWriter.PushInt(4);
            packetWriter.PushCoord(client.Character.Coordinates);
            packetWriter.PushByte(97);
            packetWriter.PushIdentity(51100, client.Character.PlayField);
            packetWriter.PushInt(0);
            packetWriter.PushInt(0);
            packetWriter.PushIdentity(40016, client.Character.PlayField);
            packetWriter.PushInt(0);
            packetWriter.PushInt(0);

            int vendorcount = VendorHandler.GetNumberofVendorsinPlayfield(client.Character.PlayField);
            if (vendorcount > 0)
            {
                packetWriter.PushInt(51035);
                packetWriter.PushInt(1);
                packetWriter.PushInt(1);
                packetWriter.PushInt(vendorcount);
                packetWriter.PushInt(VendorHandler.GetFirstVendor(client.Character.PlayField));
            }
            // TODO: Use correct World Position for each "outdoors" playfield -Suiv-
            // Playfield WorldPos X
            packetWriter.PushInt(Playfields.GetPlayfieldX(client.Character.PlayField));
            // Playfield WorldPos Z
            packetWriter.PushInt(Playfields.GetPlayfieldZ(client.Character.PlayField));

            byte[] packet = packetWriter.Finish();
            client.SendCompressed(packet);
        }
        public override void ExecuteCommand(Client client, Identity target, string[] args)
        {
            // No check needed, its done by CheckCommandArguments
            int gfx = int.Parse(args[3]);

            //begin assembling packet here
            PacketWriter packet = new PacketWriter();
            packet.PushByte(0xDF);
            packet.PushByte(0xDF);
            packet.PushShort(10);
            packet.PushShort(1);
            packet.PushShort(0);
            packet.PushInt(3086);
            packet.PushInt(client.Character.Id);
            packet.PushInt(0x4D450114);
            packet.PushIdentity(target);
            packet.PushByte(0);
            packet.Push3F1Count(1); // effects count
            // effect starts
            packet.PushIdentity(53030, 0); // effect ID (53030 = GfxEffect)
            packet.PushInt(4); // ?
            packet.PushInt(0); // Criterion count
            packet.PushInt(1); // Hits
            packet.PushInt(0); // Delay
            packet.PushInt(0); // 
            packet.PushInt(0); // 
            // effect args
            packet.PushInt(gfx); // Value
            packet.PushInt(0); // GfxLife
            packet.PushInt(0); // GfxSize
            packet.PushInt(0); // GfxRed
            packet.PushInt(0); // GfxGreen
            packet.PushInt(0); // GfxBlue
            packet.PushInt(0); // GfxFade
            // effect args end
            // effect ends
            packet.PushIdentity(50000, client.Character.Id);
            byte[] reply = packet.Finish();
            //done creating the packet
            Announce.Playfield(client.Character.PlayField, reply);
        }
示例#20
0
        public static void Send(Client client, Dynel dynel1, Dynel dynel2)
        {
            PacketWriter packetWriter = new PacketWriter();
            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushInt(0x36284f6e);
            packetWriter.PushIdentity(dynel1.Type, dynel1.Id);
            packetWriter.PushByte(0);
            packetWriter.PushInt(1); // Knubot sends 2 here
            packetWriter.PushByte(0); // and 2 here too
            packetWriter.PushIdentity(dynel2.Type, dynel2.Id); // knubot 0
            packetWriter.PushIdentity(0xc767, 0x39da2458); // temp bag ID?? Knubot 0, needs more testing....

            byte[] packet = packetWriter.Finish();
            client.SendCompressed(packet);
        }
        /// <summary>
        /// Sends chat server info to client
        /// </summary>
        /// <param name="client">Client that gets the info</param>
        public static void Send(Client client)
        {
            /* get chat settings from config */
            string chatServerIp = string.Empty;
            IPAddress tempIp;
            if (IPAddress.TryParse(ConfigReadWrite.Instance.CurrentConfig.ChatIP, out tempIp))
            {
                chatServerIp = ConfigReadWrite.Instance.CurrentConfig.ChatIP;
            }
            else
            {
                IPHostEntry chatHost = Dns.GetHostEntry(ConfigReadWrite.Instance.CurrentConfig.ChatIP);
                foreach (IPAddress ip in chatHost.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        chatServerIp = ip.ToString();
                        break;
                    }
                }
            }
            int chatPort = Convert.ToInt32(ConfigReadWrite.Instance.CurrentConfig.ChatPort);

            PacketWriter writer = new PacketWriter();

            writer.PushBytes(new byte[] { 0xDF, 0xDF });
            writer.PushShort(1);
            writer.PushShort(1);
            writer.PushShort(0);
            writer.PushInt(3086);
            writer.PushInt(client.Character.Id);
            writer.PushInt(67);
            writer.PushInt(1);
            writer.PushInt(chatServerIp.Length);
            writer.PushBytes(Encoding.ASCII.GetBytes(chatServerIp));
            writer.PushInt(chatPort);
            writer.PushInt(0);
            byte[] packet = writer.Finish();
            client.SendCompressed(packet);
        }
        public static void Send(Client client, InventoryEntries inventoryEntry)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushInt(0x052e2f0c);
            packetWriter.PushIdentity(client.Character.Type, client.Character.Id);
            packetWriter.PushByte(0);
            packetWriter.PushInt(inventoryEntry.Item.LowID);
            packetWriter.PushInt(inventoryEntry.Item.HighID);
            packetWriter.PushInt(inventoryEntry.Item.Quality);
            packetWriter.PushInt(inventoryEntry.Item.MultipleCount);

            byte[] packet = packetWriter.Finish();

            client.SendCompressed(packet);
        }
        public static void Send(Client client, NonPlayerCharacterClass knubotTarget)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushInt(0x3b132d64);
            packetWriter.PushIdentity(client.Character.Type, client.Character.Id);
            packetWriter.PushByte(0);
            packetWriter.PushShort(2);
            packetWriter.PushIdentity(knubotTarget.Type, knubotTarget.Id);
            packetWriter.PushInt(1);
            packetWriter.PushInt(0);

            byte[] packet = packetWriter.Finish();

            client.SendCompressed(packet);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="packet"></param>
        /// <param name="client"></param>
        public static void Read(byte[] packet, Client client)
        {
            // client got all the needed data and
            // wants to enter the world. After we
            // reply to this, the character will really be in game

            byte[] characterID = BitConverter.GetBytes(client.Character.Id);
            Array.Reverse(characterID);
            PacketWriter packetWriter = new PacketWriter();
            packetWriter.PushByte(0xDF);
            packetWriter.PushByte(0xDF);
            packetWriter.PushShort(10);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushInt(0x570C2039);
            packetWriter.PushIdentity(50000, client.Character.Id);
            packetWriter.PushByte(0);
            byte[] reply = packetWriter.Finish();
            Announce.Playfield(client.Character.PlayField, reply);
            Dynels.GetDynels(client);

            // Mobs get sent whenever player enters playfield, BUT (!) they are NOT synchronized, because the mobs don't save stuff yet.
            // for instance: the waypoints the mob went through will NOT be saved and therefore when you re-enter the PF, it will AGAIN
            // walk the same waypoints.
            //TODO: Fix it
            /*foreach (MobType mob in NPCPool.Mobs)
            {
                //TODO: Make cache - use pf indexing somehow.
                if (mob.pf == client.Character.pf)
                {
                    mob.SendToClient(client);
                }
            }*/
        }
        public static void Send(Client client, VendingMachine vendingMachine)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushByte(0xdf);
            packetWriter.PushByte(0xdf);
            packetWriter.PushShort(0xa);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushInt(0x58362220);
            packetWriter.PushIdentity(vendingMachine.Type, vendingMachine.Id);
            packetWriter.PushByte(1);
            packetWriter.Push3F1Count(vendingMachine.Inventory.Count);
            foreach (InventoryEntries ie in vendingMachine.Inventory)
            {
                packetWriter.PushInt(ie.Item.LowID);
                packetWriter.PushInt(ie.Item.HighID);
                packetWriter.PushInt(ie.Item.Quality);
            }
            byte[] packet = packetWriter.Finish();
            client.SendCompressed(packet);
        }
        public static void Send(Client client, int fromPage, int fromPlacement, Identity toIdentity, int toPlacement)
        {
            PacketWriter packetWriter = new PacketWriter();

            // Send Switch place ACK
            packetWriter.PushByte(0xDF);
            packetWriter.PushByte(0xDF);
            packetWriter.PushShort(0x000a);
            packetWriter.PushShort(0x0001);
            packetWriter.PushShort(0); // LENGTH
            packetWriter.PushInt(3086); // Server ID
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushInt(0x47537a24);
            packetWriter.PushInt(50000);
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushByte(0);
            packetWriter.PushInt(fromPage); // Send Container ID
            packetWriter.PushInt(fromPlacement);
            packetWriter.PushInt(toIdentity.Type);
            packetWriter.PushInt(toIdentity.Instance);
            packetWriter.PushInt(toPlacement); // changed toplacement
            byte[] packet = packetWriter.Finish();
            client.SendCompressed(packet);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="packet"></param>
        /// <param name="client"></param>
        public void Read(ref byte[] packet, Client client)
        {
            PacketReader reader = new PacketReader(ref packet);

            Header header = reader.PopHeader();
            reader.PopByte();
            byte cmd = reader.PopByte();
            Identity target = reader.PopIdentity();
            int unknown = reader.PopInt();
            string CmdStr = "";
            byte CmdByte = 0;

            #region cmd args
            switch (cmd)
            {
                case 1:
                case 7:
                case 9:
                case 13:
                case 17:
                case 19:
                case 20:
                case 21:
                case 23:
                case 24:
                case 25:
                case 26:
                case 27:
                case 28:
                    short CmdStrLen = reader.PopShort();
                    CmdStr = reader.PopString((int)CmdStrLen);
                    break;
                case 10:
                    CmdByte = reader.PopByte();
                    break;
                default:
                    break;
            }
            reader.Finish();
            #endregion
            DataTable dt;
            #region cmd handlers
            switch (cmd)
            {
                #region /org create <name>
                case 1:
                    {
                        // org create
                        /* client wants to create organization
                         * name of org is CmdStr
                         */

                        string SqlQuery = "SELECT * FROM organizations WHERE Name='" + CmdStr + "'";
                        string guild_name = null;
                        uint orgID = 0;
                        dt = ms.ReadDT(SqlQuery);
                        if (dt.Rows.Count > 0)
                        {
                            guild_name = (string)dt.Rows[0]["Name"];
                        }

                        if (guild_name == null)
                        {
                            client.SendChatText("You have created the guild: " + CmdStr);

                            string CurrDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            string SqlQuery2 = "INSERT INTO organizations (Name, creation, LeaderID, GovernmentForm) VALUES ('" + CmdStr + "', '" + CurrDate + "', '" + client.Character.ID + "', '0')";
                            ms.SqlInsert(SqlQuery2);
                            string SqlQuery3 = "SELECT * FROM organizations WHERE Name='" + CmdStr + "'";
                            dt = ms.ReadDT(SqlQuery3);
                            if (dt.Rows.Count > 0)
                            {
                                orgID = (UInt32)dt.Rows[0]["ID"];
                            }

                            // Make sure the order of these next two lines is not swapped -NV
                            client.Character.Stats.ClanLevel.Set(0);
                            client.Character.orgId = orgID;
                            break;
                        }
                        else
                        {
                            client.SendChatText("This guild already <font color=#DC143C>exists</font>");
                            break;
                        }
                    }
                #endregion

                #region /org ranks
                case 2:
                    // org ranks
                    //Displays Org Rank Structure.
                    /* Select governingform from DB, Roll through display from GovForm */
                    if (client.Character.orgId == 0)
                    {
                        client.SendChatText("You're not in an organization!");
                        break;
                    }
                    string ranksSql = "SELECT GovernmentForm FROM organizations WHERE ID = " + client.Character.orgId;
                    int govForm = -1;
                    dt = ms.ReadDT(ranksSql);
                    if (dt.Rows.Count > 0)
                    {
                        govForm = (Int32)dt.Rows[0]["GovernmentForm"];
                    }
                    client.SendChatText("Current Rank Structure: " + GetRankList(govForm));
                    break;
                #endregion

                #region /org contract
                case 3:
                    // org contract
                    break;
                #endregion

                #region unknown org command 4
                case 4:
                    Console.WriteLine("Case 4 Started");
                    break;
                #endregion

                #region /org info
                case 5:
                    {
                        Client tPlayer = null;
                        if (Misc.FindClient.FindClientByID(target.Instance, out tPlayer))
                        {
                            string orgDescription = "", orgObjective = "", orgHistory = "", orgLeaderName = "";
                            int orgGoverningForm = 0, orgLeaderID = 0;
                            dt = ms.ReadDT("SELECT * FROM organizations WHERE ID=" + tPlayer.Character.orgId);

                            if (dt.Rows.Count > 0)
                            {
                                orgDescription = (string)dt.Rows[0]["Description"];
                                orgObjective = (string)dt.Rows[0]["Objective"];
                                orgHistory = (string)dt.Rows[0]["History"];
                                orgGoverningForm = (Int32)dt.Rows[0]["GovernmentForm"];
                                orgLeaderID = (Int32)dt.Rows[0]["LeaderID"];
                            }

                            dt = ms.ReadDT("SELECT Name FROM characters WHERE ID=" + orgLeaderID);
                            if (dt.Rows.Count > 0)
                            {
                                orgLeaderName = (string)dt.Rows[0][0];
                            }

                            string TextGovForm = null;
                            if (orgGoverningForm == 0) { TextGovForm = "Department"; }
                            else if (orgGoverningForm == 1) { TextGovForm = "Faction"; }
                            else if (orgGoverningForm == 2) { TextGovForm = "Republic"; }
                            else if (orgGoverningForm == 3) { TextGovForm = "Monarchy"; }
                            else if (orgGoverningForm == 4) { TextGovForm = "Anarchism"; }
                            else if (orgGoverningForm == 5) { TextGovForm = "Feudalism"; }
                            else { TextGovForm = "Department"; }
                            string orgRank = GetRank(orgGoverningForm, tPlayer.Character.Stats.ClanLevel.StatBaseValue);
                            PacketWriter writer = new PacketWriter();
                            writer.PushBytes(new byte[] { 0xDF, 0xDF });
                            writer.PushShort(10);
                            writer.PushShort(1);
                            writer.PushShort(0);
                            writer.PushInt(3086);
                            writer.PushInt(client.Character.ID);
                            writer.PushInt(0x64582A07);
                            writer.PushIdentity(50000, tPlayer.Character.ID);
                            writer.PushByte(0);
                            writer.PushByte(2);    // OrgServer case 0x02 (Org Info)
                            writer.PushInt(0);
                            writer.PushInt(0);
                            writer.PushInt(0xDEAA); // Type (org)
                            writer.PushUInt(tPlayer.Character.orgId);    // org ID
                            writer.PushShort((short)tPlayer.Character.orgName.Length);
                            writer.PushBytes(Encoding.ASCII.GetBytes(tPlayer.Character.orgName));
                            writer.PushShort((short)orgDescription.Length);
                            writer.PushBytes(Encoding.ASCII.GetBytes(orgDescription));
                            writer.PushShort((short)orgObjective.Length);
                            writer.PushBytes(Encoding.ASCII.GetBytes(orgObjective));
                            writer.PushShort((short)orgHistory.Length);
                            writer.PushBytes(Encoding.ASCII.GetBytes(orgHistory));
                            writer.PushShort((short)TextGovForm.Length);
                            writer.PushBytes(Encoding.ASCII.GetBytes(TextGovForm));
                            writer.PushShort((short)orgLeaderName.Length);
                            writer.PushBytes(Encoding.ASCII.GetBytes(orgLeaderName));
                            writer.PushShort((short)orgRank.Length);
                            writer.PushBytes(Encoding.ASCII.GetBytes(orgRank));
                            writer.Push3F1Count(0);
                            byte[] reply = writer.Finish();

                            client.SendCompressed(reply);
                        }
                    }
                    break;
                #endregion

                #region /org disband
                case 6:
                    break;
                #endregion

                #region /org startvote <text> <duration> <entries>
                case 7:
                    // org startvote <"text"> <duration(minutes)> <entries>
                    // arguments (<text> <duration> and <entries>) are in CmdStr
                    break;
                #endregion

                #region /org vote info
                case 8:
                    // org vote info
                    break;
                #endregion

                #region /org vote <entry>
                case 9:
                    // <entry> is CmdStr
                    break;
                #endregion

                #region /org promote
                case 10:
                    {
                        // some arg in CmdByte. No idea what it is

                        //create the target namespace t_promote
                        Client t_promote = null;
                        string promoteSql = "";
                        int targetOldRank = -1;
                        int targetNewRank = -1;
                        int newPresRank = -1;
                        int oldPresRank = 0;
                        if (Misc.FindClient.FindClientByID(target.Instance, out t_promote))
                        {
                            //First we check if target is in the same org as you
                            if (t_promote.Character.orgId != client.Character.orgId)
                            {
                                //not in same org
                                client.SendChatText("Target is not in your organization!");
                                break;
                            }
                            //Target is in same org, are you eligible to promote?  Promoter Rank has to be TargetRank-2 or == 0
                            if ((client.Character.Stats.ClanLevel.Value == (t_promote.Character.Stats.ClanLevel.Value - 2)) || (client.Character.Stats.ClanLevel.Value == 0))
                            {
                                //Promoter is eligible. Start the process

                                //First we get the details about the org itself
                                promoteSql = "SELECT * FROM organizations WHERE ID = " + client.Character.orgId;
                                dt = ms.ReadDT(promoteSql);

                                int promoteGovForm = -1;
                                string promotedToRank = "";
                                string demotedFromRank = "";

                                if (dt.Rows.Count > 0)
                                {
                                    promoteGovForm = (Int32)dt.Rows[0]["GovernmentForm"];
                                }

                                //Check if new rank == 0, if so, demote promoter
                                if ((targetOldRank - 1) == 0)
                                {
                                    /* This is a bit more complex.  Here we need to promote new president first
                                         * then we go about demoting old president
                                         * finally we set the new leader in SQL
                                         * Reset OrgName to set changes
                                         */

                                    // Set new President's Rank
                                    targetOldRank = t_promote.Character.Stats.ClanLevel.Value;
                                    targetNewRank = targetOldRank - 1;
                                    promotedToRank = GetRank(promoteGovForm, (uint)targetNewRank);
                                    t_promote.Character.Stats.ClanLevel.Set(targetNewRank);
                                    // Demote the old president
                                    oldPresRank = client.Character.Stats.ClanLevel.Value;
                                    newPresRank = oldPresRank + 1;
                                    demotedFromRank = GetRank(promoteGovForm, (uint)newPresRank);
                                    client.Character.Stats.ClanLevel.Set(newPresRank);
                                    //Change the leader id in SQL
                                    string newLeadSql = "UPDATE organizations SET LeaderID = " + t_promote.Character.ID + " WHERE ID = " + t_promote.Character.orgId;
                                    ms.SqlUpdate(newLeadSql);
                                    client.SendChatText("You've passed leadership of the organization to: " + t_promote.Character.Name);
                                    t_promote.SendChatText("You've been promoted to the rank of " + promotedToRank + " by " + client.Character.Name);
                                    break;
                                }
                                else
                                {
                                    //Just Promote
                                    targetOldRank = t_promote.Character.Stats.ClanLevel.Value;
                                    targetNewRank = targetOldRank - 1;
                                    promotedToRank = GetRank(promoteGovForm, (uint)targetNewRank);
                                    t_promote.Character.Stats.ClanLevel.Set(targetNewRank);
                                    client.SendChatText("You've promoted " + t_promote.Character.Name + " to " + promotedToRank);
                                    t_promote.SendChatText("You've been promoted to the rank of " + promotedToRank + " by " + client.Character.Name);
                                }
                            }
                            else
                            {
                                //Promoter not eligible to promote
                                client.SendChatText("Your Rank is not high enough to promote " + t_promote.Character.Name);
                                break;
                            }
                        }
                        break;
                    }
                #endregion

                #region /org demote
                case 11:
                    // demote target player
                    //create the target namespace t_demote
                    Client t_demote = null;
                    string demoteSql = "";
                    int targetCurRank = -1;
                    int targetNewerRank = -1;
                    if (Misc.FindClient.FindClientByID(target.Instance, out t_demote))
                    {
                        //First we check if target is in the same org as you
                        if (t_demote.Character.orgId != client.Character.orgId)
                        {
                            //not in same org
                            client.SendChatText("Target is not in your organization!");
                            break;
                        }
                        //Target is in same org, are you eligible to demote?  Promoter Rank has to be TargetRank-2 or == 0
                        if ((client.Character.Stats.GmLevel.Value == (t_demote.Character.Stats.ClanLevel.Value - 2)) || (client.Character.Stats.ClanLevel.Value == 0))
                        {
                            //Promoter is eligible. Start the process

                            //First we get the details about the org itself
                            demoteSql = "SELECT GovernmentForm FROM organizations WHERE ID = " + client.Character.orgId;
                            dt = ms.ReadDT(demoteSql);
                            int demoteGovForm = -1;
                            string demotedToRank = "";
                            if (dt.Rows.Count > 0)
                            {
                                demoteGovForm = (Int32)dt.Rows[0]["GovernmentForm"];
                            }

                            //Check whether new rank would be lower than lowest for current govform
                            if ((targetCurRank + 1) > GetLowestRank(demoteGovForm))
                            {
                                client.SendChatText("You can't demote character any lower!");
                                break;
                            }
                            targetCurRank = t_demote.Character.Stats.GmLevel.Value;
                            targetNewerRank = targetCurRank + 1;
                            demotedToRank = GetRank(demoteGovForm, (uint)targetNewerRank);
                            t_demote.Character.Stats.ClanLevel.Set(targetNewerRank);
                            client.SendChatText("You've demoted " + t_demote.Character.Name + " to " + demotedToRank);
                            t_demote.SendChatText("You've been demoted to the rank of " + demotedToRank + " by " + client.Character.Name);
                            break;
                        }
                        else
                        {
                            //Promoter not eligible to promote
                            client.SendChatText("Your Rank is not high enough to demote " + t_demote.Character.Name);
                            break;
                        }
                    }
                    break;
                #endregion

                #region unknown org command 12
                case 12:
                    Console.WriteLine("Case 12 Started");
                    break;
                #endregion

                #region /org kick <name>
                case 13:
                    // kick <name> from org
                    // <name> is CmdStr

                    //create the t_player Client namespace, using CmdStr to find character id, in replacement of target.Instance
                    uint kickedFrom = client.Character.orgId;
                    string kickeeSql = "SELECT * FROM characters WHERE Name = '" + CmdStr + "'";
                    int kickeeId = 0;
                    dt = ms.ReadDT(kickeeSql);
                    if (dt.Rows.Count > 0)
                    {
                        kickeeId = (Int32)dt.Rows[0]["ID"];
                    }

                    Client target_player = null;
                    if (Misc.FindClient.FindClientByID(kickeeId, out target_player))
                    {
                        //Check if CmdStr is actually part of the org
                        uint kickeeOrgId = target_player.Character.orgId;
                        if (kickeeOrgId != client.Character.orgId)
                        {
                            //Not part of Org. break out.
                            client.SendChatText(CmdStr + "is not a member of your organization!");
                            break;
                        }

                        //They are part of the org, so begin the processing...
                        //First we check if the player is online...
                        string onlineSql = "SELECT online FROM characters WHERE ID = " + client.Character.ID;
                        dt = ms.ReadDT(onlineSql);
                        int onlineStatus = 0;
                        if (dt.Rows.Count > 0)
                        {
                            onlineStatus = (Int32)dt.Rows[0][0];
                        }

                        if (onlineStatus == 0)
                        {
                            //Player isn't online. Org Kicks are processed in a different method
                            break;
                        }

                        //Player is online. Start the kick.
                        target_player.Character.Stats.ClanLevel.Set(0);
                        target_player.Character.orgId = 0;
                        string kickedFromSql = "SELECT Name FROM organizations WHERE ID = " + client.Character.orgId;
                        dt = ms.ReadDT(kickedFromSql);
                        string KickedFromName = "";
                        if (dt.Rows.Count > 0)
                        {
                            KickedFromName = (string)dt.Rows[0][0];
                        }
                        target_player.SendChatText("You've been kicked from the organization " + KickedFromName);

                    }
                    break;
                #endregion

                #region /org invite
                case 14:
                    {
                        Client t_player = null;
                        if (Misc.FindClient.FindClientByID(target.Instance, out t_player))
                        {
                            PacketWriter writer = new PacketWriter();
                            writer.PushBytes(new byte[] { 0xDF, 0xDF });
                            writer.PushShort(10);
                            writer.PushShort(1);
                            writer.PushShort(0);
                            writer.PushInt(3086); //Sender
                            writer.PushInt(t_player.Character.ID);  //Receiver
                            writer.PushInt(0x64582A07); //Packet ID
                            writer.PushIdentity(50000, t_player.Character.ID); //Target Identity
                            writer.PushByte(0);
                            writer.PushByte(5); //OrgServer Case 0x05 (Invite)
                            writer.PushInt(0);
                            writer.PushInt(0);
                            writer.PushIdentity(0xDEAA, (int)client.Character.orgId); // Type (org)
                            writer.PushShort((short)client.Character.orgName.Length);
                            writer.PushBytes(Encoding.ASCII.GetBytes(client.Character.orgName));
                            writer.PushInt(0);
                            byte[] reply = writer.Finish();

                            t_player.SendCompressed(reply);
                        }
                    }
                    break;
                #endregion

                #region Org Join
                case 15:
                    {
                        //target.Instance holds the OrgID of the Org wishing to be joined.
                        int orgIdtoJoin = target.Instance;
                        string JoinSql = "SELECT * FROM organizations WHERE ID = '" + orgIdtoJoin + "' LIMIT 1";
                        int gov_form = 0;
                        dt = ms.ReadDT(JoinSql);
                        if (dt.Rows.Count > 0)
                        {
                            gov_form = (Int32)dt.Rows[0]["GovernmentForm"];
                        }

                        // Make sure the order of these next two lines is not swapped -NV
                        client.Character.Stats.ClanLevel.Set(GetLowestRank(gov_form));
                        client.Character.orgId = (uint)orgIdtoJoin;
                    }
                    break;
                #endregion

                #region /org leave
                case 16:
                    // org leave
                    // TODO: Disband org if it was leader that left org. -Suiv-
                    // I don't think a Disband happens if leader leaves. I don't think leader -can- leave without passing lead to another
                    // Something worth testing on Testlive perhaps ~Chaz
                    // Just because something happens on TL, doesnt mean its a good idea. Really tbh id prefer it if you had to explicitly type /org disband to disband rather than /org leave doing it... -NV
                    // Agreeing with NV.  Org Leader can't leave without passing lead on.  org disband requires /org disband to specifically be issued, with a Yes/No box.
                    string LeaveSql = "SELECT * FROM organizations WHERE ID = " + client.Character.orgId;
                    int govern_form = 0;
                    dt = ms.ReadDT(LeaveSql);
                    if (dt.Rows.Count > 0)
                    {
                        govern_form = (Int32)dt.Rows[0]["GovernmentForm"];
                    }

                    if ((client.Character.Stats.ClanLevel.Value == 0) && (govern_form != 4))
                    {
                        client.SendChatText("Organization Leader cannot leave organization without Disbanding or Passing Leadership!");
                    }
                    else
                    {
                        client.Character.orgId = 0;
                        client.SendChatText("You left the guild");

                    }
                    break;
                #endregion

                #region /org tax | /org tax <tax>
                case 17:
                    // gets or sets org tax
                    // <tax> is CmdStr
                    // if no <tax>, then just send chat text with current tax info

                    if (CmdStr == null)
                    {
                        client.SendChatText("The current organization tax rate is: ");
                        break;
                    }
                    else
                    {
                        break;
                    }
                #endregion

                #region /org bank
                case 18:
                    {
                        // org bank
                        dt = ms.ReadDT("SELECT * FROM organizations WHERE ID=" + client.Character.orgId);
                        if (dt.Rows.Count > 0)
                        {
                            UInt64 bank_credits = (UInt64)dt.Rows[0]["Bank"];
                            client.SendChatText("Your bank has " + bank_credits + " credits in its account");
                        }
                    }
                    break;
                #endregion

                #region /org bank add <cash>
                case 19:
                    {
                        if (client.Character.orgId == 0)
                        {
                            client.SendChatText("You are not in an organisation.");

                            break;
                        }

                        // org bank add <cash>
                        int minuscredits_fromplayer = Convert.ToInt32(CmdStr);
                        int characters_credits = client.Character.Stats.Cash.Value;

                        if (characters_credits < minuscredits_fromplayer)
                        {
                            client.SendChatText("You do not have enough Credits");
                        }
                        else
                        {
                            int total_Creditsspent = characters_credits - minuscredits_fromplayer;
                            client.Character.Stats.Cash.Set(total_Creditsspent);

                            ms.SqlUpdate("UPDATE `organizations` SET `Bank` = `Bank` + " + minuscredits_fromplayer + " WHERE `ID` = " + client.Character.orgId);
                            client.SendChatText("You have donated " + minuscredits_fromplayer + " to the organization");
                        }
                    }

                    break;
                #endregion

                #region /org bank remove <cash>
                case 20:
                    // org bank remove <cash>
                    // <cash> is CmdStr
                    // player wants to take credits from org bank
                    // only leader can do that
                    if ((client.Character.Stats.ClanLevel.Value != 0) || (client.Character.orgId == 0))
                    {
                        client.SendChatText("You're not the leader of an Organization");
                        break;
                    }
                    int remove_credits = Convert.ToInt32(CmdStr);
                    long org_bank = 0;
                    dt = ms.ReadDT("SELECT Bank FROM organizations WHERE ID = " + client.Character.orgId);
                    if (dt.Rows.Count > 0)
                    {
                        org_bank = (Int64)dt.Rows[0][0];
                    }
                    if (remove_credits > org_bank)
                    {
                        client.SendChatText("Not enough credits in Organization Bank!");
                        break;
                    }
                    else
                    {
                        long neworgbank = org_bank - remove_credits;
                        int existingcreds = 0;
                        existingcreds = client.Character.Stats.Cash.Value;
                        int newcreds = existingcreds + remove_credits;
                        ms.SqlUpdate("UPDATE organizations SET Bank = " + neworgbank + " WHERE ID = " + client.Character.orgId);
                        client.Character.Stats.Cash.Set(newcreds);
                        client.SendChatText("You've removed " + remove_credits + " credits from the organization bank");
                    }
                    break;
                #endregion

                #region /org bank paymembers <cash>
                case 21:
                    // <cash> is CmdStr
                    // give <cash> credits to every org member
                    // credits are taken from org bank
                    // only leader can do it
                    break;
                #endregion

                #region /org debt
                case 22:
                    // send player text about how big is his/her tax debt to org
                    break;
                #endregion

                #region /org history <text>
                case 23:
                    {
                        if (client.Character.Stats.ClanLevel.Value == 0)
                        {
                            // org history <history text>
                            ms.SqlUpdate("UPDATE organizations SET history = '" + CmdStr + "' WHERE ID = '" + client.Character.orgId + "'");
                            client.SendChatText("History Updated");
                        }
                        else { client.SendChatText("You must be the Organization Leader to perform this command!"); }
                    }
                    break;
                #endregion

                #region /org objective <text>
                case 24:
                    {
                        if (client.Character.Stats.ClanLevel.Value == 0)
                        {
                            // org objective <objective text>
                            ms.SqlUpdate("UPDATE organizations SET objective = '" + CmdStr + "' WHERE ID = '" + client.Character.orgId + "'");
                            client.SendChatText("Objective Updated");
                        }
                        else { client.SendChatText("You must be the Organization Leader to perform this command!"); }
                    }
                    break;
                #endregion

                #region /org description <text>
                case 25:
                    {
                        if (client.Character.Stats.ClanLevel.Value == 0)
                        {
                            // org description <description text>
                            ms.SqlUpdate("UPDATE organizations SET description = '" + CmdStr + "' WHERE ID = '" + client.Character.orgId + "'");
                            client.SendChatText("Description Updated");
                        }
                        else { client.SendChatText("You must be the Organization Leader to perform this command!"); }
                    }
                    break;
                #endregion

                #region /org name <text>
                case 26:
                    {
                        // org name <name>
                        /* Renames Organization
                         * Checks for Existing Orgs with similar name to stop crash
                         * Chaz
                         */
                        if (client.Character.Stats.ClanLevel.Value == 0)
                        {
                            string SqlQuery26 = "SELECT * FROM organizations WHERE Name LIKE '" + CmdStr + "' LIMIT 1";
                            string CurrentOrg = null;
                            dt = ms.ReadDT(SqlQuery26);
                            if (dt.Rows.Count > 0)
                            {
                                CurrentOrg = (string)dt.Rows[0]["Name"];
                            }

                            if (CurrentOrg == null)
                            {
                                string SqlQuery27 = "UPDATE organizations SET Name = '" + CmdStr + "' WHERE ID = '" + client.Character.orgId + "'";
                                ms.SqlUpdate(SqlQuery27);
                                client.SendChatText("Organization Name Changed to: " + CmdStr);

                                // Forces reloading of org name and the like
                                // XXXX TODO: Make it reload for all other members in the org
                                client.Character.orgId = client.Character.orgId;
                                break;
                            }
                            else
                            {
                                client.SendChatText("An Organization already exists with that name");
                                break;
                            }
                        }
                        else { client.SendChatText("You must be the organization leader to perform this command!"); }
                        break;
                    }
                #endregion

                #region /org governingform <text>
                case 27:
                    {
                        // org governingform <form>
                        /* Current Governing Forms:
                         * Department, Faction, Republic, Monarchy, Anarchism, Feudalism
                         */
                        //Check on whether your President or not
                        if (client.Character.Stats.ClanLevel.Value == 0)
                        {
                            //first we drop the case on the input, just to be sure.
                            Int32 GovFormNum = -1;
                            if (CmdStr == null)
                            {
                                //list gov forms
                                client.SendChatText("List of Accepted Governing Forms is: department, faction, republic, monarchy, anarchism, feudalism");
                                break;
                            }
                            //was correct input passed?
                            switch (CmdStr.ToLower())
                            {
                                case "department":
                                    GovFormNum = 0;
                                    break;
                                case "faction":
                                    GovFormNum = 1;
                                    break;
                                case "republic":
                                    GovFormNum = 2;
                                    break;
                                case "monarchy":
                                    GovFormNum = 3;
                                    break;
                                case "anarchism":
                                    GovFormNum = 4;
                                    break;
                                case "feudalism":
                                    GovFormNum = 5;
                                    break;
                                default:
                                    client.SendChatText(CmdStr + " Is an invalid Governing Form!");
                                    client.SendChatText("Accepted Governing Forms are: department, faction, republic, monarchy, anarchism, feudalism");
                                    break;
                            }
                            if (GovFormNum != -1)
                            {
                                ms.SqlUpdate("UPDATE organizations SET GovernmentForm = '" + GovFormNum + "' WHERE ID = '" + client.Character.orgId + "'");
                                foreach (int currentCharId in OrgMisc.GetOrgMembers(client.Character.orgId, true))
                                {
                                    client.Character.Stats.ClanLevel.Set(GetLowestRank(GovFormNum));
                                }
                                client.SendChatText("Governing Form is now: " + CmdStr);
                                break;
                            }
                        }
                        else
                        {
                            //Haha! You're not the org leader!
                            client.SendChatText("You must be the Org Leader to perform this command");
                            break;
                        }
                    }
                    break;
                #endregion

                #region /org stopvote <text>
                case 28:
                    // <text> is CmdStr
                    break;
                #endregion

                #region unknown command
                default:
                    break;
                #endregion
            }
            #endregion

            reader.Finish();
        }
        public static void SendPlayfield(Client client, Character character)
        {
            // ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
            // ## Weapon Item Full Update - Test ## Do Not attempt to send this packet until all values have been Verified ##
            // ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##

            // notes: one packet has to be sent for each hand!!!!!!!
            // Packet only sent when Weapons are in Posession!

            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushByte(0xDF); // starter
            packetWriter.PushByte(0xDF); // starter
            packetWriter.PushShort(0xA);
            packetWriter.PushShort(0x1);
            packetWriter.PushShort(0); // Length
            packetWriter.PushInt(3086); // Sender
            packetWriter.PushInt(character.Id); // Reciever
            packetWriter.PushInt(0x3B1D2268); // Packet ID
            packetWriter.PushInt(0xC74A); // .... Weapon maybe?
            packetWriter.PushInt(0x45AEE789); // ID
            packetWriter.PushByte(0);
            packetWriter.PushInt(0xB);
            packetWriter.PushInt(0xC350);
            packetWriter.PushInt(character.Id); // Reciever
            packetWriter.PushInt(0xC0A);
            packetWriter.PushInt(0xF424F); // constant
            packetWriter.PushShort(0);
            packetWriter.PushInt(0x6); // Placement/Location Maybe?
            packetWriter.PushInt(0x1F88); // Another Constant

            // Stat 1
            packetWriter.PushInt(0); // constant Flags
            packetWriter.PushUInt(0x403); // Item Flag
            // Stat 2
            packetWriter.PushInt(0x17); // constant Static Instance?
            packetWriter.PushInt(0x1e6d0); // Weapon ITEM ID
            // Stat 3
            packetWriter.PushInt(0x2BD); //constant ACG Item Level
            packetWriter.PushInt(0x5); // Quality-Level
            // Stat 4
            packetWriter.PushInt(0x2BE); // constant ACGItemTemplateID
            packetWriter.PushInt(0x1e6d0); // Weapon ITEM ID (low ID)
            // Stat 5
            packetWriter.PushInt(0x2BF); // constant ACGItemTemplateID2
            packetWriter.PushInt(0x1e6d1); // High ID
            // Stat 6
            packetWriter.PushInt(0x19C); // Constant MultipleCount
            packetWriter.PushInt(0x1); // Amount
            // Stat 7
            packetWriter.PushInt(0x1A); // Constant Energy
            packetWriter.PushUInt(0x28); // Ammo
            // End
            packetWriter.PushInt(0); // Empty fill

            byte[] packet = packetWriter.Finish();
            client.SendCompressed(packet);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="packet"></param>
        /// <param name="client"></param>
        public static void Read(byte[] packet, Client client)
        {
            PacketWriter packetWriter = new PacketWriter();
            PacketReader packetReader = new PacketReader(packet);

            Header header = packetReader.PopHeader();
            byte unknown1 = packetReader.PopByte();
            uint[] nanodelta = { 3, 3, 4, 2 };
            uint[] healdelta = { 3, 3, 2, 4 };

            uint baseIP = 0;
            uint characterLevel;

            characterLevel = client.Character.Stats.Level.StatBaseValue; // 54 = Level

            // Calculate base IP value for character level
            if (characterLevel > 204)
            {
                baseIP += (characterLevel - 204) * 600000;
                characterLevel = 204;
            }
            if (characterLevel > 189)
            {
                baseIP += (characterLevel - 189) * 150000;
                characterLevel = 189;
            }
            if (characterLevel > 149)
            {
                baseIP += (characterLevel - 149) * 80000;
                characterLevel = 149;
            }
            if (characterLevel > 99)
            {
                baseIP += (characterLevel - 99) * 40000;
                characterLevel = 99;
            }
            if (characterLevel > 49)
            {
                baseIP += (characterLevel - 49) * 20000;
                characterLevel = 49;
            }
            if (characterLevel > 14)
            {
                baseIP += (characterLevel - 14) * 10000; // Change 99 => 14 by Wizard
                characterLevel = 14;
            }
            baseIP += 1500 + (characterLevel - 1) * 4000;

            // Prepare reply packet

            packetWriter.PushByte(0xDF);
            packetWriter.PushByte(0xDF);
            packetWriter.PushShort(0x0a);
            packetWriter.PushShort(0x01);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(header.Sender);
            packetWriter.PushInt(0x3e205660);
            packetWriter.PushIdentity(50000, header.Sender);
            packetWriter.PushByte(0);

            int count = packetReader.PopInt();
            uint statval;
            List<int> statlist = new List<int>();
            while (count > 0)
            {
                int statNumber = packetReader.PopInt();
                statval = packetReader.PopUInt();
                client.Character.Stats.SetBaseValue(statNumber, statval);
                statlist.Add(statNumber);
                count--;
            }

            packetReader.Finish();

            statlist.Add(53); // IP
            uint usedIP = baseIP - (uint)Math.Floor(CalculateIP(client));
            client.Character.Stats.IP.StatBaseValue = usedIP;

            // Send the changed stats back to the client
            packetWriter.PushInt(statlist.Count);
            count = 0;
            while (count < statlist.Count)
            {
                statval = client.Character.Stats.GetBaseValue(statlist[count]);
                packetWriter.PushInt(statlist[count]);
                packetWriter.PushUInt(statval);
                count++;
            }

            byte[] reply = packetWriter.Finish();
            client.SendCompressed(reply);

            // and save the changes to the statsdb
            client.Character.WriteStats();
            client.Character.CalculateSkills();
        }
        public static void SendStat(Client client, int statnum, int value, Boolean announce)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.PushBytes(new byte[] { 0xDF, 0xDF, });
            packetWriter.PushShort(10);
            packetWriter.PushShort(1);
            packetWriter.PushShort(0);
            packetWriter.PushInt(3086);
            packetWriter.PushInt(client.Character.Id);
            packetWriter.PushInt(0x2B333D6E);
            packetWriter.PushIdentity(50000, client.Character.Id);
            packetWriter.PushByte(1);
            packetWriter.PushInt(1);
            packetWriter.PushInt(statnum);
            packetWriter.PushInt(value);

            byte[] reply = packetWriter.Finish();
            client.SendCompressed(reply);

            /* announce to playfield? */
            if (announce)
            {
                Announce.Playfield(client.Character.PlayField, reply);
            }
        }