Jump to content

Module:Card

From The Petit Planet Wiki
Revision as of 16:27, 2 May 2026 by Eleiyas (talk | contribs) (why upper here? make it lower instead.)
Module documentation
This Lua module is used primarily on pages in the Main namespace. It can be found on 2,428 pages, or on about 60.61% of those pages.
Edits to this Lua module will be widely noticed. To avoid large-scale disruption and unnecessary server load, any changes to this Lua module should be tested in its sandbox or testcases subpages. The tested changes can then be added to this page in one single edit.
Please consider discussing any changes in the wiki's Discord.

This module implements {{Card}}.

To update, see: Module:CentralData.

local p = {}
local lib = require('Module:Feature')
local ne = lib.isNotEmpty
local namespace = require('Module:Namespace_detect')._main
local getTemplateArgs = require('Module:Parser').getTemplateArgs
local array = require('Module:Array')

local allData = {
	--{'Neighbor', mw.loadData('Module:CentralData/Neighbors')},	
	{'Creature', mw.loadData('Module:CentralData/Creatures')},
	--{'Furniture', mw.loadData('Module:CentralData/Furniture')},
	{'Item', mw.loadData('Module:CentralData/Items')},
}

local COLON = ':'

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

function p._main(args)
	local item = args[1] or args.name or 'Unknown'
	local label = args[2] or args.label or args.text or args.count or args.amount or args.x or nil
	
	-- Reset agent-specific data to prevent carry-over from previous cards
	local nbQuality, nbGalaxy, crQuality, crGalaxy, outQuality, notice = nil, nil, nil, nil, nil, nil
	
	-- Caption Params:
	local caption = args.caption or nil
	local caption_width = args.caption_width or args.captionWidth or nil
	
    -- Image File Params:
	local type, data = p.getTypeAndData(item, args.type)
	local suffix = args.suffix or ''
	local imageSize = args.image_size or args.imageSize or lib.ternary(args.mini == '1', 48, 70)
	local imageExt = args.image_ext or args.imageExt or 'png'
	
	-- Link Params:
	local link = args.link or lib.ternary(item == 'Unknown', '', item)
	local link_suffix = args.link_suffix or ''
	
	-- 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
	
	-- If quality is still empty, set it to "Gray"
	if lib.isEmpty(quality) then
		quality = 'Gray' 
	end
	
	-- If quality contains a semicolon, then split and get first
	if string.find(quality, ";") then
		quality = array._main({
			quality,
			';',
			firstItemOnly = 1
		})
	end
	
	-- Make sure quality is lowercase
	quality = quality:lower()
	
	-- Modifiers:
	local isFirst = args.first or false
	local isUp = args.UP or args.up or false
	
	-- Attribute/galaxy
	local galaxy = args.galaxy or nil
	
	if lib.isEmpty(suffix) then
		suffix = ' Icon'
		local lcType = type:lower()
		if (lcType == 'neighbor') then
			--Grabs Neighbor Quality and Galaxy from Data to display icons
			nbQuality = data['quality']
			nbGalaxy = data['galaxy']
			
			--In Full-Size Card-Mode, if there is no specified text, Agents will default to using their full name displayed underneath.
			if args.mini ~= "1" then
				if (label == nil) then label = item 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.
			-- In cases where Agents do not have a "shortName", their full name will be displayed instead.
			if args.mini == "1" then
				if (label == nil) and data['shortName'] ~= nil then label = data['shortName']
				elseif (label == nil) and data['shortName'] == nil then label = item
				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
			--Grabs Creature Quality and Galaxy from Data to display icons
			crQuality = data['quality']
			crGalaxy = data['galaxy']
			
		elseif (lcType == 'clothing') then
			--Grabs Outfit Quality from Data to display icons
			outQuality = data['quality']	
		end
	end

	--local final_file = args.image or type .. ' ' .. lib.sanitizeFileName(item) .. suffix .. '.' .. imageExt
	local final_file = lib.sanitizeFileName(item) .. suffix .. '.' .. imageExt	
	
	local final_link = link .. link_suffix
	
	local file = '[[File:' .. final_file .. '|' .. imageSize .. 'px|alt=' .. item .. '|link=' .. final_link .. ']]'
	
	local result = mw.html.create()
	
	local card_container = result:tag('div')
		:addClass('card-container')
		:addClass('card-quality-' .. quality)
		
	if args.mini == "1" then
		card_container:addClass('mini-card')
	end
		
	local card_wrapper = card_container:tag('div')
		:addClass('card-wrapper')
		
	local card_body = card_wrapper:tag('div')
		:addClass('card-body')
		
	local card_link = card_wrapper:tag('div')
		:addClass('card-link')
		:wikitext('[[' .. final_link .. ']]')
	
	local card_image = card_body:tag('div')
		:addClass('card-image-container')
		:wikitext(file)

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

	if (caption or args.show_caption) and args.show_caption ~= "0" then
		if args.show_caption == "1" then
			caption = (data and data.name) or item
		end
		local card_caption = card_container:tag('div')
			:addClass('card-caption')
			:wikitext('[[', final_link, '|', caption, ']]')
			
		if caption_width == 'auto' or lib.isEmpty(caption_width) then
			card_caption:addClass('auto-width')
		else
			card_caption:css('width', caption_width)
		end
	end
	
	if (isFirst) then
		local card_mod_first = card_wrapper:tag('div')
			:addClass('card-modifier card-modifier-first')
			:wikitext('[[File:Icon Item First.png|x25px]]')
	end
	
	if (isUp) then
		local card_mod_up = card_wrapper:tag('div')
			: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

function p.getTypeAndData(name, type)
	local img_type = type or 'Item'
	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

return p