此模块的文档可以在模块:MMC/doc创建
local p = {}
local mArguments
local cargo
local cache = {}
local splitString = require( 'Module:SplitStringToTable' ).splitStringIntoTable
--- Return the Moves data and cache the data for reuse on the same page
---
--- @return table
local function getMovesData( game, chara, input )
-- Return cached data
if #cache > 0 then return cache end
-- Lazy initalize Module:Cargo
cargo = require( 'Module:Cargo' )
local tables = string.format('MoveData_%s', game)
local fields = 'chara,name,input,damage,guard,startup,recovery,onBlock,images,hitboxes'
local args = {
where = string.format('MoveData_%s.chara="%s" and MoveData_%s.input="%s"', game, chara, game, input),
orderBy = '_rowID',
limit = "101",
}
local results = cargo.getQueryResults( tables, fields, args, false )
local items = {}
for _, result in pairs( results ) do
items[result.input] = result
end
-- Save to cache
cache = items
-- mw.logObject(cache)
return cache
end
--- Return the move table that matches the input
---
--- @param chara string character to look for
--- @param inputs table Move input(s) to look for
--- @return table
local function getMove( game, chara, input )
local data = getMovesData( game, chara, input )
local moves = {}
local match = data[ input ]
if not match then
error( string.format( 'Could not find matching move ( %s — %s: %s)', game, chara, input ) )
end
moves[ input ] = data[ input ]
-- mw.logObject( moves, 'Matched move' )
return moves
end
local function getHTML( move, label, hitboxMode, imageNumber, game )
local html = mw.html.create('span'):addClass('tooltip')
if label then
html:wikitext(label)
else
html:wikitext( move['input'] )
end
local imgString
if hitboxMode then
imgString = move['hitboxes']
else
imgString = move['images']
end
if not imgString then
error( string.format( 'Image not found' ) )
end
if not imageNumber then imageNumber = 1 end
local imagesTable
if game == 'GBVSR' or game == 'HNK' or game == 'SMS' or game == 'SBX' or game == 'BBCT' then
imagesTable = splitString( imgString, '\\' )
else
imagesTable = splitString( imgString, ';' )
end
local ttText = mw.html.create('span'):addClass('tooltiptext')
:tag('span'):addClass('mw-parser-output')
:tag('span'):addClass('tmp')
:tag('span'):addClass('tmp-image')
:wikitext( string.format('[[File:%s|130x200px]]', imagesTable[tonumber(imageNumber)] ) )
:done()
:tag('span'):addClass('tmp-items')
:tag('span'):addClass('tmp-item')
:tag('span'):addClass('tmp-item-label')
:wikitext('Guard')
:done()
:tag('span'):addClass('tmp-item-data')
:wikitext( move['guard'] )
:done()
:done()
:tag('span'):addClass('tmp-item')
:tag('span'):addClass('tmp-item-label')
:wikitext('Startup')
:done()
:tag('span'):addClass('tmp-item-data')
:wikitext( move['startup'] )
:done()
:done()
:tag('span'):addClass('tmp-item')
:tag('span'):addClass('tmp-item-label')
:wikitext('Recovery')
:done()
:tag('span'):addClass('tmp-item-data')
:wikitext( move['recovery'] )
:done()
:done()
:tag('span'):addClass('tmp-item')
:tag('span'):addClass('tmp-item-label')
:wikitext('Advantage')
:done()
:tag('span'):addClass('tmp-item-data')
:wikitext( move['onBlock'] )
:done()
:done()
:done()
:done()
:done()
:done()
html:node(ttText)
html:done()
return html
end
function removeTextAfterSlash(str)
local index = string.find(str, "/")
if index then
return string.sub(str, 1, index - 1)
else
return str
end
end
function removeSubstring(largerString, substring)
local result = largerString:gsub(substring, '')
return result
end
function removeXrd(str)
local prefix = 'GGXRD-R2\/'
if string.sub(str, 1, #prefix) == prefix then
return string.sub(str, #prefix + 1)
else
return str
end
end
function p.main(frame)
mArguments = require( 'Module:Arguments' )
local args = mArguments.getArgs(frame)
return p._main(args)
end
--- Return the wikitext needed for the template
---
--- @return string
function p._main( args )
local game = args['game']
local chara = args['chara']
local input = args['input']
local label = args['label']
local hitboxMode = args['hitboxMode']
local imageNumber = args['imageNumber']
if not game then
game = mw.title.getCurrentTitle().rootText
end
if game == 'GGXRD-R2' then game = 'GGXRDR2' end
if not chara then
chara = mw.title.getCurrentTitle().text
chara = removeSubstring(chara, string.format('%s/', game))
end
if not input then
error( 'No inputs specified for the template' )
end
if game == 'GGXRDR2' then chara = removeXrd(chara) end
chara = removeTextAfterSlash(chara)
local moves = getMove( game, chara, input )
local html = getHTML( moves[input], label, hitboxMode, imageNumber, game )
return tostring(html) .. mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Template:TooltipMovePreview/styles.css' }
}
end
return p