Module:GatheringData: Difference between revisions

From Against the Storm Official Wiki
(Creating new data model for gathering huts (now separate from woodcutter camp))
 
(Now an instance override of BaseDataModel; functional change)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
---@module GatheringData
---@module GatheringData.lua
local GatheringData = {}
---
---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 BaseDataModel = require("Module:BaseDataModel")
local DATA_FILE = "Module:GatheringData/GatheringHuts.json"
local DATA_FILE = "Module:GatheringData/GathererHuts.json"
 
---@type BaseDataModel
local gatheringDataModel = BaseDataModel.new(DATA_FILE)
 
 
 
--Begin instance overrides
for _, building in pairs(gatheringDataModel.dataTable) do
    building[gatheringDataModel.schema.CATEGORY2] = "Gathering Camp"
    building[gatheringDataModel.schema.IS_PROVIDING_SERVICES] = false
end
 
gatheringDataModel.schema.RECIPE.PRODUCTION_TIME = "gatheringTime"
 
--IMPORTANT: The data structure has one fewer levels, like RECIPE > PRODUCT_ID
gatheringDataModel.schema.RECIPE.PRODUCT.ID = "seekedDeposits"
 
 
 
--/** 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 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.
---
---Parameters
---
---`self:BaseDataModel` - ignored
---
---`recipeData:recipePairTable` - ignored
---
---@return number stack size
function getRecipeProductAmount()
    return 1
end
 
--endregion
--/** End Function definitions. Procedure resumes. **/
 
 
 
--Override building recipe query interface
gatheringDataModel.getIDsAndRecipesWhereProductID = getIDsAndRecipesWhereProductID
gatheringDataModel.getIDsAndRecipesWhereProductIDAndBuildingID = getIDsAndRecipesWhereProductIDAndBuildingID
 
--Override recipe data retrieval interface
gatheringDataModel.getRecipeProductID = getRecipeProductID
gatheringDataModel.getRecipeProductAmount = getRecipeProductAmount




-- Benchmarking: ~0.0000 seconds
local gatheringDataModel = BaseDataModel.new(DATA_FILE)


return gatheringDataModel
return gatheringDataModel

Latest revision as of 19:38, 7 November 2024

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

---@module GatheringData.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:GatheringData/GathererHuts.json"

---@type BaseDataModel
local gatheringDataModel = BaseDataModel.new(DATA_FILE)



--Begin instance overrides
for _, building in pairs(gatheringDataModel.dataTable) do
    building[gatheringDataModel.schema.CATEGORY2] = "Gathering Camp"
    building[gatheringDataModel.schema.IS_PROVIDING_SERVICES] = false
end

gatheringDataModel.schema.RECIPE.PRODUCTION_TIME = "gatheringTime"

--IMPORTANT: The data structure has one fewer levels, like RECIPE > PRODUCT_ID
gatheringDataModel.schema.RECIPE.PRODUCT.ID = "seekedDeposits"



--/** 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 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.
---
---Parameters
---
---`self:BaseDataModel` - ignored
---
---`recipeData:recipePairTable` - ignored
---
---@return number stack size
function getRecipeProductAmount()
    return 1
end

--endregion
--/** End Function definitions. Procedure resumes. **/



--Override building recipe query interface
gatheringDataModel.getIDsAndRecipesWhereProductID = getIDsAndRecipesWhereProductID
gatheringDataModel.getIDsAndRecipesWhereProductIDAndBuildingID = getIDsAndRecipesWhereProductIDAndBuildingID

--Override recipe data retrieval interface
gatheringDataModel.getRecipeProductID = getRecipeProductID
gatheringDataModel.getRecipeProductAmount = getRecipeProductAmount



return gatheringDataModel