Module:Item

From Arknights Terra Wiki
Jump to navigation Jump to search

The Lua source code for the display of items through {{Item}}.


local p = {}
local data = {
	building = mw.loadData('Module:Item/building'),
	chip = mw.loadData('Module:Item/chip'),
	clstoken = mw.loadData('Module:Item/clstoken'),
	consumable = mw.loadData('Module:Item/consumable'),
	currency = mw.loadData('Module:Item/currency'),
	dhsticker = mw.loadData('Module:Item/dhsticker'),
	ejfold = mw.loadData('Module:Item/ejfold'),
	event = mw.loadData('Module:Item/event'),
	furnpack = mw.loadData('Module:Item/furnpack'),
	fwts = mw.loadData('Module:Item/fwts'),
	gacha = mw.loadData('Module:Item/gacha'),
	icgear = mw.loadData('Module:Item/icgear'),
	intstrat = mw.loadData('Module:Item/intstrat'),
	limpermit = mw.loadData('Module:Item/limpermit'),
	misc = mw.loadData('Module:Item/misc'),
	optoken = mw.loadData('Module:Item/optoken'),
	packpermit = mw.loadData('Module:Item/packpermit'),
	pcs = mw.loadData('Module:Item/pcs'),
	potential = mw.loadData('Module:Item/potential'),
	recalg = mw.loadData('Module:Item/recalg'),
	sanity = mw.loadData('Module:Item/sanity'),
	sffthought = mw.loadData('Module:Item/sffthought'),
	specvoucher = mw.loadData('Module:Item/specvoucher'),
	training = mw.loadData('Module:Item/training'),
	upgrade = mw.loadData('Module:Item/upgrade'),
}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local rates = {
	[0] = {bg='#FFCF00', color='#000', text='3 Stars'},
	[1] = {bg='#313131', color='#FFF', text='Guaranteed'},
	[2] = {bg='#707070', color='#FFF', text='Common'},
	[3] = {bg='#7A7A7A', color='#FFF', text='Uncommon'},
	[4] = {bg='#DDDDDD', color='#000', text='Rare'},
	[5] = {bg='#A40000', color='#FFF', text='Very Rare'},
	[6] = {bg='#01B3FC', color='#FFF', text='First Clear'},
}
local gridviews = {
	['Hope'] = false,
	['Caliginous Insight'] = false,
	['Subjective Imagination'] = false,
	['Lucid Acuity'] = false,
	['Lost Myth'] = false
}
function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end
function p._main(args)
	local key = args[1]
	if not key then error('Item name is required') end
	local item
	for k, v in pairs(data) do
		item = v[key]
		if item then
			break
		end
	end
	if not item then
		mw.log('Item not found: '..key)
		return ' <i>Item not found</i> '
	end
	local name = item.name
	local filename = item.image or name
	local text = item.title or name
	local link = item.link or name
	local tier = tonumber(item.tier) or 0
	local sign, qty = (args[2] or ''):match('^(%+?)(%-?%d+)')
	qty = tonumber(qty)
	local attrs = {
		['data-name'] = item.name,
		['data-title'] = item.title,
		['data-tier'] = tier,
		['data-use'] = item.use,
		['data-desc'] = item.desc,
		['data-obtain'] = item.obtain,
		['data-firstdrop'] = item.firstdrop,
		['data-regdrop'] = item.regdrop,
		['data-specdrop'] = item.specdrop,
		['data-base'] = item.base,
		['data-cond'] = item.cond,
	}
	local dim = tonumber(item.dim) or 60
	if tier >=1 and tier <=6 then
		dim = tonumber(item.dim) or 50
	end
	local gdim = tonumber(item.gdim) or 20
	local gridview = yesno(args.gridview)
	if gridviews[key] == false then
		gridview = false
	elseif gridview == nil then
		gridview = true
	end

	local root = mw.html.create('')
	if gridview then
		local rate = rates[tonumber(args.rate)]
		local wrapper = root:tag('div')
		:css{
			['display'] = 'inline-block',
			['position'] = 'relative',
			['margin'] = '2px 3px',
		}
		if rate then
			local rateDiv = wrapper:tag('div')
			:css{
				['font-size'] = 'xx-small',
				['text-align'] = 'center',
				['padding'] = '1px 2px',
				['margin-bottom'] = '0.5em',
				['border-radius'] = '3px',
				['background'] = rate.bg,
				['color'] = rate.color,
			}
			:wikitext(rate.text)
		end
		local item = wrapper:tag('div')
		if tier >=1 and tier <=6 then
			item:addClass('item')
		end
		local inner = item:tag('div')
		:addClass('inner')
		:addClass(({[0]='item-t0', 'item-t1', 'item-t2', 'item-t3', 'item-t4', 'item-t5', 'item-t6'})[tier])
		local a = inner:tag('div')
		:addClass('a')
		local tt = a:tag('div')
		:addClass('item-tooltip')
		:attr(attrs)
		:wikitext(('[[File:%s.png|%sx%spx|link=%s]]'):format(filename, dim, dim, link))
		if qty then
			local M = 10^6
			local K = 10^3
			if math.abs(qty) >= M then
				qty = (qty / M) .. 'M'
			elseif math.abs(qty) >= K then
				qty = (qty / K) .. 'K'
			end
			local qtyDiv = wrapper:tag('div')
			:css{position='absolute', right=0, bottom=0}
			qtyDiv:tag('div')
			:css{
				['padding'] = '0 5px',
				['background'] = 'rgba(0,0,0,0.8)',
				['box-shadow'] = '0 0 2px #000',
				['color'] = '#FFF',
				['font-size'] = '12px',
				['text-shadow'] = '-1px 0 #000, 0 1px #000, 1px 0 #000, 0 -1px #000',
				['cursor'] = 'default',
			}
			:wikitext(qty)
		end
	else
		local span = root:tag('span')
		:css('display', 'inline-block')
		if qty then
			span:wikitext(sign..qty..' ')
		end
		local tt = span:tag('span')
		:addClass('item-tooltip')
		:attr(attrs)
		:wikitext(('[[File:%s.png|%sx%spx|link=%s]] [[%s|%s]]'):format(filename, gdim, gdim, link, link, text))
	end
	return root
end
return p