Module:BuildingDataProxy: Difference between revisions
From Against the Storm Official Wiki
Creating to lazily load and federate queries out to building data models |
Anteprefix (talk | contribs) No edit summary Tag: Manual revert |
||
| (8 intermediate revisions by 2 users 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", | ||
"Module:FarmFieldsData", | "Module:FarmFieldsData", | ||
"Module:ExtractorsData", | |||
"Module:MinesData", | |||
"Module:BlightPostsData" | |||
} | } | ||
| Line 24: | Line 27: | ||
DataModelsCache[moduleName] = nil | DataModelsCache[moduleName] = nil | ||
end | end | ||
--endregion | --endregion | ||
| Line 69: | Line 47: | ||
--- | --- | ||
---@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 70: | ||
--region Public building interface | --region Public building interface | ||
function BuildingDataProxy.getID(displayName) | function BuildingDataProxy.getID(displayName) | ||
| Line 110: | Line 77: | ||
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 152: | Line 123: | ||
end | end | ||
function BuildingDataProxy.isServiceBuilding(id) | |||
return federateFunction("isServiceBuilding", id) | |||
function BuildingDataProxy. | |||
end | end | ||
Latest revision as of 02:36, 5 August 2025
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",
"Module:ExtractorsData",
"Module:MinesData",
"Module:BlightPostsData"
}
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


