Module:Card: Difference between revisions

right, trimmed out most of the garbage
No edit summary
 
(28 intermediate revisions by 4 users not shown)
Line 1: Line 1:
local p = {}
local p = {}
local lib = require('Module:Feature')
local lib = require('Module:Feature')
local ne = lib.isNotEmpty
local ne = lib.isNotEmpty
local namespace = require('Module:Namespace_detect')._main
local TemplateData = require('Module:TemplateData')
local getTemplateArgs = require('Module:Parser').getTemplateArgs
local array = require('Module:Array')
local array = require('Module:Array')
local CentralData = require('Module:CentralData')


local allData = {
local allData = CentralData.dataSheets
--{'Neighbor', mw.loadData('Module:CentralData/Neighbors')},
 
{'Creature', mw.loadData('Module:CentralData/Creatures')},
local CARD_ARGS = {
--{'Furniture', mw.loadData('Module:CentralData/Furniture')},
{
name = 'name',
alias = 1,
default = 'Unknown',
},
{
name = 'label',
alias = {2, 'text', 'count', 'amount', 'x'},
},
{
name = 'show_label',
alias = 'show_text',
type = '1',
},
{
name = 'quality',
alias = 'rarity',
default = 'Common',
},
{
name = 'type',
},
{
name = 'split',
},
{
name = 'suffix',
default = '',
},
{
name = 'image',
},
{
name = 'image_size',
alias = {'imageSize', 'size'},
},
{
name = 'image_ext',
alias = {'imageExt', 'ext', 'extension'},
default = 'png',
},
{
name = 'link',
},
{
name = 'link_suffix',
default = '',
},
{
name = 'no_link',
type = '1',
},
{
name = 'caption',
},
{
name = 'show_caption',
type = '1',
},
{
name = 'caption_width',
alias = 'captionWidth',
},
{
name = 'mini',
type = '1',
},
{
name = 'first',
type = '1',
},
{
name = 'up',
alias = 'UP',
type = '1',
},
{
name = 'notice',
},
}
}
local COLON = ':'


function p.main(frame)
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
local args = require('Module:Arguments').getArgs(frame, {
wrapper = { 'Template:Card' },
wrapper = {'Template:Card'},
parentOnly = false
parentOnly = false
})
})
return p._main(args)
return p._main(args)
end
end


function p._main(args)
local function getDataByName(dataType, dataTable, name)
local item = args[1] or args.name or 'Unknown'
local data = dataTable[name]
local label = args[2] or args.label or args.text or args.count or args.amount or args.x or nil
 
if data then
-- Reset agent-specific data to prevent carry-over from previous cards
return data
local agQuality, nbGalaxy, crQuality, crGalaxy, outQuality, notice = nil, nil, nil, nil, nil, nil
end
 
-- Caption Params:
local normalizedType = tostring(dataType or ''):lower()
local caption = args.caption or nil
if normalizedType == 'neighbor' or normalizedType == 'neighbors' then
local caption_width = args.caption_width or args.captionWidth or nil
return nil
end
    -- Image File Params:
 
local type, data = p.getTypeAndData(item, args.type)
for quality, names in pairs(dataTable) do
local suffix = args.suffix or ''
if type(names) == 'table' then
local imageSize = args.image_size or args.imageSize or lib.ternary(args.mini == '1', 48, 70)
for _, itemName in ipairs(names) do
local imageExt = args.image_ext or args.imageExt or 'png'
if itemName == name then
return {
-- Link Params:
quality = quality
local link = args.link or lib.ternary(item == 'Unknown', '', item)
}
local link_suffix = args.link_suffix or ''
end
end
-- Rank Param:
local quality = args.quality or args.rarity or nil
-- Try to get Rank From Data
if lib.isEmpty(quality) and data then
if data.quality then
quality = data.quality
end
end
end
end
 
-- If quality is still empty, set it to "Gray"
return nil
if lib.isEmpty(quality) then
end
quality = 'Gray'
 
function p.getTypeAndData(name, requestedType)
local img_type = requestedType or 'Item'
local img_data = nil
 
for _, v in ipairs(allData) do
local dataType = v[1]
local dataTable = v[2]
 
if lib.isEmpty(requestedType) or dataType == requestedType then
local data = getDataByName(dataType, dataTable, name)
 
if data then
img_type = dataType
img_data = data
break
end
end
end
end
 
