Module:Cards: Difference between revisions

From Wildfrost Wiki
Jump to navigation Jump to search
No edit summary
m (Undo revision 2785 by Lvlen-125 (talk))
Tag: Undo
Line 46: Line 46:


function p.Table(frame)
function p.Table(frame)
local arr = {{}, {}}
local headers = {}
local i = 1
local i = 2
while frame.args[i] ~= nil do
while frame.args[i] ~= nil and i < 10 do
  table.insert(arr[1], frame.args[i] or "")
  table.insert(headers, string.lower(frame.args[i] or ""))
  i = i + 1
  i = i + 1
end
end
i = i + 1
local type = frame.args[1]
while frame.args[i] ~= nil do
return frame:preprocess(_table(type, headers))
  table.insert(arr[2], string.lower(frame.args[i] or ""))
  i = i + 1
end
if #arr[2] == 0 then
arr[2] = arr[1]
local head = table.remove(arr[2], 1)
arr[1] = {head}
end
return frame:preprocess(_table(arr[1], arr[2]))
--- mw.logObject(arr[1])
--- mw.logObject(arr[2])
--- return _table(arr[1], arr[2])
end
end


function _table(entries, headers)
function _table(type, headers)
   local out = '{| class="wikitable sortable" style="text-align:center;"\n'
   local out = '{| class="wikitable sortable" style="text-align:center;"\n'
   for i, header in ipairs(headers) do
   for i, header in ipairs(headers) do
Line 87: Line 75:
   end
   end
    
    
  local entryList = {}
   for i, card in ipairs(sortedCards()) do
   for i, entry in ipairs(entries) do
    if card.Types and card.Types[type] then
  if all_cards[entry] then
out = out.."|-\n"
  all_cards[entry].UniqueName = entry
for i, header in ipairs(headers) do
  table.insert(entryList, all_cards[entry])
out = out.."|"
  else
if header == "image" then
  for i, card in ipairs(sortedCards()) do
out = out.."{{CardArt|".. card.UniqueName.."}}"
  if card.Types and card.Types[entry] then
elseif header == "name" then
    table.insert(entryList, card)
local link = card.Link or card.UniqueName
    end
out = out.."style=\"text-align:center;\"|[[".. link .. "|" .. card.Name .."]]"
  end
elseif header == "description" or header == "desc" or header == "summoncon" then
end
out = out.."style=\"text-align:center;\"|".. (_getStat(card, header) or "")
  end
elseif header == "health" and card.Health == nil and card.Scrap then
 
out = out.. card.Scrap .." {{Stat|Scrap}}"
  for i, card in ipairs(entryList) do
else
out = out.."|-\n"
out = out .. _getStat(card, header)  
for i, header in ipairs(headers) do
end
out = out.."|"
out = out.."\n"
if header == "image" then
out = out.."{{CardArt|".. card.UniqueName.."}}"
elseif header == "name" then
local link = card.Link or card.UniqueName
out = out.."style=\"text-align:center;\"|[[".. link .. "|" .. card.Name .."]]"
elseif header == "description" or header == "desc" or header == "summoncon" then
out = out.."style=\"text-align:center;\"|".. (_getStat(card, header) or "")
elseif header == "health" and card.Health == nil and card.Scrap then
out = out.. card.Scrap .." {{Stat|Scrap}}"
else
out = out .. _getStat(card, header)  
end
end
out = out.."\n"
     end
     end
   end
   end

Revision as of 15:15, 16 December 2023

To edit card information, see Module:Cards/data

GetStat

