Modül:Item
Methods for managing item data storage and formatting.
item.type(frame)(function)- Formats an item's type and weapon subtype.
- Parameter:
frameScribunto's frame object (table) - Returns: Formatted item type and weapon subtype (string)
item.sell(frame)(function)- Automatically deduces an item's selling value based on its buying value. Used only on Deltarune Wiki.
- Parameter:
frameScribunto frame object (table) - Returns: Item's selling value (string)
item.storeData(frame)(function)- Stores item infobox data in the item bucket.
- Parameter:
frameScribunto frame object (table) item.table(frame)(function)- Generates a table listing of items of a certain type.
- Parameter:
frameScribunto frame object (table) - Returns: Table with items listed (string)
--- Methods for managing item data storage and formatting.
-- @module item
-- @require Module:Util
-- @require Module:Location
-- @author [[User:KockaAdmiralac|KockaAdmiralac]]
-- @author [[User:Jacky720|Jacky720]]
-- <nowiki>
require('strict')
local item = {}
-- Package imports.
local util = require('Module:Util')
local location = require('Module:Location')
local data = mw.loadData('Module:Item/data')
-- Package variables.
local title = mw.title.getCurrentTitle()
local bucket = mw.ext.bucket
local function parseTypeList(inputTypes, equip)
if inputTypes == nil then
return {}, {}, nil
end
local types = {}
local categories = {}
local weaponType = nil
for _, itemType in ipairs(util.parseCommaList(inputTypes)) do
local itemCategory = data.itemtypes[itemType]
if itemCategory then
table.insert(types, itemType)
util.addCategory(categories, itemCategory)
if itemType == 'Weapon' then
weaponType = data.weapontypes[equip]
if weaponType then
util.addCategory(categories, weaponType)
end
end
end
end
return types, categories, weaponType
end
--- Formats an item's type and weapon subtype.
-- @function item.type
-- @param {table} frame Scribunto's frame object
-- @returns {string} Formatted item type and weapon subtype
function item.type(frame)
local types, categories = parseTypeList(frame.args[1], frame.args[2])
return util.formatBulletList(types) .. table.concat(categories)
end
--- Automatically deduces an item's selling value based on its buying value.
-- Used only on Deltarune Wiki.
-- @function item.sell
-- @param {table} frame Scribunto frame object
-- @returns {string} Item's selling value
function item.sell(frame)
local val = tonumber(frame.args[1])
if val then
return math.ceil(val / 2) .. ' D$'
end
end
--- Stores item infobox data in the item bucket.
-- @function item.storeData
-- @param {table} frame Scribunto frame object
function item.storeData(frame)
if title.namespace ~= 0 then
return
end
local args = frame:getParent().args
local types, _, weaponType = parseTypeList(args.type, args.equip)
bucket('item').put({
name = args.name or args.title or title.text,
description = args.flavortext and mw.text.killMarkers(args.flavortext) or nil,
type = types,
weapontype = weaponType or 'null',
effects = args.effects and mw.text.killMarkers(args.effects) or nil,
source = args.source,
unused = mw.ustring.find(args.source or '', 'Unused', 0, true) ~= nil,
buy = args.buy and mw.text.killMarkers(args.buy) or nil,
id = args.id and tonumber(mw.ustring.match(args.id, '^%d+')) or 999,
})
return '[[Category:Items]]'
end
local function addHeader(html, header, cond)
if not cond then
return
end
html:tag('th')
:wikitext(header)
:done()
end
local function addColumn(html, content, cond)
if not cond then
return
end
html:tag('td')
:newline()
:wikitext(content)
-- See [[Module:Soundtrack]] for why we need the second newline.
:newline()
:done()
end
--- Generates a table listing of items of a certain type.
-- @function item.table
-- @param {table} frame Scribunto frame object
-- @returns {string} Table with items listed
function item.table(frame)
local query = {}
local category = nil
local columns = {}
local args = frame:getParent().args
for arg, value in pairs(args) do
if value == 'true' then
value = true
elseif value == 'false' then
value = false
end
if arg == 'category' then
category = 'Category:' .. value
elseif arg == 'columns' then
for column in mw.text.gsplit(value, ',', true) do
columns[mw.text.trim(column)] = true
end
else
table.insert(query, {arg, '=', value})
end
end
if not args.columns then
columns = {
description = true,
effects = true,
source = true,
buy = true,
}
end
if #query == 1 and args.type then
query = {}
for type in mw.text.gsplit(args.type, ',', true) do
table.insert(query, {type = mw.text.trim(type)})
end
query = {op = 'OR', operands = query}
end
local rows = bucket('item')
.where(query, category)
.select(
'page_name',
'name',
'description',
'effects',
'source',
'buy',
'id'
)
.orderBy('id')
.run()
local t = mw.html.create('table')
:attr('class', 'wikitable items-table')
local h = t:tag('tr')
:tag('th')
:wikitext('Name')
:done()
addHeader(h, 'Description', columns.description)
addHeader(h, 'Effects', columns.effects)
addHeader(h, 'Source', columns.source)
addHeader(h, 'Buy', columns.buy)
h:done()
for _, row in ipairs(rows) do
local r = t:tag('tr')
:tag('td')
:wikitext(table.concat({'[[', row.page_name, '|', row.name, ']]'}))
:done()
addColumn(r, row.description or '', columns.description)
addColumn(r, row.effects or 'N/A', columns.effects)
addColumn(r, location.formatList(location.parseList(row.source or '')), columns.source)
addColumn(r, row.buy or 'N/A', columns.buy)
r:done()
end
return tostring(t:done())
end
return item