Esempio n. 1
0
        public static void SaveServerIP(string pIP, ushort pPort, byte pWorldID, byte pChannelID)
        {
            using (InsertQueryBuilder iqb = new InsertQueryBuilder("server_ips"))
            {
                iqb.OnDuplicateUpdate = true;
                iqb.AddColumns(false, "ip", "port");
                iqb.AddColumns(true, "world_id", "channel_id");

                iqb.AddRow(pIP, pPort, pWorldID, pChannelID);

                iqb.RunQuery();
            }
        }
Esempio n. 2
0
        public void StoreInfo(string pIP, byte[] pMachineID, int pCharacterID, byte pWorldID)
        {
            using (InsertQueryBuilder iqb = new InsertQueryBuilder("session_restarts"))
            {
                byte[] encoded_machineid = new byte[16];

                for (int i = 0; i < 16; i++)
                {
                    encoded_machineid[i] = (byte)(pMachineID[i] ^ (byte)(pCharacterID * pWorldID));
                }

                iqb.AddColumn("machine_id");
                iqb.AddColumn("ip", true);
                iqb.AddColumns(true, "character_id", "world_id");

                iqb.OnDuplicateUpdate = true;

                iqb.AddRow(encoded_machineid, pIP, pCharacterID, pWorldID);

                iqb.RunQuery();
            }

            Cache.Add(new RestartInfo
            {
                CharacterID = pCharacterID,
                IP          = pIP,
                WorldID     = pWorldID,
                MachineID   = pMachineID
            });
        }
Esempio n. 3
0
        public void Save(bool pReset, bool pClean = true)
        {
            if (IsFake)
            {
                return;
            }

            Logger_WriteLine("Trying to save...");
            if (_exporter != null)
            {
                // Try to get logfile ID from DB

                if (!Directory.Exists("sessions"))
                {
                    Directory.CreateDirectory("sessions");
                }

                string filename = "sessions/Savefile_";
                int    packets  = _exporter.GetSize();
                try
                {
                    using (InsertQueryBuilder iqb = new InsertQueryBuilder("session_logfiles"))
                    {
                        iqb.AddColumns(false, "id", "information", "at");
                        iqb.AddRow(null, packets + " > " + LogFilename, new MySQL_Connection.NowType());
                        iqb.RunQuery();

                        filename += MySQL_Connection.Instance.GetLastInsertId();
                    }
                }
                catch
                {
                    filename += "NOMYSQL_" + MasterThread.CurrentDate.ToFileTime();
                }
                filename += ".msb";

                Logger_WriteLine("Saving packets to '{0}' ({1} packets logged)", filename, _exporter.GetSize());
                _exporter.Save(filename, ServerMapleInfo.VERSION, base.HostEndPoint, base.ClientEndPoint);
                if (pReset)
                {
                    _exporter = new MSBExporter();
                }
                else if (pClean)
                {
                    _exporter = null;
                }
            }
        }
Esempio n. 4
0
        public static void Initialize()
        {
            Instance = new CommandHandler()
            {
                CommandHandlers = new Dictionary<string,Action<ClientConnection, string[]>>()
            };

            Instance.CommandHandlers.Add("report", (pConnection, pArguments) =>
            {
                return; // Disabled


                if (pArguments.Length == 1)
                {
                    string name = pArguments[0];
                    if (name.Length > 12) return;

                    bool found = false;
                    string query = "SELECT name FROM character_views WHERE name LIKE '" + MySql.Data.MySqlClient.MySqlHelper.EscapeString(name) + "%' AND mapid = " + pConnection.CharData.Stats.MapID;
                    using (var result = MySQL_Connection.Instance.RunQuery(query) as MySql.Data.MySqlClient.MySqlDataReader)
                    {
                        if (result.HasRows && result.Read())
                        {
                            name = result.GetString(0);
                            found = true;
                        }
                        else
                        {
                            pConnection.Logger_WriteLine("Could not find {0} for {1}", name, pConnection.CharData.Stats.Name);
                        }
                    }

                    if (found)
                    {
                        using (InsertQueryBuilder iqb = new InsertQueryBuilder("reports"))
                        {
                            iqb.AddColumns(false, "id", "name", "reported_by", "reported_when", "mapid", "screenshot");
                            iqb.AddRow(null, name, pConnection.CharacterInternalID, new MySQL_Connection.NowType(), pConnection.CharData.Stats.MapID, null);

                            int result = (int)MySQL_Connection.Instance.RunQuery(iqb.ToString());
                            if (result != 0)
                            {
                                pConnection.Logger_WriteLine("Reported {0} (by {1}). Requesting Screenshot...", name, pConnection.CharData.Stats.Name);

                                using (MaplePacket pack = new MaplePacket(MaplePacket.CommunicationType.ServerPacket, (ushort)0xEEFE))
                                {
                                    pack.WriteString("http://mapler.me/actions/upload_report.php");
                                    pack.WriteInt(MySQL_Connection.Instance.GetLastInsertId());
                                    pack.SwitchOver();
                                    pConnection.SendPacket(pack);
                                }
                            }
                            else
                            {
                                pConnection.Logger_WriteLine("Report FAIL {0} (by {1})", name, pConnection.CharData.Stats.Name);
                            }
                        }

                    }
                }
            });
        }
