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

模块:GiftsTable

来自星露谷物语扩展百科
[ 创建 | 刷新 ]文档页面
当前模块文档缺失,需要扩充。
-- [[模块:GiftsTable]]
-- 礼物表查询。数据见 [[模块:GiftsTable/data]](mw.loadData 缓存)。
-- 名称 / 描述分别回退到 [[模块:Name]]、[[模块:Description]](进而 [[模块:Items]])。
-- 调用:{{#invoke:GiftsTable|get|<key>|data=image|imglink|description|source|ingredients}}

local Name = require("Module:Name")
local Description = require("Module:Description")
local data = mw.loadData("Module:GiftsTable/data")

local p = {}

-- 页面上出现过的别名 / 错拼 → 数据键
local keyAliases = {
    age_blue_moon_wine = "aged_blue_moon_wine",
    bombadier_elixir = "bombardier_elixir",
    gold_carrot = "golden_carrot",
    lucky_four_leaf_clover = "four_leaf_clover",
    smelly_rafflesia = "rafflesia",
    bearberry = "bearberrys",
}

-- 数据键 → Module:Name / Module:Description 使用的英文名(空格分隔)
local nameAliases = {
    ancient_fern_seed = "ancient ferns seed",
    four_leaf_clover = "lucky four leaf clover",
    golden_carrot = "gold carrot",
    rafflesia = "smelly rafflesia",
    bombardier_elixir = "bombardier elixir",
}

local KEEP_LOWER = { of = true, the = true }
local ALL_CAPS = { cd = true, ii = true, iii = true, iv = true }

local function normalizeKey(key)
    if key == nil then
        return nil
    end
    key = mw.text.trim(tostring(key))
    if key == "" then
        return nil
    end
    key = key:lower():gsub("%s+", "_")
    return keyAliases[key] or key
end

local function deriveFileName(key)
    local parts = {}
    for part in string.gmatch(key, "[^_]+") do
        if ALL_CAPS[part] then
            table.insert(parts, string.upper(part))
        elseif KEEP_LOWER[part] then
            table.insert(parts, part)
        else
            table.insert(parts, part:sub(1, 1):upper() .. part:sub(2))
        end
    end
    return table.concat(parts, "_") .. ".png"
end

local function nameQuery(key)
    return nameAliases[key] or key
end

local function wrapLink(raw)
    if not raw or raw == "" then
        return ""
    end
    if raw:sub(1, 2) == "[[" then
        return raw
    end
    return "[[" .. raw .. "]]"
end

function p.get(frame)
    local args = frame.args
    local key = normalizeKey(args[1])
    local field = args[2] or args.data or args["data"]
    if not key or not field or field == "" then
        return ""
    end
    field = mw.text.trim(tostring(field)):lower()

    local row = data[key] or {}

    if field == "source" then
        return row.source or ""
    elseif field == "ingredients" then
        return row.ingredients or ""
    elseif field == "image" then
        local filename = row.image or deriveFileName(key)
        if filename:sub(1, 2) == "[[" then
            return filename
        end
        return "[[File:" .. filename .. "]]"
    elseif field == "imglink" then
        return wrapLink(Name.getName(nameQuery(key)))
    elseif field == "description" then
        local desc = Description.getDesc(nameQuery(key))
        if desc and desc ~= "" then
            return desc
        end
        return row.description or ""
    end

    return row[field] or ""
end

return p