打开主菜单

少前百科GFwiki β

模块:TokenExchange

罗瓦讨论 | 贡献2023年1月19日 (四) 14:54的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

此模块的文档可以在模块:TokenExchange/doc创建

local p = {}
local args = {}
local origArgs = {}
local root
local count = 0

local function addRow(rowArgs)
	root:tag('tr')
		:tag('td'):wikitext(rowArgs.icon .. rowArgs.desc):done()
		:tag('td'):attr('data-num', rowArgs.num):done()
		:tag('td'):attr('id', 'price' .. tostring(count)):wikitext(rowArgs.price)
			:done()
		:tag('td'):attr('id', 'subtotal' .. tostring(count))
	count = count + 1
end

local function _main()
	root = mw.html.create('table'):attr('id', 'receipt'):addClass('stattable')
	local headers = {'奖励', '数量', '单价', '小计'}
	local headerRow = root:tag('tr')
	for i = 1, 4 do headerRow:tag('th'):wikitext(headers[i]) end
	for i = 1, 25 do
		local desc = args['desc' .. tostring(i)] or args['奖励' .. tostring(i)]
		local num = args['num' .. tostring(i)] or args['数量' .. tostring(i)]
		local price = args['price' .. tostring(i)] or args['单价' .. tostring(i)]
		if desc and desc ~= '' then
			if not (num or price) then error('Num or price not given.') end
			num = tonumber(num)
			price = tonumber(price)
			if not (num or price) then error('Nums and prices must be numbers.') end
			if num*price == 0 then error('Nums and prices cannot be 0.') end
			local icon = args['icon' .. tostring(i)] or args['图标' .. tostring(i)] or ''
			if icon ~= '' then icon = string.format('[[file:%s|48px]]', icon) end
			addRow{ icon = icon, desc = desc, num = num, price = price }
		end
	end
	root:tag('tr'):css{['text-align'] = 'center', ['font-weight'] = 'bold'}
		:tag('td'):wikitext('合计'):done():tag('td'):attr{colspan = 3, id = 'total'}
	return tostring(root)
end

function p.main(frame)
	args = frame == mw.getCurrentFrame() and frame:getParent().args or frame
	return _main()
end

return p