◆42Lab是非盈利性、非官方的云图计划维基百科。
◆诚邀指挥官们加入编辑部共同建设wiki。点这里加入42Lab
如果您发现某些内容错误/空缺,请勇于修正/添加!参与进来其实很容易!
◆有任何意见、建议、纠错,欢迎在任意评论区评论
◆编辑讨论QQ群741423564 微博@GFwiki少前百科 欢迎您

模块:ActivityShop

来自42LAB

可在模块:ActivityShop/doc创建此模块的帮助文档

local p = {}
local root
local count = 0

local function addTableForItem(rowArgs)
	local tableForItem = root:tag('tr'):tag('td'):tag('table'):addClass('wikitable')
	local desc = tableForItem:tag('tr'):tag('td'):attr('rowspan', 2)
	desc:wikitext(rowArgs.icon ~= '' and mw.getCurrentFrame():expandTemplate{
		title = '物品图标', args = {
			['稀有度'] = rowArgs.rank, ['图标名'] = rowArgs.icon, ['数量'] = rowArgs.num
		}
	} .. '<br>' or '')
	desc:wikitext(rowArgs.desc)
	local prices, nums = {}, {}
	local lowValue = #(rowArgs.prices) == 4
	for i,c in ipairs(rowArgs.prices) do
		if i < 4 and #c ~= 0 then lowValue = false end
		for t,p in ipairs(c) do
			desc:done():tag('td'):addClass('class' .. tostring(i)):wikitext(p)
			prices[#prices+1] = p
		end
	end
	desc:done():tag('td'):attr('rowspan', 2):done():tag('td'):attr('rowspan', 2)
	local numsRow = tableForItem:tag('tr')
	for i,c in ipairs(rowArgs.nums) do
		for t,n in ipairs(c) do
			numsRow:tag('td'):addClass('class' .. tostring(i)):wikitext(n)
			nums[#nums+1] = n
		end
	end
	tableForItem:attr{ ['data-prices'] = table.concat(prices, ','),
		['data-nums'] = table.concat(nums, ','),
		['data-low-value'] = tostring(lowValue)}
end

local function unmarshal(str, dest)
	local classes = mw.text.split(str, '/', true)
	for i,c in ipairs(classes) do
		dest[i] = c == '' and {} or mw.text.split(c, ',', true)
	end
end

local function _tiered(arg)
	root = mw.html.create('div'):tag('table'):attr('id', 'tiered-shop-table')
	local split = mw.text.split
	local rows = split(arg, '\n', true)
	for _,r in ipairs(rows) do
		if r ~= '' then
			local entries = split(r, ';;', true)
			local rowArgs = {
				icon = entries[1], desc = entries[2], num = entries[3],
				rank = entries[4], prices = {}, nums = {}
			}
			unmarshal(entries[5], rowArgs.prices)
			unmarshal(entries[6], rowArgs.nums)
			addTableForItem(rowArgs)
		end
	end
	return tostring(root)
end

local function addRow(row)
	local image = row[2] ~= '' and '[[file:' .. image .. '|48px]]' or ''
	root:tag('tr')
		:tag('td'):wikitext(image .. row[1]):done()
		:tag('td'):attr('data-num', row[3]):done()
		:tag('td'):attr('id', 'price' .. tostring(count)):wikitext(row[4])
			:done()
		:tag('td'):attr('id', 'subtotal' .. tostring(count))
	count = count + 1
end

local function _main(argTable)
	local ustr = mw.ustring
	root = mw.html.create('table'):attr('id', 'receipt'):addClass('wikitable')
	local headers = {'奖励', '数量', '单价', '小计'}
	local headerRow = root:tag('tr')
	for i = 1, 4 do headerRow:tag('th'):wikitext(headers[i]) end
	local csv = mw.text.split(argTable, '\n', true)
	for _, v in ipairs(csv) do
		if v ~= '' then
			local row = mw.text.split(v, ',', true)
			local correct = {}
			for i = 1, #row - 3 do
				correct[#correct+1] = row[i]
			end
			addRow{
				mw.text.trim(table.concat(correct, ','), '"'),
				mw.text.trim(row[#row-2], '"'),
				tonumber(row[#row-1]),
				tonumber(row[#row])
			}
		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)
	local args = frame == mw.getCurrentFrame() and frame:getParent().args or frame
	return _main(args[1])
end

function p.mainTemplate(frame)
	return _main(mw.text.trim(frame.args[1]))
end

function p.tiered(frame)
	return _tiered(mw.text.trim(frame.args[1]))
end

return p