Modify item tracker to be more robust in saving

Roland Solymosi [07-01-20 - 13:48]
Modify item tracker to be more robust in saving
Filename
MyCollection/Internals/Classes/Bag.lua
MyCollection/Internals/Classes/Inventory.lua
MyCollection/Internals/Classes/Item.lua
MyCollection/Internals/Classes/Piece.lua
MyCollection/Internals/Classes/Set.lua
MyCollection/Internals/Functions/Callbacks.lua
diff --git a/MyCollection/Internals/Classes/Bag.lua b/MyCollection/Internals/Classes/Bag.lua
index c88f7ae..f8a772d 100644
--- a/MyCollection/Internals/Classes/Bag.lua
+++ b/MyCollection/Internals/Classes/Bag.lua
@@ -22,6 +22,7 @@ Bag.savedTableReference = nil
 -- Properties
 Bag.worn = nil
 Bag.backpack = nil
+Bag.characterId = nil

 -- Functions
 function Bag:FindAll(setId, equipType, traitType, armorType, weaponType)
@@ -44,17 +45,30 @@ function Bag:FindAll(setId, equipType, traitType, armorType, weaponType)
     return items
 end

-function Bag:AddItem(item)
-    local saveCopy = item:CopyForSave()
-
-    if item.bagId == Constants.BagTypes.Worn then
-        self.worn[item.slotId] = item
-        self.savedTableReference[Constants.BagTypes.Worn][item.slotId] = saveCopy
-    end
-    if item.bagId == Constants.BagTypes.Backpack then
-        self.backpack[item.slotId] = item
-        self.savedTableReference[Constants.BagTypes.Backpack][item.slotId] = saveCopy
+function Bag:AddItem(bagId, slotId)
+    local item = nil
+    if bagId == Constants.BagTypes.Worn then
+        self.savedTableReference[Constants.BagTypes.Worn][slotId] = {}
+        item = Classes.Item.New(self.savedTableReference[Constants.BagTypes.Worn][slotId], characterId, bagId, slotId)
+        if item ~= nil then
+            self.worn[slotId] = item
+        else
+            self.savedTableReference[Constants.BagTypes.Worn][slotId] = nil
+            table.remove(self.savedTableReference[Constants.BagTypes.Worn], slotId)
+        end
     end
+    if bagId == Constants.BagTypes.Backpack then
+        self.savedTableReference[Constants.BagTypes.Backpack][slotId] = {}
+        item = Classes.Item.New(self.savedTableReference[Constants.BagTypes.Backpack][slotId], self.characterId, bagId, slotId)
+        if item ~= nil then
+            self.backpack[slotId] = item
+        else
+            self.savedTableReference[Constants.BagTypes.Backpack][slotId] = nil
+            table.remove(self.savedTableReference[Constants.BagTypes.Backpack], slotId)
+        end
+    end
+
+    return item
 end

 function Bag:RemoveItem(bagId, slotId)
@@ -89,24 +103,24 @@ end
 function Bag:Initialize(savedTable, characterId)
     self.worn = {}
     self.backpack = {}
+    self.characterId = characterId

     self.savedTableReference = savedTable
     if savedTable == nil then
         savedTable = {}
     end
-
     if (savedTable[Constants.BagTypes.Worn] == nil) then savedTable[Constants.BagTypes.Worn] = {} end
     if (savedTable[Constants.BagTypes.Backpack] == nil) then savedTable[Constants.BagTypes.Backpack] = {} end

     if (next(savedTable[Constants.BagTypes.Worn]) ~= nil) then
         for slotId, item in pairs(savedTable[Constants.BagTypes.Worn]) do
