Module:GladeResourcesController: Difference between revisions

From Against the Storm Official Wiki
first version, still need to refine significantly
 
Adding caption generation; adding comments
Line 65: Line 65:
--region Private methods
--region Private methods


---Goes through the provided list of deposits and finds the maximum number of bonuses, so that the view can be standardized.
---@private
-----Goes through the provided list of deposits and finds the maximum number of bonuses, so that the view can be standardized.
---
---
---@param depositsList table array of deposit records retrieved from GladeResourcesData
---@param depositsList table array of deposit records retrieved from GladeResourcesData
Line 82: Line 83:




local function renderViewStart(depositsList, maxBonuses, displayOverride)
---@private
---Sets up the view, based on the display override or lack thereof. Returns whatever else needs to be appended to do so.
---
---@param depositsList table list of deposits from GladeResourcesData
---@param maxBonuses number slots for bonus resources
---@param caption string caption for the table
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderViewStart(depositsList, maxBonuses, caption, displayOverride)
 
    displayOverride = displayOverride --TODO write the other view modes


     local numDeposits = #depositsList
     local numDeposits = #depositsList
Line 91: Line 102:
             [VIEW_ARG_HEAD] = numDeposits .. " deposits found.",
             [VIEW_ARG_HEAD] = numDeposits .. " deposits found.",
             [VIEW_ARG_DESC] = "Description?",
             [VIEW_ARG_DESC] = "Description?",
             [VIEW_ARG_CAPTION] = "Caption??",
             [VIEW_ARG_CAPTION] = caption,
             [VIEW_ARG_NUM_DEPOSITS] = numDeposits,
             [VIEW_ARG_NUM_DEPOSITS] = numDeposits,
             [VIEW_ARG_MAX_BONUSES] = maxBonuses,
             [VIEW_ARG_MAX_BONUSES] = maxBonuses,
Line 102: Line 113:




local function renderProduct(deposit)
---@private
-----Converts the data about the product in the specified deposit for the view.
---
---@param deposit table from GladeResourceData
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderProduct(deposit, displayOverride)
 
    displayOverride = displayOverride --TODO write the other display modes


     local productID, amount = GladeResourcesData.getDepositPrimaryProduct(deposit)
     local productID, amount = GladeResourcesData.getDepositPrimaryProduct(deposit)
Line 128: Line 147:




local function renderBonus(deposit, index)
---@private
-----Converts the data about the specified bonus in the specified deposit for the view.
---
---@param deposit table from GladeResourceData
---@param index number which bonus from deposit to get
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderBonus(deposit, index, displayOverride)
 
    displayOverride = displayOverride --TODO write the other display modes


     local productID, amount, chance = GladeResourcesData.getDepositBonusResourceAt(deposit, index)
     local productID, amount, chance = GladeResourcesData.getDepositBonusResourceAt(deposit, index)
Line 161: Line 189:




---@private
-----Makes a row for one deposit.
---
---@param deposit table from GladeResourceData
---@param maxBonuses number slots for bonus resources
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderViewOneRow(deposit, maxBonuses, displayOverride)
local function renderViewOneRow(deposit, maxBonuses, displayOverride)
    displayOverride = displayOverride --TODO write the other display modes


     local viewArgs = {
     local viewArgs = {
         [VIEW_ARG_DEPOSIT] = GladeResourcesData.getDepositName(deposit),
         [VIEW_ARG_DEPOSIT] = GladeResourcesData.getDepositName(deposit),
         [VIEW_ARG_CHARGES] = GladeResourcesData.getDepositCharges(deposit),
         [VIEW_ARG_CHARGES] = GladeResourcesData.getDepositCharges(deposit),
         [VIEW_ARG_PRODUCT] = renderProduct(deposit),
         [VIEW_ARG_PRODUCT] = renderProduct(deposit, displayOverride),
         [VIEW_ARG_MAX_BONUSES] = maxBonuses,
         [VIEW_ARG_MAX_BONUSES] = maxBonuses,
         [VIEW_ARG_BONUS_1] = renderBonus(deposit, 1),
         [VIEW_ARG_BONUS_1] = renderBonus(deposit, 1, displayOverride),
         [VIEW_ARG_BONUS_2] = renderBonus(deposit, 2),
         [VIEW_ARG_BONUS_2] = renderBonus(deposit, 2, displayOverride),
         [VIEW_ARG_BONUS_3] = renderBonus(deposit, 3),
         [VIEW_ARG_BONUS_3] = renderBonus(deposit, 3, displayOverride),
         [VIEW_ARG_BONUS_4] = renderBonus(deposit, 4),
         [VIEW_ARG_BONUS_4] = renderBonus(deposit, 4, displayOverride),
     }
     }


