updted allcardsbag

This commit is contained in:
Chr1Z93 2024-03-25 17:28:26 +01:00
parent d930b97da8
commit f41f4f9107

View File

@ -57,25 +57,20 @@ function onObjectLeaveContainer(container, _)
end end
end end
-- Create the card indexes by iterating all cards in the bag, parsing their -- Create the card indexes by iterating all cards in the bag, parsing their metadata
-- metadata, and creating the keyed lookup tables for the cards. This is a -- and creating the keyed lookup tables for the cards. This is a coroutine which will
-- coroutine which will spread the workload by processing 20 cards before -- spread the workload by processing 20 cards before yielding.
-- yielding.
function buildIndex() function buildIndex()
local cardCount = 0 local cardCount = 0
indexingDone = false indexingDone = false
-- process the allcardsbag itself -- process the allcardsbag itself
for _, cardData in ipairs(self.getData().ContainedObjects) do for _, cardData in ipairs(self.getData().ContainedObjects) do
-- using the more efficient 'json.parse()' to speed this process up addCardToIndex(cardData)
local cardMetadata = json.parse(cardData.GMNotes) cardCount = cardCount + 1
if cardMetadata then if cardCount > 19 then
addCardToIndex(cardData, cardMetadata) cardCount = 0
cardCount = cardCount + 1 coroutine.yield(0)
if cardCount > 19 then
cardCount = 0
coroutine.yield(0)
end
end end
end end
@ -88,46 +83,44 @@ function buildIndex()
-- process containers -- process containers
if cardData.ContainedObjects then if cardData.ContainedObjects then
for _, deepCardData in ipairs(cardData.ContainedObjects) do for _, deepCardData in ipairs(cardData.ContainedObjects) do
local deepCardMetadata = json.parse(deepCardData.GMNotes) addCardToIndex(deepCardData)
if deepCardMetadata ~= nil then
addCardToIndex(deepCardData, deepCardMetadata)
cardCount = cardCount + 1
if cardCount > 9 then
cardCount = 0
coroutine.yield(0)
end
end
end
-- process single cards
else
local cardMetadata = json.parse(cardData.GMNotes)
if cardMetadata ~= nil then
addCardToIndex(cardData, cardMetadata)
cardCount = cardCount + 1 cardCount = cardCount + 1
if cardCount > 9 then if cardCount > 19 then
cardCount = 0 cardCount = 0
coroutine.yield(0) coroutine.yield(0)
end end
end end
-- process single cards
else
addCardToIndex(cardData)
cardCount = cardCount + 1
if cardCount > 19 then
cardCount = 0
coroutine.yield(0)
end
end end
end end
end end
buildSupplementalIndexes() buildSupplementalIndexes()
indexingDone = true indexingDone = true
return 1 return 1
end end
-- Adds a card to any indexes it should be a part of, based on its metadata. -- Adds a card to any indexes it should be a part of, based on its metadata
---@param cardData table TTS object data for the card ---@param cardData table TTS object data for the card
---@param cardMetadata table SCED metadata for the card function addCardToIndex(cardData)
function addCardToIndex(cardData, cardMetadata) -- using the more efficient 'json.parse()' to speed this process up
local dataTable = { data = cardData, metadata = cardMetadata } local cardMetadata = json.parse(cardData.GMNotes)
if not cardMetadata then return end
-- use the ZoopGuid as fallback if no id present -- use the ZoopGuid as fallback if no id present
cardIdIndex[cardMetadata.id or cardMetadata.TtsZoopGuid] = dataTable cardIdIndex[cardMetadata.id or cardMetadata.TtsZoopGuid] = { data = cardData, metadata = cardMetadata }
-- also add data for alternate ids
if cardMetadata.alternate_ids ~= nil then if cardMetadata.alternate_ids ~= nil then
for _, alternateId in ipairs(cardMetadata.alternate_ids) do for _, alternateId in ipairs(cardMetadata.alternate_ids) do
cardIdIndex[alternateId] = dataTable cardIdIndex[alternateId] = { data = cardData, metadata = cardMetadata }
end end
end end
end end
@ -135,11 +128,9 @@ end
function buildSupplementalIndexes() function buildSupplementalIndexes()
for cardId, card in pairs(cardIdIndex) do for cardId, card in pairs(cardIdIndex) do
local cardMetadata = card.metadata local cardMetadata = card.metadata
-- If the ID key and the metadata ID don't match this is a duplicate card created by an -- If the ID key and the metadata ID don't match this is a duplicate card created by an alternate_id, and we should skip it
-- alternate_id, and we should skip it
if cardId == cardMetadata.id then if cardId == cardMetadata.id then
-- Add card to the basic weakness list, if appropriate. Some weaknesses have -- Add card to the basic weakness list, if appropriate. Some weaknesses have multiple copies, and are added multiple times
-- multiple copies, and are added multiple times
if cardMetadata.weakness then if cardMetadata.weakness then
table.insert(uniqueWeaknessList, cardMetadata.id) table.insert(uniqueWeaknessList, cardMetadata.id)
if cardMetadata.basicWeaknessCount ~= nil then if cardMetadata.basicWeaknessCount ~= nil then
@ -149,56 +140,31 @@ function buildSupplementalIndexes()
end end
end end
-- Add the card to the appropriate class and level indexes -- Excludes signature cards (which have no class or level)
local isGuardian = false if cardMetadata.class ~= nil and cardMetadata.level ~= nil then
local isSeeker = false local upgradeKey
local isMystic = false if cardMetadata.level > 0 then
local isRogue = false
local isSurvivor = false
local isNeutral = false
local upgradeKey
-- Excludes signature cards (which have no class or level) and alternate
-- ID entries
if (cardMetadata.class ~= nil and cardMetadata.level ~= nil) then
isGuardian = string.match(cardMetadata.class, "Guardian")
isSeeker = string.match(cardMetadata.class, "Seeker")
isMystic = string.match(cardMetadata.class, "Mystic")
isRogue = string.match(cardMetadata.class, "Rogue")
isSurvivor = string.match(cardMetadata.class, "Survivor")
isNeutral = string.match(cardMetadata.class, "Neutral")
if (cardMetadata.level > 0) then
upgradeKey = "-upgrade" upgradeKey = "-upgrade"
else else
upgradeKey = "-level0" upgradeKey = "-level0"
end end
if (isGuardian) then
table.insert(classAndLevelIndex["Guardian"..upgradeKey], cardMetadata.id) -- parse classes (separated by "|") and add the card to the appropriate class and level indices
end for str in cardMetadata.class:gmatch("([^|]+)") do
if (isSeeker) then table.insert(classAndLevelIndex[str .. upgradeKey], cardMetadata.id)
table.insert(classAndLevelIndex["Seeker"..upgradeKey], cardMetadata.id)
end
if (isMystic) then
table.insert(classAndLevelIndex["Mystic"..upgradeKey], cardMetadata.id)
end
if (isRogue) then
table.insert(classAndLevelIndex["Rogue"..upgradeKey], cardMetadata.id)
end
if (isSurvivor) then
table.insert(classAndLevelIndex["Survivor"..upgradeKey], cardMetadata.id)
end
if (isNeutral) then
table.insert(classAndLevelIndex["Neutral"..upgradeKey], cardMetadata.id)
end end
-- add to cycle index
local cycleName = cardMetadata.cycle local cycleName = cardMetadata.cycle
if cycleName ~= nil then if cycleName ~= nil then
cycleName = string.lower(cycleName) cycleName = string.lower(cycleName)
if string.match(cycleName, "return") then
cycleName = string.sub(cycleName, 11) -- remove "return to " from cycle names
end cycleName = cycleName:gsub("return to ", "")
if cycleName == "the night of the zealot" then
cycleName = "core" -- override cycle name for night of the zealot
end cycleName = cycleName:gsub("the night of the zealot", "core")
if cycleIndex[cycleName] == nil then if cycleIndex[cycleName] == nil then
cycleIndex[cycleName] = { } cycleIndex[cycleName] = { }
end end
@ -207,29 +173,34 @@ function buildSupplementalIndexes()
end end
end end
end end
-- sort class and level indices
for _, indexTable in pairs(classAndLevelIndex) do for _, indexTable in pairs(classAndLevelIndex) do
table.sort(indexTable, cardComparator) table.sort(indexTable, cardComparator)
end end
-- sort cycle indices
for _, indexTable in pairs(cycleIndex) do for _, indexTable in pairs(cycleIndex) do
table.sort(indexTable) table.sort(indexTable)
end end
-- sort weakness indices
table.sort(basicWeaknessList, cardComparator) table.sort(basicWeaknessList, cardComparator)
table.sort(uniqueWeaknessList, cardComparator) table.sort(uniqueWeaknessList, cardComparator)
end end
-- Comparison function used to sort the class card bag indexes. Sorts by card -- Comparison function used to sort the class card bag indexes. Sorts by card level, then name, then subname.
-- level, then name, then subname.
function cardComparator(id1, id2) function cardComparator(id1, id2)
local card1 = cardIdIndex[id1] local card1 = cardIdIndex[id1]
local card2 = cardIdIndex[id2] local card2 = cardIdIndex[id2]
if (card1.metadata.level ~= card2.metadata.level) then if card1.metadata.level ~= card2.metadata.level then
return card1.metadata.level < card2.metadata.level return card1.metadata.level < card2.metadata.level
end elseif card1.data.Nickname ~= card2.data.Nickname then
if (card1.data.Nickname ~= card2.data.Nickname) then
return card1.data.Nickname < card2.data.Nickname return card1.data.Nickname < card2.data.Nickname
else
return card1.data.Description < card2.data.Description
end end
return card1.data.Description < card2.data.Description
end end
function isIndexReady() function isIndexReady()