-- If quality contains a semicolon, then split and get first
return img_type, img_data
if string.find(quality, ";") then
end
 
local function normalizeBool(value)
return value == true or value == '1' or value == 1 or value == 'yes'
end
 
local function normalizeClassName(value)
return tostring(value or '')
:lower()
:gsub('%s+', '-')
end
 
local function normalizeQuality(quality)
quality = tostring(quality or '')
 
if quality:find(';', 1, true) then
quality = array._main({
quality = array._main({
quality,
quality,
Line 66: Line 170:
})
})
end
end
 
-- Make sure quality is uppercase
if lib.isEmpty(quality) then
quality = quality:upper()
quality = 'Common'
end
-- Modifiers:
 
local isFirst = args.first or false
return normalizeClassName(quality)
local isUp = args.UP or args.up or false
end
 
-- Attribute/galaxy
local function stripLinkPrefix(value)
local galaxy = args.galaxy or nil
value = tostring(value or '')
 
if lib.isEmpty(suffix) then
-- Converts "Luca: Myriad Meadows" to "Myriad Meadows".
local lcType = type:lower()
return mw.text.trim(value:gsub('^.-:%s*', '', 1))
if (lcType == 'neighbor') then
end
suffix = ' Icon'
 
--Grabs Agent Rank, Attribute and Specialty from Data to display icons
local function setCaptionWidth(node, width)
agQuality = data['quality']
if width == 'auto' or lib.isEmpty(width) then
agAttribute = data['attribute']
node:addClass('auto-width')
nbGalaxy = data['galaxy']
else
node:css('width', width)
--In Full-Size Card-Mode, if there is no specified text, Agents will default to using their full name displayed underneath.
end
if args.mini ~= "1" then
end
if (label == nil) then label = item end
 
-- Generates the second icon for a split card.
local function makeSplitIcon(name, mini)
name = tostring(name or '')
 
local size = mini and 74 or 56
 
return '[[File:' ..
lib.sanitizeFileName(name) ..
' Icon.png|' ..
size .. 'x' .. size ..
'px|link=' ..
name ..
']]'
end
 
function p._processArgs(args)
local out = TemplateData.processArgs(args, CARD_ARGS)
 
-- Basic identity
out.name = out.name or 'Unknown'
out.split = out.split or ''
 
-- Use only a manually supplied type for lookup.
local requestedType = out.nonDefaults
and out.nonDefaults.type
or nil
 
out.type, out.data = p.getTypeAndData(
out.name,
requestedType
)
 
-- Base name for assets.
-- Removes a trailing disambiguation such as "(Currency)".
out.image_name = out.name--:gsub('%s*%([^()]*%)$', '')
 
-- Boolean parameters
out.mini = normalizeBool(out.mini)
out.first = normalizeBool(out.first)
out.up = normalizeBool(out.up)
out.no_link = normalizeBool(out.no_link)
out.show_label = normalizeBool(out.show_label)
out.show_caption = normalizeBool(out.show_caption)
 
-- Image defaults
out.suffix = out.suffix or ''
out.image_ext = out.image_ext or 'png'
 
if lib.isEmpty(out.image_size) then
out.image_size = out.mini and 48 or 70
end
 
if lib.isEmpty(out.suffix) then
out.suffix = ' Icon'
end
 
-- Data-driven defaults
if out.data then
if out.data.name then
out.defaults.label = out.data.name
out.defaults.caption = out.data.name
end
 
if out.data.title then
out.defaults.caption = out.data.title
end
 
if out.data.quality then
out.defaults.quality = out.data.quality
 
if not (out.nonDefaults and out.nonDefaults.quality) then
out.quality = out.data.quality
end
end
-- In Mini-Size Card-Mode, if there is no specified text, Agents will use their "shortName" pulled from /data as the displayed text.
end
-- In cases where Agents do not have a "shortName", their full name will be displayed instead.
end
if args.mini == "1" then
 
if (label == nil) and data['shortName'] ~= nil then label = data['shortName']
-- Quality / rarity
elseif (label == nil) and data['shortName'] == nil then label = item
out.quality = normalizeQuality(out.quality)
end
 
-- Label handling
if out.show_label then
out.label = out.data
and (out.data.name or out.data.title)
or out.name
end
 
if out.label == '0' then
out.label = nil
end
 
-- Type-specific behavior
local lcType = tostring(out.type or ''):lower()
 