Line 182: Line 219:




---@private
---Goes through the list of deposits and makes rows for each.
---
---@param depositsList table list of deposits from GladeResourcesData
---@param maxBonuses number slots for bonus resources
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderViewAllRows(depositsList, maxBonuses, displayOverride)
local function renderViewAllRows(depositsList, maxBonuses, displayOverride)


Line 195: Line 239:




local function renderViewEnd()
---@private
--not using parameters depositsList, displayOverride
---Closes out the view, based on the display override or lack thereof. Returns whatever else needs to be appended to do so.
---
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderViewEnd(displayOverride)
 
    displayOverride = displayOverride --TODO write the other display modes


     local wikiMarkup = ""
     local wikiMarkup = ""
Line 204: Line 254:
     }
     }
     return wikiMarkup
     return wikiMarkup
end
local function writeCaptionForDeposits(depositsList)
    local categories = {}
    for _, deposit in ipairs(depositsList) do
        local category = GladeResourcesData.getDepositCategory(deposit)
        if not categories[category] then
            categories[category] = 1
        else
            categories[category] = categories[category] + 1
        end
    end
    local caption = ""
    for category, categoryCount in pairs(categories) do
        if categoryCount > 1 then
            category = category .. "s"
        end
        if caption == "" then
            caption = category
        else
            caption = caption .. " & " .. category
        end
    end
    return caption
end
end


Line 249: Line 326:


     local maxBonuses = countMaxBonusesInList(depositsList)
     local maxBonuses = countMaxBonusesInList(depositsList)
    local caption = writeCaptionForDeposits(depositsList)


     local wikiMarkup = ""
     local wikiMarkup = ""
     wikiMarkup = wikiMarkup .. renderViewStart(depositsList, maxBonuses, displayOverride)
     wikiMarkup = wikiMarkup .. renderViewStart(depositsList, maxBonuses, caption, displayOverride)
     wikiMarkup = wikiMarkup .. renderViewAllRows(depositsList, maxBonuses, displayOverride)
     wikiMarkup = wikiMarkup .. renderViewAllRows(depositsList, maxBonuses, displayOverride)
     wikiMarkup = wikiMarkup .. renderViewEnd(depositsList, displayOverride)
     wikiMarkup = wikiMarkup .. renderViewEnd(displayOverride)


     return wikiMarkup
     return wikiMarkup

Revision as of 01:42, 13 March 2025

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

---@class GladeResourcesController
local GladeResourcesController = {}



--region Dependencies

local GladeResourcesData = require("Module:GladeResourcesData")
local GoodsData = require("Module:GoodsData")
local DataModelsWithGladeResourcesRecipes = {
    require("Module:MinesData"),
    require("Module:CampsData"),
    require("Module:FarmsData"),
    require("Module:FishingData"),
    require("Module:GatheringData"),
}

local VIEW_TEMPLATE_START = "Deposit/view"
local VIEW_TEMPLATE_ROW = "Deposit/view/row"
local VIEW_TEMPLATE_END = "Deposit/view/end"

local VIEW_BUILDING_LINK = "Building_link/view"
local VIEW_RESOURCE_LINK = "Resource_link/view"

--endregion



--region Private constants

local VIEW_ARG_HEAD = "heading"
local VIEW_ARG_DESC = "description"
local VIEW_ARG_CAPTION = "caption"
local VIEW_ARG_CHARGES = "charges"
local VIEW_ARG_NUM_DEPOSITS = "numdeposits"
local VIEW_ARG_MAX_BONUSES = "maxbonuses"

