Module:Card

From Zombie Panic! Official Wiki
Revision as of 11:57, 11 June 2025 by Wuffesan (talk | contribs) (Created page with "local p = {} local lib = require('Module:Feature') local ne = lib.isNotEmpty local link_label = require('Module:Link_Label')._main local namespace = require('Module:Namespace_detect')._main local getTemplateArgs = require('Module:Parser').getTemplateArgs local array = require('Module:Array') local allData = { {'Survivor', mw.loadData('Module:Card/Survivors')}, {'Weapon', mw.loadData('Module:Card/Weapons')}, {'Item', mw.loadData('Module:Card/Items')}, } local COLON =...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This module implements {{Card}}.


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

local allData = {
	{'Survivor', mw.loadData('Module:Card/Survivors')},
	{'Weapon', mw.loadData('Module:Card/Weapons')},
	{'Item', mw.loadData('Module:Card/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 specific data to prevent carry-over from previous cards
	local notice = 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 ''
	
	-- Modifiers:
	local isFirst = args.first or false
	local isUp = args.UP or args.up or false
	
	if lib.isEmpty(suffix) then
		local lcType = type:lower()
		if (lcType == 'agent') then
			suffix = ' Icon'
			
			--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
			
		else
			suffix = ' Icon' 
		end
	end

	local final_file = type .. ' ' .. 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')
		
	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
	
	if args.mini ~= "1" then  -- Only display the icons if mini is not set to "1"
	    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