欢迎大家来到沙石镇时光中文维基!本站编辑权限开放,欢迎加入中文维基 QQ 群「沙海时光」:372816689
目前正在进行全站数据更新,期间可能会存在显示异常的问题。

全站通知:

模块:Item

来自沙石镇时光维基
跳到导航 跳到搜索

注意

目前该页面的代码逻辑与英维有一定差异,待更新。
由于该模块已被广泛使用,更新代码需要完全调试好确定无误再操作。


local Helper = require("Module:Helper")
local ItemPrototype = Helper.LazyLoad("Module:AssetItemPrototypeItem")
local Refine = Helper.LazyLoad("Module:AssetRefineConfigRefine")
local Text = Helper.LoadAsset("Module:AssetItemChinese")
local ItemId = Helper.LazyLoad("Module:ItemId")
local ExhibitionItemBaseDatas = Helper.LazyLoad("Module:AssetExhibitionItemBaseDatas")
local ItemHelper = require("Module:ItemHelper")
local cache = require "mw.ext.LuaCache"
local KEY_PREFIX = "Module:Item"
local EXP_TIME = 172800

-- Major Functions --
local function getBaseQuality(item)
    for i = 1, 4 do
        if item.gradeWeight[i] ~= 0 then
            return i - 1
        end
    end
    return 0
end

local function getBuyPrice(item)
    local quality = getBaseQuality(item)
    local basePrice = item.buyPrice
    if quality == 2 then
        return math.ceil(basePrice * 1.25)
    elseif quality == 3 then
        return math.ceil(basePrice * 1.6)
    else
        return basePrice
    end
end

