Skip to content

Commit

Permalink
Serialization of Reagents in cqi
Browse files Browse the repository at this point in the history
  • Loading branch information
derfloh205 committed Feb 19, 2024
1 parent f5c6fe6 commit efeef0e
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 52 deletions.
15 changes: 12 additions & 3 deletions Cache/ItemCount.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ end

---@param itemID ItemInfo
---@param crafterUID string
---@return number count
---@return ItemID? alternativeItemID
---@return number? alternativeCount
function CraftSim.CACHE.ITEM_COUNT:Get(itemID, bank, uses, reagentBank, crafterUID)
local playerCrafterUID = CraftSim.UTIL:GetPlayerCrafterUID()
crafterUID = crafterUID or playerCrafterUID
Expand All @@ -45,19 +48,25 @@ function CraftSim.CACHE.ITEM_COUNT:Get(itemID, bank, uses, reagentBank, crafterU
if isPlayer then
-- always from api and then cache
local count = GetItemCount(itemID, bank, uses, reagentBank)
local altCount = nil
if alternativeItemID then
count = count + GetItemCount(alternativeItemID, bank, uses, reagentBank)
altCount = GetItemCount(alternativeItemID, bank, uses, reagentBank)
CraftSim.CACHE.ITEM_COUNT:Update(alternativeItemID, altCount)
end
CraftSim.CACHE.ITEM_COUNT:Update(itemID, count)
return count
return count, alternativeItemID, altCount
end


local count = CraftSimItemCountCache[crafterUID][itemID]
local altCount = nil
if alternativeItemID then
altCount = CraftSimItemCountCache[crafterUID][alternativeItemID]
end
if not count then
return 0 -- not cached yet
else
return count
return count, alternativeItemID, altCount
end
end

Expand Down
12 changes: 9 additions & 3 deletions Data/Classes/CraftQueueItem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ end
---@field recipeID number
---@field amount? number
---@field crafterData CraftSim.CrafterData
---@field requiredReagents CraftingReagentInfo[]
---@field requiredReagents CraftSim.Reagent.Serialized[]
---@field optionalReagents CraftingReagentInfo[]
---@field professionGearSet CraftSim.ProfessionGearSet.Serialized
---@field subRecipeDepth number
Expand All @@ -86,7 +86,7 @@ function CraftSim.CraftQueueItem:Serialize()
local serializedData = {
recipeID = recipeData.recipeID,
crafterData = recipeData.crafterData,
requiredReagents = recipeData.reagentData:GetRequiredCraftingReagentInfoTbl(),
requiredReagents = recipeData.reagentData:SerializeRequiredReagents(),
optionalReagents = recipeData.reagentData:GetOptionalCraftingReagentInfoTbl(),
professionGearSet = recipeData.professionGearSet:Serialize(),
subRecipeDepth = recipeData.subRecipeDepth,
Expand Down Expand Up @@ -132,7 +132,13 @@ function CraftSim.CraftQueueItem:Deserialize(serializedData)
recipeData.optimizedSubRecipes[itemID] = deserializeCraftQueueRecipeData(serializedSubRecipeData)
end

recipeData:SetReagentsByCraftingReagentInfoTbl(GUTIL:Concat { serializedCraftQueueItem.requiredReagents, serializedCraftQueueItem.optionalReagents })
local requiredReagentsCraftingReagentInfos = {}
for _, serializedReagent in ipairs(serializedData.requiredReagents) do
local reagent = CraftSim.Reagent:Deserialize(serializedReagent)
tAppendAll(requiredReagentsCraftingReagentInfos, reagent:GetCraftingReagentInfos())
end

recipeData:SetReagentsByCraftingReagentInfoTbl(GUTIL:Concat { requiredReagentsCraftingReagentInfos, serializedCraftQueueItem.optionalReagents })

recipeData:SetNonQualityReagentsMax()

Expand Down
5 changes: 4 additions & 1 deletion Data/Classes/Reagent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ function CraftSim.Reagent:HasQuantityXTimes(crafterUID)
for q, reagentItem in pairs(self.items) do
if reagentItem.quantity > 0 then
--print("-" .. tostring(reagentItem.item:GetItemName()) .. "(" .. q .. ")")
local itemCount = CraftSim.CRAFTQ:GetItemCountFromCraftQueueCache(reagentItem.item:GetItemID(), true, false,
-- use original item if available
local itemID = (reagentItem.originalItem and reagentItem.originalItem:GetItemID()) or
reagentItem.item:GetItemID()
local itemCount = CraftSim.CRAFTQ:GetItemCountFromCraftQueueCache(itemID, true, false,
true,
crafterUID)
--print("--player item count: " .. tostring(itemCount))
Expand Down
25 changes: 17 additions & 8 deletions Data/Classes/ReagentData.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ function CraftSim.ReagentData:new(recipeData, schematicInfo)
end
end

---Serializes the required reagents list for sending via the addon channel
---@return CraftSim.Reagent.Serialized[]
function CraftSim.ReagentData:SerializeReagents()
function CraftSim.ReagentData:SerializeRequiredReagents()
return GUTIL:Map(self.requiredReagents, function(reagent)
return reagent:Serialize()
end)
Expand Down Expand Up @@ -412,7 +411,12 @@ function CraftSim.ReagentData:GetTooltipText(multiplier, crafterUID)
text = text .. inlineIcon
if not requiredReagent.hasQuality then
local reagentItem = requiredReagent.items[1]
local itemCount = CraftSim.CRAFTQ:GetItemCountFromCraftQueueCache(reagentItem.item:GetItemID(),
local itemID = reagentItem.item:GetItemID()
-- crafting tooltip should show original item
if reagentItem.originalItem then
itemID = reagentItem.originalItem:GetItemID()
end
local itemCount = CraftSim.CRAFTQ:GetItemCountFromCraftQueueCache(itemID,
true,
false, true, crafterUID)
local quantityText = f.r(tostring(requiredReagent.requiredQuantity * multiplier) ..
Expand All @@ -425,9 +429,9 @@ function CraftSim.ReagentData:GetTooltipText(multiplier, crafterUID)
local crafterText = ""
-- add crafterInfo text if reagent is supposed to be crafted by the player
-- check for quantity not needed for non quality items
if self.recipeData:IsSelfCraftedReagent(reagentItem.item:GetItemID()) then
if self.recipeData:IsSelfCraftedReagent(itemID) then
local optimizedReagentRecipeData = self.recipeData.optimizedSubRecipes
[reagentItem.item:GetItemID()]
[itemID]
if optimizedReagentRecipeData then
crafterText = f.white(" (" ..
optimizedReagentRecipeData:GetFormattedCrafterText(false, true, 12, 12) .. ")")
Expand All @@ -439,7 +443,12 @@ function CraftSim.ReagentData:GetTooltipText(multiplier, crafterUID)
local totalCount = requiredReagent:GetTotalQuantity()
local totalCountOK = totalCount * multiplier >= requiredReagent.requiredQuantity * multiplier
for qualityID, reagentItem in pairs(requiredReagent.items) do
local itemCount = CraftSim.CRAFTQ:GetItemCountFromCraftQueueCache(reagentItem.item:GetItemID(), true,
local itemID = reagentItem.item:GetItemID()
-- crafting tooltip should show original item
if reagentItem.originalItem then
itemID = reagentItem.originalItem:GetItemID()
end
local itemCount = CraftSim.CRAFTQ:GetItemCountFromCraftQueueCache(itemID, true,
false, true,
crafterUID)
local quantityText = f.r(
Expand All @@ -451,8 +460,8 @@ function CraftSim.ReagentData:GetTooltipText(multiplier, crafterUID)
local qualityIcon = GUTIL:GetQualityIconString(qualityID, 20, 20)
local crafterText = ""
-- add crafterInfo text if reagent is supposed to be crafted by the player
if self.recipeData:IsSelfCraftedReagent(reagentItem.item:GetItemID()) and reagentItem.quantity > 0 then
local optimizedReagentRecipeData = self.recipeData.optimizedSubRecipes[reagentItem.item:GetItemID()]
if self.recipeData:IsSelfCraftedReagent(itemID) and reagentItem.quantity > 0 then
local optimizedReagentRecipeData = self.recipeData.optimizedSubRecipes[itemID]
if optimizedReagentRecipeData then
crafterText = f.white(" (" ..
optimizedReagentRecipeData:GetFormattedCrafterText(false, true, 12, 12) .. ")")
Expand Down
27 changes: 20 additions & 7 deletions Data/Classes/ReagentItem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ CraftSim.ReagentItem = CraftSim.CraftSimObject:extend()

local print = CraftSim.DEBUG:SetDebugPrint(CraftSim.CONST.DEBUG_IDS.DATAEXPORT)

---@param itemID number
---@param qualityID number?
function CraftSim.ReagentItem:new(itemID, qualityID)
---@param originalItemID ItemID
---@param qualityID QualityID?
function CraftSim.ReagentItem:new(originalItemID, qualityID)
-- consider possible exception mappings
itemID = CraftSim.CONST.REAGENT_ID_EXCEPTION_MAPPING[itemID] or itemID
local alternativeItemID = CraftSim.CONST.REAGENT_ID_EXCEPTION_MAPPING[originalItemID]
local itemID = alternativeItemID or originalItemID

self.qualityID = qualityID
--- how much of that reagentItem has been allocated for this recipe
self.quantity = 0
self.item = Item:CreateFromItemID(itemID)
if alternativeItemID then
self.originalItem = Item:CreateFromItemID(originalItemID)
end
end

---@return CraftSim.ReagentListItem
Expand All @@ -27,7 +31,12 @@ function CraftSim.ReagentItem:GetAsReagentListItem()
end

function CraftSim.ReagentItem:Copy()
local copy = CraftSim.ReagentItem(self.item:GetItemID(), self.qualityID)
local copy = nil
if self.originalItem then
copy = CraftSim.ReagentItem(self.originalItem:GetItemID(), self.qualityID)
else
copy = CraftSim.ReagentItem(self.item:GetItemID(), self.qualityID)
end
copy.quantity = self.quantity

return copy
Expand All @@ -44,15 +53,17 @@ function CraftSim.ReagentItem:Clear()
self.quantity = 0
end

--- returns wether the player has enough of the given required item's allocations (times the multiplier)
--- returns wether the player has enough of the given required item's allocations (times the multiplier) for crafting
---@param multiplier number? default: 1
---@param crafterUID string
function CraftSim.ReagentItem:HasItem(multiplier, crafterUID)
multiplier = multiplier or 1
if not self.item then
return false
end
local itemID = self.item:GetItemID()
-- only count the item actually used in the recipe (originalItem if we have one)
-- in the case of e.g. rimefin tuna we want to count the non frosted one only (will be the original)
local itemID = (self.originalItem and self.originalItem:GetItemID()) or self.item:GetItemID()
local itemCount = CraftSim.CRAFTQ:GetItemCountFromCraftQueueCache(itemID, true, false, true,
crafterUID)
return itemCount >= (self.quantity * multiplier)
Expand All @@ -62,12 +73,14 @@ end
---@field qualityID number
---@field quantity number
---@field itemID number
---@field originalItemID number

function CraftSim.ReagentItem:Serialize()
local serizalized = {}
serizalized.qualityID = self.qualityID
serizalized.quantity = self.quantity
serizalized.itemID = self.item:GetItemID()
serizalized.originalItemID = self.originalItem and self.originalItem:GetItemID()
return serizalized
end

Expand Down
1 change: 1 addition & 0 deletions Data/News.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function CraftSim.NEWS:GET_NEWS(itemMap)
f.p .. f.bb("CraftResults"),
f.a .. "- Fixed a Typo in the Item Result Log saying 'Profit: :'",
f.a .. " instead of 'Saved Reagents:",
f.p .. "Fixed a bug leading to an error when moving a frame while crafting",
f.p .. "Russian Translation Update",
f.a .. "- Thanks to " .. f.bb("https://github.com/SerGlushko"),
}
Expand Down
48 changes: 20 additions & 28 deletions Modules/CraftQueue/CraftQueue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,20 @@ function CraftSim.CRAFTQ:OnCraftRecipe(recipeData, amount, enchantItemTarget)
CraftSim.CRAFTQ.currentlyCraftedRecipeData = recipeData
end

function CraftSim.CRAFTQ:GetNonSoulboundAlternativeItemID(itemID)
if GUTIL:isItemSoulbound(itemID) then
-- if item is soulbound check if there is non soulbound alternative item
local alternativeItemID = CraftSim.CONST.REAGENT_ID_EXCEPTION_MAPPING[itemID]
if alternativeItemID and not GUTIL:isItemSoulbound(alternativeItemID) then
print("Found non soulbound alt item: " .. tostring(alternativeItemID))
return alternativeItemID
else
return nil
end
end
return itemID
end

function CraftSim.CRAFTQ:CreateAuctionatorShoppingList()
if CraftSimOptions.craftQueueShoppingListPerCharacter then
CraftSim.CRAFTQ.CreateAuctionatorShoppingListPerCharacter()
Expand Down Expand Up @@ -428,8 +442,11 @@ function CraftSim.CRAFTQ.CreateAuctionatorShoppingListPerCharacter()
for crafterUID, reagentMap in pairs(reagentMapPerCharacter) do
--- convert to Auctionator Search Strings and deduct item count
local searchStrings = GUTIL:Map(reagentMap, function(info, itemID)
if GUTIL:isItemSoulbound(itemID) then
itemID = CraftSim.CRAFTQ:GetNonSoulboundAlternativeItemID(itemID)
if not itemID then
return nil
else
info.itemName = select(1, GetItemInfo(itemID)) -- 100% already loaded in this case when its used as alt item in ReagentItem
end

local itemCount = CraftSim.CACHE.ITEM_COUNT:Get(itemID, true, false, true, crafterUID)
Expand Down Expand Up @@ -521,7 +538,8 @@ function CraftSim.CRAFTQ.CreateAuctionatorShoppingListAll()

--- convert to Auctionator Search Strings and deduct item count (of all crafters total)
local searchStrings = GUTIL:Map(reagentMap, function(info, itemID)
if GUTIL:isItemSoulbound(itemID) then
itemID = CraftSim.CRAFTQ:GetNonSoulboundAlternativeItemID(itemID)
if not itemID then
return nil
end
-- subtract the total item count of all crafter's cached inventory
Expand Down Expand Up @@ -564,32 +582,6 @@ function CraftSim.CRAFTQ:GetItemCountFromCraftQueueCache(itemID, bank, uses, rea
return itemCount
end

---@param itemName string
---@param qualityID number | string?
---@param quantity number?
---@return string auctionatorShoppingListItemString
function CraftSim.CRAFTQ:GetAuctionatorShoppingListItemString(itemName, qualityID, quantity)
return '"' .. itemName .. '"' .. ';;0;0;0;0;0;0;0;0;;' .. (qualityID or '#') .. ';;' .. (quantity or '')
end

---@return string name
---@return number | nil qualityID
---@return number | nil quantity
function CraftSim.CRAFTQ:ParseAuctionatorShoppingListItemString(itemString)
local name, qualityID, quantity = itemString:match('"([^"]+)";;0;0;0;0;0;0;0;0;;(%S+);;(%d+)$')
if qualityID == '#' then
qualityID = nil
else
qualityID = tonumber(qualityID)
end
if quantity == '' then
quantity = nil
else
quantity = tonumber(quantity)
end
return name, qualityID, quantity
end

---@param recipeData CraftSim.RecipeData
function CraftSim.CRAFTQ:IsRecipeQueueable(recipeData)
return
Expand Down
2 changes: 1 addition & 1 deletion Modules/CustomerService/CustomerService.lua
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ function CraftSim.CUSTOMER_SERVICE.OnRecipeUpdateRequest(payload)
recipeIcon = recipeData.recipeIcon,
supportsQualities = recipeData.supportsQualities,
upgradeChance = upgradeChance,
reagents = recipeData.reagentData:SerializeReagents(),
reagents = recipeData.reagentData:SerializeRequiredReagents(),
optionalReagents = recipeData.reagentData:SerializeOptionalReagentSlots(),
finishingReagents = recipeData.reagentData:SerializeFinishingReagentSlots(),
resultData = recipeData.resultData:Serialize(),
Expand Down
2 changes: 1 addition & 1 deletion Util/Const.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ CraftSim.CONST.BASE_RECRAFT_RECIPE_IDS = {
}

CraftSim.CONST.REAGENT_ID_EXCEPTION_MAPPING = {
[199345] = 200074 -- frosted rimefin tuna to rimefin tuna, due to frosted generating it, and rimefin tuna being soulbound
[199345] = 200074, -- frosted rimefin tuna to rimefin tuna, due to frosted generating it, and rimefin tuna being soulbound
}

CraftSim.CONST.EXCEPTION_ORDER_ITEM_IDS = {
Expand Down

0 comments on commit efeef0e

Please sign in to comment.