维护提醒

BWIKI 全站将于 9 月 3 日(全天)进行维护,期间无法编辑任何页面或发布新的评论。

全站通知:

模块:Tag

来自星露谷物语维基
跳到导航 跳到搜索
[ 创建 | 刷新 ]文档页面
当前模块文档缺失,需要扩充。
local p = {}
local Helper = require("Module:Helper")

local ObjectData = Helper.LazyLoad('Module:Object/data')
local BigCraftableData = Helper.LazyLoad('Module:Bundles/data')
local FurnitureData = Helper.LazyLoad('Module:Furniture/data')
local FishData = Helper.LazyLoad('Module:Fish/data')

local tagsCache = {}

local function sanitizeContextTag(tag)
    return tag:gsub("[^%w_]", "_"):lower()
end

local function addBasicIdTags(itemId, tags)
    local qualifiedId = itemId
    if not itemId:match("^%([A-Z]+%)") then
        qualifiedId = "(O)" .. itemId
    end
    
    tags[#tags + 1] = sanitizeContextTag("id_" .. qualifiedId)
    
    local itemType = qualifiedId:match("^%(([A-Z]+)%)")
    if itemType then
        local baseId = qualifiedId:match("^%([A-Z]+%)(.+)")
        tags[#tags + 1] = sanitizeContextTag("id_" .. itemType .. "_" .. baseId)
    end
end

local function addObjectTags(itemId, tags)
    local objectData = ObjectData[itemId]
    if not objectData then return end
    
    if objectData.ContextTags then
        for _, tag in ipairs(objectData.ContextTags) do
            tags[#tags + 1] = tag
        end
    end
    
    if objectData.GeodeDrops or not objectData.GeodeDropsDefaultItems then
        tags[#tags + 1] = "geode"
    end
    
    if objectData.CanBeGivenAsGift == false then
        tags[#tags + 1] = "not_giftable"
    end
end

local function addFishTags(itemId, tags)
    local fishData = FishData[itemId]
    if not fishData then return end
    
    if fishData.trapLocation then
        tags[#tags + 1] = "fish_trap_location_" .. fishData.trapLocation
    else
        if fishData.motion then
            tags[#tags + 1] = "fish_motion_" .. fishData.motion
        end
        
        if fishData.difficulty then
            local difficulty = tonumber(fishData.difficulty) or 0
            local difficultyTag
            if difficulty <= 33 then
                difficultyTag = "fish_difficulty_easy"
            elseif difficulty <= 66 then
                difficultyTag = "fish_difficulty_medium"
            elseif difficulty <= 100 then
                difficultyTag = "fish_difficulty_hard"
            else
                difficultyTag = "fish_difficulty_extremely_hard"
            end
            tags[#tags + 1] = difficultyTag
        end
        
        if fishData.weather then
            tags[#tags + 1] = "fish_favor_weather_" .. fishData.weather
        end
    end
end

local function addCategoryTags(category, tags)
    local categoryTag
    
    if category == -999 then categoryTag = "category_litter"
    elseif category == -101 then categoryTag = "category_trinket"
    elseif category == -100 then categoryTag = "category_clothing"
    elseif category == -99 then categoryTag = "category_tool"
    elseif category == -98 then categoryTag = "category_weapon"
    elseif category == -97 then categoryTag = "category_boots"
    elseif category == -96 then categoryTag = "category_ring"
    elseif category == -95 then categoryTag = "category_hat"
    elseif category == -81 then categoryTag = "category_greens"
    elseif category == -80 then categoryTag = "category_flowers"
    elseif category == -79 then categoryTag = "category_fruits"
    elseif category == -75 then categoryTag = "category_vegetable"
    elseif category == -74 then categoryTag = "category_seeds"
    elseif category == -29 then categoryTag = "category_equipment"
    elseif category == -28 then categoryTag = "category_monster_loot"
    elseif category == -27 then categoryTag = "category_syrup"
    elseif category == -26 then categoryTag = "category_artisan_goods"
    elseif category == -25 then categoryTag = "category_ingredients"
    elseif category == -24 then categoryTag = "category_furniture"
    elseif category == -23 then categoryTag = "category_sell_at_fish_shop"
    elseif category == -22 then categoryTag = "category_tackle"
    elseif category == -21 then categoryTag = "category_bait"
    elseif category == -20 then categoryTag = "category_junk"
    elseif category == -19 then categoryTag = "category_fertilizer"
    elseif category == -18 then categoryTag = "category_sell_at_pierres_and_marnies"
    elseif category == -17 then categoryTag = "category_sell_at_pierres"
    elseif category == -16 then categoryTag = "category_building_resources"
    elseif category == -15 then categoryTag = "category_metal_resources"
    elseif category == -14 then categoryTag = "category_meat"
    elseif category == -12 then categoryTag = "category_minerals"
    elseif category == -9 then categoryTag = "category_big_craftable"
    elseif category == -8 then categoryTag = "category_crafting"
    elseif category == -7 then categoryTag = "category_cooking"
    elseif category == -6 then categoryTag = "category_milk"
    elseif category == -5 then categoryTag = "category_egg"
    elseif category == -4 then categoryTag = "category_fish"
    elseif category == -2 then categoryTag = "category_gem"
    end
    
    if categoryTag then
        tags[#tags + 1] = categoryTag
    end
end

local function addItemTypeSpecificTags(itemId, tags)
    local itemType = itemId:match("^%(([A-Z]+)%)")
    
    if itemType == "BC" then
        local craftableData = BigCraftableData[itemId:match("^%([A-Z]+%)(.+)")]
        if craftableData and craftableData.ContextTags then
            for _, tag in ipairs(craftableData.ContextTags) do
                tags[#tags + 1] = tag
            end
        end
    elseif itemType == "F" then
        local furnitureData = FurnitureData[itemId:match("^%([A-Z]+%)(.+)")]
        if furnitureData and furnitureData.ContextTags then
            for _, tag in ipairs(furnitureData.ContextTags) do
                tags[#tags + 1] = tag
            end
        end
    elseif itemType == "O" or not itemType then
        local baseId = itemType and itemId:match("^%([A-Z]+%)(.+)") or itemId
        addObjectTags(baseId, tags)
    end
end

local function addInternalNameTags(itemId, internalName, objectType, tags)
    if internalName then
        tags[#tags + 1] = "item_" .. sanitizeContextTag(internalName)
    end
    
    if objectType then
        tags[#tags + 1] = "item_type_" .. sanitizeContextTag(objectType)
    end
end

function p.getContextTags(frame)
    local itemId = frame.args[1]
    if not itemId then return "" end
    
    local tags = {}
    
    addBasicIdTags(itemId, tags)
    addItemTypeSpecificTags(itemId, tags)
    
    local baseId = itemId:match("^%([A-Z]+%)(.+)") or itemId
    local objectData = ObjectData[baseId]
    
    if objectData then
        if objectData.Name then
            addInternalNameTags(itemId, objectData.Name, objectData.Type, tags)
        end
        
        if objectData.Category then
            addCategoryTags(tonumber(objectData.Category), tags)
        end
        
        if tonumber(objectData.Category) == -4 then
            addFishTags(baseId, tags)
        end
    end
    
    return table.concat(tags, ",")
end

function p.getTagsTable(frame)
    local itemId = frame.args[1]
    if not itemId then return "{}" end
    
    local tags = {}
    
    addBasicIdTags(itemId, tags)
    addItemTypeSpecificTags(itemId, tags)
    
    local baseId = itemId:match("^%([A-Z]+%)(.+)") or itemId
    local objectData = ObjectData[baseId]
    
    if objectData then
        if objectData.Name then
            addInternalNameTags(itemId, objectData.Name, objectData.Type, tags)
        end
        
        if objectData.Category then
            addCategoryTags(tonumber(objectData.Category), tags)
        end
        
        if tonumber(objectData.Category) == -4 then
            addFishTags(baseId, tags)
        end
    end
    
    local result = "{"
    for i, tag in ipairs(tags) do
        if i > 1 then result = result .. "," end
        result = result .. '"' .. tag .. '"'
    end
    result = result .. "}"
    
    return result
end

local function generateTagsForItem(itemId)
    local tags = {}
    
    addBasicIdTags(itemId, tags)
    addItemTypeSpecificTags(itemId, tags)
    
    local baseId = itemId:match("^%([A-Z]+%)(.+)") or itemId
    local objectData = ObjectData[baseId]
    
    if objectData then
        if objectData.Name then
            addInternalNameTags(itemId, objectData.Name, objectData.Type, tags)
        end
        
        if objectData.Category then
            addCategoryTags(tonumber(objectData.Category), tags)
        end
        
        if tonumber(objectData.Category) == -4 then
            addFishTags(baseId, tags)
        end
    end
    
    return tags
end

function p.generateTagsData()
    local tagsData = {}
    
    for itemId, objectData in pairs(ObjectData) do
        local qualifiedId = "(O)" .. itemId
        tagsData[itemId] = generateTagsForItem(itemId)
        tagsData[qualifiedId] = generateTagsForItem(qualifiedId)
    end
    
    if BigCraftableData then
        for itemId, craftableData in pairs(BigCraftableData) do
            local qualifiedId = "(BC)" .. itemId
            tagsData[itemId] = generateTagsForItem(itemId)
            tagsData[qualifiedId] = generateTagsForItem(qualifiedId)
        end
    end
    
    if FurnitureData then
        for itemId, furnitureData in pairs(FurnitureData) do
            local qualifiedId = "(F)" .. itemId
            tagsData[itemId] = generateTagsForItem(itemId)
            tagsData[qualifiedId] = generateTagsForItem(qualifiedId)
        end
    end
    
    return tagsData
end

function p.getTagsByItemId(itemId)
    if tagsCache[itemId] then
        return tagsCache[itemId]
    end
    
    local tags = generateTagsForItem(itemId)
    tagsCache[itemId] = tags
    return tags
end

function p.test(frame)
    local testItems = {"16", "137", "24", "388", "390"}
    local results = {}
    
    for _, itemId in ipairs(testItems) do
        local tags = p.getTagsByItemId(itemId)
        results[#results + 1] = "Item " .. itemId .. ": " .. table.concat(tags, ", ")
    end
    
    return table.concat(results, "\n")
end

p.Data = Helper.LazyLoad('Module:Tag/data')

return p