updated code

This commit is contained in:
Chr1Z93 2024-10-18 21:54:07 +02:00
parent ee12ee10ca
commit 7627643221
2 changed files with 106 additions and 43 deletions

View File

@ -59,14 +59,19 @@ function processCard(cardData)
local customDeckId, customDeckData = next(cardData["CustomDeck"]) local customDeckId, customDeckData = next(cardData["CustomDeck"])
-- if this card already has the correct back settings -- if this card already has the correct back settings
if customDeckData["BackURL"] == newBack and customDeckData["BackIsHidden"] then return false end if customDeckData["BackURL"] == newBack and customDeckData["BackIsHidden"] then
return false
end
-- skip cards with decksheets as back -- skip cards with decksheets as back
if (customDeckData["NumHeight"] == 1 and customDeckData["NumWidth"] == 1) if (customDeckData["NumHeight"] ~= 1 or customDeckData["NumWidth"] ~= 1)
or customDeckData["UniqueBack"] == false then and customDeckData["UniqueBack"] then
return false
end
-- update data
customDeckData["BackIsHidden"] = true customDeckData["BackIsHidden"] = true
customDeckData["BackURL"] = newBack customDeckData["BackURL"] = newBack
deckChanges[customDeckId] = newBack deckChanges[customDeckId] = newBack
end
return true return true
end end

View File

