Actions

Module

Module:DataFetch Test

From Project Rebearth

Revision as of 18:08, 16 February 2026 by Sharkie (talk | contribs) (Created page with "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 b...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

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

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

        local targetData = args.data
        local biome = args.biome
        local resource = args.resource or args.res
        local name = args.name or args.building

        local buildingData = data[name]

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