-            self.worn[slotId] = Classes.Item.New(characterId, Constants.BagTypes.Worn, slotId, item.setId, item.equipType, item.traitType, item.armorType, item.weaponType, item.link)
+            self.worn[slotId] = Classes.Item.New(self.savedTableReference[Constants.BagTypes.Worn][slotId], characterId, Constants.BagTypes.Worn, slotId)
         end
     end

     if (next(savedTable[Constants.BagTypes.Backpack]) ~= nil) then
         for slotId, item in pairs(savedTable[Constants.BagTypes.Backpack]) do
-            self.backpack[slotId] = Classes.Item.New(characterId, Constants.BagTypes.Backpack, slotId, item.setId, item.equipType, item.traitType, item.armorType, item.weaponType, item.link)
+            self.backpack[slotId] = Classes.Item.New(self.savedTableReference[Constants.BagTypes.Backpack][slotId], characterId, Constants.BagTypes.Backpack, slotId)
         end
     end
 end
\ No newline at end of file
diff --git a/MyCollection/Internals/Classes/Inventory.lua b/MyCollection/Internals/Classes/Inventory.lua
index 225b963..71f31b1 100644
--- a/MyCollection/Internals/Classes/Inventory.lua
+++ b/MyCollection/Internals/Classes/Inventory.lua
@@ -48,13 +48,14 @@ function Inventory:FindAll(setId, equipType, traitType, armorType, weaponType)
     return items
 end

-function Inventory:AddItem(item)
-    local characterId = Dependencies.Officials.GetCurrentCharacterId()
-    if Extensions.Constants.IsCharacterBag(item.bagId) then
-        self:GetOrCreateBag(characterId):AddItem(item)
+function Inventory:AddItem(bagId, slotId)
+    local item = nil
+    if Extensions.Constants.IsCharacterBag(bagId) then
+        local characterId = Dependencies.Officials.GetCurrentCharacterId()
+        item = self:GetOrCreateCharacterBag(characterId):AddItem(bagId, slotId)
     else
-        if Extensions.Constants.IsBank(item.bagId) then
-            self:AddItemToBank(item)
+        if Extensions.Constants.IsBank(bagId) then
+            item = self:AddItemToBank(bagId, slotId)
         end
     end

@@ -62,13 +63,15 @@ function Inventory:AddItem(item)
         local _, setName, _, _, _, _ = Dependencies.LibSets.IsSetByItemLink(item.link)
         Logger:Debug("Added bagId:'".. item.bagId .. "', slotId: '".. item.slotId .. "' setName: '".. setName .."' Link: ".. item.link)
     end
+
+    return item
 end

 function Inventory:RemoveItem(bagId, slotId)
     local item = nil
-    local characterId = Dependencies.Officials.GetCurrentCharacterId()
     if Extensions.Constants.IsCharacterBag(bagId) then
-        item = self:GetOrCreateBag(characterId):RemoveItem(bagId, slotId)
+        local characterId = Dependencies.Officials.GetCurrentCharacterId()
+        item = self:GetOrCreateCharacterBag(characterId):RemoveItem(bagId, slotId)
     else
         if Extensions.Constants.IsBank(bagId) then
             if (self.banks[bagId][slotId] ~= nil) then
@@ -90,16 +93,27 @@ function Inventory:AddBag(characterId)
     self.bags[characterId] = Classes.Bag.New(self.savedTableReference.bags[characterId], characterId)
 end

-function Inventory:GetOrCreateBag(characterId)
+function Inventory:GetOrCreateCharacterBag(characterId)
     if self.bags[characterId] == nil then
         self:AddBag(characterId)
     end
     return self.bags[characterId]
 end

-function Inventory:AddItemToBank(item)
-    self.banks[item.bagId][item.slotId] = item
-    self.savedTableReference.banks[item.bagId][item.slotId] = item:CopyForSave()
+function Inventory:AddItemToBank(bagId, slotId)
+    local characterId = Dependencies.Officials.GetCurrentCharacterId()
+
+    self.savedTableReference.banks[bagId][slotId] = {}
+    local item = Classes.Item.New(self.savedTableReference.banks[bagId][slotId], characterId, bagId, slotId)
+
+    if item ~= nil then
+        self.banks[bagId][slotId] = item
+    else
+        self.savedTableReference.banks[bagId][slotId] = nil
+        table.remove(self.savedTableReference.banks[bagId], slotId)
+    end
+
+    return item
 end

 function Inventory:RemoveItemFromBank(bagId, slotId)
