Mobius Final Fantasy Wiki
(add white stars, and percentage value beside Defense and Crit.)
m (woops.)
Line 103: Line 103:
 
req = args['req'..i] or req or 'mod'
 
req = args['req'..i] or req or 'mod'
 
if string.find(req, '(%d+)') then
 
if string.find(req, '(%d+)') then
req = star .. string.match(req, '(%d+)')
+
req = BLACK .. string.match(req, '(%d+)')
 
else
 
else
 
req = 'mod'
 
req = 'mod'

Revision as of 07:42, 7 December 2018

Documentation for this module may be created at Module:Weapon stats/doc

local getArgs = require('Module:Arguments').getArgs
local p = {}

--[[
Demo:
	p._main{
		name = "Blank Blade",
		hp = "0, 0, 0, 0",
		atk = "5, 20, 35, 50",
		brk = "5, 15, 25, 35",
		mag = "0, 5, 10, 15",
		cri = "0, 0, 0, 0",
		spd = "0, 0, 0, 1",
		def = "0, 0, 0, 0",
		abi1 = "Ultimate Charger", min1 = "1", max1 = "3", req1 = "3",
		abi2 = "Life Draw", min2 = "20", max2 = "30", req2 = "mod",
		abi3 = "Healing Victory", min3 = "10", max3 = "30", req3 = "mod",
		abi4 = "Boost Ultimate", min4 = "1", max4 = "1", req4 = "mod",
	}
]]

local BLACK = '★'
local WHITE = '☆'
local maxStatColumns = 4 -- {Name, Name+, Name Bis, Name X}

local function makeStatRow(key, values)
	local row = mw.html.create('tr')
	row	:tag('th')
		:attr("scope", "row")
		:wikitext(key)
	for i=1,maxStatColumns do
		local v = values[i] or ''
		row	:tag('td')
			:wikitext(v)
	end
	return row
end

local function makeStatHeaderRow()
	local row = mw.html.create("tr")
	row	:tag("th")
	for i=1,maxStatColumns do
		row	:tag("th")
			:attr("scope", "col")
			:wikitext(BLACK..i)
	end
	return row
end

local function star_format(s, show_percent)
	if tonumber(s) == nil then return s end
	local n = tonumber(s)
	if n > 0 then
		local white = WHITE:rep(n/10)
		local black = BLACK:rep(n%10)
		if show_percent then
			return white .. black .. ' (' .. n * 5 .. '%)'
		else
			return white .. black
		end
			
	end
	return '-'
end


local function handle_num(str)
	local sep = '%s*,%s*'
	return mw.text.split(str or '', sep)
end

local function handle_stars(str, show_percent)
	local t = {}
	local sep = '%s*,%s*'
	for s in mw.text.gsplit(str or '', sep) do
		t[#t+1] = star_format(s, show_percent)
	end
	return t
end

--[[
	TODO: Handle min and max values such as:
	1
	+1
	1%
	+1%
	+ 1
	+ 1%
]]
local function handle_abilities(args)
	local t = {}
	for i=1,4 do
		local name, min, max, req
		if i == 4 then
			name = "Boost Ultimate"
			min = "1"
			max = "1"
			req = "mod"
		end
		name = args['abi'..i] or name or '???'
		min = args['min'..i] or min or ''
		max = args['max'..i] or max or ''
		req = args['req'..i] or req or 'mod'
		if string.find(req, '(%d+)') then
			req = BLACK .. string.match(req, '(%d+)')
		else
			req = 'mod'
		end
		t[i] = {name=name, min=min, max=max, req=req}
	end
	return t
end

function p.main(frame)
	local args = getArgs(frame, {
		wrappers = "Template:Weapon stats"
	})
	return p._main(args)
end

function p._main(args)
	local name = args.name
	-- Numeric stats
	local hps = handle_num(args.hp)
	local atks = handle_num(args.atk)
	local brks = handle_num(args.brk)
	local mags = handle_num(args.mag)
	-- Star stats
	local cris = handle_stars(args.cri, true)
	local spds = handle_stars(args.spd)
	local defs = handle_stars(args.def, true)
	-- Abilities
	local abis = handle_abilities(args)
	
	local root = mw.html.create()
	local statTableRoot = root:tag('table'):addClass("wikitable")
	if name then
		statTableRoot:tag("caption"):wikitext("Stats for "..name)
	end
	statTableRoot:node(makeStatHeaderRow())
	statTableRoot:node(makeStatRow("HP", hps))
	statTableRoot:node(makeStatRow("Attack", atks))
	statTableRoot:node(makeStatRow("Break Power", brks))
	statTableRoot:node(makeStatRow("Magic", mags))
	statTableRoot:node(makeStatRow("Crit Chance", cris))
	statTableRoot:node(makeStatRow("Speed", spds))
	statTableRoot:node(makeStatRow("Defense", defs))

	local abilityTableRoot = root:tag("table"):addClass("wikitable")
	if name then
		abilityTableRoot:tag("caption"):wikitext("Auto-Abilities for "..name)
	end
	local tr = abilityTableRoot:tag('tr')
		tr:tag('th'):wikitext("Auto-Ability")
		tr:tag('th'):wikitext("Initial")
		tr:tag('th'):wikitext("Final")
		tr:tag('th'):wikitext("Requirement")
	for i,ability in ipairs(abis) do
		tr = abilityTableRoot:tag('tr')
		tr:tag('td'):wikitext(ability.name)
		tr:tag('td'):wikitext(ability.min)
		tr:tag('td'):wikitext(ability.max)
		tr:tag('td'):wikitext(ability.req)
	end
	
	return tostring(root)
end
return p