Usage: {{#invoke:Cards|GetStat|<card>|<stat>}}

Returns the respective stat of the card.
Possible values for <stat>: name health scrap attack counter other desc summoncon tribes price gold challenge unlock

Table

Usage: {{#invoke:Cards|Table|<entry>|<header_list>}} OR
{{#invoke:Cards|Table|<entry_list>|*|<header_list>}}

Returns a table based on the given paramenters. <entry> is either a card name or type.
Types include: Boss Pet NonPetCompanion Companion Item Shade Clunker EnemyClunker Enemy Miniboss Boss ShopItem InventorsHut HotSpring Charm CursedCharm

Append a tribename before the type to filter cards exclusive to that tribe, eg SnowdwellersCompanion

<entry_list> is a list of valid <entry> values, input as multiple parameters, i.e. <entry1>|<entry2>|... etc..

<header_list> is a list of valid stats, input as multiple parameters, i.e. <stat1>|<stat2>|... etc..
Stats include: name health scrap attack counter other desc summoncon tribes price challenge

The first usage option is used when only 1 entry is needed, e.g. the only entry is a type category. Otherwise, use the second option.

If challenge is among the header stats, the table will be sorted based off the entry's ChallengeOrder value, if it exists.

CardInfobox

Usage: {{#invoke:Cards|CardInfobox|<card>|<template_params>}}

Returns the infobox of the card.

<template_params> is a list of template parameters input as multiple parameters, i.e. <p1>=<data1>|<p2>=<data2>|... etc.. These parameters are the same parameters used by Template:Infobox. Parameters without inputs are autofilled with fitting module data, to the best of its ability.

CharmInfobox

Usage: {{#invoke:Cards|CharmInfobox|<charm>|<template_params>}}

Returns the infobox of the charm.

<template_params> is a list of template parameters input as multiple parameters, i.e. <p1>=<data1>|<p2>=<data2>|... etc.. These parameters are the same parameters used by Template:Infobox. Parameters without inputs are autofilled with fitting module data, to the best of its ability.

Itembox

Usage: {{#invoke:Cards|Itembox|<card>|<template_params>}}

Returns the itembox of the card.

<template_params> is a list of template parameters input as multiple parameters, i.e. <p1>=<data1>|<p2>=<data2>|... etc.. These parameters are the same parameters used by Template:Itembox. Parameters without inputs are autofilled with fitting module data, to the best of its ability.

NavBoxSection

Usage: {{#invoke:Cards|NavBoxSection|<type>}}

Returns a list of links for cards of a certain type, for use in Template:NavboxCards.

suggested values for <type>: Boss Pet NonPetCompanion Item Shade Clunker EnemyClunker Enemy Miniboss Boss


local p = {}

--local KeyData = mw.loadData( 'Module:Code/data')
local all_cards = require("Module:Cards/data").cards

function p.GetStat(frame)
	local card = all_cards[frame.args.name or frame.args[1]] 
	if card == nil then 
		return ""
	end
	local stat =  frame.args.stat or frame.args[2]  
	return frame:preprocess(_getStat(card, stat))
end 

function _getStat(card, stat)
	stat = string.lower(stat)
	if stat == "name" then
		return card.Name or ""
	elseif stat == "health" then
		return card.Health or ""
	elseif stat == "scrap" then
		return card.Scrap or ""
	elseif stat == "attack" then
		return card.Attack or ""
	elseif stat == "counter" then
		return card.Counter or ""
	elseif stat == "other" then
		return card.Other or ""
	elseif stat == "description" or stat == "desc" then
		return card.Desc or ""
	elseif stat == "summoncon" then
		return card.SummonCon or ""
	elseif stat == "tribes" then
		local out = ""
		for i, tribe in ipairs(card.Tribes) do
			out = out .. "[[Tribes#" .. tribe .. "|".. tribe.."]]"
			if i < #card.Tribes then
				out = out .. ",<br>"
			end
		end
		return out
	else
		error("Argument Invalid:" .. stat)
	end
end

function p.Table(frame)
	local headers = {}
	local i = 2
	while frame.args[i] ~= nil and i < 10 do
	  	table.insert(headers, string.lower(frame.args[i] or ""))
	  	i = i + 1
	end
	local type = frame.args[1]
	return frame:preprocess(_table(type, headers))
end

function _table(type, headers)
  local out = '{| class="wikitable sortable" style="text-align:center;"\n'
  for i, header in ipairs(headers) do
  	if header == "scrap" or header == "health" or header == "attack" or header == "counter" then
  		local statname = header:gsub("^%l", string.upper)
  		out = out .. "![[File:"..statname..".png|20px|link=Stats#Primary Stats]] "..statname.."\n"
  	elseif header == "tribes" then
  		out = out .. "!Tribe-exclusive?\n"
  	elseif header == "name" then
  		out = out .. "!Card Name\n"
  	elseif header == "desc" then
  		out = out .. "!Description\n"
  	elseif header == "summoncon" then
  		out = out .. "!Summon conditions\n"
  	else
  		out = out .. "!" .. header:gsub("^%l", string.upper) .. "\n"
  	end
  end
  
  for i, card in ipairs(sortedCards()) do
    if card.Types and card.Types[type] then
		out = out.."|-\n"
		for i, header in ipairs(headers) do
			out = out.."|"
			if header == "image" then
				out = out.."{{CardArt|".. card.UniqueName.."}}"
			elseif header == "name" then
				local link = card.Link or card.UniqueName
				out = out.."style=\"text-align:center;\"|[[".. link .. "|" .. card.Name .."]]"
			elseif header == "description" or header == "desc" or header == "summoncon" then
				out = out.."style=\"text-align:center;\"|".. (_getStat(card, header) or "")
			elseif header == "health" and card.Health == nil and card.Scrap then
				out = out.. card.Scrap .." {{Stat|Scrap}}"
			else
				out = out .. _getStat(card, header) 
			end
			out = out.."\n"
		end
    end
  end
  return out .. "|}"
end

function sortedCards()
	local sortedTable = {}
	for name, card in pairs(all_cards) do
		card.UniqueName = name
		table.insert(sortedTable, card)
	end
	table.sort(sortedTable, function(a,b)
		return a.UniqueName < b.UniqueName
	end)
	return sortedTable
end

function p.CardInfobox(frame) 
	local card = frame.args[1]
	card = card:match( "^%s*(.-)%s*$" )
	return frame:preprocess(_cardInfobox(all_cards[card], frame.args))
end

function _cardInfobox(card, params)
	local out = "{{infobox|category=Card"
	out = out .. "|name=" .. (params.name or card.Name or "")
	out = out .. "|image=" .. (params.image or "")
	out = out .. "|width=" .. (params.width or "")
	if params.hpType then
		out = out .. "|hpType=" .. params.hpType 
	elseif card.Health == nil and card.Scrap then
		out = out .. "|hpType=scrap"
	end
	out = out .. "|health=" .. (params.health or card.Health or card.Scrap or "")
	out = out .. "|attack=" .. (params.attack or card.Attack or "")
	out = out .. "|counter=" .. (params.counter or card.Counter or "")
	out = out .. "|other=<div style=\"text-align: center;\">" .. (params.other or card.Other or "") .. "</div>"
	out = out .. "|description=" .. (params.description or card.Desc or "")
	out = out .. "}}"
    return out
end

function p.NavBoxSection(frame)
	local type = frame.args[1]
	return frame:preprocess(_navBoxSection(type))
end

function _navBoxSection(type)
	local out = ""
	local first = true
	for i, card in ipairs(sortedCards()) do
    	if card.Types and card.Types[type] then
    		if first then
    			first = false
    		else 
    			out = out .. " • "
    		end
    		out = out.. "[[" .. (card.Link or card.UniqueName) .. "|" .. card.Name .. "]]"
    	end
    end
	return out
end

return p