Module:Ultimate ability

local mArguments local yesno = require("Module:Yesno") local mm = require("Module:Math")

local p = {} local BLACK = mw.ustring.char(0x2605) local WHITE = mw.ustring.char(0x2606) local multis = {1.0, 1.1, 1.2, 1.3, 1.4, 1.7, 2.0, 2.3, 2.6, 3.0} local gauges = {160, 155, 150, 140, 135, 130, 125, 110, 100, 80}

-- @todo Use jobtype module? local jobtype_icon_paths = { warrior = "Warrior Icon.png", mage = "Mage Icon.png", ranger = "Ranger Icon.png", monk = "Monk Icon.png", meia = "Mage Icon.png", sarah = "Ranger Icon.png", sophie = "Monk Icon.png", } local function get_jobtype_icon(s) local s = (s or ''):lower local filepath = jobtype_icon_paths[s] if filepath then local name = filepath:gsub(" Icon.png", ""):upper return string.format('', filepath, name) end end

local function number_to_stars(n) local total = tonumber(n) or 0 local w_count = math.floor(total / 10) local b_count = total % 10 return string.rep(WHITE, w_count) .. string.rep(BLACK, b_count) end

local function parse_percent(s) local s = tostring(s) s = s:match("(%d+)%%?") return tonumber(s) or 0 end

local function parse_stars(s) local s = tostring(s) :gsub("&#9733;", BLACK) :gsub("&#x2605;", BLACK) :gsub("&starf;", BLACK) :gsub("&#9734;", WHITE) :gsub("&#x2606;", WHITE) :gsub("&star;",  WHITE) local pattern = "^%s*(["..BLACK..WHITE.."]*)%s*x?%s*(%d*)%s*$" local stars, digit = mw.ustring.match(s, pattern) if digit == "" and stars == "" then mw.log(string.format("Count not find stars or numbers: %q", s)) return 0 elseif digit == "" and stars ~= "" then local _, bcount, wcount _, bcount = string.gsub(stars, BLACK, "") _, wcount = string.gsub(stars, WHITE, "") return bcount + wcount * 10 elseif digit ~= "" and (stars == "" or stars == BLACK) then return tonumber(digit) elseif digit ~= "" and stars ~= "" then mw.log(string.format("Number and multiple stars specified: %q", s)) -- Stars were specified multiple times AND digits also follow. return 0 end end

function p._ultimate_ability(args) local name = args.name local job = args.job -- @todo Query `jobs` table? local jobtype = args.jobtype or args.type local jobtype_icon = get_jobtype_icon(jobtype) local info = args.info or args.description local atk = parse_percent(args.atk or args.attack) local brk = parse_percent(args.brk or args.break_power or args.breakpower) local crt = parse_stars(args.crt or args.crit_chance or args.critchance) local const_atk = yesno(args.const_attack) local const_brk = yesno(args.const_breakpower) -- @todo Somehow gather semantic data about added effects. local extra = args.extra

local rows = {} for lvl=1,10 do		local m = multis[lvl] local bar_n = gauges[lvl] local atk_n = const_atk and atk or (atk * m)		local brk_n = const_brk and brk or (brk * m)		local crt_n = crt local stars = number_to_stars(crt) local row = { level = lvl, gauge = bar_n, attack = atk_n .. '%',			break_power = brk_n .. '%',			crit_chance = string.format("%s (%d%%)", stars, crt_n*5), }		rows[#rows+1] = row end -- Stat table local root = mw.html.create("div") :addClass("ultimate-ability") :tag('div') :tag('span'):addClass('jobtype icon'):wikitext(jobtype_icon):done :tag('strong'):addClass('name'):wikitext(name or "???"):done :done :tag('blockquote') :addClass("description") :wikitext(info or "???") :done local wikitable = root:tag("table") :addClass("wikitable") :tag("tr") :tag("th"):wikitext("ALv."):done :tag("th"):wikitext("Ultimate Gauge"):done :tag("th"):wikitext("Attack"):done :tag("th"):wikitext("Break Power"):done :tag("th"):wikitext("Crit Chance"):done :done for i,row in ipairs(rows) do		wikitable :tag('tr') :tag('td'):wikitext(row.level):done :tag('td'):wikitext(row.gauge):done :tag('td'):wikitext(row.attack):done :tag('td'):wikitext(row.break_power):done :tag('td'):wikitext(row.crit_chance):done :done end if extra then root :tag("dl") :addClass("added-effects") :tag("dt"):wikitext("Added Effects"):done :tag("dd"):wikitext(extra):done :done end return tostring(root) end

function p.ultimate_ability(frame) mArguments = require("Module:Arguments") local args = mArguments.getArgs(frame, {		wrappers = "Template:Ultimate ability stat",	}) return p._ultimate_ability(args) end

return p