Module:Enemy

From Arknights Terra Wiki
Jump to navigation Jump to search

The Lua source code for the display of enemies through {{Enemy}}.


local p = {}
local data = {
	normal = mw.loadData('Module:Enemy/normal'),
	elite = mw.loadData('Module:Enemy/elite'),
	boss = mw.loadData('Module:Enemy/boss'),
}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
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 return '&#32;<i>Enemy not found</i>&#32;' end
	local enemy
	local enemyType -- determined by the DB they are located in
	for _, _type in ipairs({'normal', 'elite', 'boss'}) do
		enemy = data[_type][key]
		enemyType = _type
		if enemy then break end
	end
	if not enemy then return '' end
	local node = p._enemy_data{
		enemy.name,
		enemy.code,
		race=enemy.race,
		title=enemy.title,
		link=enemy.link,
		type=enemyType,
		qty=args[2],
		qty2=args[3],
		gridview=args.gridview,
		nolink=args.nolink,
		mode=args.mode or 'normal',
	}
	if yesno(args.gridview) then
		local div = mw.html.create('div')
		:attr('style', 'display:inline-block; position:relative; margin:5px;')
		:node(node)
		return div
	else
		return node
	end
end
function p._enemy_data(args)
	local pagename = args.name or args[1]
	local link = args.link or pagename
	local text = args.title or pagename
	local mode = args.mode or 'normal'
	local attrs = {
		['data-name'] = args[1],
		['data-title'] = args.title,
		['data-code'] = args[2],
		['data-race'] = args.race,
		['data-type'] = args.type,
		['data-link'] = args.link,
	}
	local qty
	if args.qty then
		if not args.qty2 then
			qty = args.qty
		elseif mode == 'mc' then
			qty = (args.qty + args.qty2)..' ('..args.qty..'/'..args.qty2..')'
		elseif mode == 'normal' then
			qty = args.qty..'&ndash;'..args.qty2
		end
	end
	local root = mw.html.create('')
	if yesno(args.gridview) then
		local a = root:tag('div')
			:addClass('a')
		local tooltip = a:tag('div')
			:addClass('enemy-tooltip')
			:attr(attrs)
			:wikitext('[[File:'..args[1]..' sprite.png|60x60px|link='..link..( text and '|'..text or '' )..']]')
		if qty then
			a:tag('div')
				:attr('style', "text-align:center; margin-top:2px;")
				:wikitext('&times;'..qty)
		end
	else
		local tooltip = root:tag('span')
			:addClass('enemy-tooltip')
			:attr(attrs)
			:wikitext(yesno(args.nolink) and text or '[['..link..'|'..text..']]')
		if qty then
			root:wikitext('&#32;&times;'..qty)
		end
	end
	return root
end
return p