@@ -118,11 +132,11 @@ function Inventory:Clear(keepOtherCharacters)
     if keepOtherCharacters == nil or keepOtherCharacters then
         local characterId = Dependencies.Officials.GetCurrentCharacterId()
         if self.bags[characterId] ~= nil then
-            self:GetOrCreateBag(characterId):Clear()
+            self:GetOrCreateCharacterBag(characterId):Clear()
         end
     else
         for characterId, _ in pairs(self.bags) do
-            self:GetOrCreateBag(characterId):Clear()
+            self:GetOrCreateCharacterBag(characterId):Clear()
         end
     end
 end
@@ -133,7 +147,7 @@ function Inventory:Refresh()
     Dependencies.Officials.SharedInventory:RefreshInventory(Constants.BagTypes.SubscriberBank)

     local characterId = Dependencies.Officials.GetCurrentCharacterId()
-    self:GetOrCreateBag(characterId):Refresh()
+    self:GetOrCreateCharacterBag(characterId):Refresh()
 end

 function Inventory:Initialize(savedTable)
@@ -141,13 +155,13 @@ function Inventory:Initialize(savedTable)

     if (next(savedTable.banks[Constants.BagTypes.Bank]) ~= nil) then
         for slotId, item in pairs(savedTable.banks[Constants.BagTypes.Bank]) do
-            self.banks[Constants.BagTypes.Bank][slotId] = Classes.Item.New(characterId, Constants.BagTypes.Bank, slotId, item.setId, item.equipType, item.traitType, item.armorType, item.weaponType, item.link)
+            self.banks[Constants.BagTypes.Bank][slotId] = Classes.Item.New(self.savedTableReference.banks[Constants.BagTypes.Bank][slotId], characterId, Constants.BagTypes.Bank, slotId)
         end
     end

     if (next(savedTable.banks[Constants.BagTypes.SubscriberBank]) ~= nil) then
         for slotId, item in pairs(savedTable.banks[Constants.BagTypes.SubscriberBank]) do
-            self.banks[Constants.BagTypes.SubscriberBank][slotId] = Classes.Item.New(characterId, Constants.BagTypes.SubscriberBank, slotId, item.setId, item.equipType, item.traitType, item.armorType, item.weaponType, item.link)
+            self.banks[Constants.BagTypes.SubscriberBank][slotId] = Classes.Item.New(self.savedTableReference.banks[Constants.BagTypes.SubscriberBank][slotId], characterId, Constants.BagTypes.SubscriberBank, slotId)
         end
     end

diff --git a/MyCollection/Internals/Classes/Item.lua b/MyCollection/Internals/Classes/Item.lua
index 87e05b4..ac32f12 100644
--- a/MyCollection/Internals/Classes/Item.lua
+++ b/MyCollection/Internals/Classes/Item.lua
@@ -9,28 +9,29 @@ Classes.Item.__index = Classes.Item
 local Item = Classes.Item

 -- Constructor
-function Item.New(characterId, bagId, slotId, setId, equipType, traitType, armorType, weaponType, link)
+function Item.New(savedTable, characterId, bagId, slotId)
     local instance = {}
-    setmetatable(instance, Item)
+    setmetatable(instance, Item)

-    instance:Initialize(characterId, bagId, slotId, setId, equipType, traitType, armorType, weaponType, link)
-
-    if Extensions.Constants.IsEquipment(instance.equipType) then
+    if instance:Initialize(savedTable, characterId, bagId, slotId) then
         return instance
     else
         return nil
     end
 end