if lcType == 'neighbor' then
if lib.isEmpty(out.label) then
if out.mini and out.data and out.data.shortName then
out.label = out.data.shortName
elseif not out.mini then
out.label = out.name
end
end
-- hackish workaround to account for the slight transparent borders on hoyolab agent icons
if (tonumber(imageSize) ~= nil) then
imageSize = tonumber(imageSize) + 2
end
elseif (lcType == 'creature') then
suffix = ' Icon'
--Grabs Creature Rank and Galaxy from Data to display icons
crQuality = data['quality']
crGalaxy = data['galaxy']
elseif (lcType == 'npc') then
suffix = ' Icon'
elseif (lcType == 'clothing') then
suffix = ' Icon'
--Grabs Outfit Rank from Data to display icons
outQuality = data['quality']
end
end
if tonumber(out.image_size) then
out.image_size = tonumber(out.image_size) + 2
end
end
if lcType == 'redirect' then
out.defaults.link = stripLinkPrefix(out.name)
if not (out.nonDefaults and out.nonDefaults.link) then
out.link = stripLinkPrefix(out.name)
end
end
-- Caption defaults
if out.show_caption and lib.isEmpty(out.caption) then
if out.data then
out.caption = out.data.title
or out.data.name
or out.name
else
out.caption = out.name
end
end
-- Link handling
if out.link == nil then
out.link = lib.ternary(
out.name == 'Unknown',
'',
out.name
)
end
out.link_suffix = out.link_suffix or ''
out.final_link = out.link .. out.link_suffix
if out.no_link then
out.final_link = ''
end
end


local final_file = args.image or type .. ' ' .. lib.sanitizeFileName(item) .. suffix .. '.' .. imageExt
-- Main image filename
if ne(out.image) then
local final_link = link .. link_suffix
out.final_file = out.image
else
local file = '[[File:' .. final_file .. '|' .. imageSize .. 'px|alt=' .. item .. '|link=' .. final_link .. ']]'
out.final_file =
lib.sanitizeFileName(out.image_name) ..
local result = mw.html.create()
out.suffix ..
'.' ..
local card_container = result:tag('div')
out.image_ext
end
 
-- Main image wikitext
local linkPart = out.no_link
and '|link='
or ('|link=' .. out.final_link)
 
out.image_wikitext = table.concat({
'[[File:',
out.final_file,
'|',
out.image_size,
'px|alt=',
out.data and (out.data.name or out.data.title)
or out.name,
linkPart,
']]'
})
 
return out
end
 
function p._main(args)
local a = p._processArgs(args)
local isSplit = ne(a.split)
 
-- Outer container
local node_container = mw.html.create('div')
:addClass('card-container')
:addClass('card-container')
:addClass('card-quality-' .. quality)
 
if isSplit then
if args.mini == "1" then
node_container:addClass('card-split-card')
card_container:addClass('mini-card')
end
end
 
local card_wrapper = card_container:tag('div')
if a.mini then
node_container:addClass('mini-card')
end
 
-- Wrapper and body
local node_card = node_container:tag('span')
:addClass('card-wrapper')
:addClass('card-wrapper')
:tag('span')
local card_body = card_wrapper:tag('div')
:addClass('card-body')
:addClass('card-body')
 
-- Galaxy class on body
local card_link = card_wrapper:tag('div')
if not a.mini and ne(a.galaxy) then
:addClass('card-link')
node_card:addClass(
:wikitext('[[' .. final_link .. ']]')
'card-galaxy-' .. normalizeClassName(a.galaxy)
)
local card_image = card_body:tag('div')
end
 
-- Main image container
local imageContainer = node_card:tag('span')
:addClass('card-image-container')
:addClass('card-image-container')
:wikitext(file)
:addClass('card-quality-' .. a.quality)
 
-- Split or regular image
if isSplit then
imageContainer:addClass('card-split-image')
 
-- First item: top-left half
imageContainer:tag('span')
:addClass('card-split-half')
:addClass('card-split-half-a')
:wikitext(a.image_wikitext)
 
-- Second item: bottom-right half
imageContainer:tag('span')
:addClass('card-split-half')
:addClass('card-split-half-b')
:wikitext(makeSplitIcon(a.split, a.mini))
else
imageContainer:wikitext(a.image_wikitext)
end
 
-- Notice label
if not a.mini and ne(a.notice) then
node_card:tag('span')
:addClass('card-notice-label')
:wikitext(a.notice)
end
 
