BryghtShadow (talk | contribs) mNo edit summary |
No edit summary |
||
(2 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
+ | local mArguments -- lazy load. |
||
− | local getArgs = require('Module:Arguments').getArgs |
||
+ | |||
− | local _main2 = require("Module:Weapon stats/sandbox")._main |
||
local p = {} |
local p = {} |
||
+ | local title = mw.title.getCurrentTitle() |
||
− | --[[ |
||
+ | local PAGENAME = title.text |
||
− | Demo: |
||
+ | local NAMESPACE = title.namespace |
||
− | p._main{ |
||
+ | local MAX = { |
||
− | name = "Blank Blade", |
||
+ | ["HP"] = 2000, |
||
− | hp = "0, 0, 0, 0", |
||
+ | ["Attack"] = 200, |
||
− | atk = "5, 20, 35, 50", |
||
+ | ["Break Power"] = 200, |
||
− | brk = "5, 15, 25, 35", |
||
+ | ["Magic"] = 200, |
||
− | mag = "0, 5, 10, 15", |
||
+ | ["Crit Chance"] = 5, |
||
− | cri = "0, 0, 0, 0", |
||
+ | ["Speed"] = 2, |
||
− | spd = "0, 0, 0, 1", |
||
+ | ["Defense"] = 5, |
||
− | def = "0, 0, 0, 0", |
||
+ | } |
||
− | abi1 = "Ultimate Charger", min1 = "1", max1 = "3", req1 = "3", |
||
+ | local BLACK = mw.ustring.char(0x2605) -- "★" |
||
− | abi2 = "Life Draw", min2 = "20", max2 = "30", req2 = "mod", |
||
+ | local WHITE = mw.ustring.char(0x2606) -- "★" |
||
− | abi3 = "Healing Victory", min3 = "10", max3 = "30", req3 = "mod", |
||
− | abi4 = "Boost Ultimate", min4 = "1", max4 = "1", req4 = "mod", |
||
− | } |
||
− | ]] |
||
+ | local function parse_attrs(name, values) |
||
− | local BLACK = '★' |
||
− | local |
+ | local name = name or "" |
+ | local values = values or "" |
||
− | local maxStatColumns = 4 -- {Name, Name+, Name Bis, Name X} |
||
+ | local max = MAX[name] |
||
− | |||
+ | local t = {nil, nil, nil, nil, nil} |
||
− | local function makeStatRow(key, values) |
||
+ | for i,x in ipairs(mw.text.split(values, "%s*,%s*")) do |
||
− | local row = mw.html.create('tr') |
||
+ | t[i] = tonumber(mw.text.trim(x)) |
||
− | row :tag('th') |
||
− | :attr("scope", "row") |
||
− | :wikitext(key) |
||
− | for i=1,maxStatColumns do |
||
− | local v = values[i] or '' |
||
− | row :tag('td') |
||
− | :wikitext(v) |
||
end |
end |
||
+ | t.name = name |
||
− | return row |
||
+ | t[5] = max |
||
+ | return t |
||
end |
end |
||
− | local function |
+ | local function parse_stars(name, values) |
− | local |
+ | local name = name or "" |
+ | local values = values or "" |
||
− | row :tag("th") |
||
+ | local max = MAX[name] |
||
− | for i=1,maxStatColumns do |
||
+ | local t = {nil, nil, nil, nil, nil} |
||
− | row :tag("th") |
||
+ | for i,x in ipairs(mw.text.split(values, "%s*,%s*")) do |
||
− | :attr("scope", "col") |
||
+ | x = mw.text.trim(x) |
||
− | :wikitext(BLACK..i) |
||
+ | local black, count = x:gsub(BLACK, "*"):match("^([%*]*)x?(%d*)") |
||
− | end |
||
+ | if count ~= "" and (black == "" or black == "*") then |
||
− | return row |
||
+ | t[i] = tonumber(count) |
||
− | end |
||
+ | elseif black ~= "" then |
||
− | |||
+ | t[i] = #black |
||
− | 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 |
||
− | |||
end |
end |
||
+ | t.name = name |
||
− | return '-' |
||
+ | t[5] = max |
||
+ | return t |
||
end |
end |
||
+ | |||
− | |||
+ | local function parse_ability(name, values, mod, max) |
||
− | |||
− | local |
+ | local name = name or "???" |
− | local |
+ | local values = values or "" |
+ | local mod = tonumber(string.match(mod or '', '^%s*([%+%-]?%d*)%%?%s*$')) |
||
− | return mw.text.split(str or '', sep) |
||
+ | local max = tonumber(string.match(max or '', '^%s*([%+%-]?%d*)%%?%s*$')) |
||
− | end |
||
+ | local t = {nil, nil, nil, nil, nil} |
||
− | |||
+ | for i,x in ipairs(mw.text.split(values, "%s*,%s*")) do |
||
− | local function handle_stars(str, show_percent) |
||
+ | t[i] = tonumber(x:match("^%s*([%+%-]?%d*)%%?%s*$")) |
||
− | 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 |
end |
||
+ | t.name = name |
||
+ | t.mod = mod |
||
+ | t[5] = max |
||
return t |
return t |
||
end |
end |
||
+ | |||
− | |||
+ | function p._main(args) |
||
− | --[[ |
||
+ | if args == nil then return "" end |
||
− | TODO: Handle min and max values such as: |
||
+ | local name = args.name or PAGENAME |
||
− | 1 |
||
+ | if (name == nil or name == "") then name = "???" end |
||
− | +1 |
||
+ | local caption = "Stats & Auto-Abilities of <em>"..name.."</em>" |
||
− | 1% |
||
− | + | ||
+ | local data = { |
||
− | + 1 |
||
+ | {BLACK..1, BLACK..2, BLACK..3, BLACK..4, BLACK..5, name="", mod="Mod", type="header"}, |
||
− | + 1% |
||
+ | -- Extract base attributes (HP, Attack, Break Power, Magic) |
||
− | ]] |
||
+ | parse_attrs("HP", args.hp), |
||
− | local function handle_abilities(args) |
||
+ | parse_attrs("Attack", args.atk), |
||
− | local t = {} |
||
+ | parse_attrs("Break Power", args.brk), |
||
− | for i=1,4 do |
||
+ | parse_attrs("Magic", args.mag), |
||
− | local name, min, max, req |
||
+ | -- Extract stars (Crit Chance, Speed, and Defense) |
||
− | if i == 4 then |
||
+ | parse_stars("Crit Chance", args.crt or args.cri), |
||
− | name = "Boost Ultimate" |
||
+ | parse_stars("Speed", args.spd), |
||
− | min = "1" |
||
+ | parse_stars("Defense", args.def), |
||
− | max = "1" |
||
+ | -- Extract auto-abilities (1, 2, 3, 4) |
||
− | req = "mod" |
||
+ | 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), |
||
+ | parse_ability(args.abi4, args.val4, args.mod4, args.max4), |
||
+ | -- @todo Find weapon that hasn't got hidden "Boost Ultimate+1" as 4th. |
||
+ | parse_ability("Boost Ultimate", nil, "1", "1"), |
||
+ | } |
||
+ | |||
+ | -- Storage |
||
+ | if NAMESPACE == 0 then |
||
+ | local frame = mw.getCurrentFrame() |
||
+ | for i=1,4 do |
||
+ | local cargoData = { |
||
+ | _table = 'weapon_stats', |
||
+ | rarity = i, |
||
+ | } |
||
+ | for _,stat in ipairs(data) do |
||
+ | local key = stat.name:lower():gsub(" ", "_") |
||
+ | if (key == 'hp' or |
||
+ | key == 'attack' or |
||
+ | key == 'break_power' or |
||
+ | key == 'magic' or |
||
+ | key == 'crit_chance' or |
||
+ | key == 'speed' or |
||
+ | key == 'defense') then |
||
+ | cargoData[key] = stat[i] |
||
+ | end |
||
+ | end |
||
+ | frame:callParserFunction("#cargo_store:", cargoData) |
||
+ | mw.logObject(cargoData) |
||
end |
end |
||
+ | end |
||
− | name = args['abi'..i] or name or '???' |
||
+ | |||
− | min = args['min'..i] or min or '' |
||
+ | -- Display |
||
− | max = args['max'..i] or max or '' |
||
+ | local root = mw.html.create("") |
||
− | req = args['req'..i] or req or 'mod' |
||
+ | local wikitable = root:tag("table") |
||
− | if string.find(req, '(%d+)') then |
||
+ | :addClass("wikitable") |
||
− | req = BLACK .. string.match(req, '(%d+)') |
||
+ | :cssText("text-align:right;") |
||
− | else |
||
+ | :tag("caption") |
||
− | req = 'mod' |
||
+ | :wikitext(caption) |
||
+ | :done() |
||
+ | for i,entry in ipairs(data) do |
||
+ | if entry.name ~= "???" or i ~= 12 then |
||
+ | local tagname = entry.type == "header" and "th" or "td" |
||
+ | local 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 |
||
+ | row:tag(tagname):wikitext(entry[key] or "-") |
||
+ | end |
||
end |
end |
||
− | t[i] = {name=name, min=min, max=max, req=req} |
||
end |
end |
||
− | return |
+ | return tostring(root) |
end |
end |
||
+ | |||
− | |||
function p.main(frame) |
function p.main(frame) |
||
+ | mArguments = require("Module:Arguments") |
||
− | local args = getArgs(frame, { |
||
+ | local args = mArguments.getArgs(frame, { |
||
− | wrappers = "Template:Weapon stats" |
||
+ | wrappers = { |
||
+ | "Template:Weapon stats", |
||
+ | } |
||
}) |
}) |
||
return p._main(args) |
return p._main(args) |
||
end |
end |
||
− | |||
− | function p._main(args) |
||
− | local cats = {} |
||
− | -- Transition to using array notation for auto-abilities |
||
− | for i=1,4 do |
||
− | if args['req'..i] == nil then |
||
− | --pass |
||
− | elseif args['req'..i] == 'mod' then |
||
− | cats[#cats+1] = "Weapon stats with deprecated parameters" |
||
− | args['mod'..i] = args['mod'..i] or args['min'..i] |
||
− | elseif tonumber(args['req'..i]) then |
||
− | cats[#cats+1] = "Weapon stats with deprecated parameters" |
||
− | args['val'..i] = mw.text.split(args['val'..i] or '', ",") |
||
− | for j=1,math.min(4, tonumber(args['req'..i])) do |
||
− | args['val'..i][j] = tostring(tonumber(args['val'..i][j]) or '-') |
||
− | end |
||
− | for j=tonumber(args['req'..i]),4 do |
||
− | args['val'..i][j] = tostring(tonumber(args['min'..i]) or '-') |
||
− | end |
||
− | args['val'..i] = table.concat(args['val'..i], ',') |
||
− | end |
||
− | end |
||
− | if args.val1 or args.val2 or args.val3 or args.val4 then |
||
− | for i,cat in ipairs(cats) do |
||
− | cats[i] = string.format("[[Category:%s]]", cat) |
||
− | end |
||
− | local stats = tostring(_main2(args)) |
||
− | stats = stats .. table.concat(cats) |
||
− | return stats |
||
− | end |
||
+ | --[[ Usage: |
||
− | local name = args.name |
||
+ | _main{ |
||
− | -- Numeric stats |
||
+ | name = "Lightbringer", |
||
− | local hps = handle_num(args.hp) |
||
+ | hp = "100, 200, 300, 400", |
||
− | local atks = handle_num(args.atk) |
||
+ | atk = "15, 40, 60, 80", |
||
− | local brks = handle_num(args.brk) |
||
+ | brk = "10, 20, 30, 40", |
||
− | local mags = handle_num(args.mag) |
||
+ | mag = "15, 30, 40, 60", |
||
− | -- Star stats |
||
+ | crt = "0, 0, 0, 0", |
||
− | local cris = handle_stars(args.crt or args.cri, true) |
||
+ | spd = "0, 1, 1, 1", |
||
− | local spds = handle_stars(args.spd) |
||
+ | def = "15, 15, 15, 15", |
||
− | local defs = handle_stars(args.def, true) |
||
+ | abi1 = "Piercing Break", val1 = "30, 50, 50, 50", max1 = "100", |
||
− | -- Abilities |
||
+ | abi2 = "Auto-Charge Ultimate", mod2 = "2", max2 = "8", |
||
− | local abis = handle_abilities(args) |
||
+ | abi3 = "Steelguard", mod3 = "1", max3 = "5", |
||
− | |||
+ | } |
||
− | 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 |
||
− | |||
function p.test() |
function p.test() |
||
mw.log(p._main{ |
mw.log(p._main{ |
||
− | name = " |
+ | name = "Lightbringer", |
− | hp = " |
+ | hp = "100, 200, 300, 400", |
− | atk = " |
+ | atk = "15, 40, 60, 80", |
− | brk = " |
+ | brk = "10, 20, 30, 40", |
− | mag = " |
+ | mag = "15, 30, 40, 60", |
− | + | crt = "0, 0, 0, 0", |
|
− | spd = "0, |
+ | spd = "0, 1, 1, 1", |
− | def = " |
+ | def = "15, 15, 15, 15", |
− | abi1 = " |
+ | abi1 = "Piercing Break", val1 = "30, 50, 50, 50", max1 = "100", |
− | abi2 = " |
+ | abi2 = "Auto-Charge Ultimate", mod2 = "2", max2 = "8", |
− | abi3 = " |
+ | abi3 = "Steelguard", mod3 = "1", max3 = "5", |
+ | }) |
||
− | abi4 = "Boost Ultimate", min4 = "1", max4 = "1", req4 = "mod", |
||
+ | mw.log(p._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 |
end |
||
+ | |||
− | |||
return p |
return p |
Latest revision as of 05:42, 24 June 2019
Documentation for this module may be created at Module:Weapon stats/doc
local mArguments -- lazy load.
local p = {}
local title = mw.title.getCurrentTitle()
local PAGENAME = title.text
local NAMESPACE = title.namespace
local MAX = {
["HP"] = 2000,
["Attack"] = 200,
["Break Power"] = 200,
["Magic"] = 200,
["Crit Chance"] = 5,
["Speed"] = 2,
["Defense"] = 5,
}
local BLACK = mw.ustring.char(0x2605) -- "★"
local WHITE = mw.ustring.char(0x2606) -- "★"
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 i,x in ipairs(mw.text.split(values, "%s*,%s*")) do
t[i] = tonumber(mw.text.trim(x))
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 i,x in ipairs(mw.text.split(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[i] = tonumber(count)
elseif black ~= "" then
t[i] = #black
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 i,x in ipairs(mw.text.split(values, "%s*,%s*")) do
t[i] = tonumber(x:match("^%s*([%+%-]?%d*)%%?%s*$"))
end
t.name = name
t.mod = mod
t[5] = max
return t
end
function p._main(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 <em>"..name.."</em>"
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),
parse_ability(args.abi4, args.val4, args.mod4, args.max4),
-- @todo Find weapon that hasn't got hidden "Boost Ultimate+1" as 4th.
parse_ability("Boost Ultimate", nil, "1", "1"),
}
-- Storage
if NAMESPACE == 0 then
local frame = mw.getCurrentFrame()
for i=1,4 do
local cargoData = {
_table = 'weapon_stats',
rarity = i,
}
for _,stat in ipairs(data) do
local key = stat.name:lower():gsub(" ", "_")
if (key == 'hp' or
key == 'attack' or
key == 'break_power' or
key == 'magic' or
key == 'crit_chance' or
key == 'speed' or
key == 'defense') then
cargoData[key] = stat[i]
end
end
frame:callParserFunction("#cargo_store:", cargoData)
mw.logObject(cargoData)
end
end
-- Display
local root = mw.html.create("")
local wikitable = root:tag("table")
:addClass("wikitable")
:cssText("text-align:right;")
:tag("caption")
:wikitext(caption)
:done()
for i,entry in ipairs(data) do
if entry.name ~= "???" or i ~= 12 then
local tagname = entry.type == "header" and "th" or "td"
local 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
row:tag(tagname):wikitext(entry[key] or "-")
end
end
end
return tostring(root)
end
function p.main(frame)
mArguments = require("Module:Arguments")
local args = mArguments.getArgs(frame, {
wrappers = {
"Template:Weapon stats",
}
})
return p._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",
}
]]
function p.test()
mw.log(p._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(p._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 p