+Item.savedTableReference = nil
+
 -- Properties
+Item.bagId = nil
+Item.slotId = nil
+Item.characterId = nil
+
 Item.equipType = nil
 Item.armorType = nil
 Item.weaponType = nil
 Item.traitType = nil
 Item.setId = nil
-Item.bagId = nil
-Item.slotId = nil
-Item.characterId = nil
 Item.link = nil

 -- Functions
@@ -60,66 +61,69 @@ function Item:GetId()
     end
 end

-function Item:Initialize(characterId, bagId, slotId, setId, equipType, traitType, armorType, weaponType, link)
+function Item:Initialize(savedTable, characterId, bagId, slotId)
+    self.savedTableReference = savedTable
+
     self.bagId = bagId
     self.slotId = slotId
     self.characterId = characterId
+
+    if savedTable.equipType == nil then
+        _, _, _, _, _, self.equipType, _, _ = Dependencies.Officials.GetItemInfo(self.bagId, self.slotId)
+    else
+        self.equipType = savedTable.equipType
+    end
+    self.savedTableReference.equipType = self.equipType
+
+    -- Skip further read if not equipment
+    if not Extensions.Constants.IsEquipment(self.equipType) then
+        return false
+    end

-    if link == nil then
+    if savedTable.link == nil then
         self.link = Dependencies.Officials.GetItemLink(self.bagId, self.slotId)
     else
-        self.link = link
+        self.link = savedTable.link
     end
+    self.savedTableReference.link = self.link

-    if setId == nil then
+    if savedTable.setId == nil then
         _, _, self.setId, _, _, _ = Dependencies.LibSets.IsSetByItemLink(self.link)
     else
-        self.setId = setId
-    end
-
-    if equipType == nil then
-        _, _, _, _, _, self.equipType, _, _ = Dependencies.Officials.GetItemInfo(self.bagId, self.slotId)
-    else
-        self.equipType = equipType
+        self.setId = savedTable.setId
     end
-
-    -- Skip further read if not equipment
-    if not Extensions.Constants.IsEquipment(self.equipType) then
-        return
+    self.savedTableReference.setId = self.setId
+
+    -- Skip further if not set item
+    if self.setId == nil or self.setId == 0 then
+        return false
     end

-    if traitType == nil then
+    if savedTable.traitType == nil then
         self.traitType = Dependencies.Officials.GetItemTrait(self.bagId, self.slotId)
     else
-        self.traitType = traitType
+        self.traitType = savedTable.traitType
     end
+    self.savedTableReference.traitType = self.traitType

     if Extensions.Constants.IsWeaponOrShield(self.equipType) then
-        if weaponType == nil then
+        if savedTable.weaponType == nil then
             self.weaponType = Dependencies.Officials.GetItemWeaponType(self.bagId, self.slotId)
         else
-            self.weaponType = weaponType
+            self.weaponType = savedTable.weaponType
         end
         self.equipType = Extensions.Constants.GetEquipTypeOfWeapon(self.weaponType)
+        self.savedTableReference.equipType = self.equipType
     else
         if Extensions.Constants.IsArmor(self.equipType) then
-            if armorType == nil then
+            if savedTable.armorType == nil then
                 self.armorType = Dependencies.Officials.GetItemArmorType(self.bagId, self.slotId)
             else
-                self.armorType = armorType
+                self.armorType = savedTable.armorType
             end
+            self.savedTableReference.armorType = self.armorType
         end
     end
-end
-
--- For saving purpose
-function Item:CopyForSave()
-    return {
-        setId = self.setId,
-        equipType = self.equipType,
-        traitType = self.traitType,
-        armorType = self.armorType,
-        weaponType = self.weaponType,
-        link = self.link
-    }
+
+    return true
 end