@ -3,17 +3,14 @@ local searchLib = require("util/SearchLib")
local idList = {} local idList = {}
local cardWidth = 250
local cardHeight = cardWidth * 1.4
local galleryTemplateStart = [[<style> local galleryTemplateStart = [[<style>
.sced-image { .sced-spacer { height: 60px; }
max-width: 49%; .sced-card { width: 250px; border-radius: 5px; margin: 0; transition: transform 0.25s ease-in-out; }
height: auto; .sced-slice { background-repeat: no-repeat; }
border-radius: 8px; .sced-card:hover { transform: scale(1.45); }
box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.4); .sced-card-container {
margin: 0;
transition: transform 0.3s ease-in-out;
}
.sced-image:hover { transform: scale(1.45); }
.sced-container {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
justify-content: center; justify-content: center;
@ -21,19 +18,18 @@ local galleryTemplateStart = [[<style>
margin-top: 5px; margin-top: 5px;
padding: 10px; padding: 10px;
} }
.sced-spacer { height: 50px; }
@media (max-width: 600px) { @media (max-width: 600px) {
.sced-image { max-width: 100%; }
.sced-image:hover { transform: none; }
.sced-spacer { height: 0px; } .sced-spacer { height: 0px; }
.sced-card { width: 100%; height: auto; }
.sced-card:hover { transform: none; }
} }
</style> </style>
<div class="sced-container">\n]] <div class="sced-container">]]
local galleryTemplateMiddle = [[ <img class="sced-image" src="{{IMAGE_URL}}"/>\n]] local templateCardRegular = [[ <img class="sced-card sced-regular" src="{{IMAGE_URL}}"/>]]
local galleryTemplateEnd = [[</div>\n<div class="sced-spacer"></div>]] local templateCardSlice = [[ <div class="sced-card sced-card-slice" style="background-image: url('{{IMAGE_URL}}'); background-size: {{SHEET_SIZE_1}}px {{SHEET_SIZE_2}}px; background-position: {{OFFSET_1}}px {{OFFSET_2}}px;"></div>]]
local galleryTemplateEnd = [[</div>]] .. "\n" .. [[<div class="sced-spacer"></div>]]
function onLoad() function onLoad()
-- "generate" button
local buttonParameters = {} local buttonParameters = {}
buttonParameters.function_owner = self buttonParameters.function_owner = self
buttonParameters.height = 200 buttonParameters.height = 200
@ -70,7 +66,7 @@ function generate(_, playerColor)
end end
-- sort the idList -- sort the idList
table.sort(idList, sortById) table.sort(idList, sortByMetadata)
-- construct the string (new line for each instruction) -- construct the string (new line for each instruction)
local descriptionParts = {} local descriptionParts = {}
@ -108,17 +104,11 @@ function generate(_, playerColor)
-- maybe create a card gallery for included cards -- maybe create a card gallery for included cards
if createGallery then if createGallery then
table.insert(descriptionParts, "\n\n") table.insert(descriptionParts, "\n\n" .. galleryTemplateStart .. "\n")
table.insert(descriptionParts, galleryTemplateStart)
-- add cards addedImages = {}
for _, entry in ipairs(idList) do for _, entry in ipairs(idList) do
if entry.face then maybeAddImageToGallery(descriptionParts, entry)
table.insert(descriptionParts, galleryTemplateMiddle:gsub("{{IMAGE_URL}}", entry.face))
end
if entry.back then
table.insert(descriptionParts, galleryTemplateMiddle:gsub("{{IMAGE_URL}}", entry.back))
end
end end
table.insert(descriptionParts, galleryTemplateEnd) table.insert(descriptionParts, galleryTemplateEnd)
end end
@ -148,7 +138,7 @@ function generate(_, playerColor)
"\nAfter doing so, the Deck Importer will be able to spawn these cards IF they are added to the mod's card index (for example by " .. "\nAfter doing so, the Deck Importer will be able to spawn these cards IF they are added to the mod's card index (for example by " ..
"throwing them into the 'Additional Cards Box' next to the Player Cards Panel in the upper left corner of the table." .. "throwing them into the 'Additional Cards Box' next to the Player Cards Panel in the upper left corner of the table." ..
"\nIt also supports '- remove:' instructions to automatically remove placeholder cards.\nIf you are using a custom " .. "\nIt also supports '- remove:' instructions to automatically remove placeholder cards.\nIf you are using a custom " ..
"investigator, make sure to use a 'remove' instruction to remove the original one.\n" .. timestamp .. "\n" .. description "investigator, make sure to use a 'remove' instruction to remove the original one.\n\n" .. timestamp .. "\n" .. description
}) })
end end
@ -161,19 +151,74 @@ function getIdFromData(metadata)
end end
end end
function processCard(cardData, playerColor) function maybeAddImageToGallery(t, data)
local id = getIdFromData(JSON.decode(cardData.GMNotes) or {}) if data.sheetH and data.sheetW then
if id then -- add cards from decksheets
local cardDetails = { id = id, name = cardData.Nickname } local element = templateCardSlice
element = element:gsub("{{SHEET_SIZE_1}}", data.sheetW * cardWidth):gsub("{{SHEET_SIZE_2}}", data.sheetH * cardHeight)
element = element:gsub("{{OFFSET_1}}", data.col * cardWidth):gsub("{{OFFSET_2}}", data.row * cardHeight)
-- if this is not a decksheet, add its URL maybeAddCardSlice(t, element, data.face, data.cardId)
if data.uniqueBack then
maybeAddCardSlice(t, element, data.back, data.cardId)
else
maybeAddCardRegular(t, data.back)
end
else
-- add regular cards
maybeAddCardRegular(t, data.face)
maybeAddCardRegular(t, data.back)
end
end
function maybeAddCardRegular(t, url)
if not url then return end
if not addedImages[url] then
addedImages[url] = true
table.insert(t, templateCardRegular:gsub("{{IMAGE_URL}}", url) .. "\n")
end
end
function maybeAddCardSlice(t, element, url, cardId)
if not url then return end
-- initialize table
addedImages[url] = addedImages[url] or {}
if not addedImages[url][cardId] then
addedImages[url][cardId] = true
table.insert(t, element:gsub("{{IMAGE_URL}}", url) .. "\n")
end
end
function processCard(cardData, playerColor)
local md = JSON.decode(cardData.GMNotes) or {}
local id = getIdFromData(md)
if id then
local cardDetails = {
id = id,
name = cardData.Nickname,
metadata = md,
cardId = math.floor(cardData.CardID % 100)
}
-- add images unless minicard
if md.type ~= "Minicard" then
local _, customDeckData = next(cardData["CustomDeck"]) local _, customDeckData = next(cardData["CustomDeck"])
if customDeckData["NumHeight"] == 1 and customDeckData["NumWidth"] == 1 then
cardDetails.face = customDeckData["FaceURL"] cardDetails.face = customDeckData["FaceURL"]
if customDeckData["NumHeight"] > 1 or customDeckData["NumWidth"] > 1 then
cardDetails.sheetH = customDeckData["NumHeight"]
cardDetails.sheetW = customDeckData["NumWidth"]
cardDetails.row, cardDetails.col = getGridPosition(cardDetails.cardId, customDeckData["NumWidth"])
end
-- also add the back if it's not one of the regular backs -- also add the back if it's not one of the regular backs
if customDeckData["BackURL"] ~= CARD_BACK_URL.PlayerCard and customDeckData["BackURL"] ~= CARD_BACK_URL.PlayerCard then if customDeckData["BackURL"] ~= CARD_BACK_URL.PlayerCard and customDeckData["BackURL"] ~= CARD_BACK_URL.PlayerCard then
cardDetails.back = customDeckData["BackURL"] cardDetails.back = customDeckData["BackURL"]
cardDetails.uniqueBack = customDeckData["UniqueBack"]
end end
end end
@ -183,13 +228,26 @@ function processCard(cardData, playerColor)
end end
end end
function sortById(a, b) function sortByMetadata(a, b)
local numA = tonumber(a.id) local numA = tonumber(a.id)
local numB = tonumber(b.id) local numB = tonumber(b.id)
if numA and numB then local isInvestigatorA = (a.metadata.type == "Investigator")
local isInvestigatorB = (b.metadata.type == "Investigator")
if isInvestigatorA and not isInvestigatorB then
return true
elseif isInvestigatorB and not isInvestigatorA then
return false
elseif numA and numB then
return numA < numB return numA < numB
else else
return a.name < b.name return a.name < b.name
end end
end end
function getGridPosition(index, columns)
local row = math.floor(index / columns) + 1
local column = (index % columns) + 1
return row, column
end