Actions

Module

DataFetch: Difference between revisions

From Project Rebearth

Sharkie (talk | contribs)
No edit summary
Sharkie (talk | contribs)
No edit summary
Line 1: Line 1:
-- Written with ❤️ by fshark (initially, at least)
-- Written with ❤️ by fshark (initially, at least)


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


local cache
assert(data, "Data failed to load.")


local function load()
local p = {}
    if not cache then
        local title = mw.title.new("Module:DataFetch/data.json")
        if title and title.exists then
            cache = mw.text.jsonDecode(title:getContent())
        else
            cache = {}
        end
    end
    return cache
end
 
local data = load()


function p.BuildingCost(f)
function p.BuildingCost(f)
Line 27: Line 16:
         error("Missing arguments")
         error("Missing arguments")
     end
     end
     local buildingData = data[name]
     return data[name].cost.byBiome[biome][resource] or 0
    return buildingData.cost.byBiome[biome][resource] or 0
end
end


return p
return p

Revision as of 22:36, 15 February 2026

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

-- Written with ❤️ by fshark (initially, at least)

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.")

local p = {}

function p.BuildingCost(f)
    local args = f.args
    local name = args.name
    local biome = args.biome
    local resource = args.resource
    if not name or not biome or not resource then
        error("Missing arguments")
    end
    return data[name].cost.byBiome[biome][resource] or 0
end

return p