Jump to content

Module:Drop Tables: Difference between revisions

From The Petit Planet Wiki
m Eleiyas moved page Module:Lootbox to Module:Drop Tables without leaving a redirect
Undo revision 37017 by Eleiyas (talk)
Tag: Undo
 
(4 intermediate revisions by the same user not shown)
Line 2: Line 2:
local lib = require('Module:Feature')
local lib = require('Module:Feature')
local Item = require('Module:Item')._main
local Item = require('Module:Item')._main
local category = mw.html.create():wikitext('[[Category:Lootboxes]]')
local category = mw.html.create():wikitext('[[Category:Drop Tables]]')
local TS = lib.thousandsSeparator
local TS = lib.thousandsSeparator
local total = {}
local total = {}
Line 47: Line 47:
end
end
if not args.lootbox or mw.title.getCurrentTitle().rootText == args.lootbox then
if not args.dtable or mw.title.getCurrentTitle().rootText == args.dtable then
out:node(require('Module:Namespace detect').main{main=category})
out:node(require('Module:Namespace detect').main{main=category})
end
end
Line 75: Line 75:
size = 25
size = 25
}):wikitext('<br />')
}):wikitext('<br />')
if (args.nocat == nil and i.nocat == nil) then category:wikitext('[[Category:', name, ' Lootboxes]]') end
if (args.nocat == nil and i.nocat == nil) then category:wikitext('[[Category:', name, ' Drop Tables]]') end
end
end
end
end

Latest revision as of 21:28, 25 October 2026

Module documentation

This module implements {{Drop Tables}}.

local p = {}
local lib = require('Module:Feature')
local Item = require('Module:Item')._main
local category = mw.html.create():wikitext('[[Category:Drop Tables]]')
local TS = lib.thousandsSeparator
local total = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	if tonumber(frame:callParserFunction('#var:transclude', '') or '0') == 1 then
		args.nocat = 1
	end
	
	return p._main(args)
end

function p._main(args)
	local out = mw.html.create('table'):addClass('article-table wikitable sortable ' .. (args.class or ''))
	local delim = args.delim or ';;'
	
	-- Header row
	local tr = out:tag('tr')
	tr:tag('th'):wikitext('Item')
	if lib.isNotEmpty(args.note) then tr:tag('th'):wikitext('Notes') end
	
	-- Table body
	for _, raw in ipairs(args) do
		local data = p.splitParams(raw, delim)
		data.size = data.size or args.size or '35' -- first column image size
		data.cost_delim = data.cost_delim or args.cost_delim or ','
		out:tag('tr'):node(p.ItemRow(data, args))
	end
	
	-- Footer row
	if next(total) ~= nil and tonumber(args.total) == 1 then
		local tr = out:tag('tr')
		tr:tag('th'):wikitext('Total cost for all items')
		local th = tr:tag('th'):attr('colspan', 3 + (lib.isNotEmpty(args.refresh) and 1 or 0) + (lib.isNotEmpty(args.note) and 1 or 0))
		for name, info in lib.spairs(total) do
			th:node(Item{
				name = name,
				link = name,
				count = TS(info.base) .. (info.base2>0 and (' (' .. TS(info.base2) .. ')') or ''),
				size = 25
			}):wikitext('<br />')
		end
	end
	
	if not args.dtable or mw.title.getCurrentTitle().rootText == args.dtable then
		out:node(require('Module:Namespace detect').main{main=category})
	end
	
	return out
end

function p.ItemRow(data, args)
	local row = mw.html.create()
	local cost = data[2]
	
	function getCost(i)
		i.mult = tonumber(i.mult) or 1
		i.mult2 = tonumber(i.mult2) or 0
		if not data[1] and lib.isNotEmpty(data.x) and data.x ~= '1' then i.cell:wikitext('&times;', data.x, ' per ') end
		if tonumber(i.value) == nil then
			local items = lib.split(i.value, data.cost_delim or ',')
			for a, item in ipairs(items) do
				local name, amount = string.match(item, '^(.-)%*(.-)$')
				amount = tonumber((amount:gsub(',', '')))
				if name == currency then i.cell:attr('data-sort-value', amount) end
				i.cell:node(Item{
					name = name,
					link = name,
					count = (TS(amount * i.mult) .. (i.mult2 > 0 and (' (' .. TS(amount * i.mult2) .. ')') or '')),
					notext = 1,
					size = 25
				}):wikitext('<br />')
				if (args.nocat == nil and i.nocat == nil) then category:wikitext('[[Category:', name, ' Drop Tables]]') end
			end
		end
	end
	if data[1] then
		local info = {}
		local specific = data.specific or lib.ternary(tonumber(data.blueprint)==1, 'Blueprint: ' .. data[1], data[1])
		info.name = data[1]
		info.size = data.size
		info.count = data.x
		info.link = data.link or specific
		info.text = data.text or specific
		info['type'] = data['type']
		
		--So we don't have dupe pages for Inspo Cards (etc.) and thousands of redirects.
		if (data['type'] == 'Inspo Card') then
			info.text = ('Inspo Card: ' .. info.text)
		end	
		if (data['type'] == 'Eye Color') then
			info.text = ('Eye Color: ' .. info.text)
		end	
		if (data['type'] == 'Eye Style') then
			info.text = ('Eye Style: ' .. info.text)
		end	
		if (data['type'] == 'Hair Color') then
			info.text = ('Hair Color: ' .. info.text)
		end	
		if (data['type'] == 'Hairstyle') then
			info.text = ('Hairstyle: ' .. info.text)
		end
		
		row:tag('td'):node(Item(info))
		if (not args.nocat) then category:wikitext('[[Category:Contains ', specific, '|', specific, ']]') end
	end
	if data.refresh or args.refresh then
		refreshKeys = {d = 'Daily', w = 'Weekly', m = 'Monthly', v = 'Per Version'}
		row:tag('td'):wikitext(refreshKeys[data.refresh or 1] or data.refresh or '')
	end
	if data.note or args.note then row:tag('td'):wikitext(data.note or '') end
	
	return row
end

function p.splitParams(entry, paramDelim)
	entry = lib.split(entry, paramDelim)
	local returns = {}
	for i, param in ipairs(entry) do
		local name, val = string.match(param, '^%s*(.-)%s*{?{?=}?}?%s*(.-)%s*$')
		if name ~= nil and name ~= '' and val ~= nil then --named params
			returns[name] = val
		elseif param ~= nil and param ~= '' then --unnamed params
			table.insert(returns, param)
		end
	end
	return returns
end

return p