Module:Weapon stats/sandbox

local getArgs = require("Module:Arguments").getArgs

local title = (mw and mw.title and mw.title.getCurrentTitle) or {} local PAGENAME = title.text local MAX = { ["HP"] = 2000, ["Attack"] = 200, ["Break Power"] = 200, ["Magic"] = 200, ["Crit Chance"] = 5, ["Speed"] = 2, ["Defense"] = 5, } local BLACK = "★"

local function parse_attrs(name, values) local name = name or "" local values = values or "" local max = MAX[name] local t = {nil, nil, nil, nil, nil} for x in mw.text.gsplit(values, "%s*,%s*") do   t[#t+1] = tonumber(mw.text.trim(x)) or '-' end t.name = name t[5] = max return t end

local function parse_stars(name, values) local name = name or "" local values = values or "" local max = MAX[name] local t = {nil, nil, nil, nil, nil} for x in mw.text.gsplit(values, "%s*,%s*") do 	x = mw.text.trim(x) local black, count = x:gsub(BLACK, "*"):match("^([%*]*)x?(%d*)") if count ~= "" and (black == "" or black == "*") then t[#t+1] = tonumber(count) elseif black ~= "" then t[#t+1] = #black else t[#t+1] = '-' end end t.name = name t[5] = max return t end

local function parse_ability(name, values, mod, max) local name = name or "???" local values = values or "" local mod = tonumber(string.match(mod or '', '^%s*([%+%-]?%d*)%%?%s*$')) local max = tonumber(string.match(max or '', '^%s*([%+%-]?%d*)%%?%s*$')) local t = {nil, nil, nil, nil, nil} for x in values:gmatch("([^,]+)") do   t[#t+1] = tonumber(x:match("^%s*([%+%-]?%d*)%%?%s*$")) or '-' end t.name = name t.mod = mod t[5] = max return t end

local _main = function(args) if args == nil then return "" end local name = args.name or PAGENAME if (name == nil or name == "") then name = "???" end local caption = "Stats & Auto-Abilities of "..name.." "

local data = { {BLACK..1, BLACK..2, BLACK..3, BLACK..4, BLACK..5, name="", mod="Mod", type="header"}, -- Extract base attributes (HP, Attack, Break Power, Magic) parse_attrs("HP", args.hp), parse_attrs("Attack", args.atk), parse_attrs("Break Power", args.brk), parse_attrs("Magic", args.mag), -- Extract stars (Crit Chance, Speed, and Defense) parse_stars("Crit Chance", args.crt or args.cri), parse_stars("Speed", args.spd), parse_stars("Defense", args.def), -- Extract auto-abilities (1, 2, 3, 4) parse_ability(args.abi1, args.val1, args.mod1, args.max1), parse_ability(args.abi2, args.val2, args.mod2, args.max2), parse_ability(args.abi3, args.val3, args.mod3, args.max3), -- TODO: Find weapon that hasn't got hidden "Boost Ultimate+1" as 4th. parse_ability("Boost Ultimate", nil, "1", "1"), }

local root = mw.html.create("") local wikitable = root:tag("table") wikitable:addClass("wikitable") :cssText("text-align:right;") local caption = wikitable:tag("caption"):wikitext(caption) local row for i,entry in ipairs(data) do   local tagname = entry.type == "header" and "th" or "td" row = wikitable:tag("tr") local nameColumn = row:tag("th") nameColumn:wikitext(entry.name) if i > 1 then nameColumn:cssText("text-align:left") end for _,key in ipairs{1, 2, 3, 4, "mod", 5} do     local cell = entry[key] or "-" row:tag(tagname):wikitext(cell) end end return tostring(root) end

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

-- Usage: _main{    name = "Lightbringer",    hp = "100, 200, 300, 400",    atk = "15, 40, 60, 80",    brk = "10, 20, 30, 40",    mag = "15, 30, 40, 60",    crt = "0, 0, 0, 0",    spd = "0, 1, 1, 1",    def = "15, 15, 15, 15",    abi1 = "Piercing Break", val1 = "30, 50, 50, 50", max1 = "100",    abi2 = "Auto-Charge Ultimate", mod2 = "2", max2 = "8",    abi3 = "Steelguard", mod3 = "1", max3 = "5",  } local function test mw.log( _main{    name = "Lightbringer",    hp = "100, 200, 300, 400",    atk = "15, 40, 60, 80",    brk = "10, 20, 30, 40",    mag = "15, 30, 40, 60",    crt = "0, 0, 0, 0",    spd = "0, 1, 1, 1",    def = "15, 15, 15, 15",    abi1 = "Piercing Break", val1 = "30, 50, 50, 50", max1 = "100",    abi2 = "Auto-Charge Ultimate", mod2 = "2", max2 = "8",    abi3 = "Steelguard", mod3 = "1", max3 = "5",  }) mw.log(_main{ name = "Arc Mirage", hp = "0, -, -, 0" , atk = "15, -, -, 55" , brk = "15, -, -, 55" , mag = "15, -, -, 60" , cri = "2, , , 2" , spd = "0, , , 1" , def = "0, , , 0" , abi1 = "Kill & Draw" , val1 = "2,2,2,2" , max1 = "" , abi2 = "Elemental Third Strike" , val2 = "2,2,2,2" , max2 = ""} ) end

return { main = main, _main = _main, test= test, }