Actions

Module

DataFetch: Difference between revisions

From Project Rebearth

Sharkie (talk | contribs)
No edit summary
Sharkie (talk | contribs)
No edit summary
Line 28: Line 28:
         end
         end
     end
     end
end
local function valueFind(t, ...)
    for i = 1, select("#", ...) do
        local k = select(i, ...)
        local v = t[k]
        if type(v) == "table" then
            return valueFind(v, ...)
        end
        if v ~= nil then
            return v
        end
    end
    return NOT_APPLICABLE
end
end


Line 36: Line 50:


         local targetData = getValidArg(args.data)
         local targetData = getValidArg(args.data)
         local biome = getValidArg(args.biome)
         local biome = getBiomeKey(getValidArg(args.biome))
         local resource = getValidArg(args.resource, args.res)
         local resource = getValidArg(args.resource, args.res)
         local name = getValidArg(args.name)
         local name = getValidArg(args.name)
Line 42: Line 56:
         local buildingData = data[name]
         local buildingData = data[name]


         if targetData == "cost" then
         if not buildingData then
             local costData = buildingData.cost
             return NOT_APPLICABLE
            if not costData then
        end
                return NOT_APPLICABLE
            end
            local biomeValues = costData[getBiomeKey(biome)]
            assert(biomeValues, "Invalid biome argument") -- every building with a cost value has a biome value, so this one will throw an error when invalid instead of returning N/A.
            return biomeValues[resource] or NOT_APPLICABLE


         elseif targetData == "rate" then
         -- if targetData == "cost" then
            local rateData = buildingData.rate
        --    local costData = buildingData.cost
            if not rateData then
        --    if not costData then
                return NOT_APPLICABLE
        --        return NOT_APPLICABLE
            end
        --    end
            return rateData[resource] or NOT_APPLICABLE
        --    local biomeValues = costData[getBiomeKey(biome)]
        --    return biomeValues[resource] or NOT_APPLICABLE


         elseif targetData == "balance" then
         -- elseif targetData == "rate" then
            local rateData = buildingData.balance
        --    local rateData = buildingData.rate
            if not rateData then
        --    if not rateData then
                return NOT_APPLICABLE
        --        return NOT_APPLICABLE
            end
        --    end
            return rateData[resource] or NOT_APPLICABLE
        --    return rateData[resource] or NOT_APPLICABLE
         end
 
        -- elseif targetData == "balance" then
        --    local rateData = buildingData.balance
        --    if not rateData then
        --        return NOT_APPLICABLE
        --    end
        --    return rateData[resource] or NOT_APPLICABLE
         -- end
 
        -- attempt to retrieve other data if none has been returned yet
        return valueFind(buildingData, targetData, resource, biome)
     end
     end
}
}

Revision as of 20:50, 16 February 2026

Documentation for this module may be created at Module:DataFetch/doc

-- Written with ❤️ by fshark

local NOT_APPLICABLE = "N/A"

local title = mw.title.new("Module:DataFetch/data.json")
local data = title and title.exists and mw.text.jsonDecode(title:getContent())

assert(data, "Data failed to load. Please check \"Module:DataFetch/data.json\" exists.")

local biomeKeys = {
    wood = "w",
    stone = "s",
    earth = "e",
    arctic = "a"
}

-- if the biome argument is the name of the biome rather than its letter, return the corresponding letter
local function getBiomeKey(biomeName)
    return biomeKeys[string.lower(biomeName)] or biomeName
end

-- returns first argument that is not an empty string
local function getValidArg(...)
    for i = 1, select("#", ...) do
        local arg = select(i, ...)
        if arg and arg ~= "" then
            return arg
        end
    end
end

local function valueFind(t, ...)
    for i = 1, select("#", ...) do
        local k = select(i, ...)
        local v = t[k]
        if type(v) == "table" then
            return valueFind(v, ...)
        end
        if v ~= nil then
            return v
        end
    end
    return NOT_APPLICABLE
end

return {
    -- Universal getter for any property, for use with Template:BuildingData
    GetBuildingData = function(f)
        local args = f.args

        local targetData = getValidArg(args.data)
        local biome = getBiomeKey(getValidArg(args.biome))
        local resource = getValidArg(args.resource, args.res)
        local name = getValidArg(args.name)

        local buildingData = data[name]

        if not buildingData then
            return NOT_APPLICABLE
        end

        -- if targetData == "cost" then
        --     local costData = buildingData.cost
        --     if not costData then
        --         return NOT_APPLICABLE
        --     end
        --     local biomeValues = costData[getBiomeKey(biome)]
        --     return biomeValues[resource] or NOT_APPLICABLE

        -- elseif targetData == "rate" then
        --     local rateData = buildingData.rate
        --     if not rateData then
        --         return NOT_APPLICABLE
        --     end
        --     return rateData[resource] or NOT_APPLICABLE

        -- elseif targetData == "balance" then
        --     local rateData = buildingData.balance
        --     if not rateData then
        --         return NOT_APPLICABLE
        --     end
        --     return rateData[resource] or NOT_APPLICABLE
        -- end

        -- attempt to retrieve other data if none has been returned yet
        return valueFind(buildingData, targetData, resource, biome)
    end
}