全站通知:

模块:Crops

来自星露谷物语维基
跳到导航 跳到搜索
[ 创建 | 刷新 ]文档页面
当前模块文档缺失,需要扩充。
local utils = require("Module:Utils")
local items = require("Module:Items")
local Object = require("Module:Object")
---@diagnostic disable-next-line: undefined-global
local mw = mw
local CropsData = mw.loadData("Module:Crops/data")
local ShopsData = mw.loadData("Module:Shops/data")
local p = {}

local Stores = {
    ['Joja'] = '[[Joja超市]]',
    ['Raccoon'] = '[[大树桩(地点)|浣熊]]',
    ['Sandy'] = '[[绿洲]]',
    ['SeedShop'] = '[[皮埃尔的杂货店]]',
    ['Traveler'] = '[[旅行商人]]'
}
local StoresRaw = {
    ['Joja'] = '超市',
    ['Raccoon'] = '浣熊',
    ['Sandy'] = '绿洲',
    ['SeedShop'] = '杂货店',
    ['Traveler'] = '旅行'
}

function getItem(frame)
    local id = items.getId(frame.args[1])
    if id:sub(1, 3) == "(O)" then
        id = id:sub(4)
        return id
    else
        return nil
    end
end

function getData(id)
    for key, entry in pairs(CropsData) do
        if entry['HarvestItemId'] == id then return entry end
    end
    return nil
end

function getDataKey(id)
    for key, entry in pairs(CropsData) do
        if entry['HarvestItemId'] == id then return key end
    end
    return nil
end

function getDataField(id, field)
    for key, entry in pairs(CropsData) do
        if entry['HarvestItemId'] == id then return entry[field] end
    end
    return nil
end

-- =p.canRegrow{args={"Carrot"}}
function p.canRegrow(frame)
    local id = getItem(frame)
    if id == nil then return '' end
    local regrow = getDataField(id, 'RegrowDays')
    if regrow == nil then return '' end
    if regrow == -1 then
        return '否'
    else
        return '是'
    end
end

-- =p.regrowDays{args={"Carrot"}}
function p.regrowDays(frame)
    local id = getItem(frame)
    if id == nil then return '' end
    local regrow = getDataField(id, 'RegrowDays')
    if regrow == nil then return '' end
    if regrow == -1 then
        return '否'
    else
        if regrow == 1 then return '是(每天)' end
        return '是(每 ' .. regrow .. ' 天)'
    end
    return regrow
end

-- =p.isRaised{args={"Carrot"}}
-- 判定是否为棚架作物
function p.isRaised(frame)
    local id = getItem(frame)
    if id == nil then return '' end
    local raised = getDataField(id, 'IsRaised')
    if raised == nil then return '' end
    if raised == true then
        return '是'
    else
        return '否'
    end
    return raised
end

-- =p.daysInPhase{args={"Carrot"}}
function p.daysInPhase(frame)
    local id = getItem(frame)
    if id == nil then return '' end
    local phase = getDataField(id, 'DaysInPhase')
    if phase == nil then return '' end
    local str = ''
    local total = 0
    for _, value in ipairs(phase) do
        if str == '' then
            str = value
        else
            str = str .. ' + ' .. value
        end
        total = total + value
    end
    str = str .. ' = ' .. total
    return str
end

local function contains(text, substring)
    return string.find(text, substring, 1, true) ~= nil
end

-- =p.getPrice{args={"胡萝卜"}}
function p.getPrice(frame)
    local id = getItem(frame)
    id = getDataKey(id)
    local CanPurchase = false
    local SpecialStore = false
    local NormalStore = false
    local Store = ''
    local Price = -1
    for key, entry in pairs(ShopsData) do
        if entry['Currency'] == 0 then
            for _, item in ipairs(entry['Items']) do
                if item['ItemId'] == '(O)' .. id then
                    CanPurchase = true
                    if key ~= 'Joja' and not contains(key, 'Festival') and
                        not contains(key, 'IslandTrade') then
                        Store = key
                    end
                    if item['TradeItemId'] ~= nil then
                        SpecialStore = true
                    else
                        NormalStore = true
                    end
                    if item['Price'] ~= -1 then
                        Price = item['Price']
                    end
                end
            end
        end
    end
    if Store == '' then return '特殊' end

    if CanPurchase == true and NormalStore == true then
        if Price == -1 then
            local basePrice = tonumber(Object.getPriceById(id)) or 0
            Price = basePrice * 2
        end
        return utils.expandTemplate("模板:Price", {Price}) .. '(' ..
                   Stores[Store] .. ')'
    end

    return '特殊(' .. Stores[Store] .. ')'
end

-- =p.getStore{args={"胡萝卜"}}
function p.getStore(frame)
    local id = getItem(frame)
    id = getDataKey(id)
    local Store = ''
    for key, entry in pairs(ShopsData) do
        if entry['Currency'] == 0 then
            for _, item in ipairs(entry['Items']) do
                if item['ItemId'] == '(O)' .. id then
                    if key ~= 'Joja' and not contains(key, 'Festival') and
                        not contains(key, 'IslandTrade') then
                        Store = key
                    end
                end
            end
        end
    end
    return StoresRaw[Store]
end

return p