local VIEW_ARG_DEPOSIT = "deposit"
local VIEW_ARG_PRODUCT = "primary"
local VIEW_ARG_BONUS_1 = "bonus1"
local VIEW_ARG_BONUS_2 = "bonus2"
local VIEW_ARG_BONUS_3 = "bonus3"
local VIEW_ARG_BONUS_4 = "bonus4"

--endregion



--region Localization string constants

local ERROR_MESSAGE_PRODUCT_AND_BONUS_BUT_NOT_MATCH = "When specifying both a product and bonus resource, they must match."

--endregion



--region Private member variables

local frame

--endregion



--region Private methods

---@private
-----Goes through the provided list of deposits and finds the maximum number of bonuses, so that the view can be standardized.
---
---@param depositsList table array of deposit records retrieved from GladeResourcesData
---@return number maximum bonuses across all in the list
local function countMaxBonusesInList(depositsList)
    local maxFound = 0
    for _, deposit in ipairs(depositsList) do
        local numBonuses = GladeResourcesData.getDepositNumBonusResources(deposit)
        if numBonuses > maxFound then
            maxFound = numBonuses
        end
    end
    return maxFound
end



---@private
---Sets up the view, based on the display override or lack thereof. Returns whatever else needs to be appended to do so.
---
---@param depositsList table list of deposits from GladeResourcesData
---@param maxBonuses number slots for bonus resources
---@param caption string caption for the table
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderViewStart(depositsList, maxBonuses, caption, displayOverride)

    displayOverride = displayOverride --TODO write the other view modes

    local numDeposits = #depositsList

    local wikiMarkup = frame:expandTemplate{
        title = VIEW_TEMPLATE_START,
        args = {
            [VIEW_ARG_HEAD] = numDeposits .. " deposits found.",
            [VIEW_ARG_DESC] = "Description?",
            [VIEW_ARG_CAPTION] = caption,
            [VIEW_ARG_NUM_DEPOSITS] = numDeposits,
            [VIEW_ARG_MAX_BONUSES] = maxBonuses,
        },
    }

    return wikiMarkup
end



---@private
-----Converts the data about the product in the specified deposit for the view.
---
---@param deposit table from GladeResourceData
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderProduct(deposit, displayOverride)

    displayOverride = displayOverride --TODO write the other display modes

    local productID, amount = GladeResourcesData.getDepositPrimaryProduct(deposit)
    local iconFilename = GoodsData.getIcon(productID)
    local productName = GoodsData.getName(productID)

    local wikiMarkup = ""
    if amount > 1 then
        wikiMarkup = wikiMarkup .. "''" .. amount .. "'' "
    end

    wikiMarkup = wikiMarkup .. frame:expandTemplate{
        title = VIEW_RESOURCE_LINK,
        args = {
            ["iconsize"] = "medium",
            ["iconfilename"] = iconFilename,
            ["name"] = productName,
            ["display"] = "notext",
        }
    }

    return wikiMarkup
end



---@private
-----Converts the data about the specified bonus in the specified deposit for the view.
---
---@param deposit table from GladeResourceData
---@param index number which bonus from deposit to get
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderBonus(deposit, index, displayOverride)

    displayOverride = displayOverride --TODO write the other display modes

    local productID, amount, chance = GladeResourcesData.getDepositBonusResourceAt(deposit, index)

    if not productID then
        return ""
    end

    local iconFilename = GoodsData.getIcon(productID)
    local productName = GoodsData.getName(productID)

    local wikiMarkup = ""
    if amount and amount > 1 then
        wikiMarkup = wikiMarkup .. "'''" .. amount .. "''' "
    end

    wikiMarkup = wikiMarkup .. frame:expandTemplate{
        title = VIEW_RESOURCE_LINK,
        args = {
            ["iconsize"] = "medium",
            ["iconfilename"] = iconFilename,
            ["name"] = productName,
            ["display"] = "notext",
        }
    }

    wikiMarkup = wikiMarkup .. "'''" .. chance*100 .. "%'''"

    return wikiMarkup
end