\ No newline at end of file
diff --git a/MyCollection/Internals/Classes/Piece.lua b/MyCollection/Internals/Classes/Piece.lua
index 3553cb6..f2106f9 100644
--- a/MyCollection/Internals/Classes/Piece.lua
+++ b/MyCollection/Internals/Classes/Piece.lua
@@ -49,8 +49,8 @@ function Piece:HasItem()
     return false
 end

-function Piece:AddReference(item)
-    if self.traitType == nil or self.traitType == 0 or item.traitType == self.traitType then
+function Piece:AddReference(item)
+    if self.traitType == nil or self.traitType == 0 or item.traitType == self.traitType then
         self.references[item:GetId()] = item
     end

diff --git a/MyCollection/Internals/Classes/Set.lua b/MyCollection/Internals/Classes/Set.lua
index 140d928..816f40c 100644
--- a/MyCollection/Internals/Classes/Set.lua
+++ b/MyCollection/Internals/Classes/Set.lua
@@ -81,16 +81,16 @@ end
 function Set:AddItem(item)
     if Extensions.Constants.IsWeaponOrShield(item.equipType) then
         self.pieces.weapons[item.weaponType]:AddReference(item)
+    else
+        if Extensions.Constants.IsJewellery(item.equipType) then
+            self.pieces.jewelleries[item.equipType]:AddReference(item)
+        else
+            if Extensions.Constants.IsArmor(item.equipType) and item.armorType == self.armorType then
+                self.pieces.armors[item.equipType]:AddReference(item)
+            end
+        end
     end
-
-    if Extensions.Constants.IsJewellery(item.equipType) then
-        self.pieces.jewelleries[item.equipType]:AddReference(item)
-    end
-
-    if Extensions.Constants.IsArmor(item.equipType) and item.armoryType == self.armorType then
-        self.pieces[item.equipType]:AddReference(item)
-    end
-end
+end

 function Set:RemoveItem(item)
     if Extensions.Constants.IsWeaponOrShield(item.equipType) then
@@ -102,7 +102,7 @@ function Set:RemoveItem(item)
     end

     if Extensions.Constants.IsArmor(item.equipType) and item.armoryType == self.armorType then
-        self.pieces[item.equipType]:RemoveReference(item)
+        self.pieces.armors[item.equipType]:RemoveReference(item)
     end
 end

diff --git a/MyCollection/Internals/Functions/Callbacks.lua b/MyCollection/Internals/Functions/Callbacks.lua
index 7275246..d6fb931 100644
--- a/MyCollection/Internals/Functions/Callbacks.lua
+++ b/MyCollection/Internals/Functions/Callbacks.lua
@@ -8,26 +8,22 @@ Functions.Callbacks = {}
 local Callbacks = Functions.Callbacks

 function Callbacks.Added(bagId, slotId, itemObject)
-    local characterId = Dependencies.Officials.GetCurrentCharacterId()
-    local item = Classes.Item.New(characterId, bagId, slotId)
-    if (item ~= nil and item.setId ~= nil and item.setId ~= 0) then
-        Data.Inventory:AddItem(item)
+    local item = Data.Inventory:AddItem(bagId, slotId)
+    if item ~= nil then
         Data.Collection:AddItem(item)
     end
 end

 function Callbacks.Removed(bagId, slotId, itemObject)
     local item = Data.Inventory:RemoveItem(bagId, slotId)
-    if (item ~= nil) then
+    if item ~= nil then
         Data.Collection:RemoveItem(item)
     end
 end

 function Callbacks.Updated(bagId, slotId, itemObject)
-    local characterId = Dependencies.Officials.GetCurrentCharacterId()
-    local item = Classes.Item.New(characterId, bagId, slotId)
-    if (item ~= nil and item.setId ~= nil and item.setId ~= 0) then
-        Data.Inventory:AddItem(item)
+    local item = Data.Inventory:AddItem(bagId, slotId)
+    if item ~= nil then
         Data.Collection:AddItem(item)
     end
 end