Actions

Module

DataFetch: Difference between revisions

From Project Rebearth

Sharkie (talk | contribs)
No edit summary
Sharkie (talk | contribs)
new version
Line 1: Line 1:
-- Written with ❤️ by fshark (initially, at least)
local NOT_APPLICABLE = "N/A"


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


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


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


function p.BuildingCost(f)
-- if the biome argument is the name of the biome rather than its letter, return the corresponding letter
     local args = f.args
local function getBiomeKey(biomeName)
    local name = args.name
     return biomeKeys[string.lower(biomeName)] or biomeName
     local biome = args.biome
end
    local resource = args.resource
 
    if not name or not biome or not resource then
-- returns first argument that is not an empty string
         error("Missing arguments")
local function getValidArg(...)
     for i = 1, select("#", ...) do
        local arg = select(i, ...)
        if arg and arg ~= "" then
            return arg
         end
     end
     end
    return data[name].cost.byBiome[biome][resource] or 0
end
end


return p
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 = getValidArg(args.biome)
        local resource = getValidArg(args.resource, args.res)
        local name = getValidArg(args.name, args.building)
 
        local buildingData = data[name]
 
        if targetData == "cost" then
            local biomeValues = buildingData.cost[getBiomeKey(biome)]
            assert(biomeValues, "Invalid biome argument")
            return biomeValues[resource] or NOT_APPLICABLE
        end
    end
}

Revision as of 19:39, 16 February 2026

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

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

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 = getValidArg(args.biome)
        local resource = getValidArg(args.resource, args.res)
        local name = getValidArg(args.name, args.building)

        local buildingData = data[name]

        if targetData == "cost" then
            local biomeValues = buildingData.cost[getBiomeKey(biome)]
            assert(biomeValues, "Invalid biome argument")
            return biomeValues[resource] or NOT_APPLICABLE
        end
    end
}