local function getSellPrice(item, quality, ignoreCantSell)
    if item.cantSold == 1 and not ignoreCantSell then
        return false
        -- nil cannot be put into array, so use false instead :(
    end
    local price = item.sellPrice.id0 / item.sellPrice.id1
    if quality == nil then
        quality = getBaseQuality(item)
    end
    if quality == 2 then
        -- blue quality
        price = price * 1.25
    elseif quality == 3 then
        price = price * 1.6
        -- purple quality
    end
    if item.sellPrice.id1 == 1 then
        price = math.floor(price)
    end
    return price
end

-- Helpers --
local function findItem(idOrName)
    -- ID
    if tonumber(idOrName) ~= nil then
        return ItemPrototype[tonumber(idOrName)]
    end

    -- name
    return ItemPrototype[ItemHelper.getId(idOrName)]
end

local function verifyResults(results, name)
    if #results == 0 then
        error("Cannot find item \"" .. name .. "\"")
    end
    local firstResult = results[1]
    for _, result in ipairs(results) do
        if result ~= firstResult then
            error("Found multiple items with name\"" .. name .. "\"")
        end
    end
    return firstResult
end

-- Exported APIs --
local p = {}

function p.BuyPrice(frame)
    local hash = Helper.HashArgs(frame.args)
    local cacheKey = KEY_PREFIX .. "BuyPrice" .. hash

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local idOrName = frame.args[1]

    if idOrName == nil then
        idOrName = frame:getParent():getTitle()
    end

    local item = findItem(idOrName)
    local result = getBuyPrice(item)
    local s, _ = tostring(result):reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", "")
    cache.set(cacheKey, s, EXP_TIME)
    return s
end

function p.SellPrice(frame)
    local idOrName = frame.args[1]
    local cacheKey = KEY_PREFIX .. "SellPrice" .. idOrName

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local item = findItem(idOrName)
    local result = getSellPrice(item)

    -- format output
    if not result then
        -- unsellable
        result = ""
    elseif result % 1 == 0 then
        -- add comma for integer
        local s, _ = tostring(result):reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", "")
        result = s
    else
        -- return result
    end

    cache.set(cacheKey, result, EXP_TIME)
    return result
end

function p.Description(frame)
    local idOrName = frame.args[1]
    local cacheKey = KEY_PREFIX .. "Description" .. idOrName

    -- if (cache.get(cacheKey)) then
    -- local result = cache.get(cacheKey)
    -- return result
    -- end

    local item = findItem(idOrName)

    if (item) then
        local exhibitionItemBaseData = ExhibitionItemBaseDatas[item.id]
        local categories = ""

        if (exhibitionItemBaseData) then
            categories = categories .. "[[Category:Museum donations]]"
        else
        end

        local result = p.getText(item.infoId) .. categories
        result = mw.getCurrentFrame():preprocess(result)
        cache.set(cacheKey, result, EXP_TIME)
        return result
    else
    end
end

function p.BaseQuality(frame)
    local cacheKey = KEY_PREFIX .. "BaseQuality" .. frame.args[1]

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local idOrName = frame.args[1]
    local item = findItem(idOrName)
    local result = getBaseQuality(item)

    local texts = {
        "Ordinary",
        "<span style=\"color:#44bd89>Outstanding</span>",
        "<span style=\"color:#5d8ace>Perfect</span>",
        "<span style=\"color:#b84eb2>Rare</span>"
    }

    local ret = texts[result + 1]
    cache.set(cacheKey, ret, EXP_TIME)
    return ret
end

p.Implemented = function(frame)
    local cacheKey = KEY_PREFIX .. "Implemented" .. frame.args[1]

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local name = frame.args[1]
    local result

    if ItemHelper.isImplemented(name) then
        result = frame.args[2] or "1"
    else
        result = frame.args[3] or ""
    end

    cache.set(cacheKey, result, EXP_TIME)
    return result
end

p.ListCategory = function(frame)
    local hash = Helper.HashArgs(frame.args)
    local cacheKey = KEY_PREFIX .. "ListCategory" .. hash

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local isTarget = {}

    for _, tag in ipairs(frame.args) do
        isTarget[tonumber(tag)] = true
    end

    local names = {}

    for name, id in pairs(ItemId) do
        if ItemHelper.isImplemented(id) then
            local tags = ItemPrototype[id].itemTag
            for _, tag in ipairs(tags) do
                if isTarget[tag] then
                    table.insert(names, name)
                    break
                end
            end
        end
    end

    table.sort(names)
    local items = {}

    for i, name in ipairs(names) do
        items[i] = ItemHelper.addIconAndLink(name)
    end

    local result = table.concat(items, "<br>")
    cache.set(cacheKey, result, EXP_TIME)
    return result
end

p.Source = function(frame)
    local firstArg = frame.args[1]
    local cacheKey = KEY_PREFIX .. "Source" .. firstArg

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local itemId = ItemHelper.getId(firstArg)
    local result = ItemHelper.getSource(itemId)

    if (result and result ~= "") then
        cache.set(cacheKey, result, EXP_TIME)
        return result
    else
        error(firstArg .. " does not have any source, or is currently unimplemented")
    end
end

p.id = function(frame)
	local itemName = frame.args[1]
    local cacheKey = KEY_PREFIX .. "findItemId" .. itemName

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local result = ItemHelper.getId(itemName)
    cache.set(cacheKey, result, EXP_TIME)
    return result
end

p.findItemId = function(itemName)
    local cacheKey = KEY_PREFIX .. "findItemId" .. itemName

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local result = ItemHelper.getId(itemName)
    cache.set(cacheKey, result, EXP_TIME)
    return result
end

p.getBaseQuality = function(itemId)
    local cacheKey = KEY_PREFIX .. "getBaseQuality" .. itemId

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local result = getBaseQuality(ItemPrototype[itemId])
    cache.set(cacheKey, result, EXP_TIME)
    return result
end

p.isQualityPossible = function(itemId, quality)
    local hash = Helper.HashArgs(itemId, quality)
    local cacheKey = KEY_PREFIX .. "isQualityPossible" .. hash

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local weights = ItemPrototype[itemId].gradeWeight
    local result = false

    if weights[quality + 1] > 0 then
        result = true
    end

    if (result) then
    else
        if Refine[itemId] ~= nil then
            for i = 0, quality - 1 do
                if weights[i + 1] > 0 then
                    result = true
                    break
                end
            end
        end
    end

    cache.set(cacheKey, result, EXP_TIME)
    return result
end

p.getItemName = function(itemId)
    local cacheKey = KEY_PREFIX .. "getItemName" .. itemId

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local name = p.getText(ItemPrototype[itemId].nameId)
    cache.set(cacheKey, name, EXP_TIME)
    return name
end

p.getSellPrice = function(itemId, quality, ignoreCantSell)
    local hash = Helper.HashArgs(itemId, quality, ignoreCantSell)
    local cacheKey = KEY_PREFIX .. "getSellPrice" .. hash

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local result = getSellPrice(ItemPrototype[itemId], quality, ignoreCantSell)
    cache.set(cacheKey, result, EXP_TIME)
    return result
end

function p.Type(frame)
    local idOrName = frame.args[1]
    local cacheKey = KEY_PREFIX .. "Type" .. idOrName

    if (cache.get(cacheKey)) then
        local result = cache.get(cacheKey)
        return result
    end

    local item = findItem(idOrName)
    local types = ""

    local enumTag = {
        [0] = "工坊设备|工坊设备",
        ["Creation"] = "工坊设备|工坊设备",
        [1] = "装备|装备",
        ["Equipment"] = "装备|装备",
        [2] = "消耗品|食物",
        ["Food"] = "消耗品|食物",
        [3] = "家具|家具",
        ["Furniture"] = "家具|家具",
        [4] = "原料|原料",
        ["Material"] = "原料|原料",
        [5] = "书籍|书籍",
        ["HiddenHandBook"] = "书籍|书籍",
        -- unimplemented
        [6] = "精炼",
        ["Refine"] = "精炼",
        [7] = "设备插件",
        ["MachinePlugin"] = "设备插件",
        [8] = "武器|武器",
        ["Weapon"] = "武器|武器",
        [9] = "调料",
        ["Seasoning"] = "调料",
        [10] = "食材|食材",
        ["Ingredient"] = "食材|食材",
        [11] = "实验核心",
        ["ExperimentCore"] = "实验核心",
        [12] = "Max",
        ["Max"] = "Max"
    }

    if (item.tags) then
        for _, tag in ipairs(item.tags) do
            types = types .. "[[:Category:" .. enumTag[tag] .. "]]" .. "[[Category:" .. enumTag[tag] .. "]]" .. "<br>"
        end

    end
    local result = string.sub(types, 1, string.len(types) - 4)
    cache.set(cacheKey, result, EXP_TIME)
    return result
end

local hasGenderDifference = Helper.Cached(
    function (itemId) return ItemPrototype[itemId]["femaleIconPath"] ~= "" end,
    "Item|hasGenderDifference",
    nil,
    1
)

--local hasGenderDifference =
--function (itemId)
--	mw.log(ItemPrototype[itemId]["femaleIconPath"])
--	return ItemPrototype[itemId]["femaleIconPath"] ~= "" 
--end

p.GenderDifferent = function(frame)
    local itemName = frame.args[1]
    local itemId = ItemHelper.getId(itemName)
    if hasGenderDifference(itemId) then
        return '1'
    else
        return ''
    end
end

p.getText = function(id)
    return Text[id]
end

p.debug = function()
    local args = {
        [1] = "Wedding Shoes"
    }

    local frame = {
        args = args
    }

    mw.log(p.GenderDifferent(frame))
end

return p