Esempio n. 5
0
        public static void SaveItem(ClientConnection pConnection, ushort pInventory, short pSlot, ItemBase pItem, InsertQueryBuilder itemsTable, bool pDontSetChecksum = false)
        {
            if (!itemsTable.HasColumns)
            {
                itemsTable.OnDuplicateUpdate = true;
                itemsTable.AddColumn("character_id", false);
                itemsTable.AddColumn("itemid", true);
                itemsTable.AddColumn("inventory", false);
                itemsTable.AddColumn("slot", false);
                itemsTable.AddColumn("checksum", true);
                itemsTable.AddColumns(true,
                                      "cashid", "amount", "expires", "slots", "scrolls",
                                      "str", "dex", "int", "luk", "maxhp", "maxmp",
                                      "weaponatt", "weapondef", "magicatt", "magicdef", "acc", "avo", "hands", "jump", "speed", "name", "flags",
                                      "itemlevel", "itemexp",
                                      "hammers",
                                      "battlemodedmg",
                                      "statusflag",
                                      "potential1", "potential2", "potential3",
                                      "potential4", "potential5", "potential6",
                                      "display_id",
                                      "socketstate",
                                      "nebulite1", "nebulite2", "nebulite3",
                                      "uniqueid",
                                      "bagid",
                                      "moreflags",
                                      "durability",
                                      "max_scissors"
                                      );
            }

            int checksum = pItem.GetChecksum();


            if (pItem is ItemEquip)
            {
                var equip = pItem as ItemEquip;

                itemsTable.AddRow(
                    pConnection.CharacterInternalID,
                    equip.ItemID,
                    pInventory,
                    pSlot,
                    checksum,
                    equip.CashID,
                    equip.Amount,
                    equip.Expires,
                    equip.Slots, equip.Scrolls,
                    equip.Str, equip.Dex, equip.Int, equip.Luk,
                    equip.HP, equip.MP,
                    equip.Watk, equip.Wdef, equip.Matk, equip.Mdef,
                    equip.Acc, equip.Avo, equip.Hands, equip.Jump, equip.Speed,
                    equip.Name, equip.Flags,
                    equip.ItemLevel, equip.ItemEXP,
                    equip.ViciousHammer,
                    equip.BattleModeDamage,
                    equip.StatusFlags,
                    equip.Potential1, equip.Potential2, equip.Potential3,
                    equip.Potential4, equip.Potential5, equip.Potential6,
                    equip.DisplayID,
                    equip.SocketState,
                    equip.Nebulite1, equip.Nebulite2, equip.Nebulite3,
                    equip.UniqueID,
                    pItem.BagID,
                    equip.SetFlags,
                    equip.Durability,
                    equip.MaxScissors
                    );
            }
            else
            {
                string name  = pItem is ItemRechargable ? (pItem as ItemRechargable).CraftName : null;
                int    flags = pItem is ItemRechargable ? (pItem as ItemRechargable).Flags : 0;

                itemsTable.AddRow(
                    pConnection.CharacterInternalID,
                    pItem.ItemID,
                    pInventory,
                    pSlot,
                    checksum,
                    pItem.CashID,
                    pItem.Amount,
                    pItem.Expires,
                    null, null,
                    null, null, null, null,
                    null, null,
                    null, null, null, null,
                    null, null, null, null, null,
                    name, flags,
                    null, null,
                    null,
                    null,
                    null,
                    null, null, null,
                    null, null, null,
                    null,
                    null,
                    null, null, null,
                    pItem is ItemRechargable ? (pItem as ItemRechargable).UniqueID : 0,
                    pItem.BagID,
                    null,
                    null,
                    null
                    );
            }

            if (!pDontSetChecksum)
            {
                AccountDataCache.Instance.SetChecksumOfSlot(pConnection.CharacterID, pConnection.WorldID, pInventory, pSlot, checksum);
            }
        }
Esempio n. 6
0
        public static void Initialize()
        {
            Instance = new CommandHandler()
            {
                CommandHandlers = new Dictionary <string, Action <ClientConnection, string[]> >()
            };

            Instance.CommandHandlers.Add("report", (pConnection, pArguments) =>
            {
                return; // Disabled


                if (pArguments.Length == 1)
                {
                    string name = pArguments[0];
                    if (name.Length > 12)
                    {
                        return;
                    }

                    bool found   = false;
                    string query = "SELECT name FROM character_views WHERE name LIKE '" + MySql.Data.MySqlClient.MySqlHelper.EscapeString(name) + "%' AND mapid = " + pConnection.CharData.Stats.MapID;
                    using (var result = MySQL_Connection.Instance.RunQuery(query) as MySql.Data.MySqlClient.MySqlDataReader)
                    {
                        if (result.HasRows && result.Read())
                        {
                            name  = result.GetString(0);
                            found = true;
                        }
                        else
                        {
                            pConnection.Logger_WriteLine("Could not find {0} for {1}", name, pConnection.CharData.Stats.Name);
                        }
                    }

                    if (found)
                    {
                        using (InsertQueryBuilder iqb = new InsertQueryBuilder("reports"))
                        {
                            iqb.AddColumns(false, "id", "name", "reported_by", "reported_when", "mapid", "screenshot");
                            iqb.AddRow(null, name, pConnection.CharacterInternalID, new MySQL_Connection.NowType(), pConnection.CharData.Stats.MapID, null);

                            int result = (int)MySQL_Connection.Instance.RunQuery(iqb.ToString());
                            if (result != 0)
                            {
                                pConnection.Logger_WriteLine("Reported {0} (by {1}). Requesting Screenshot...", name, pConnection.CharData.Stats.Name);

                                using (MaplePacket pack = new MaplePacket(MaplePacket.CommunicationType.ServerPacket, (ushort)0xEEFE))
                                {
                                    pack.WriteString("http://mapler.me/actions/upload_report.php");
                                    pack.WriteInt(MySQL_Connection.Instance.GetLastInsertId());
                                    pack.SwitchOver();
                                    pConnection.SendPacket(pack);
                                }
                            }
                            else
                            {
                                pConnection.Logger_WriteLine("Report FAIL {0} (by {1})", name, pConnection.CharData.Stats.Name);
                            }
                        }
                    }
                }
            });
        }
        public void StoreInfo(string pIP, byte[] pMachineID, int pCharacterID, byte pWorldID)
        {
            using (InsertQueryBuilder iqb = new InsertQueryBuilder("session_restarts"))
            {
                byte[] encoded_machineid = new byte[16];

                for (int i = 0; i < 16; i++)
                {
                    encoded_machineid[i] = (byte)(pMachineID[i] ^ (byte)(pCharacterID * pWorldID));
                }

                iqb.AddColumn("machine_id");
                iqb.AddColumn("ip", true);
                iqb.AddColumns(true, "character_id", "world_id");

                iqb.OnDuplicateUpdate = true;

                iqb.AddRow(encoded_machineid, pIP, pCharacterID, pWorldID);

                iqb.RunQuery();
            }

            Cache.Add(new RestartInfo
            {
                CharacterID = pCharacterID,
                IP = pIP,
                WorldID = pWorldID,
                MachineID = pMachineID
            });
        }
