Actions

Module

DataFetch: Difference between revisions

From Project Rebearth

Sharkie (talk | contribs)
Created page with "-- Written with ❤️ by fshark (initially, at least) local p = {} local cache local function load() 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) local args = f.args local name = args.name loca..."
 
No edit summary
 
(10 intermediate revisions by one other user not shown)
Line 1: Line 1:
-- Written with ❤️ by fshark (initially, at least)
-- Written with ❤️ by fshark


local p = {}
local NOT_APPLICABLE = "N/A"


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


local function load()
assert(data, "Data failed to load. Please check \"Module:DataFetch/data.json\" exists.")
     if not cache then
 
         local title = mw.title.new("Module:DataFetch/data.json")
local abbreviations = {
         if title and title.exists then
    -- biomes
             cache = mw.text.jsonDecode(title:getContent())
    wood = "w",
        else
    stone = "s",
            cache = {}
    earth = "e",
    arctic = "a",
    -- crops
    empty = "e",
    potato = "p",
    grain = "g",
    maize = "m",
    sheep = "s",
    goat = "b",
    cow = "c",
}
 
-- for example: if the biome argument is the name of the biome rather than its letter, return the corresponding letter
local function getAbbreviation(biomeName)
     return abbreviations[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 string.lower(arg)
         end
         end
     end
     end
     return cache
    -- if no args; should not happen unless Template:BuildingData has not been updated yet.
     return ""
end
end


local data = load()
local function valueFind(t, ...)
 
     for i = 1, select("#", ...) do
function p.BuildingCost(f)
        local k = select(i, ...)
     local args = f.args
        local v = t[k]
    local name = args.name
        if type(v) == "table" then
    local biome = args.biome
            return valueFind(v, ...)
    local resource = args.resource
        end
    if not name or not biome or not resource then
        if v ~= nil then
         error("Missing arguments")
            return v
         end
     end
     end
    local buildingData = data[name]
     return NOT_APPLICABLE
     return buildingData.cost.byBiome[biome][resource]
end
end


return p
return {
    -- Universal getter for any property, for use with Template:BuildingData
    GetBuildingData = function(f)
        local args = f.args
        local section = getValidArg(args.section)
        local name = getValidArg(args.name)
 
        local source
        if section ~= "" then
            source = data[section]
        else
            source = data["buildings"] or data
        end
 
        if not source then
            return NOT_APPLICABLE
        end
 
        local buildingData = source[name]
 
        if not buildingData then
            return NOT_APPLICABLE
        end
 
        local targetData = getValidArg(args.data)
        local biome = getAbbreviation(getValidArg(args.biome))
        local resource = getValidArg(args.resource, args.res)
        local crop = getAbbreviation(getValidArg(args.crop))
 
        return valueFind(buildingData, targetData, resource, biome, crop)
    end
}

Latest revision as of 18:37, 1 March 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 abbreviations = {
    -- biomes
    wood = "w",
    stone = "s",
    earth = "e",
    arctic = "a",
    -- crops
    empty = "e",
    potato = "p",
    grain = "g",
    maize = "m",
    sheep = "s",
    goat = "b",
    cow = "c",
}

-- for example: if the biome argument is the name of the biome rather than its letter, return the corresponding letter
local function getAbbreviation(biomeName)
    return abbreviations[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 string.lower(arg)
        end
    end
    -- if no args; should not happen unless Template:BuildingData has not been updated yet.
    return ""
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 section = getValidArg(args.section)
        local name = getValidArg(args.name)

        local source
        if section ~= "" then
            source = data[section]
        else
            source = data["buildings"] or data
        end

        if not source then
            return NOT_APPLICABLE
        end

        local buildingData = source[name]

        if not buildingData then
            return NOT_APPLICABLE
        end

        local targetData = getValidArg(args.data)
        local biome = getAbbreviation(getValidArg(args.biome))
        local resource = getValidArg(args.resource, args.res)
        local crop = getAbbreviation(getValidArg(args.crop))

        return valueFind(buildingData, targetData, resource, biome, crop)
    end
}