Module:Inventory slot/Utils

local loader = require('Module:Loader') local string, table, uiText, rarityTier = loader.require('String', 'Table', 'UIText', 'RarityTier')

local p = {}

-- Defines what data (fields) to process and transmit from Tooltips/Templates -- Same as 'otherInputBoxes' on MediaWiki:Gadget-TooltipsEditor.js local otherInputBoxes = { { 'name' }, { 'image', optional = true }, { 'link', optional = true }, }

function p._templateReplacements(values, default) local ret = {} local mandatoryFields = table.merge({ 'title', 'text' }, table.map(table.filter(otherInputBoxes, function(v)		return not v.optional	end), function(v) return v[1] end)) local optionalFields = table.map(table.filter(otherInputBoxes, function(v) return v.optional end), function(v) return v[1] end) local allFields = table.merge({}, mandatoryFields, optionalFields) for _, v in ipairs(mandatoryFields) do if not default[v] then error('Key ' .. key .. ' no default ' .. v) end end local g = { id = values.id } for _, v in ipairs(allFields) do g[v] = default[v] end if type(values) ~= 'table' then values = { values } end for _,item in ipairs(values) do -- ipairs: won't read global settings local repLs = {} local function repl(str) local function _repl(args) for _,sym in ipairs(args) do					str = str:gsub(('{%s}'):format(sym), repLs[sym] or '') end return str end -- Apply all replacements local special, numeric = string.split('osrl', ''), {} local ret = _repl(special) -- special(1st) for i in ret:gmatch('{(%d+)}') do				table.push(numeric, i)			end local ret = _repl(numeric) -- numeric after special(1st) local ret = _repl(special) -- special(2nd) after numeric return ret end local loc = {} local raw, id, vars, rare if type(item) == 'table' then raw = item[1] id, rare = item.id or g.id or item[1], item.r or item.rarity vars = table.slice(item, 2) for _, v in ipairs(allFields) do loc[v] = item[v] or g[v] end else raw, id = item, g.id or item for _, v in ipairs(allFields) do loc[v] = g[v] end end for i,v in ipairs(type(vars) == 'table' and vars or { vars }) do			repLs[tostring(i-1)] = tostring(v):gsub('%%','%%%%') -- so that it does not incorrectly remove '%'s		end repLs['o'], repLs['s'] = raw, raw id = repl(id) repLs['s'] = id		if rare then repLs['r'] = ('&%s'):format(uiText.getFormatting(rare)) repLs['l'] = ('%s&l%s&r'):format(repLs['r'], rarityTier._getRarity(rare):upper) end for k, v in pairs(loc) do			loc[k] = repl(v) end ret.push(loc) end return ret end

return p