Esempio n. 8
0
        public void Save(bool pReset, bool pClean = true)
        {
            if (IsFake) return;

            Logger_WriteLine("Trying to save...");
            if (_exporter != null)
            {
                // Try to get logfile ID from DB

                if (!Directory.Exists("sessions"))
                    Directory.CreateDirectory("sessions");

                string filename = "sessions/Savefile_";
                int packets = _exporter.GetSize();
                try
                {
                    using (InsertQueryBuilder iqb = new InsertQueryBuilder("session_logfiles"))
                    {
                        iqb.AddColumns(false, "id", "information", "at");
                        iqb.AddRow(null, packets + " > " + LogFilename, new MySQL_Connection.NowType());
                        iqb.RunQuery();

                        filename += MySQL_Connection.Instance.GetLastInsertId();
                    }
                }
                catch
                {
                    filename += "NOMYSQL_" + MasterThread.CurrentDate.ToFileTime();
                }
                filename += ".msb";

                Logger_WriteLine("Saving packets to '{0}' ({1} packets logged)", filename, _exporter.GetSize());
                _exporter.Save(filename, ServerMapleInfo.VERSION, base.HostEndPoint, base.ClientEndPoint);
                if (pReset)
                    _exporter = new MSBExporter();
                else if (pClean)
                    _exporter = null;
            }
        }
        public virtual void HandleFamiliarList(ClientConnection pConnection, MaplePacket pPacket)
        {
            using (InsertQueryBuilder familiars = new InsertQueryBuilder("familiars"))
            {
                familiars.OnDuplicateUpdate = true;

                familiars.AddColumn("character_id");
                familiars.AddColumn("mobid");
                familiars.AddColumns(true,
                    "name", "fitality_cur", "fitality_max",
                    "starttime", "endtime", "unktime"
                );

                for (int i = pPacket.ReadInt(); i > 0; i--)
                {
                    pPacket.ReadInt(); // Weird id?
                    int mobid = pPacket.ReadInt();
                    string name = pPacket.ReadString(13);
                    byte currentfit = pPacket.ReadByte();
                    byte maxfit = pPacket.ReadByte();
                    pPacket.ReadInt();
                    pPacket.ReadByte();
                    DateTime starttime = DateTime.FromFileTime(pPacket.ReadLong());
                    DateTime unktime = DateTime.FromFileTime(pPacket.ReadLong());
                    DateTime endtime = DateTime.FromFileTime(pPacket.ReadLong());

                    pPacket.ReadByte();
                    pPacket.ReadByte();


                    familiars.AddRow(
                        pConnection.CharacterInternalID,
                        mobid,
                        name,
                        currentfit,
                        maxfit,
                        starttime,
                        endtime,
                        unktime
                        );

                }

                familiars.RunQuery();
            }
        }
Esempio n. 10
0
        public virtual void HandleBuddyList(ClientConnection pConnection, MaplePacket pPacket)
        {
            byte mode = pPacket.ReadByte();
            if (mode != 0x07) return;

            MySQL_Connection.Instance.RunQuery(string.Format("DELETE FROM buddies WHERE character_id = {0}", pConnection.CharacterInternalID));
            using (InsertQueryBuilder buddies = new InsertQueryBuilder("buddies"))
            {
                buddies.OnDuplicateUpdate = true;
                buddies.AddColumn("character_id");
                buddies.AddColumn("friend_id");
                buddies.AddColumns(true, "friend_name", "group_name");

                byte amount = pPacket.ReadByte();
                for (byte i = 0; i < amount; i++)
                {
                    int bid = pPacket.ReadInt();
                    string bname = pPacket.ReadString(13);
                    pPacket.Skip(1 + 4);
                    string gname = pPacket.ReadString(13);
                    pPacket.Skip(4);

                    buddies.AddRow(
                        pConnection.CharacterInternalID,
                        bid,
                        bname,
                        gname
                        );
                }
                buddies.RunQuery();
            }
        }
Esempio n. 11
0
        public virtual void HandleSpawnAndroid(ClientConnection pConnection, MaplePacket pPacket)
        {
            Android android = new Android();
            android.Decode(pPacket);

            if (android.ID == pConnection.CharacterID)
            {
                // Save android
                using (InsertQueryBuilder iqb = new InsertQueryBuilder("androids"))
                {
                    iqb.OnDuplicateUpdate = true;
                    iqb.AddColumn("character_id");
                    iqb.AddColumns(true, "name", "type", "skin", "hair", "face");
                    for (int i = 1; i <= 7; i++)
                        iqb.AddColumn("equip" + i, true);

                    iqb.AddRow(pConnection.CharacterInternalID, android.Name, android.Type, android.Skin, android.Hair, android.Face,
                        android.Equips[0], android.Equips[1], android.Equips[2],
                        android.Equips[3], android.Equips[4], android.Equips[5],
                        android.Equips[6]);
                    iqb.RunQuery();
                }
                Logger.WriteLine("Saved android '{0}' of {1}.", android.Name, pConnection.CharData.Stats.Name);

            }
        }
