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
-- Create the card indexes by iterating all cards in the bag, parsing their
-- metadata, and creating the keyed lookup tables for the cards. This is a
-- coroutine which will spread the workload by processing 20 cards before
-- yielding.
-- Create the card indexes by iterating all cards in the bag, parsing their metadata
-- and creating the keyed lookup tables for the cards. This is a coroutine which will
-- spread the workload by processing 20 cards before yielding.
function buildIndex()
local cardCount = 0
indexingDone = false
-- process the allcardsbag itself
for _, cardData in ipairs(self.getData().ContainedObjects) do
-- using the more efficient 'json.parse()' to speed this process up
local cardMetadata = json.parse(cardData.GMNotes)
if cardMetadata then
addCardToIndex(cardData, cardMetadata)
cardCount = cardCount + 1
if cardCount > 19 then
cardCount = 0
coroutine.yield(0)
end
addCardToIndex(cardData)
cardCount = cardCount + 1
if cardCount > 19 then
cardCount = 0
coroutine.yield(0)
end
end
@ -88,46 +83,44 @@ function buildIndex()
-- process containers
if cardData.ContainedObjects then
for _, deepCardData in ipairs(cardData.ContainedObjects) do
local deepCardMetadata = json.parse(deepCardData.GMNotes)
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)
addCardToIndex(deepCardData)
cardCount = cardCount + 1
if cardCount > 9 then
if cardCount > 19 then
cardCount = 0
coroutine.yield(0)
end
end
-- process single cards
else
addCardToIndex(cardData)
cardCount = cardCount + 1
if cardCount > 19 then
cardCount = 0
coroutine.yield(0)
end
end
end
end
buildSupplementalIndexes()
indexingDone = true
return 1
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 cardMetadata table SCED metadata for the card
function addCardToIndex(cardData, cardMetadata)
local dataTable = { data = cardData, metadata = cardMetadata }
function addCardToIndex(cardData)
-- using the more efficient 'json.parse()' to speed this process up
local cardMetadata = json.parse(cardData.GMNotes)
if not cardMetadata then return end
-- 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
for _, alternateId in ipairs(cardMetadata.alternate_ids) do
cardIdIndex[alternateId] = dataTable
cardIdIndex[alternateId] = { data = cardData, metadata = cardMetadata }
end
end
end
@ -135,11 +128,9 @@ end
function buildSupplementalIndexes()
for cardId, card in pairs(cardIdIndex) do
local cardMetadata = card.metadata
-- 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
-- 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
if cardId == cardMetadata.id then
-- Add card to the basic weakness list, if appropriate. Some weaknesses have
-- multiple copies, and are added multiple times
-- Add card to the basic weakness list, if appropriate. Some weaknesses have multiple copies, and are added multiple times
if cardMetadata.weakness then
table.insert(uniqueWeaknessList, cardMetadata.id)
if cardMetadata.basicWeaknessCount ~= nil then
@ -149,56 +140,31 @@ function buildSupplementalIndexes()
end
end
-- Add the card to the appropriate class and level indexes
local isGuardian = false
local isSeeker = false
local isMystic = false
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
-- Excludes signature cards (which have no class or level)
if cardMetadata.class ~= nil and cardMetadata.level ~= nil then
local upgradeKey
if cardMetadata.level > 0 then
upgradeKey = "-upgrade"
else
upgradeKey = "-level0"
end
if (isGuardian) then
table.insert(classAndLevelIndex["Guardian"..upgradeKey], cardMetadata.id)
end
if (isSeeker) then
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)
-- parse classes (separated by "|") and add the card to the appropriate class and level indices
for str in cardMetadata.class:gmatch("([^|]+)") do
table.insert(classAndLevelIndex[str .. upgradeKey], cardMetadata.id)
end
-- add to cycle index
local cycleName = cardMetadata.cycle
if cycleName ~= nil then
cycleName = string.lower(cycleName)
if string.match(cycleName, "return") then
cycleName = string.sub(cycleName, 11)
end
if cycleName == "the night of the zealot" then
cycleName = "core"
end
-- remove "return to " from cycle names
cycleName = cycleName:gsub("return to ", "")
-- override cycle name for night of the zealot
cycleName = cycleName:gsub("the night of the zealot", "core")
if cycleIndex[cycleName] == nil then
cycleIndex[cycleName] = { }
end
@ -207,29 +173,34 @@ function buildSupplementalIndexes()
end
end
end
-- sort class and level indices
for _, indexTable in pairs(classAndLevelIndex) do
table.sort(indexTable, cardComparator)
end
-- sort cycle indices
for _, indexTable in pairs(cycleIndex) do
table.sort(indexTable)
end
-- sort weakness indices
table.sort(basicWeaknessList, cardComparator)
table.sort(uniqueWeaknessList, cardComparator)
end
-- Comparison function used to sort the class card bag indexes. Sorts by card
-- level, then name, then subname.
-- Comparison function used to sort the class card bag indexes. Sorts by card level, then name, then subname.
function cardComparator(id1, id2)
local card1 = cardIdIndex[id1]
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
end
if (card1.data.Nickname ~= card2.data.Nickname) then
elseif card1.data.Nickname ~= card2.data.Nickname then
return card1.data.Nickname < card2.data.Nickname
else
return card1.data.Description < card2.data.Description
end
return card1.data.Description < card2.data.Description
end
function isIndexReady()