(replace error() with assert()) |
(add punisher() function) |
||
Line 5: | Line 5: | ||
p.fields = 'id,name,input,target,damage,reach,tracksLeft,tracksRight,startup,recv,tot,crush,block,hit,ch,notes' | p.fields = 'id,name,input,target,damage,reach,tracksLeft,tracksRight,startup,recv,tot,crush,block,hit,ch,notes' | ||
-- Get move id for querying, which is the 1st unnamed param of a template. | |||
-- Moves are queried like this: {{TempateName|<moveId>}} | |||
function getQueryId(frame) | |||
local id = assert(frame:getParent().args, '1st unnamed param must be move id') | |||
return mw.text.trim(id) -- whitespace is stripped only from named params | |||
end | |||
function moveNotFoundMsg(id) | |||
return "move with id = '" .. id .. "' not found" | |||
end | |||
p.display = function(frame) | p.display = function(frame) | ||
Line 16: | Line 26: | ||
while (parentId ~= nil and parentId ~= '') do | while (parentId ~= nil and parentId ~= '') do | ||
local parent = cargo.query(tables, 'input,target,damage,parent', { where = "id='" .. parentId .. "'" })[1] | local parent = cargo.query(tables, 'input,target,damage,parent', { where = "id='" .. parentId .. "'" })[1] | ||
assert(parent, ' | assert(parent, 'parent = "' .. parentId .. '" not found') | ||
parentId = parent['parent'] | parentId = parent['parent'] | ||
Line 46: | Line 56: | ||
p.query = function(frame) | p.query = function(frame) | ||
local id = getQueryId(frame) | |||
local id = | |||
local result = cargo.query(tables, p.fields, { where = "id = '" .. id .. "'" })[1] | local result = cargo.query(tables, p.fields, { where = "id = '" .. id .. "'" })[1] | ||
assert(result, | assert(result, moveNotFoundMsg) | ||
for k, v in pairs(result) do | for k, v in pairs(result) do | ||
local override = args[k] | local override = frame:getParent().args[k] | ||
if override ~= nil then | if override ~= nil then | ||
result[k] = override | result[k] = override | ||
Line 61: | Line 68: | ||
return frame:expandTemplate{ title = 'MoveDataCargoTest/Display', args = result } | return frame:expandTemplate{ title = 'MoveDataCargoTest/Display', args = result } | ||
end | |||
p.punisher = function(frame) | |||
local id = getQueryId(frame) | |||
local input = '' | |||
local damage = 0 | |||
local hit = nil | |||
while (id ~= '') do | |||
local move = cargo.query(tables, 'parent,input,damage,hit', { where = "id = '" .. id .. '"' })[1] | |||
assert(move, moveNotFoundMsg) | |||
input = move['input'] .. input | |||
damage = damage + move['damage'] | |||
if hit == nil then | |||
hit = move['hit'] | |||
end | |||
id = move['parent'] | |||
end | |||
return '||' .. input .. '||' .. damage .. '||' .. hit | |||
end | end | ||
return p | return p |
Revision as of 16:28, 24 August 2023
Documentation for this module may be created at Module:Move7/doc
local p = {};
local cargo = mw.ext.cargo
local tables = 'MoveDataCargoTest'
p.fields = 'id,name,input,target,damage,reach,tracksLeft,tracksRight,startup,recv,tot,crush,block,hit,ch,notes'
-- Get move id for querying, which is the 1st unnamed param of a template.
-- Moves are queried like this: {{TempateName|<moveId>}}
function getQueryId(frame)
local id = assert(frame:getParent().args, '1st unnamed param must be move id')
return mw.text.trim(id) -- whitespace is stripped only from named params
end
function moveNotFoundMsg(id)
return "move with id = '" .. id .. "' not found"
end
p.display = function(frame)
local function appendFrom(dst, src)
for k, v in pairs(src) do
dst[k] = v
end
end
local function getLeads(parentId)
local leads = {}
while (parentId ~= nil and parentId ~= '') do
local parent = cargo.query(tables, 'input,target,damage,parent', { where = "id='" .. parentId .. "'" })[1]
assert(parent, 'parent = "' .. parentId .. '" not found')
parentId = parent['parent']
parent['parent'] = nil
for k, v in pairs(parent) do
local leadName = k .. 'Lead'
if leads[leadName] == nil then
leads[leadName] = ''
end
leads[leadName] = v .. leads[leadName]
end
end
return leads
end
local leads = getLeads(frame:getParent().args['parent'])
if next(leads) == nil then
return frame:expandTemplate{ title = 'MoveData', args = frame:getParent().args }
end
local args = {}
appendFrom(args, frame:getParent().args)
appendFrom(args, leads)
return frame:expandTemplate{ title = 'MoveData', args = args }
end
p.query = function(frame)
local id = getQueryId(frame)
local result = cargo.query(tables, p.fields, { where = "id = '" .. id .. "'" })[1]
assert(result, moveNotFoundMsg)
for k, v in pairs(result) do
local override = frame:getParent().args[k]
if override ~= nil then
result[k] = override
end
end
return frame:expandTemplate{ title = 'MoveDataCargoTest/Display', args = result }
end
p.punisher = function(frame)
local id = getQueryId(frame)
local input = ''
local damage = 0
local hit = nil
while (id ~= '') do
local move = cargo.query(tables, 'parent,input,damage,hit', { where = "id = '" .. id .. '"' })[1]
assert(move, moveNotFoundMsg)
input = move['input'] .. input
damage = damage + move['damage']
if hit == nil then
hit = move['hit']
end
id = move['parent']
end
return '||' .. input .. '||' .. damage .. '||' .. hit
end
return p