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: | "Module:WorkshopsData", | ||
"Module: | "Module:InstitutionsData", | ||
"Module: | "Module:FarmsData", | ||
"Module:GatheringData", | "Module:GatheringData", | ||
"Module:FishingData", | "Module:FishingData", | ||
"Module:RainCollectorsData", | "Module:RainCollectorsData", | ||
"Module: | "Module:CampsData", | ||
"Module:HearthsData", | "Module:HearthsData", | ||
"Module:WarehousesData", | "Module:WarehousesData", | ||
Line 24: | Line 24: | ||
DataModelsCache[moduleName] = nil | DataModelsCache[moduleName] = nil | ||
end | end | ||
--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 | ---@param parameter1 ? | ||
---@param parameter2 ? | |||
---@param parameter3 ? | |||
---@return ? whatever the function returns | ---@return ? whatever the function returns | ||
local function federateFunction(functionName, | 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, | 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 | ||
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: | ||
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