Module:GladeResourcesData

From Against the Storm Official Wiki
Revision as of 20:21, 8 March 2025 by Aeredor (talk | contribs) (Created to start serving data for glade resources (aka deposits aka nodes))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

---Sample output of the first item from the JSON table:
---table#1 {
---    ["charges"] = 20,
---    ["description"] = "A very common plant, it thrives thanks to the magical rain. Requires a gathering camp with a <sprite name=grade1> recipe or better.",
---    ["displayName"] = "Reed Field (Small)",
---    ["extraProduction"] = table#2 {
---        table#3 {
---            ["amount"] = 1,
---            ["chance"] = 0.2,
---            ["name"] = "[Food Raw] Roots",
---        },
---        table#4 {
---            ["amount"] = 1,
---            ["chance"] = 0.2,
---            ["name"] = "[Mat Raw] Clay",
---        },
---    },
---    ["iconName"] = "Icon_Resource_Reeds",
---    ["id"] = "Moor Node Reed Deposit - Small",
---    ["label"] = "Gathering Node",
---    ["minGradeToCollect"] = "1",
---    ["production"] = table#5 {
---        ["amount"] = 1,
---        ["name"] = "[Mat Raw] Reeds",
---    },
---    ["type"] = 0,
---    ["xSize"] = 1,
---    ["ySize"] = 1,
---}
---
---@class GladeResourcesData
local GladeResourcesData = {}



--region Dependencies

local JsonUtils = require("Module:JsonUtils")

--endregion



--region Private constants

local DATA_FILE = "Module:GladeResourcesData/Glade_Resources.json"

local SCHEMA = {
    CHARGES = "charges",
    DESCRIPTION = "description",
    NAME = "displayName",

    SUBTABLE_BONUSES = "extraProduction",
        BONUS_AMOUNT = "amount",
        BONUS_CHANCE = "chance",
        BONUS_GOOD_ID = "name",

    ICON = "iconName",
    NODE_ID = "id",
    CATEGORY = "label",
    GRADE_REQUIRED = "minGradeToCollect",

    SUBTABLE_RECIPE = "production",
        RECIPE_AMOUNT = "amount",
        RECIPE_GOOD_ID = "name",

    TYPE_CODE = "type",
    SIZE_X = "xSize",
    SIZE_Y = "ySize",
}

--endregion



--region Private member variables

local gladeResourcesData

--I did some time trials with first populating a lookup map for main-resource IDs to all the entries in the JSON data where that resource was the main product. It took about 3 times as long to create the lookup map before return my selection vs. just looping through and returning my selection directly:
--
--For 10,000 trials
--When creating a lookup map first:
--  Total time: 1.1892
--  Method average time: 0.00011892
--When searching directly--
--  Total time: 0.46102
--  Method average time: 4.6102e-5
--
--I don't believe that most use cases & page views of this module will be calling this module more than 3 times for more than 3 different main resources. This means that leveraging the caching of the module with this lookup map will NOT pay off, but will slow down the majority of page loads that use this module unnecessarily.
--I believe most use cases & page loads will call this module twice: (1) to get the deposits where there's a specified main resource and (2) to get the deposits where there's a specified bonus resource. That would require separate lookup maps for each search, and most use cases would only need the lookup map once.
--Therefore, I have decided not to use lookup maps for this module.

--endregion



--region Private methods

local function loadData()

    if not gladeResourcesData then
        gladeResourcesData = JsonUtils.convertJSONToLuaTable(DATA_FILE)
    end

    return gladeResourcesData
end

local function isDepositPrimaryProductAtIndex(goodID, index)
    return goodID == gladeResourcesData[index][SCHEMA.SUBTABLE_RECIPE][SCHEMA.RECIPE_GOOD_ID]
end

local function isDepositBonusProductAtIndex(goodID, index)
    for _, bonus in ipairs(gladeResourcesData[index][SCHEMA.SUBTABLE_BONUSES]) do
        if goodID == bonus[SCHEMA.BONUS_GOOD_ID] then
            return true
        end
    end
    return false
end

--endregion



--region Public methods

function GladeResourcesData.getDepositsWithPrimaryResource(goodID)

    loadData()

    local matchingDeposits = {}
    for i = 1,#gladeResourcesData do
        if isDepositPrimaryProductAtIndex(goodID, i) then
            table.insert(matchingDeposits, gladeResourcesData[i])
        end
    end

    return matchingDeposits
end

function GladeResourcesData.getDepositsWithBonusResource(goodID)

    loadData()

    local matchingDeposits = {}
    for i = 1,#gladeResourcesData do
        if isDepositBonusProductAtIndex(goodID, i) then
            table.insert(matchingDeposits, gladeResourcesData[i])
        end
    end

    return matchingDeposits
end

function GladeResourcesData.getDepositsWithPrimaryAndBonusResource(goodID)

    loadData()

    local matchingDeposits = {}
    for i = 1,#gladeResourcesData do
        if isDepositPrimaryProductAtIndex(goodID, i) and isDepositBonusProductAtIndex(goodID, i) then
            table.insert(matchingDeposits, gladeResourcesData[i])
        end
    end

    return matchingDeposits
end

--endregion



return GladeResourcesData