モジュール:Item
このモジュールについての説明文ページを モジュール:Item/doc に作成できます
--- 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 infoboxData = mw.loadData('Module:Infobox/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 = infoboxData.itemtypes[itemType]
if itemCategory then
table.insert(types, itemType)
util.addCategory(categories, itemCategory)
if itemType == 'Weapon' then
weaponType = infoboxData.weapontypes[equip]
if weaponType then
table.insert(categories, weaponType)
end
util.addCategory(categories, weaponType)
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,
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,
id = args.id and tonumber(mw.ustring.match(args.id, '^%d+')) or 999,
})
return '[[Category:Items]]'
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 = {}
for arg, value in pairs(frame:getParent().args) do
if value == 'true' then
value = true
elseif value == 'false' then
value = false
end
table.insert(query, {arg, '=', value})
end
local data = bucket('item')
.where(query)
.select(
'page_name',
'name',
'description',
'effects',
'source',
'buy',
'id'
)
.orderBy('id')
.run()
local itemtable = mw.html.create('table')
:attr('class', 'wikitable items-table')
:tag('tr')
:tag('th')
:wikitext('Name')
:done()
:tag('th')
:wikitext('Description')
:done()
:tag('th')
:wikitext('Effects')
:done()
:tag('th')
:wikitext('Source')
:done()
:tag('th')
:wikitext('Buy')
:done()
:done()
for _, row in ipairs(data) do
itemtable:tag('tr')
:tag('td')
:wikitext(table.concat({'[[', row.page_name, '|', row.name, ']]'}))
:done()
:tag('td')
:newline()
:wikitext(row.description or '')
-- See [[Module:Soundtrack]] for why we need the second newline.
:newline()
:done()
:tag('td')
:newline()
:wikitext(row.effects or 'N/A')
:newline()
:done()
:tag('td')
:newline()
:wikitext(location.formatList(location.parseList(row.source or '')))
:newline()
:done()
:tag('td')
:newline()
:wikitext(row.buy or 'N/A')
:newline()
:done()
:done()
end
return tostring(itemtable:done())
end
return item