Module:Item/Mod: Difference between revisions
From Growtopia
Jump to navigationJump to search
>HashJona mNo edit summary |
>HashJona refactor |
||
| Line 1: | Line 1: | ||
local | local mods = require('Module:Item/ModList') | ||
local item = {} | local item = {} | ||
function joinWithConnector(list, connector) | function joinWithConnector(list, connector) | ||
connector = connector or | connector = connector or 'and' | ||
local n = #list | local n = #list | ||
if n == 0 then | if n == 0 then | ||
return | return '' | ||
elseif n == 1 then | elseif n == 1 then | ||
return list[1] | return list[1] | ||
elseif n == 2 then | elseif n == 2 then | ||
return list[1] .. | return list[1] .. ' ' .. connector .. ' ' .. list[2] | ||
elseif n >= 3 then | elseif n >= 3 then | ||
local firstPart = table.concat(list, | local firstPart = table.concat(list, ', ', 1, n - 1) | ||
return firstPart .. | return firstPart .. ', ' .. connector .. ' ' .. list[n] | ||
end | |||
end | |||
function startsWith(str, prefix) | |||
return str:sub(1, #prefix) == prefix | |||
end | |||
function length(obj) | |||
local count = 0 | |||
for _ in pairs(obj) do | |||
count = count + 1 | |||
end | |||
return count; | |||
end | |||
function nonEmpty(value, fallback) | |||
if value == nil or value == '' then | |||
return fallback | |||
else | |||
return value | |||
end | end | ||
end | end | ||
| Line 85: | Line 43: | ||
return keys | return keys | ||
end | end | ||
function debugResult(var, debugFlag) | function debugResult(var, debugFlag) | ||
if debugFlag == | if debugFlag == '1' then | ||
return | return '<pre>' .. var .. '</pre>' | ||
else | else | ||
return mw.getCurrentFrame():preprocess(var) | return mw.getCurrentFrame():preprocess(var) | ||
| Line 94: | Line 51: | ||
end | end | ||
function | function parseNotes(args) | ||
local | local notes = '' | ||
for | local categories = {} | ||
local keys = parseArgs(args) | |||
local currentPage = mw.title.getCurrentTitle() | |||
for i = 2, #keys do | |||
local argIndex = keys[i] | |||
local key = args[argIndex] | |||
local effect = mods[key] | |||
if effect ~= nil then | |||
if type(effect) == 'function' then | |||
effect = effect(args) | |||
end | |||
if effect['hide_mod_category'] ~= true | |||
and currentPage | |||
and not startsWith(currentPage.text, effect['mod_category'] .. '/') then | |||
table.insert(categories, "''[[" .. effect['mod_category'] .. '|'.. effect['effect'] .."]]''") | |||
end | |||
if effect.note ~= '' then | |||
notes = notes .. '|' .. effect.note | |||
end | |||
end | |||
end | |||
categories = joinWithConnector(categories, args["connector"]) | |||
if categories ~= '' then | |||
notes = '|Also grants ' .. categories .. notes | |||
end | end | ||
for i = 1, 2 do | |||
local note = args['note_' .. i] or '' | |||
if note ~= '' then | |||
notes = notes .. '|' .. note | |||
for i = 1, | |||
local note = args['note_' .. i] or | |||
if note ~= | |||
notes = notes .. | |||
end | end | ||
end | end | ||
| Line 126: | Line 91: | ||
end | end | ||
function item. | function item.parseDescription(args) | ||
local parsedEffects = {} | local parsedEffects = {} | ||
local categories = {} | local categories = {} | ||
| Line 135: | Line 100: | ||
local argIndex = keys[i] | local argIndex = keys[i] | ||
local key = args[argIndex] | local key = args[argIndex] | ||
local effect = mods[key] | |||
if effect | |||
if effect ~= nil then | |||
if type(effect | if type(effect) == 'function' then | ||
effect = effect(args) | |||
end | end | ||
if addCat then | if addCat then | ||
table.insert(categories, | table.insert(categories, '[[' .. effect.category .. ']]') | ||
end | end | ||
table.insert(parsedEffects, | table.insert(parsedEffects, effect.description) | ||
end | end | ||
end | end | ||
if (#keys - #parsedEffects) ~= 1 and addCat then | if (#keys - #parsedEffects) ~= 1 and addCat then | ||
table.insert(categories, | table.insert(categories, '[[Category:ItemModError]]') | ||
end | end | ||
return { | return { | ||
description = joinWithConnector(parsedEffects, args[ | description = joinWithConnector(parsedEffects, args['connector']), | ||
categories = table.concat(categories) | categories = table.concat(categories) | ||
} | } | ||
end | end | ||
| Line 183: | Line 127: | ||
local title = parent['title'] or mw.title.getCurrentTitle().text or 'unknown' | local title = parent['title'] or mw.title.getCurrentTitle().text or 'unknown' | ||
local label = parent['label'] or parent[1] | local label = parent['label'] or parent[1] | ||
local parsedEffects = item. | local parsedEffects = item.parseDescription(parent) | ||
local baseSentence = "When equipped, the '''" .. mw.text.nowiki(title) .. "''' grants the ''" .. mw.text.nowiki(label) .. "'' [[Mods|mod]]" | local baseSentence = "When equipped, the '''" .. mw.text.nowiki(title) .. "''' grants the ''" .. mw.text.nowiki(label) .. "'' [[Mods|mod]]" | ||
local fullSentence | local fullSentence | ||
if parsedEffects.description == | if parsedEffects.description == '' then | ||
fullSentence = baseSentence .. | fullSentence = baseSentence .. '.' | ||
else | else | ||
fullSentence = baseSentence .. | fullSentence = baseSentence .. ', which allows the player to ' .. parsedEffects.description .. '.' | ||
end | end | ||
return fullSentence .. parsedEffects.categories | return fullSentence .. parsedEffects.categories | ||
| Line 197: | Line 141: | ||
function item.table(frame) | function item.table(frame) | ||
local parent = frame:getParent().args | local parent = frame:getParent().args | ||
local name = parent['title'] or | local name = parent['title'] or '%PAGE%' | ||
local label = parent['label'] or parent[1] | local label = parent['label'] or parent[1] | ||
local notes = parseNotes(parent) | local notes = parseNotes(parent) | ||
return | return '\n{{Mod|' .. name .. '|' .. label .. notes .. '}}' | ||
end | end | ||
| Line 228: | Line 166: | ||
for k, v in pairs(effect) do | for k, v in pairs(effect) do | ||
table.insert(doc, "|'''" .. k .. "'''") | table.insert(doc, "|'''" .. k .. "'''") | ||
if type(v) == | if type(v) == 'function' then | ||
local defaultValue = v({}) | local defaultValue = v({}).description | ||
local value = v(args) | local value = v(args).description | ||
if(defaultValue == value) then | if(defaultValue == value) then | ||
table.insert(doc, | table.insert(doc, '|' .. nonEmpty(defaultValue, "''No default value''")) | ||
else | else | ||
table.insert(doc, | table.insert(doc, '|' .. '{{Bullet}} ' .. nonEmpty(defaultValue, "''No default value''") .. '<br>{{Bullet}} ' .. value) | ||
end | end | ||
else | else | ||
table.insert(doc, | table.insert(doc, '|' .. v) | ||
end | end | ||
table.insert(doc, '|-') | table.insert(doc, '|-') | ||
Revision as of 16:02, 29 March 2025
Documentation for this module may be created at Module:Item/Mod/doc
local mods = require('Module:Item/ModList')
local item = {}
function joinWithConnector(list, connector)
connector = connector or 'and'
local n = #list
if n == 0 then
return ''
elseif n == 1 then
return list[1]
elseif n == 2 then
return list[1] .. ' ' .. connector .. ' ' .. list[2]
elseif n >= 3 then
local firstPart = table.concat(list, ', ', 1, n - 1)
return firstPart .. ', ' .. connector .. ' ' .. list[n]
end
end
function startsWith(str, prefix)
return str:sub(1, #prefix) == prefix
end
function length(obj)
local count = 0
for _ in pairs(obj) do
count = count + 1
end
return count;
end
function nonEmpty(value, fallback)
if value == nil or value == '' then
return fallback
else
return value
end
end
function parseArgs(args)
local keys = {}
for k, _ in pairs(args) do
if type(k) == 'number' then
table.insert(keys, k)
end
end
return keys
end
function debugResult(var, debugFlag)
if debugFlag == '1' then
return '<pre>' .. var .. '</pre>'
else
return mw.getCurrentFrame():preprocess(var)
end
end
function parseNotes(args)
local notes = ''
local categories = {}
local keys = parseArgs(args)
local currentPage = mw.title.getCurrentTitle()
for i = 2, #keys do
local argIndex = keys[i]
local key = args[argIndex]
local effect = mods[key]
if effect ~= nil then
if type(effect) == 'function' then
effect = effect(args)
end
if effect['hide_mod_category'] ~= true
and currentPage
and not startsWith(currentPage.text, effect['mod_category'] .. '/') then
table.insert(categories, "''[[" .. effect['mod_category'] .. '|'.. effect['effect'] .."]]''")
end
if effect.note ~= '' then
notes = notes .. '|' .. effect.note
end
end
end
categories = joinWithConnector(categories, args["connector"])
if categories ~= '' then
notes = '|Also grants ' .. categories .. notes
end
for i = 1, 2 do
local note = args['note_' .. i] or ''
if note ~= '' then
notes = notes .. '|' .. note
end
end
return notes
end
function item.parseDescription(args)
local parsedEffects = {}
local categories = {}
local addCat = not args['no_cat']
local keys = parseArgs(args)
for i = 2, #keys do
local argIndex = keys[i]
local key = args[argIndex]
local effect = mods[key]
if effect ~= nil then
if type(effect) == 'function' then
effect = effect(args)
end
if addCat then
table.insert(categories, '[[' .. effect.category .. ']]')
end
table.insert(parsedEffects, effect.description)
end
end
if (#keys - #parsedEffects) ~= 1 and addCat then
table.insert(categories, '[[Category:ItemModError]]')
end
return {
description = joinWithConnector(parsedEffects, args['connector']),
categories = table.concat(categories)
}
end
function item.description(frame)
local parent = frame:getParent().args
local title = parent['title'] or mw.title.getCurrentTitle().text or 'unknown'
local label = parent['label'] or parent[1]
local parsedEffects = item.parseDescription(parent)
local baseSentence = "When equipped, the '''" .. mw.text.nowiki(title) .. "''' grants the ''" .. mw.text.nowiki(label) .. "'' [[Mods|mod]]"
local fullSentence
if parsedEffects.description == '' then
fullSentence = baseSentence .. '.'
else
fullSentence = baseSentence .. ', which allows the player to ' .. parsedEffects.description .. '.'
end
return fullSentence .. parsedEffects.categories
end
function item.table(frame)
local parent = frame:getParent().args
local name = parent['title'] or '%PAGE%'
local label = parent['label'] or parent[1]
local notes = parseNotes(parent)
return '\n{{Mod|' .. name .. '|' .. label .. notes .. '}}'
end
function item.documentation(frame)
local doc = {}
local args = {
misc = '<span style="background:#FF9999">misc</span>',
skin = '<span style="background:#FF9999">skin</span>',
gem_chance = '<span style="background:#FF9999">gem_chance</span>',
block_chance = '<span style="background:#FF9999">block_chance</span>'
}
table.insert(doc, '{|class="article-table" border="0" cellpadding="1" cellspacing="1" width="100%"')
table.insert(doc, '|-')
table.insert(doc, '! scope="col" style="width:15%" |Parameter')
table.insert(doc, '! scope="col" style="width:10%" |Value')
table.insert(doc, '! scope="col" style="width:75%" |Meaning')
table.insert(doc, '|-')
table.insert(doc, '| rowspan=' .. length(effect) .. ' | <span style="background:#FF9999">Mod</span>')
for k, v in pairs(effect) do
table.insert(doc, "|'''" .. k .. "'''")
if type(v) == 'function' then
local defaultValue = v({}).description
local value = v(args).description
if(defaultValue == value) then
table.insert(doc, '|' .. nonEmpty(defaultValue, "''No default value''"))
else
table.insert(doc, '|' .. '{{Bullet}} ' .. nonEmpty(defaultValue, "''No default value''") .. '<br>{{Bullet}} ' .. value)
end
else
table.insert(doc, '|' .. v)
end
table.insert(doc, '|-')
end
table.insert(doc, '|}')
return debugResult(table.concat(doc, '\n'), frame.args['debug'])
end
return item