模块:Object:修订间差异
来自星露谷物语扩展百科
更多操作
无编辑摘要 |
调整 |
||
| 第1行: | 第1行: | ||
local Utils = require("Module:Utils") | |||
local Items = require("Module:Items") | |||
---@diagnostic disable-next-line: undefined-global | |||
local mw = mw | |||
local ObjectData = mw.loadData('Module:Object/data') | local ObjectData = mw.loadData('Module:Object/data') | ||
local p = {} | local p = {} | ||
- | local ERROR_NOT_FOUND = "物品不存在。" | ||
local nameIndex | |||
local function normalizeId(value) | |||
if value == nil then return nil end | |||
local text = tostring(value):match("^%s*(.-)%s*$") | |||
if text == "" then return nil end | |||
return text | |||
end | |||
local function fetchItemById(rawId) | |||
local id = normalizeId(rawId) | |||
if not id then return nil, nil end | |||
return ObjectData[id], id | |||
end | |||
local function ensureNameIndex() | |||
if nameIndex then return nameIndex end | |||
nameIndex = {} | |||
for id, data in pairs(ObjectData) do | |||
local name = data and data.Name | |||
if type(name) == "string" then | |||
local normalized = Utils.normalizeKey(name) | |||
if normalized and normalized ~= "" and not nameIndex[normalized] then | |||
nameIndex[normalized] = id | |||
end | |||
end | |||
end | |||
return nameIndex | |||
end | |||
local function matchField(fields, requested) | |||
local key = Utils.normalizeKey(requested) | |||
if not key then return nil end | |||
for fieldName, value in pairs(fields) do | |||
if Utils.normalizeKey(fieldName) == key then return value end | |||
end | |||
return nil | |||
end | |||
-- mw.logObject(p.getFieldsById{ args = { "137"} }) | -- mw.logObject(p.getFieldsById{ args = { "137"} }) | ||
function p.getFieldsById( | function p.getFieldsById(input) | ||
local id = | local args = Utils.resolveArgs(input) | ||
local item = | local id = args[1] or args["1"] | ||
if not item then return | local item = fetchItemById(id) | ||
if not item then return ERROR_NOT_FOUND end | |||
local fields = { | |||
Name = item.Name, | |||
Type = item.Type, | Type = item.Type, | ||
Category = item.Category, | Category = item.Category, | ||
| 第17行: | 第63行: | ||
ContextTags = item.ContextTags | ContextTags = item.ContextTags | ||
} | } | ||
local requestedField = args[2] or args["2"] | |||
if requestedField and requestedField ~= "" then | |||
return matchField(fields, requestedField) or "" | |||
local | |||
end | end | ||
return fields | |||
return | |||
end | end | ||
-- =p.getColorById{ args = { "137"} } | -- =p.getColorById{ args = { "137"} } | ||
-- mw.logObject(p.getColorById{ args = { "137"} }) | -- mw.logObject(p.getColorById{ args = { "137"} }) | ||
function p.getColorById( | function p.getColorById(input) | ||
local id = | local id = Utils.getArg(input) | ||
local item = | local item = fetchItemById(id) | ||
local tags = item and item.ContextTags | |||
if type(tags) ~= "table" then return "" end | |||
end | |||
for _, tag in ipairs(tags) do | |||
if type(tag) == "string" and tag:match("^color_") then | |||
local formatted = tag:gsub("color_", ""):gsub("_", " ") | |||
formatted = formatted:gsub("(%a)(%w*)", function(first, rest) | |||
return first:upper() .. rest:lower() | |||
return | end) | ||
return formatted | |||
end | end | ||
end | end | ||
return " | return "" | ||
end | end | ||
-- mw.logObject(p.getPriceById{ args = { "137"} }) | -- mw.logObject(p.getPriceById{ args = { "137"} }) | ||
function p.getPriceById( | function p.getPriceById(input) | ||
local id = Utils.getArg(input) | |||
local item = fetchItemById(id) | |||
if not item then return "" end | |||
return item.Price or "" | |||
end | end | ||
-- mw.logObject(p.getPriceByName{ args = { "Smallmouth Bass"} }) | -- mw.logObject(p.getPriceByName{ args = { "Smallmouth Bass"} }) | ||
function p.getPriceByName( | function p.getPriceByName(input) | ||
local | local name = Utils.normalizeKey(Utils.getArg(input)) | ||
if not name then return "" end | |||
local id = Items.getId(name):gsub("%(O%)", "") | |||
if not id or id == "" then return "" end | |||
return p.getPriceById(id) | |||
end | end | ||
return p | return p | ||
2025年10月17日 (五) 20:56的版本
local Utils = require("Module:Utils")
local Items = require("Module:Items")
---@diagnostic disable-next-line: undefined-global
local mw = mw
local ObjectData = mw.loadData('Module:Object/data')
local p = {}
local ERROR_NOT_FOUND = "物品不存在。"
local nameIndex
local function normalizeId(value)
if value == nil then return nil end
local text = tostring(value):match("^%s*(.-)%s*$")
if text == "" then return nil end
return text
end
local function fetchItemById(rawId)
local id = normalizeId(rawId)
if not id then return nil, nil end
return ObjectData[id], id
end
local function ensureNameIndex()
if nameIndex then return nameIndex end
nameIndex = {}
for id, data in pairs(ObjectData) do
local name = data and data.Name
if type(name) == "string" then
local normalized = Utils.normalizeKey(name)
if normalized and normalized ~= "" and not nameIndex[normalized] then
nameIndex[normalized] = id
end
end
end
return nameIndex
end
local function matchField(fields, requested)
local key = Utils.normalizeKey(requested)
if not key then return nil end
for fieldName, value in pairs(fields) do
if Utils.normalizeKey(fieldName) == key then return value end
end
return nil
end
-- mw.logObject(p.getFieldsById{ args = { "137"} })
function p.getFieldsById(input)
local args = Utils.resolveArgs(input)
local id = args[1] or args["1"]
local item = fetchItemById(id)
if not item then return ERROR_NOT_FOUND end
local fields = {
Name = item.Name,
Type = item.Type,
Category = item.Category,
Price = item.Price,
ContextTags = item.ContextTags
}
local requestedField = args[2] or args["2"]
if requestedField and requestedField ~= "" then
return matchField(fields, requestedField) or ""
end
return fields
end
-- =p.getColorById{ args = { "137"} }
-- mw.logObject(p.getColorById{ args = { "137"} })
function p.getColorById(input)
local id = Utils.getArg(input)
local item = fetchItemById(id)
local tags = item and item.ContextTags
if type(tags) ~= "table" then return "" end
for _, tag in ipairs(tags) do
if type(tag) == "string" and tag:match("^color_") then
local formatted = tag:gsub("color_", ""):gsub("_", " ")
formatted = formatted:gsub("(%a)(%w*)", function(first, rest)
return first:upper() .. rest:lower()
end)
return formatted
end
end
return ""
end
-- mw.logObject(p.getPriceById{ args = { "137"} })
function p.getPriceById(input)
local id = Utils.getArg(input)
local item = fetchItemById(id)
if not item then return "" end
return item.Price or ""
end
-- mw.logObject(p.getPriceByName{ args = { "Smallmouth Bass"} })
function p.getPriceByName(input)
local name = Utils.normalizeKey(Utils.getArg(input))
if not name then return "" end
local id = Items.getId(name):gsub("%(O%)", "")
if not id or id == "" then return "" end
return p.getPriceById(id)
end
return p