From bc95c139dedec9b7df23447bec344a8dd4a10c00 Mon Sep 17 00:00:00 2001 From: Filip Maj Date: Sat, 9 Sep 2017 11:25:58 -0400 Subject: [PATCH] Implement DB updates for player. Cleaned up DB calls. No longer track slot, it's ordered by index id (no concept of slots anyway in client). --- FFXIVClassic Map Server/Database.cs | 108 +++++------------- .../actors/chara/player/Inventory.cs | 53 +++++++-- 2 files changed, 70 insertions(+), 91 deletions(-) diff --git a/FFXIVClassic Map Server/Database.cs b/FFXIVClassic Map Server/Database.cs index adf7f2b0..9c329835 100644 --- a/FFXIVClassic Map Server/Database.cs +++ b/FFXIVClassic Map Server/Database.cs @@ -1225,7 +1225,6 @@ namespace FFXIVClassic_Map_Server serverItemId, itemId, quantity, - slot, itemType, quality, durability, @@ -1237,36 +1236,36 @@ namespace FFXIVClassic_Map_Server materia5 FROM characters_inventory INNER JOIN server_items ON serverItemId = server_items.id - WHERE characterId = @charId AND inventoryType = @type AND slot >= @slot ORDER BY slot"; + WHERE characterId = @charId AND inventoryType = @type"; MySqlCommand cmd = new MySqlCommand(query, conn); cmd.Parameters.AddWithValue("@charId", player.actorId); - cmd.Parameters.AddWithValue("@slot", slotOffset); cmd.Parameters.AddWithValue("@type", type); + ushort slot = 0; using (MySqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { - uint uniqueId = reader.GetUInt32(0); - uint itemId = reader.GetUInt32(1); - int quantity = reader.GetInt32(2); - ushort slot = reader.GetUInt16(3); + uint uniqueId = reader.GetUInt32("serverItemId"); + uint itemId = reader.GetUInt32("itemId"); + int quantity = reader.GetInt32("quantity"); - byte itemType = reader.GetByte(4); - byte qualityNumber = reader.GetByte(5); + byte itemType = reader.GetByte("itemType"); + byte qualityNumber = reader.GetByte("quality"); - int durability = reader.GetInt32(6); - ushort spiritBind = reader.GetUInt16(7); + int durability = reader.GetInt32("durability"); + ushort spiritBind = reader.GetUInt16("spiritBind"); - byte materia1 = reader.GetByte(8); - byte materia2 = reader.GetByte(9); - byte materia3 = reader.GetByte(10); - byte materia4 = reader.GetByte(11); - byte materia5 = reader.GetByte(12); + byte materia1 = reader.GetByte("materia1"); + byte materia2 = reader.GetByte("materia2"); + byte materia3 = reader.GetByte("materia3"); + byte materia4 = reader.GetByte("materia4"); + byte materia5 = reader.GetByte("materia5"); InventoryItem item = new InventoryItem(uniqueId, itemId, quantity, itemType, qualityNumber, durability, spiritBind, materia1, materia2, materia3, materia4, materia5); item.slot = slot; + slot++; items.Add(item); } } @@ -1327,10 +1326,8 @@ namespace FFXIVClassic_Map_Server return insertedItem; } - public static InventoryItem AddItem(Player player, InventoryItem addedItem, uint type) - { - InventoryItem insertedItem = null; - + public static void AddItem(Player player, InventoryItem addedItem, uint type) + { using (MySqlConnection conn = new MySqlConnection(String.Format("Server={0}; Port={1}; Database={2}; UID={3}; Password={4}", ConfigConstants.DATABASE_HOST, ConfigConstants.DATABASE_PORT, ConfigConstants.DATABASE_NAME, ConfigConstants.DATABASE_USERNAME, ConfigConstants.DATABASE_PASSWORD))) { try @@ -1339,8 +1336,8 @@ namespace FFXIVClassic_Map_Server string query = @" INSERT INTO characters_inventory - (characterId, slot, inventoryType, serverItemId, quantity) - SELECT @charId, IFNULL(MAX(SLOT)+1, 0), @inventoryType, @uid, @quantity FROM characters_inventory WHERE characterId = @charId AND inventoryType = @inventoryType; + (characterId, inventoryType, serverItemId, quantity) + SELECT @charId, @inventoryType, @uid, @quantity FROM characters_inventory WHERE characterId = @charId AND inventoryType = @inventoryType; "; MySqlCommand cmd = new MySqlCommand(query, conn); @@ -1348,7 +1345,7 @@ namespace FFXIVClassic_Map_Server cmd.Parameters.AddWithValue("@uid", addedItem.uniqueId); cmd.Parameters.AddWithValue("@charId", player.actorId); cmd.Parameters.AddWithValue("@inventoryType", type); - cmd.Parameters.AddWithValue("@quantity", insertedItem.quantity); + cmd.Parameters.AddWithValue("@quantity", addedItem.quantity); cmd.ExecuteNonQuery(); } @@ -1361,11 +1358,9 @@ namespace FFXIVClassic_Map_Server conn.Dispose(); } } - - return insertedItem; } - public static void SetQuantity(Player player, uint slot, ushort type, int quantity) + public static void SetQuantity(Player player, ulong serverItemId, int quantity) { using (MySqlConnection conn = new MySqlConnection(String.Format("Server={0}; Port={1}; Database={2}; UID={3}; Password={4}", ConfigConstants.DATABASE_HOST, ConfigConstants.DATABASE_PORT, ConfigConstants.DATABASE_NAME, ConfigConstants.DATABASE_USERNAME, ConfigConstants.DATABASE_PASSWORD))) { @@ -1376,54 +1371,13 @@ namespace FFXIVClassic_Map_Server string query = @" UPDATE characters_inventory SET quantity = @quantity - WHERE characterId = @charId AND slot = @slot AND inventoryType = @type; - "; - - MySqlCommand cmd = new MySqlCommand(query, conn); - cmd.Parameters.AddWithValue("@charId", player.actorId); - cmd.Parameters.AddWithValue("@quantity", quantity); - cmd.Parameters.AddWithValue("@slot", slot); - cmd.Parameters.AddWithValue("@type", type); - cmd.ExecuteNonQuery(); - - } - catch (MySqlException e) - { - Program.Log.Error(e.ToString()); - } - finally - { - conn.Dispose(); - } - } - - } - - public static void RemoveItem(Player player, ulong serverItemId, ushort type) - { - using (MySqlConnection conn = new MySqlConnection(String.Format("Server={0}; Port={1}; Database={2}; UID={3}; Password={4}; Allow User Variables=True", ConfigConstants.DATABASE_HOST, ConfigConstants.DATABASE_PORT, ConfigConstants.DATABASE_NAME, ConfigConstants.DATABASE_USERNAME, ConfigConstants.DATABASE_PASSWORD))) - { - try - { - conn.Open(); - - string query = @" - SELECT slot INTO @slotToDelete FROM characters_inventory WHERE serverItemId = @serverItemId; - UPDATE characters_inventory - SET slot = slot - 1 - WHERE characterId = @charId AND slot > @slotToDelete AND inventoryType = @type; - - DELETE FROM characters_inventory - WHERE serverItemId = @serverItemId AND inventoryType = @type; - - DELETE FROM server_items - WHERE id = @serverItemId; + WHERE characterId = @charId and serverItemId = @serverItemId; "; MySqlCommand cmd = new MySqlCommand(query, conn); cmd.Parameters.AddWithValue("@charId", player.actorId); cmd.Parameters.AddWithValue("@serverItemId", serverItemId); - cmd.Parameters.AddWithValue("@type", type); + cmd.Parameters.AddWithValue("@quantity", quantity); cmd.ExecuteNonQuery(); } @@ -1439,7 +1393,7 @@ namespace FFXIVClassic_Map_Server } - public static void RemoveItem(Player player, ushort slot, ushort type) + public static void RemoveItem(Player player, ulong serverItemId) { using (MySqlConnection conn = new MySqlConnection(String.Format("Server={0}; Port={1}; Database={2}; UID={3}; Password={4}; Allow User Variables=True", ConfigConstants.DATABASE_HOST, ConfigConstants.DATABASE_PORT, ConfigConstants.DATABASE_NAME, ConfigConstants.DATABASE_USERNAME, ConfigConstants.DATABASE_PASSWORD))) { @@ -1448,23 +1402,13 @@ namespace FFXIVClassic_Map_Server conn.Open(); string query = @" - SELECT serverItemId INTO @serverItemId FROM characters_inventory WHERE characterId = @charId AND slot = @slot; - DELETE FROM characters_inventory - WHERE characterId = @charId AND slot = @slot AND inventoryType = @type; - - DELETE FROM server_items - WHERE id = @serverItemId; - - UPDATE characters_inventory - SET slot = slot - 1 - WHERE characterId = @charId AND slot > @slot AND inventoryType = @type; + WHERE characterId = @charId and serverItemId = @itemDBCode; "; MySqlCommand cmd = new MySqlCommand(query, conn); cmd.Parameters.AddWithValue("@charId", player.actorId); - cmd.Parameters.AddWithValue("@slot", slot); - cmd.Parameters.AddWithValue("@type", type); + cmd.Parameters.AddWithValue("@serverItemId", serverItemId); cmd.ExecuteNonQuery(); } diff --git a/FFXIVClassic Map Server/actors/chara/player/Inventory.cs b/FFXIVClassic Map Server/actors/chara/player/Inventory.cs index 8f9e5ef2..7cfd11ac 100644 --- a/FFXIVClassic Map Server/actors/chara/player/Inventory.cs +++ b/FFXIVClassic Map Server/actors/chara/player/Inventory.cs @@ -123,7 +123,13 @@ namespace FFXIVClassic_Map_Server.actors.chara.player int oldQuantity = item.quantity; item.quantity = Math.Min(item.quantity + quantityCount, gItem.maxStack); isDirty[i] = true; - quantityCount -= (gItem.maxStack - oldQuantity); + quantityCount -= (gItem.maxStack - oldQuantity); + + if (owner is Player) + { + Database.SetQuantity((Player)owner, item.uniqueId, item.quantity); + } + if (quantityCount <= 0) break; } @@ -145,10 +151,17 @@ namespace FFXIVClassic_Map_Server.actors.chara.player isDirty[endOfListIndex] = true; list[endOfListIndex++] = addedItem; quantityCount -= gItem.maxStack; + + if (owner is Player) + { + Database.AddItem((Player)owner, addedItem, inventoryCode); + } } if (owner is Player) - SendUpdatePackets((Player)owner); + { + SendUpdatePackets((Player)owner); + } return true; } @@ -176,12 +189,20 @@ namespace FFXIVClassic_Map_Server.actors.chara.player if (item.itemId == itemId) { int oldQuantity = item.quantity; - //Stack nomnomed - if (item.quantity - quantityCount <= 0) - list[i] = null; - //Stack reduced - else + //Stack nomnomed + if (item.quantity - quantityCount <= 0) + { + if (owner is Player) + Database.RemoveItem((Player)owner, list[i].uniqueId); + list[i] = null; + } + //Stack reduced + else + { item.quantity -= quantityCount; + if (owner is Player) + Database.SetQuantity((Player)owner, list[i].uniqueId, list[i].quantity); + } isDirty[i] = true; @@ -213,7 +234,10 @@ namespace FFXIVClassic_Map_Server.actors.chara.player } if (toDelete == null) - return; + return; + + if (owner is Player) + Database.RemoveItem((Player)owner, toDelete.uniqueId); list[slot] = null; isDirty[slot] = true; @@ -223,7 +247,10 @@ namespace FFXIVClassic_Map_Server.actors.chara.player public void RemoveItemAtSlot(ushort slot) { if (slot >= endOfListIndex) - return; + return; + + if (owner is Player) + Database.RemoveItem((Player)owner, list[slot].uniqueId); list[slot] = null; isDirty[slot] = true; @@ -243,9 +270,17 @@ namespace FFXIVClassic_Map_Server.actors.chara.player if (list[slot].quantity <= 0) { + if (owner is Player) + Database.RemoveItem((Player)owner, list[slot].uniqueId); + list[slot] = null; doRealign(); } + else + { + if (owner is Player) + Database.SetQuantity((Player)owner, list[slot].uniqueId, list[slot].quantity); + } isDirty[slot] = true; if (owner is Player)