Module:BuildingDataProxy: Difference between revisions

From Against the Storm Official Wiki
(Creating to lazily load and federate queries out to building data models)
 
(Updating to new secondary categories; removing unused functional types)
 
(One intermediate revision by the same user not shown)
Line 8: Line 8:
-- Just store the strings, and we'll load lazily at runtime as needed. The order of this array controls the load order of data models.
-- Just store the strings, and we'll load lazily at runtime as needed. The order of this array controls the load order of data models.
local DATA_MODELS_NAMES_ARRAY = {
local DATA_MODELS_NAMES_ARRAY = {
     "Module:WorkshopsData2",
     "Module:WorkshopsData",
     "Module:InstitutionsData2",
     "Module:InstitutionsData",
     "Module:FarmsData2",
     "Module:FarmsData",
     "Module:GatheringData",
     "Module:GatheringData",
     "Module:FishingData",
     "Module:FishingData",
     "Module:RainCollectorsData",
     "Module:RainCollectorsData",
     "Module:CampsData2",
     "Module:CampsData",
     "Module:HearthsData",
     "Module:HearthsData",
     "Module:WarehousesData",
     "Module:WarehousesData",
Line 24: Line 24:
     DataModelsCache[moduleName] = nil
     DataModelsCache[moduleName] = nil
end
end
--endregion
--region Private constants
local LOOKUP_DATA_MODEL_FUNCTIONAL_TYPES = {
    ["Module:CampsData2"] = "Gathering Camp",
    ["Module:FarmFieldsData"] = "Farm",
    ["Module:FarmsData2"] = "Farm",
    ["Module:FishingData"] = "Fishing Hut",
    ["Module:GatheringData"] = "Gathering Camp",
    ["Module:HearthsData"] = "Hearth",
    ["Module:InstitutionsData2"] = "Service Building",
    ["Module:RainCollectorsData"] = "Extraction Building",
    ["Module:WarehousesData"] = "Storage Building",
    ["Module:WorkshopsData2"] = "Production Building",
}
--endregion
--region Private member variables


--endregion
--endregion
Line 69: Line 44:
---
---
---@param functionName string the name of the function to call
---@param functionName string the name of the function to call
---@param parameter string name or ID
---@param parameter1 ?
---@param parameter2 ?
---@param parameter3 ?
---@return ? whatever the function returns
---@return ? whatever the function returns
local function federateFunction(functionName, parameter)
local function federateFunction(functionName, parameter1, parameter2, parameter3)


     for _, modelName in ipairs(DATA_MODELS_NAMES_ARRAY) do
     for _, modelName in ipairs(DATA_MODELS_NAMES_ARRAY) do
         local DataModel = cacheModel(modelName)
         local DataModel = cacheModel(modelName)
         local result = DataModel[functionName](DataModel, parameter)
         local result = DataModel[functionName](DataModel, parameter1, parameter2, parameter3)
         -- If we found it, return, otherwise keep going
         -- If we found it, return, otherwise keep going
         if result then
         if result then
Line 90: Line 67:


--region Public building interface
--region Public building interface
-- getID(displayName)
-- getCategory(id)
-- getCityScore(id)
-- getConstructionCosts(id) -- returns { [goodName] = stack size }
-- getConstructionTime(id)
-- getDescription(id)
-- getIcon(id)
-- isMovable(id)
-- getName(id)
-- getNumberOfWorkplaces(id)
-- getSize(id) -- returns string "X x Y"
-- getStorage(id)


function BuildingDataProxy.getID(displayName)
function BuildingDataProxy.getID(displayName)
Line 110: Line 74:
function BuildingDataProxy.getCategory(id)
function BuildingDataProxy.getCategory(id)
     return federateFunction("getCategory", id)
     return federateFunction("getCategory", id)
end
function BuildingDataProxy.getSecondCategory(id)
    return federateFunction("getSecondCategory", id)
end
end


Line 150: Line 118:
function BuildingDataProxy.getStorage(id)
function BuildingDataProxy.getStorage(id)
     return federateFunction("getStorage", id)
     return federateFunction("getStorage", id)
end
function BuildingDataProxy.isServiceBuilding(id)
    return federateFunction("isServiceBuilding", id)
end
end


Line 155: Line 127:




--region Public methods
---getFunctionalType
---Looks up a descriptive "functional type" for the specified building, based on the data model in which it was found. Used to augment the category with more information. Returns nil if the building is not found.
---
---@param id string the ID of the building
---@return string describing the functional type of the building
function BuildingDataProxy.getFunctionalType(id)
    for _, modelName in ipairs(DATA_MODELS_NAMES_ARRAY) do
        local DataModel = cacheModel(modelName)
        local validName = DataModel:getName(id)
        if validName then
            return LOOKUP_DATA_MODEL_FUNCTIONAL_TYPES[modelName]
        end
    end
    return nil
end
--endregion


return BuildingDataProxy
return BuildingDataProxy

Latest revision as of 18:57, 10 November 2024

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

---@module BuildingDataProxy
local BuildingDataProxy = {}



--region Dependencies

-- Just store the strings, and we'll load lazily at runtime as needed. The order of this array controls the load order of data models.
local DATA_MODELS_NAMES_ARRAY = {
    "Module:WorkshopsData",
    "Module:InstitutionsData",
    "Module:FarmsData",
    "Module:GatheringData",
    "Module:FishingData",
    "Module:RainCollectorsData",
    "Module:CampsData",
    "Module:HearthsData",
    "Module:WarehousesData",
    "Module:FarmFieldsData",
}

local DataModelsCache = {}
for _, moduleName in pairs(DATA_MODELS_NAMES_ARRAY) do
    DataModelsCache[moduleName] = nil
end

--endregion



--region Private methods

local function cacheModel(modelName)

    if DataModelsCache[modelName] == nil then
        DataModelsCache[modelName] = require(modelName)
    end

    return DataModelsCache[modelName]
end

---fetchFromFunction
---Loops through the data models, calling the provided function on each in turn using the specified parameter (name or ID) until one of them returns a result. Returns nil if no data models found the specified name or ID.
---
---@param functionName string the name of the function to call
---@param parameter1 ?
---@param parameter2 ?
---@param parameter3 ?
---@return ? whatever the function returns
local function federateFunction(functionName, parameter1, parameter2, parameter3)

    for _, modelName in ipairs(DATA_MODELS_NAMES_ARRAY) do
        local DataModel = cacheModel(modelName)
        local result = DataModel[functionName](DataModel, parameter1, parameter2, parameter3)
        -- If we found it, return, otherwise keep going
        if result then
            return result
        end
    end
    -- nil if not found
    return nil
end

--endregion



--region Public building interface

function BuildingDataProxy.getID(displayName)
    return federateFunction("getID", displayName)
end

function BuildingDataProxy.getCategory(id)
    return federateFunction("getCategory", id)
end

function BuildingDataProxy.getSecondCategory(id)
    return federateFunction("getSecondCategory", id)
end

function BuildingDataProxy.getCityScore(id)
    return federateFunction("getCityScore", id)
end

function BuildingDataProxy.getConstructionCosts(id)
    return federateFunction("getConstructionCosts", id)
end

function BuildingDataProxy.getConstructionTime(id)
    return federateFunction("getConstructionTime", id)
end

function BuildingDataProxy.getDescription(id)
    return federateFunction("getDescription", id)
end

function BuildingDataProxy.getIcon(id)
    return federateFunction("getIcon", id)
end

function BuildingDataProxy.isMovable(id)
    return federateFunction("isMovable", id)
end

function BuildingDataProxy.getName(id)
    return federateFunction("getName", id)
end

function BuildingDataProxy.getNumberOfWorkplaces(id)
    return federateFunction("getNumberOfWorkplaces", id)
end

function BuildingDataProxy.getSize(id)
    return federateFunction("getSize", id)
end

function BuildingDataProxy.getStorage(id)
    return federateFunction("getStorage", id)
end

function BuildingDataProxy.isServiceBuilding(id)
    return federateFunction("isServiceBuilding", id)
end

--endregion



return BuildingDataProxy