Esempio n. 12
0
        public static void SaveServerIP(string pIP, ushort pPort, byte pWorldID, byte pChannelID)
        {
            using (InsertQueryBuilder iqb = new InsertQueryBuilder("server_ips"))
            {
                iqb.OnDuplicateUpdate = true;
                iqb.AddColumns(false, "ip", "port");
                iqb.AddColumns(true, "world_id", "channel_id");

                iqb.AddRow(pIP, pPort, pWorldID, pChannelID);

                iqb.RunQuery();
            }
        }
Esempio n. 13
0
        public static void SaveItem(ClientConnection pConnection, ushort pInventory, short pSlot, ItemBase pItem, InsertQueryBuilder itemsTable, bool pDontSetChecksum = false)
        {
            if (!itemsTable.HasColumns)
            {
                itemsTable.OnDuplicateUpdate = true;
                itemsTable.AddColumn("character_id", false);
                itemsTable.AddColumn("itemid", true);
                itemsTable.AddColumn("inventory", false);
                itemsTable.AddColumn("slot", false);
                itemsTable.AddColumn("checksum", true);
                itemsTable.AddColumns(true, 
                    "cashid", "amount", "expires", "slots", "scrolls", 
                    "str", "dex", "int", "luk", "maxhp", "maxmp", 
                    "weaponatt", "weapondef", "magicatt", "magicdef", "acc", "avo", "hands", "jump", "speed", "name", "flags",
                    "itemlevel", "itemexp",
                    "hammers",
                    "battlemodedmg",
                    "statusflag",
                    "potential1", "potential2", "potential3", 
                    "potential4", "potential5", "potential6",
                    "display_id",
                    "socketstate", 
                    "nebulite1", "nebulite2", "nebulite3",
                    "uniqueid",
                    "bagid",
                    "moreflags",
                    "durability",
                    "max_scissors"
                    );
            }

            int checksum = pItem.GetChecksum();


            if (pItem is ItemEquip)
            {
                var equip = pItem as ItemEquip;

                itemsTable.AddRow(
                    pConnection.CharacterInternalID,
                    equip.ItemID,
                    pInventory,
                    pSlot,
                    checksum,
                    equip.CashID,
                    equip.Amount,
                    equip.Expires,
                    equip.Slots, equip.Scrolls,
                    equip.Str, equip.Dex, equip.Int, equip.Luk,
                    equip.HP, equip.MP,
                    equip.Watk, equip.Wdef, equip.Matk, equip.Mdef,
                    equip.Acc, equip.Avo, equip.Hands, equip.Jump, equip.Speed,
                    equip.Name, equip.Flags,
                    equip.ItemLevel, equip.ItemEXP, 
                    equip.ViciousHammer, 
                    equip.BattleModeDamage,
                    equip.StatusFlags,
                    equip.Potential1, equip.Potential2, equip.Potential3, 
                    equip.Potential4, equip.Potential5, equip.Potential6,
                    equip.DisplayID,
                    equip.SocketState, 
                    equip.Nebulite1, equip.Nebulite2, equip.Nebulite3,
                    equip.UniqueID,
                    pItem.BagID,
                    equip.SetFlags,
                    equip.Durability,
                    equip.MaxScissors
                );
            }
            else
            {

                string name = pItem is ItemRechargable ? (pItem as ItemRechargable).CraftName : null;
                int flags = pItem is ItemRechargable ? (pItem as ItemRechargable).Flags : 0;

                itemsTable.AddRow(
                    pConnection.CharacterInternalID,
                    pItem.ItemID,
                    pInventory,
                    pSlot,
                    checksum,
                    pItem.CashID,
                    pItem.Amount,
                    pItem.Expires,
                    null, null,
                    null, null, null, null,
                    null, null,
                    null, null, null, null,
                    null, null, null, null, null,
                    name, flags,
                    null, null, 
                    null, 
                    null,
                    null,
                    null, null, null, 
                    null, null, null,
                    null,
                    null, 
                    null, null, null,
                    pItem is ItemRechargable ? (pItem as ItemRechargable).UniqueID : 0,
                    pItem.BagID,
                    null,
                    null,
                    null
                    );

            }

            if (!pDontSetChecksum)
                AccountDataCache.Instance.SetChecksumOfSlot(pConnection.CharacterID, pConnection.WorldID, pInventory, pSlot, checksum);
        }
        public void SaveData(ClientConnection pConnection)
        {
            GW_CharacterStat stats = Stats;
            Queries.AddOrUpdateCharacter(null, stats.ID, stats.Name, pConnection.UserID, pConnection.WorldID, stats.Level, stats.JobID,
                stats.Str, stats.Dex, stats.Int, stats.Luk,
                stats.HP, stats.MaxHP, stats.MP, stats.MaxMP, stats.AP, stats.SP,
                stats.EXP, stats.Fame, stats.MapID, stats.MapPos, stats.Gender, stats.Skin,
                stats.Face, stats.Hair);


            InsertQueryBuilder itemsTable = new InsertQueryBuilder("items");
            itemsTable.OnDuplicateUpdate = true;
            itemsTable.AddColumn("character_id", false);
            itemsTable.AddColumn("itemid", true);
            itemsTable.AddColumn("inventory", false);
            itemsTable.AddColumn("slot", false);
            itemsTable.AddColumn("checksum", true);
            itemsTable.AddColumns(true, "cashid", "amount", "slots", "scrolls", "str", "dex", "int", "luk", "maxhp", "maxmp", "weaponatt", "weapondef", "magicatt", "magicdef", "acc", "avo", "hands", "jump", "speed", "name", "flags", "hammers", "potential1", "potential2", "potential3", "potential4", "potential5", "socketstate", "socket1", "socket2", "socket3");

            InsertQueryBuilder petTable = new InsertQueryBuilder("pets");
            petTable.OnDuplicateUpdate = true;
            petTable.AddColumn("cashid", false);
            petTable.AddColumn("name", true);
            petTable.AddColumn("closeness", true);
            petTable.AddColumn("fullness", true);
            petTable.AddColumn("level", true);

            int internalid = 0;


            if (Internal_Storage.Store.Instance.KnownCharlist.ContainsKey(stats.ID) &&
                Internal_Storage.Store.Instance.KnownCharlist[stats.ID].ContainsKey(pConnection.WorldID))
            {
                // Update info!
                var internalinfo = Internal_Storage.Store.Instance.KnownCharlist[stats.ID][pConnection.WorldID];
                internalid = internalinfo.InternalID;
                Dictionary<byte, short> updatethese = new Dictionary<byte, short>();
                Dictionary<byte, short> addthese = new Dictionary<byte, short>();

                CharacterInventory inventory = Inventory;

                for (int i = 0; i < Inventory.EquipmentItems.Length; i++)
                {
                    var equips = Inventory.EquipmentItems[i];
                    foreach (var equipdata in equips)
                    {
                        short slot = (short)(i == 2 ? equipdata.Key : -equipdata.Key);
                        var equip = equipdata.Value;
                        bool addrow = false;

                        if (internalinfo.SlotHashes[0].ContainsKey(slot))
                        {
                            int hash = internalinfo.SlotHashes[0][slot];
                            int objhash = equipdata.Value.GetChecksum();
                            if (hash != objhash)
                            {
                                Logger.WriteLine("{0} != {1}", hash, objhash);
                                addrow = true;
                            }

                            internalinfo.SlotHashes[0].Remove(slot);
                        }
                        else
                        {
                            addrow = true;
                        }

                        if (addrow)
                        {
                            itemsTable.AddRow(
                                internalid,
                                equip.ItemID,
                                0,
                                i == 2 ? equipdata.Key : -equipdata.Key,
                                equip.GetChecksum(),
                                equip.CashID,
                                equip.Amount,
                                equip.Slots, equip.Scrolls,
                                equip.Str, equip.Dex, equip.Int, equip.Luk,
                                equip.HP, equip.MP,
                                equip.Watk, equip.Wdef, equip.Matk, equip.Mdef,
                                equip.Acc, equip.Avo, equip.Hands, equip.Jump, equip.Speed,
                                equip.Name, equip.Flags,
                                equip.ViciousHammer,
                                equip.Potential1, equip.Potential2, equip.Potential3, equip.Potential4, equip.Potential5,
                                equip.SocketState, equip.Socket1, equip.Socket2, equip.Socket3
                                );
                        }
                    }
                }

                Logger.WriteLine("Done equips");

                for (int i = 0; i < Inventory.InventoryItems.Length; i++)
                {
                    var items = Inventory.InventoryItems[i];
                    foreach (var itemdata in items)
                    {
                        var item = itemdata.Value;
                        bool addrow = false;
                        if (internalinfo.SlotHashes[i + 1].ContainsKey(itemdata.Key))
                        {
                            int hash = internalinfo.SlotHashes[i + 1][itemdata.Key];
                            int objhash = itemdata.Value.GetChecksum();
                            if (hash != objhash)
                            {
                                Logger.WriteLine("{0} != {1}", hash, objhash);
                                addrow = true;
                            }


                            internalinfo.SlotHashes[i + 1].Remove(itemdata.Key);
                        }
                        else
                        {
                            addrow = true;
                        }

                        if (addrow)
                        {
                            string name = item is ItemRechargable ? (item as ItemRechargable).CraftName : null;
                            int flags = item is ItemRechargable ? (item as ItemRechargable).Flags : 0;

                            itemsTable.AddRow(
                                internalid,
                                item.ItemID,
                                i + 1,
                                itemdata.Key,
                                (item is ItemRechargable ? (item as ItemRechargable).GetChecksum() : item.GetChecksum()),
                                item.CashID,
                                item.Amount,
                                null, null,
                                null, null, null, null,
                                null, null,
                                null, null, null, null,
                                null, null, null, null, null,
                                name, flags,
                                null,
                                null, null, null, null, null,
                                null, null, null, null
                                );

                            if (item is ItemPet)
                            {
                                var pet = item as ItemPet;
                                petTable.AddRow(pet.CashID, pet.Petname, pet.Closeness, pet.Fullness, pet.Level);
                            }
                        }
                    }
                }

                Logger.WriteLine("Done items");

                {
                    string removequery = "";
                    bool added = false;
                    for (byte i = 0; i < internalinfo.SlotHashes.Length; i++)
                    {
                        foreach (var leftovers in internalinfo.SlotHashes[i])
                        {
                            Logger.WriteLine("Deleting item @ inv {0} slot {1}", leftovers.Key, leftovers.Value);
                            added = true;
                            removequery += string.Format("DELETE FROM items WHERE character_id = {2} AND inventory = {0} AND slot = {1};\r\n", i, leftovers.Key, internalid);
                        }
                    }
                    if (added)
                    {
                        System.IO.File.WriteAllText("removequery.sql", removequery);
                        MySQL_Connection.Instance.RunQuery(removequery);
                    }
                }
            }
            else
            {
                Logger.WriteLine("Saving Items");

                using (var result = MySQL_Connection.Instance.RunQuery("SELECT internal_id FROM characters WHERE id = " + stats.ID + " AND world_id = " + pConnection.WorldID + "") as MySql.Data.MySqlClient.MySqlDataReader)
                {
                    if (result.Read())
                    {
                        internalid = result.GetInt32(0);
                    }
                    else
                    {
                        Logger.WriteLine("OH GOD COULD NOT GET INTERNAL ID");
                        return;
                    }
                }


                string itemlist = "";
                itemlist += "INSERT INTO items VALUES ";
                List<string> querylist = new List<string>();
                for (int i = 0; i < Inventory.EquipmentItems.Length; i++)
                {
                    var equips = Inventory.EquipmentItems[i];
                    foreach (var equipdata in equips)
                    {
                        var equip = equipdata.Value;

                        itemsTable.AddRow(
                            internalid,
                            equip.ItemID,
                            0,
                            i == 2 ? equipdata.Key : -equipdata.Key,
                            equip.GetChecksum(),
                            equip.CashID,
                            equip.Amount,
                            equip.Slots, equip.Scrolls,
                            equip.Str, equip.Dex, equip.Int, equip.Luk,
                            equip.HP, equip.MP,
                            equip.Watk, equip.Wdef, equip.Matk, equip.Mdef,
                            equip.Acc, equip.Avo, equip.Hands, equip.Jump, equip.Speed,
                            equip.Name, equip.Flags,
                            equip.ViciousHammer,
                            equip.Potential1, equip.Potential2, equip.Potential3, equip.Potential4, equip.Potential5,
                            equip.SocketState, equip.Socket1, equip.Socket2, equip.Socket3
                            );
                    }
                }

                Logger.WriteLine("Done equips");

                for (int i = 0; i < Inventory.InventoryItems.Length; i++)
                {
                    var items = Inventory.InventoryItems[i];
                    foreach (var itemdata in items)
                    {
                        var item = itemdata.Value;

                        string name = item is ItemRechargable ? (item as ItemRechargable).CraftName : null;
                        int flags = item is ItemRechargable ? (item as ItemRechargable).Flags : 0;

                        itemsTable.AddRow(
                            internalid,
                            item.ItemID,
                            i + 1,
                            itemdata.Key,
                            (item is ItemRechargable ? (item as ItemRechargable).GetChecksum() : item.GetChecksum()),
                            item.CashID,
                            item.Amount,
                            null, null,
                            null, null, null, null,
                            null, null,
                            null, null, null, null,
                            null, null, null, null, null,
                            name, flags,
                            null,
                            null, null, null, null, null,
                            null, null, null, null
                            );

                        if (item is ItemPet)
                        {
                            var pet = item as ItemPet;
                            petTable.AddRow(pet.CashID, pet.Petname, pet.Closeness, pet.Fullness, pet.Level);
                        }
                    }
                }

                Logger.WriteLine("Done items");



            }

            if (itemsTable.RowCount > 0)
            {
                string q = itemsTable.ToString();
                System.IO.File.WriteAllText("insert-update.sql", q);
                int result = (int)MySQL_Connection.Instance.RunQuery(q);
                Logger.WriteLine("Result: {0}", result);
            }

            Logger.WriteLine("Saved item data");

            if (petTable.RowCount > 0)
            {
                string q = petTable.ToString();
                System.IO.File.WriteAllText("insert-update-pet.sql", q);
                int result = (int)MySQL_Connection.Instance.RunQuery(q);
                Logger.WriteLine("Result: {0}", result);
            }

            Logger.WriteLine("Saved item data");
            Internal_Storage.Store.Instance.LoadInventoryHashes(internalid, true);

            {

                InsertQueryBuilder questsTable = new InsertQueryBuilder("quests_running");
                questsTable.OnDuplicateUpdate = true;
                questsTable.AddColumn("character_id", false);
                questsTable.AddColumn("questid", true);
                questsTable.AddColumn("data", true);

                foreach (var quest in Quests.Running)
                {
                    questsTable.AddRow(internalid, quest.Key, quest.Value);
                }

                if (questsTable.RowCount > 0)
                {
                    string q = questsTable.ToString();
                    System.IO.File.WriteAllText("insert-update-quests.sql", q);
                    int result = (int)MySQL_Connection.Instance.RunQuery(q);
                    Logger.WriteLine("Result: {0}", result);
                }
            }
            {
                InsertQueryBuilder doneTable = new InsertQueryBuilder("quests_done");
                doneTable.AddColumn("character_id", false);
                doneTable.AddColumn("questid", false);
                doneTable.AddColumn("time", false);

                foreach (var quest in Quests.Done)
                {
                    doneTable.AddRow(internalid, quest.Key, quest.Value);
                }

                if (doneTable.RowCount > 0)
                {
                    string q = doneTable.ToString();
                    System.IO.File.WriteAllText("insert-update-quests-done.sql", q);
                    int result = (int)MySQL_Connection.Instance.RunQuery(q);
                    Logger.WriteLine("Result: {0}", result);
                }
            }

            pConnection.CharacterInternalID = internalid;
            pConnection.CharacterID = Internal_Storage.Store.Instance.KnownCharlist_INTERNAL[internalid].ID;

        }
