打开/关闭菜单
331
1.7K
131
11.8K
星露谷物语扩展百科
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:Object:修订间差异

来自星露谷物语扩展百科
Sizau留言 | 贡献
getAllFishTagById
Sizau留言 | 贡献
无编辑摘要
 
(未显示同一用户的4个中间版本)
第1行: 第1行:
local Helper = require("Module:Helper")
local Utils = require("Module:Utils")
local ObjectData = Helper.LazyLoad('Module:Object/Data')
local Items = require("Module:Items")
 
---@diagnostic disable-next-line: undefined-global
local mw = mw
local ObjectData = mw.loadData('Module:Object/data')


local p = {}
local p = {}


-- =p.getFieldsById{ args = { "137"} }
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(frame)
function p.getFieldsById(input)
     local id = frame.args[1]
    local args = Utils.resolveArgs(input)
     local item = ObjectData[id]
     local id = args[1] or args["1"]
     if not item then return "Error: ID not found." end
     local item = fetchItemById(id)
   
     if not item then return ERROR_NOT_FOUND end
     return {
 
    Name = item.Name,
     local fields = {
        Name = item.Name,
         Type = item.Type,
         Type = item.Type,
         Category = item.Category,
         Category = item.Category,
第18行: 第63行:
         ContextTags = item.ContextTags
         ContextTags = item.ContextTags
     }
     }
    local requestedField = args[2] or args["2"]
    if requestedField and requestedField ~= "" then
        return matchField(fields, requestedField) or ""
    end
    return fields
end
end


-- =p.getFirstFishTagById{ args = { "137"} }
-- =p.getColorById{ args = { "137"} }
-- mw.logObject(p.getFirstFishTagById{ args = { "137"} })
-- mw.logObject(p.getColorById{ args = { "137"} })
function p.getFirstFishTagById(frame)
function p.getColorById(input)
     local id = frame.args[1]
     local id = Utils.getArg(input)
     local item = ObjectData[id]
     local item = fetchItemById(id)
     if not item or not item.ContextTags then return "" end
     local tags = item and item.ContextTags
   
    if type(tags) ~= "table" then return "" end
     for _, tag in ipairs(item.ContextTags) do
 
         if tag:match("^fish_") and tag ~= "fish_has_roe" and tag ~= "fish_pond" and tag ~= "fish_mines" and tag ~= "fish_night_market" then
     for _, tag in ipairs(tags) do
             return tag
         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
     end
     end
第35行: 第91行:
end
end


-- =p.getAllFishTagById{ args = { "137"} }
-- 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
 
-- mw.logObject(p.getAllFishTagById{ args = { "137"} })
-- mw.logObject(p.getAllFishTagById{ args = { "137"} })
function p.getAllFishTagById(frame)
function p.getAllFishTagById(input)
     local id = frame.args[1]
     local id = Utils.getArg(input)
     local item = ObjectData[id]
     local item = ObjectData[id]
     if not item or not item.ContextTags then return "" end
     if not item or not item.ContextTags then return "" end
第48行: 第120行:
     end
     end
     return new_tags
     return new_tags
end
-- =p.getColorById{ args = { "137"} }
-- mw.logObject(p.getColorById{ args = { "137"} })
function p.getColorById(frame)
    local id = frame.args[1]
    local item = ObjectData[id]
    if not item or not item.ContextTags then return "" end
   
    for _, tag in ipairs(item.ContextTags) do
        if tag:match("^color_") then
        local result = tag
        result = result:gsub("color_", ""):gsub("^%l", string.upper)
            return result
        end
    end
    return ""
end
function p.getIdByName(frame)
    local name = mw.ustring.lower(frame.args[1])
    for id, item in pairs(ObjectData) do
        if mw.ustring.lower(item.Name) == name then
        mw.logObject(id) -- !!!
            return id
        end
    end
    return "Error: Name not found."
end
-- =p.getFieldsByName{ args = { "Smallmouth Bass"} }
-- mw.logObject(p.getFieldsByName{ args = { "Smallmouth Bass"} })
function p.getFieldsByName(frame)
local id = p.getIdByName({ args = { frame.args[1] } })
    local name = mw.ustring.lower(frame.args[1])
if id then
        return p.getFieldsById({ args = { id } })
    end
    return "Error: Name not found."
end
-- mw.logObject(p.getPriceById{ args = { "137"} })
function p.getPriceById(frame)
return p.getFieldsById({ args = { frame.args[1] } })["Price"]
end
-- mw.logObject(p.getPriceByName{ args = { "Smallmouth Bass"} })
function p.getPriceByName(frame)
local id = p.getIdByName({ args = { frame.args[1] } })
return p.getPriceById({ args = { id } })
end
end


return p
return p

2025年10月17日 (五) 20:57的最新版本

[ 创建 | 刷新 ]文档页面
当前模块文档缺失,需要扩充。
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

-- mw.logObject(p.getAllFishTagById{ args = { "137"} })
function p.getAllFishTagById(input)
    local id = Utils.getArg(input)
    local item = ObjectData[id]
    if not item or not item.ContextTags then return "" end
    local new_tags = {}
    for _, tag in ipairs(item.ContextTags) do
        if tag:match("^fish_") and (tag == "fish_legendary" or tag == "fish_desert" or tag == "fish_semi_rare" or tag == "fish_carnivorous" or tag == "fish_freshwater" or tag == "fish_crab_pot" or tag == "fish_ocean" or tag == "fish_river" or tag == "fish_lake") then
            table.insert(new_tags, tag)
        end
    end
    return new_tags
end

return p