Module:Character/Skills

From Granblue Fantasy Wiki
< Module:Character
Revision as of 01:03, 14 January 2020 by Hal (talk | contribs)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Character/Skills/doc

local p = {}
local MAX_SKILLS = 4
local SKILL_ROW_HTML = [[<tr>
  <td class="skill-icon"> {icons} </td>
  <td class="skill-name"> {display_name|???} </td>
  <td> {cd|???} </td>
  <td> {dur|???} </td>
  <td> {oblevel|???} </td>
  <td style="text-align:left;"> {effdesc|???} </td>
</tr>]]

local function notEmpty(s)
	return s ~= nil and s:match('%S') ~= nil and s
end
local function formatIconList(icons)
	local n, r = 0
	if icons then
		for p in icons:gmatch('[^%s,][^,]*[^%s,]') do
			n, r = n+1, (r or '') .. ('[[File:%s|40px|link=]]'):format(p)
		end
	end
	return (r or '[[File:40ph.png|40px|center|link=Ability Icon List]]')
	       .. (n > 1 and '[[Category:2 or more skill icons]]' or '')
end
local function formatTemplate(text, args)
	return (text:gsub('{([^|}]+)(|?)([^}]*)}', function(k, pipe, phv)
		return args[k] or (pipe == '|' and phv) or nil
	end))
end
local function renderSkillTemplate(frame, template, argsTable)
	if template then
		return frame:expandTemplate({title=template, args=argsTable})
	end
	return formatTemplate(SKILL_ROW_HTML, argsTable)
end
local function parseEffectTags(effdesc)
	return ''
end
local function addCargoSkillRow(frame, info, skillType)
	local args = {
		skillid=info.skill_id,
		names=info.display_name,
		icons=info.icon,
		obtain=info.oblevel,
		cooldowns=info.cd,
		durations=info.dur,
		effdesc=info.effdesc,
		effect_tags=parseEffectTags(info.effdesc),
		skill_type=skillType,
		is_linked_skill=info.cd and info.cd:match('Linked Skill:') and '1' or '0',
	}
	if info.icon then
		local i = 1
		for p in info.icon:gmatch('[^%s,][^,]*[^%s,]') do
			args["icon" .. i], i = p, i + 1
			if i > 4 then break end
		end
	end
	if info.display_name:match('skill%-upgrade%-text') then
		local names = info.display_name:gsub('<br%s*/?>',''):gsub('<span[^>]+skill%-upgrade%-text[^>]+>.-</span>', '\0')
		local i = 1
		for p in names:gmatch('%Z+') do
			p = p:match('^%s*(.-)%s*$')
			if p ~= '' then
				args["name" .. i], i = p, i + 1
				if i > 4 then break end
			end
		end
	elseif info.name:match(' / ') then
		local i = 1
		for p in info.name:gmatch('[^/]+') do
			p = p:match('^%s*(.-)%s*$')
			if p ~= '' then
				args["name" .. i], i = p, i + 1
				if i > 4 then break end
			end
		end
	end
	args.name1 = args.name1 or args.names
	args.icon1 = args.icon1 or args.icons
	mw.logObject(args, 'SkillsCargo args')
	return frame:expandTemplate{title='Template:Character/SkillsCargo', args=args}
end

function p.renderSkillTable(frame, args, idSuffix)
	local iargs = args or frame.args
	args = args or frame:getParent().args
	local idSuffix = (idSuffix or iargs.idsuffix or ''):match('%a+') or ''
	local _idSuffix = idSuffix ~= '' and '_' .. idSuffix or ''

	local o = {}
	o[#o+1] = ([[<div class="table-container character character__details character--element-%s">
  <table class="wikitable" style="width:100%%; text-align:center; text-size-adjust: none;">
  <tr><th colspan="6">%s %s</th></tr>
  <tr style="font-weight:bold;">
    <th style="background:#FFF;">Icon</th>
    <th style="background:#FFF;">Name</th>
    <th style="background:#FFF; width:35px;">Cooldown</th>
    <th style="background:#FFF; width:35px;">Duration</th>
    <th style="background:#FFF; width:35px;">Obtained</th>
    <th style="background:#FFF;">Effect</th>
  </tr>]]):format(
		(args.element or 'unknown'):lower(),
		args['abilitytitle' .. _idSuffix] or 'Skills',
		args['abilitysubtitle' .. _idSuffix] or ''
	)

	local sa = {}
	local template = iargs.template or nil
	local storeInCargo = not notEmpty(iargs.skip_cargo_store)
	for i=1,MAX_SKILLS do
		local p = 'a' .. i .. idSuffix .. '_'
		if notEmpty(args[p .. 'name']) or notEmpty(args[p .. 'name_custom']) then
			sa.name = args[p .. 'name']
			sa.name_custom = args[p .. 'name_custom']
			sa.display_name = notEmpty(sa.name_custom) or sa.name
			sa.icon = args[p .. 'icon']
			sa.cd = args[p .. 'cd']
			sa.dur = args[p .. 'dur']
			sa.oblevel = args[p .. 'oblevel']
			sa.effdesc = args[p .. 'effdesc']
			sa.icons = formatIconList(sa.icon)
			sa.skill_id = i .. idSuffix
			o[#o+1] = renderSkillTemplate(frame, template, sa)
			o[#o+1] = storeInCargo and addCargoSkillRow(frame, sa, 'skill') or nil
		end
	end

	o[#o+1] = '</table></div>'
	return #o > 2 and table.concat(o, '\n') or nil
end
function p.renderSkillTables(frame, args)
	local o = {p.renderSkillTable(frame, args, '')}
	for s in ('abcd'):gmatch('.') do
		o[#o+1] = p.renderSkillTable(frame, args, s)
	end
	return table.concat(o, '\n')
end

return p