Module:FishingData

From Against the Storm Official Wiki

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

---@module FishingData.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:FishingData/FishingHuts.json"

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



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

fishingDataModel.schema.RECIPE.PRODUCTION_TIME = "gatheringTime"

--IMPORTANT: The data structure has one fewer levels, like RECIPE > PRODUCT_ID
fishingDataModel.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
fishingDataModel.getIDsAndRecipesWhereProductID = getIDsAndRecipesWhereProductID
fishingDataModel.getIDsAndRecipesWhereProductIDAndBuildingID = getIDsAndRecipesWhereProductIDAndBuildingID

--Override recipe data retrieval interface
fishingDataModel.getRecipeProductAmount = getRecipeProductAmount



return fishingDataModel