-- First modifier
if a.first then
node_card:tag('span')
:addClass('card-modifier')
:addClass('card-modifier-first')
:wikitext(
'[[File:Icon Item First.png|x25px|link=|alt=First]]'
)
end
 
-- UP modifier
if a.up then
node_card:tag('span')
:addClass('card-modifier')
:addClass('card-modifier-up')
:wikitext(
'[[File:Icon Item UP.png|x25px|link=|alt=UP]]'
)
end


--[[if label and not (args.label == "0" or args.text == "0") then --]]
-- Label below the image
    if (label or args.show_text) and (args.label ~= "0" or args.text ~= "0") then  
if ne(a.label) then
if args.show_label == "1" or args.show_text == "1" then
node_container:tag('span')
label = data and data.name or item
end
local card_label = card_container:tag('div')
:addClass('card-label')
:addClass('card-label')
:wikitext(label)
:wikitext(a.label)
end
end


if (caption or args.show_caption) and args.show_caption ~= "0" then
-- Caption
if args.show_caption == "1" then
-- Split captions are opt-in and use spans instead of list items.
caption = (data and data.name) or item
if a.show_caption and isSplit then
end
local node_caption = node_container:tag('span')
local card_caption = card_container:tag('div')
:addClass('card-caption')
:addClass('card-split-caption')
 
local list = node_caption:tag('span')
:addClass('card-split-caption-list')
 
list:tag('span')
:addClass('card-split-caption-item')
:wikitext('[[' .. a.name .. ']]')
 
list:tag('span')
:addClass('card-split-caption-item')
:wikitext('[[' .. a.split .. ']]')
 
setCaptionWidth(node_caption, a.caption_width)
 
elseif a.show_caption and ne(a.caption) then
local node_caption = node_container:tag('span')
:addClass('card-caption')
:addClass('card-caption')
:wikitext('[[', final_link, '|', caption, ']]')
 
if a.no_link or lib.isEmpty(a.final_link) then
if caption_width == 'auto' or lib.isEmpty(caption_width) then
node_caption:wikitext(a.caption)
card_caption:addClass('auto-width')
else
else
card_caption:css('width', caption_width)
node_caption:wikitext(
'[[' .. a.final_link .. '|' .. a.caption .. ']]'
)
end
end
setCaptionWidth(node_caption, a.caption_width)
end
end
 
if (isFirst) then
return tostring(node_container)
local card_mod_first = card_wrapper:tag('div')
end
:addClass('card-modifier card-modifier-first')
 
:wikitext('[[File:Icon Item First.png|x25px]]')
function p.templateData(frame)
end
return TemplateData.templateData(CARD_ARGS, {
description = 'Creates an item-style card with image, quality, label, split images, modifiers, and optional data-driven icons.',
if (isUp) then
format = 'inline'
local card_mod_up = card_wrapper:tag('div')
}, frame)
:addClass('card-modifier card-modifier-up')
:wikitext('[[File:Icon Item UP.png|x25px]]')
end
-- Attribute icon, applied automatically for agents or manually specified for anything else
if args.mini ~= "1" then  -- Only display the icons if mini is not set to "1"
if ne(nbGalaxy) then
card_body:addClass('card-galaxy-' .. string.lower(nbGalaxy))
card_body:tag('span')
:addClass('card-icon-galaxy')
:wikitext('[[File:Icon ', nbGalaxy, '.png|alt=', nbGalaxy, '|link=', nbGalaxy, '|20px]]')
end
if ne(crGalaxy) then
card_body:addClass('card-galaxy-' .. string.lower(crGalaxy))
card_body:tag('span')
:addClass('card-icon-galaxy')
:wikitext('[[File:Icon ', crGalaxy, '.png|alt=', crGalaxy, '|link=', crGalaxy, '|20px]]')
end
    local notice = args.notice or nil
    if ne(notice) then
    card_body:tag('span')
:addClass('card-notice-label')
:wikitext(notice)
    end
end
return tostring(result)
end
end


function p.getTypeAndData(name, type)
function p.syntax(frame)
local img_type = type or 'Item'
return TemplateData.syntax(CARD_ARGS, frame)
local img_data = nil
for i, v in ipairs(allData) do
-- Only if the type was either not provided in the card args or if the provided type from card args is equal
if lib.isEmpty(type) or v[1] == type then
local data = v[2][name]
if data then
img_type = v[1]
img_data = data
break
end
end
end
return img_type, img_data
end
end


return p
return p