Module:CampsData: Difference between revisions
From Against the Storm Official Wiki
(Now an instance override of BaseDataModel; functional change) |
m (forgot self parameter on overridden functions) |
||
Line 119: | Line 119: | ||
---Gets the efficiency grade from the provided recipe. | ---Gets the efficiency grade from the provided recipe. | ||
---@param self BaseDataModel this instance, with specific data | |||
---@param _ recipePairTable, ignored | ---@param _ recipePairTable, ignored | ||
---@return number efficiency grade | ---@return number efficiency grade | ||
function getRecipeGrade(_) | function getRecipeGrade(self, _) | ||
return 0 | return 0 | ||
end | end | ||
Line 129: | Line 130: | ||
---Gets the product ID from the provided recipe. | ---Gets the product ID from the provided recipe. | ||
--- | --- | ||
---@param self BaseDataModel this instance, with specific data | |||
---@param recipeData recipePairTable | ---@param recipeData recipePairTable | ||
---@return string product ID | ---@return string product ID | ||
function getRecipeProductID(recipeData) | function getRecipeProductID(self, recipeData) | ||
return recipeData.recipe[self.schema.RECIPE.PRODUCT.ID] | return recipeData.recipe[self.schema.RECIPE.PRODUCT.ID] | ||
end | end | ||
Line 140: | Line 142: | ||
---Gets the amount of product produced by the provided recipe. | ---Gets the amount of product produced by the provided recipe. | ||
---@param self BaseDataModel this instance, with specific data | |||
---@param _ recipePairTable, ignored | ---@param _ recipePairTable, ignored | ||
---@return number stack size | ---@return number stack size | ||
function getRecipeProductAmount(_) | function getRecipeProductAmount(self, _) | ||
return 1 | return 1 | ||
end | end |
Revision as of 02:23, 7 November 2024
Documentation for this module may be created at Module:CampsData/doc
---@module CampsData.lua --- ---This module does not define an _actual_ derived class, but it creates an instance of one via prototyping patterns. Understand this file as a big procedure, top-to-bottom, rather than a class definition. --- ---Initializes a BaseDataModel with the data file associated with this module. Makes necessary modifications to the basic data, including schema and method overrides, to permit the exceptions associated with data in this model. --region Localization string constants local ERROR_MESSAGE_INSTANCE_NOT_INITIALIZED = "This instance's data table was never initialized. Please check how and where this method was called to diagnose the problem" --endregion --Create instance local BaseDataModel = require("Module:BaseDataModel") local DATA_FILE = "Module:CampsData/Camps.json" ---@type BaseDataModel local campsDataModel = BaseDataModel.new(DATA_FILE) --Begin instance overrides for _, building in pairs(campsDataModel.dataTable) do building[campsDataModel.schema.CATEGORY2] = "Gathering Camp" end campsDataModel.schema.RECIPE.PRODUCTION_TIME = "gatheringTime" --IMPORTANT: The data structure has one fewer levels, like RECIPE > PRODUCT_ID campsDataModel.schema.RECIPE.PRODUCT.ID = "seekedResource" --/** Function definitions follow. **/ --Collapse the regions in the IDE to see the procedure pick up with the assignments of these functions to override the associated member methods of BaseDataModel. --region Public building interface --no overrides! --endregion --region Public building recipe query interface local INDEX_PAIR_BUILDING_ID = "buildingID" local INDEX_PAIR_RECIPE = "recipe" ---@public ---`Overrides BaseDataModel` --- ---Gets all this instance's buildings' recipes that have the specified product. Returns an array of pairs of building IDs and recipe data. --- ---@param self BaseDataModel this instance, with specific data ---@param productID string the product ---@return table array of pairs of building IDs and recipe data, or {} if none found function getIDsAndRecipesWhereProductID(self, productID) if not self.dataTable then error(ERROR_MESSAGE_INSTANCE_NOT_INITIALIZED) end local ret = {} for id, building in pairs(self.dataTable) do for _, recipe in ipairs(building[self.schema.RECIPES]) do --If there's a matching seeked deposit if recipe[self.schema.RECIPE.PRODUCT.ID] == productID then -- Add as an buildingID-recipe pair table.insert(ret, { [INDEX_PAIR_BUILDING_ID] = id, [INDEX_PAIR_RECIPE] = recipe, }) end end end return ret end ---@public ---`Overrides BaseDataModel` --- ---Gets all this instance's specified building's recipes that produce the specified product. Returns an array of pairs of building IDs and recipe data. --- ---@param self BaseDataModel this instance, with specific data ---@param productID string the product ---@param buildingID string the building ---@return table array of pairs of building IDs and recipe data, or {} if none found function getIDsAndRecipesWhereProductIDAndBuildingID(self, productID, buildingID) --Can't findID from here, but that's okay, this is a small data file: just loop. local ret = {} for id, building in pairs(self.dataTable) do if building[self.schema.ID] == buildingID then for _, recipe in ipairs(building[self.schema.RECIPES]) do --If there's a matching seeked deposit if recipe[self.schema.RECIPE.PRODUCT.ID] == productID then -- Add as an buildingID-recipe pair table.insert(ret, { [INDEX_PAIR_BUILDING_ID] = id, [INDEX_PAIR_RECIPE] = recipe, }) end end end end return ret end --endregion --region Public recipe data retrieval interface ---@public ---`Overrides BaseDataModel` --- ---Gets the efficiency grade from the provided recipe. ---@param self BaseDataModel this instance, with specific data ---@param _ recipePairTable, ignored ---@return number efficiency grade function getRecipeGrade(self, _) return 0 end ---@public ---`Overrides BaseDataModel` ---Gets the product ID from the provided recipe. --- ---@param self BaseDataModel this instance, with specific data ---@param recipeData recipePairTable ---@return string product ID function getRecipeProductID(self, recipeData) return recipeData.recipe[self.schema.RECIPE.PRODUCT.ID] end ---@public ---`Overrides BaseDataModel` --- ---Gets the amount of product produced by the provided recipe. ---@param self BaseDataModel this instance, with specific data ---@param _ recipePairTable, ignored ---@return number stack size function getRecipeProductAmount(self, _) return 1 end --endregion --/** End Function definitions. Procedure resumes. **/ --Override building recipe query interface campsDataModel.getIDsAndRecipesWhereProductID = getIDsAndRecipesWhereProductID campsDataModel.getIDsAndRecipesWhereProductIDAndBuildingID = getIDsAndRecipesWhereProductIDAndBuildingID --Override recipe data retrieval interface campsDataModel.getRecipeGrade = getRecipeGrade campsDataModel.getRecipeProductID = getRecipeProductID campsDataModel.getRecipeProductAmount = getRecipeProductAmount return campsDataModel