---@private
-----Makes a row for one deposit.
---
---@param deposit table from GladeResourceData
---@param maxBonuses number slots for bonus resources
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderViewOneRow(deposit, maxBonuses, displayOverride)

    displayOverride = displayOverride --TODO write the other display modes

    local viewArgs = {
        [VIEW_ARG_DEPOSIT] = GladeResourcesData.getDepositName(deposit),
        [VIEW_ARG_CHARGES] = GladeResourcesData.getDepositCharges(deposit),
        [VIEW_ARG_PRODUCT] = renderProduct(deposit, displayOverride),
        [VIEW_ARG_MAX_BONUSES] = maxBonuses,
        [VIEW_ARG_BONUS_1] = renderBonus(deposit, 1, displayOverride),
        [VIEW_ARG_BONUS_2] = renderBonus(deposit, 2, displayOverride),
        [VIEW_ARG_BONUS_3] = renderBonus(deposit, 3, displayOverride),
        [VIEW_ARG_BONUS_4] = renderBonus(deposit, 4, displayOverride),
    }

    return frame:expandTemplate{
        title = VIEW_TEMPLATE_ROW,
        args = viewArgs,
    }
end



---@private
---Goes through the list of deposits and makes rows for each.
---
---@param depositsList table list of deposits from GladeResourcesData
---@param maxBonuses number slots for bonus resources
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderViewAllRows(depositsList, maxBonuses, displayOverride)

    local wikiMarkup = ""
    for _, deposit in ipairs(depositsList) do
        wikiMarkup = wikiMarkup .. "\n"
        wikiMarkup = wikiMarkup .. renderViewOneRow(deposit, maxBonuses, displayOverride)
    end

    return wikiMarkup
end



---@private
---Closes out the view, based on the display override or lack thereof. Returns whatever else needs to be appended to do so.
---
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderViewEnd(displayOverride)

    displayOverride = displayOverride --TODO write the other display modes

    local wikiMarkup = ""
    wikiMarkup = wikiMarkup .. frame:expandTemplate{
        title = VIEW_TEMPLATE_END,
        args = {},
    }
    return wikiMarkup
end

local function writeCaptionForDeposits(depositsList)

    local categories = {}
    for _, deposit in ipairs(depositsList) do
        local category = GladeResourcesData.getDepositCategory(deposit)
        if not categories[category] then
            categories[category] = 1
        else
            categories[category] = categories[category] + 1
        end
    end

    local caption = ""
    for category, categoryCount in pairs(categories) do
        if categoryCount > 1 then
            category = category .. "s"
        end
        if caption == "" then
            caption = category
        else
            caption = caption .. " & " .. category
        end
    end

    return caption
end

--endregion



--region Public methods

---Called from Template:Deposit. Returns markup text for display by using external view templates.
---
---@param mwFrame table the Mediawiki calling context for the template
---@return string wiki markup
function GladeResourcesController.main(mwFrame)

    frame = mwFrame
    local requiredProduct = frame.args.product
    local requiredBonus = frame.args.bonus
    local requiredBuilding = frame.args.building
    local displayOverride = frame.args.display

    --Unset blanks back to nil
    if requiredProduct == "" then
        requiredProduct = nil
    end
    if requiredBonus == "" then
        requiredBonus = nil
    end
    if requiredBuilding == "" then
        requiredBuilding = nil
    end

    local depositsList = {}
    if requiredProduct and requiredBonus then
        if requiredProduct ~= requiredBonus then
            return ERROR_MESSAGE_PRODUCT_AND_BONUS_BUT_NOT_MATCH
        else
            depositsList = GladeResourcesData.getAllDepositsWherePrimaryAndBonusResource(requiredProduct)
        end
    elseif requiredProduct then
        depositsList = GladeResourcesData.getAllDepositsWherePrimaryResource(requiredProduct)
    elseif requiredBonus then
        depositsList = GladeResourcesData.getAllDepositsWhereBonusResource(requiredBonus)
    end

    local maxBonuses = countMaxBonusesInList(depositsList)
    local caption = writeCaptionForDeposits(depositsList)

    local wikiMarkup = ""
    wikiMarkup = wikiMarkup .. renderViewStart(depositsList, maxBonuses, caption, displayOverride)
    wikiMarkup = wikiMarkup .. renderViewAllRows(depositsList, maxBonuses, displayOverride)
    wikiMarkup = wikiMarkup .. renderViewEnd(displayOverride)

    return wikiMarkup
end

--endregion



return GladeResourcesController