Esempio n. 15
0
        public void SaveData(ClientConnection pConnection)
        {
            GW_CharacterStat stats = Stats;

            Queries.AddOrUpdateCharacter(null, stats.ID, stats.Name, pConnection.UserID, pConnection.WorldID, stats.Level, stats.JobID,
                                         stats.Str, stats.Dex, stats.Int, stats.Luk,
                                         stats.HP, stats.MaxHP, stats.MP, stats.MaxMP, stats.AP, stats.SP,
                                         stats.EXP, stats.Fame, stats.MapID, stats.MapPos, stats.Gender, stats.Skin,
                                         stats.Face, stats.Hair);


            InsertQueryBuilder itemsTable = new InsertQueryBuilder("items");

            itemsTable.OnDuplicateUpdate = true;
            itemsTable.AddColumn("character_id", false);
            itemsTable.AddColumn("itemid", true);
            itemsTable.AddColumn("inventory", false);
            itemsTable.AddColumn("slot", false);
            itemsTable.AddColumn("checksum", true);
            itemsTable.AddColumns(true, "cashid", "amount", "slots", "scrolls", "str", "dex", "int", "luk", "maxhp", "maxmp", "weaponatt", "weapondef", "magicatt", "magicdef", "acc", "avo", "hands", "jump", "speed", "name", "flags", "hammers", "potential1", "potential2", "potential3", "potential4", "potential5", "socketstate", "socket1", "socket2", "socket3");

            InsertQueryBuilder petTable = new InsertQueryBuilder("pets");

            petTable.OnDuplicateUpdate = true;
            petTable.AddColumn("cashid", false);
            petTable.AddColumn("name", true);
            petTable.AddColumn("closeness", true);
            petTable.AddColumn("fullness", true);
            petTable.AddColumn("level", true);

            int internalid = 0;


            if (Internal_Storage.Store.Instance.KnownCharlist.ContainsKey(stats.ID) &&
                Internal_Storage.Store.Instance.KnownCharlist[stats.ID].ContainsKey(pConnection.WorldID))
            {
                // Update info!
                var internalinfo = Internal_Storage.Store.Instance.KnownCharlist[stats.ID][pConnection.WorldID];
                internalid = internalinfo.InternalID;
                Dictionary <byte, short> updatethese = new Dictionary <byte, short>();
                Dictionary <byte, short> addthese    = new Dictionary <byte, short>();

                CharacterInventory inventory = Inventory;

                for (int i = 0; i < Inventory.EquipmentItems.Length; i++)
                {
                    var equips = Inventory.EquipmentItems[i];
                    foreach (var equipdata in equips)
                    {
                        short slot   = (short)(i == 2 ? equipdata.Key : -equipdata.Key);
                        var   equip  = equipdata.Value;
                        bool  addrow = false;

                        if (internalinfo.SlotHashes[0].ContainsKey(slot))
                        {
                            int hash    = internalinfo.SlotHashes[0][slot];
                            int objhash = equipdata.Value.GetChecksum();
                            if (hash != objhash)
                            {
                                Logger.WriteLine("{0} != {1}", hash, objhash);
                                addrow = true;
                            }

                            internalinfo.SlotHashes[0].Remove(slot);
                        }
                        else
                        {
                            addrow = true;
                        }

                        if (addrow)
                        {
                            itemsTable.AddRow(
                                internalid,
                                equip.ItemID,
                                0,
                                i == 2 ? equipdata.Key : -equipdata.Key,
                                equip.GetChecksum(),
                                equip.CashID,
                                equip.Amount,
                                equip.Slots, equip.Scrolls,
                                equip.Str, equip.Dex, equip.Int, equip.Luk,
                                equip.HP, equip.MP,
                                equip.Watk, equip.Wdef, equip.Matk, equip.Mdef,
                                equip.Acc, equip.Avo, equip.Hands, equip.Jump, equip.Speed,
                                equip.Name, equip.Flags,
                                equip.ViciousHammer,
                                equip.Potential1, equip.Potential2, equip.Potential3, equip.Potential4, equip.Potential5,
                                equip.SocketState, equip.Socket1, equip.Socket2, equip.Socket3
                                );
                        }
                    }
                }

                Logger.WriteLine("Done equips");

                for (int i = 0; i < Inventory.InventoryItems.Length; i++)
                {
                    var items = Inventory.InventoryItems[i];
                    foreach (var itemdata in items)
                    {
                        var  item   = itemdata.Value;
                        bool addrow = false;
                        if (internalinfo.SlotHashes[i + 1].ContainsKey(itemdata.Key))
                        {
                            int hash    = internalinfo.SlotHashes[i + 1][itemdata.Key];
                            int objhash = itemdata.Value.GetChecksum();
                            if (hash != objhash)
                            {
                                Logger.WriteLine("{0} != {1}", hash, objhash);
                                addrow = true;
                            }


                            internalinfo.SlotHashes[i + 1].Remove(itemdata.Key);
                        }
                        else
                        {
                            addrow = true;
                        }

                        if (addrow)
                        {
                            string name  = item is ItemRechargable ? (item as ItemRechargable).CraftName : null;
                            int    flags = item is ItemRechargable ? (item as ItemRechargable).Flags : 0;

                            itemsTable.AddRow(
                                internalid,
                                item.ItemID,
                                i + 1,
                                itemdata.Key,
                                (item is ItemRechargable ? (item as ItemRechargable).GetChecksum() : item.GetChecksum()),
                                item.CashID,
                                item.Amount,
                                null, null,
                                null, null, null, null,
                                null, null,
                                null, null, null, null,
                                null, null, null, null, null,
                                name, flags,
                                null,
                                null, null, null, null, null,
                                null, null, null, null
                                );

                            if (item is ItemPet)
                            {
                                var pet = item as ItemPet;
                                petTable.AddRow(pet.CashID, pet.Petname, pet.Closeness, pet.Fullness, pet.Level);
                            }
                        }
                    }
                }

                Logger.WriteLine("Done items");

                {
                    string removequery = "";
                    bool   added       = false;
                    for (byte i = 0; i < internalinfo.SlotHashes.Length; i++)
                    {
                        foreach (var leftovers in internalinfo.SlotHashes[i])
                        {
                            Logger.WriteLine("Deleting item @ inv {0} slot {1}", leftovers.Key, leftovers.Value);
                            added        = true;
                            removequery += string.Format("DELETE FROM items WHERE character_id = {2} AND inventory = {0} AND slot = {1};\r\n", i, leftovers.Key, internalid);
                        }
                    }
                    if (added)
                    {
                        System.IO.File.WriteAllText("removequery.sql", removequery);
                        MySQL_Connection.Instance.RunQuery(removequery);
                    }
                }
            }
            else
            {
                Logger.WriteLine("Saving Items");

                using (var result = MySQL_Connection.Instance.RunQuery("SELECT internal_id FROM characters WHERE id = " + stats.ID + " AND world_id = " + pConnection.WorldID + "") as MySql.Data.MySqlClient.MySqlDataReader)
                {
                    if (result.Read())
                    {
                        internalid = result.GetInt32(0);
                    }
                    else
                    {
                        Logger.WriteLine("OH GOD COULD NOT GET INTERNAL ID");
                        return;
                    }
                }


                string itemlist = "";
                itemlist += "INSERT INTO items VALUES ";
                List <string> querylist = new List <string>();
                for (int i = 0; i < Inventory.EquipmentItems.Length; i++)
                {
                    var equips = Inventory.EquipmentItems[i];
                    foreach (var equipdata in equips)
                    {
                        var equip = equipdata.Value;

                        itemsTable.AddRow(
                            internalid,
                            equip.ItemID,
                            0,
                            i == 2 ? equipdata.Key : -equipdata.Key,
                            equip.GetChecksum(),
                            equip.CashID,
                            equip.Amount,
                            equip.Slots, equip.Scrolls,
                            equip.Str, equip.Dex, equip.Int, equip.Luk,
                            equip.HP, equip.MP,
                            equip.Watk, equip.Wdef, equip.Matk, equip.Mdef,
                            equip.Acc, equip.Avo, equip.Hands, equip.Jump, equip.Speed,
                            equip.Name, equip.Flags,
                            equip.ViciousHammer,
                            equip.Potential1, equip.Potential2, equip.Potential3, equip.Potential4, equip.Potential5,
                            equip.SocketState, equip.Socket1, equip.Socket2, equip.Socket3
                            );
                    }
                }

                Logger.WriteLine("Done equips");

                for (int i = 0; i < Inventory.InventoryItems.Length; i++)
                {
                    var items = Inventory.InventoryItems[i];
                    foreach (var itemdata in items)
                    {
                        var item = itemdata.Value;

                        string name  = item is ItemRechargable ? (item as ItemRechargable).CraftName : null;
                        int    flags = item is ItemRechargable ? (item as ItemRechargable).Flags : 0;

                        itemsTable.AddRow(
                            internalid,
                            item.ItemID,
                            i + 1,
                            itemdata.Key,
                            (item is ItemRechargable ? (item as ItemRechargable).GetChecksum() : item.GetChecksum()),
                            item.CashID,
                            item.Amount,
                            null, null,
                            null, null, null, null,
                            null, null,
                            null, null, null, null,
                            null, null, null, null, null,
                            name, flags,
                            null,
                            null, null, null, null, null,
                            null, null, null, null
                            );

                        if (item is ItemPet)
                        {
                            var pet = item as ItemPet;
                            petTable.AddRow(pet.CashID, pet.Petname, pet.Closeness, pet.Fullness, pet.Level);
                        }
                    }
                }

                Logger.WriteLine("Done items");
            }

            if (itemsTable.RowCount > 0)
            {
                string q = itemsTable.ToString();
                System.IO.File.WriteAllText("insert-update.sql", q);
                int result = (int)MySQL_Connection.Instance.RunQuery(q);
                Logger.WriteLine("Result: {0}", result);
            }

            Logger.WriteLine("Saved item data");

            if (petTable.RowCount > 0)
            {
                string q = petTable.ToString();
                System.IO.File.WriteAllText("insert-update-pet.sql", q);
                int result = (int)MySQL_Connection.Instance.RunQuery(q);
                Logger.WriteLine("Result: {0}", result);
            }

            Logger.WriteLine("Saved item data");
            Internal_Storage.Store.Instance.LoadInventoryHashes(internalid, true);

            {
                InsertQueryBuilder questsTable = new InsertQueryBuilder("quests_running");
                questsTable.OnDuplicateUpdate = true;
                questsTable.AddColumn("character_id", false);
                questsTable.AddColumn("questid", true);
                questsTable.AddColumn("data", true);

                foreach (var quest in Quests.Running)
                {
                    questsTable.AddRow(internalid, quest.Key, quest.Value);
                }

                if (questsTable.RowCount > 0)
                {
                    string q = questsTable.ToString();
                    System.IO.File.WriteAllText("insert-update-quests.sql", q);
                    int result = (int)MySQL_Connection.Instance.RunQuery(q);
                    Logger.WriteLine("Result: {0}", result);
                }
            }
            {
                InsertQueryBuilder doneTable = new InsertQueryBuilder("quests_done");
                doneTable.AddColumn("character_id", false);
                doneTable.AddColumn("questid", false);
                doneTable.AddColumn("time", false);

                foreach (var quest in Quests.Done)
                {
                    doneTable.AddRow(internalid, quest.Key, quest.Value);
                }

                if (doneTable.RowCount > 0)
                {
                    string q = doneTable.ToString();
                    System.IO.File.WriteAllText("insert-update-quests-done.sql", q);
                    int result = (int)MySQL_Connection.Instance.RunQuery(q);
                    Logger.WriteLine("Result: {0}", result);
                }
            }

            pConnection.CharacterInternalID = internalid;
            pConnection.CharacterID         = Internal_Storage.Store.Instance.KnownCharlist_INTERNAL[internalid].ID;
        }