Module:GladeResourcesController: Difference between revisions
From Against the Storm Official Wiki
Adding caption generation; adding comments |
Now does title, description, and caption |
||
| Line 49: | Line 49: | ||
--region Localization string constants | --region Localization string constants | ||
local ERROR_MESSAGE_PRODUCT_AND_BONUS_BUT_NOT_MATCH = "When | local ERROR_MESSAGE_PRODUCT_AND_BONUS_BUT_NOT_MATCH = "When searching for a good as both a main and bonus resource, the parameters must match." | ||
--endregion | --endregion | ||
| Line 66: | Line 66: | ||
---@private | ---@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 79: | Line 79: | ||
end | end | ||
return maxFound | return maxFound | ||
end | |||
---@private | |||
---Tallies up the categories and concatenates them. Also makes them plural if there are more than one of a particular category. | |||
--- | |||
---@param depositsList table | |||
---@return string the categories among the deposits | |||
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 | |||
---@private | |||
---Tallies up the descriptions. If there's exactly one, it returns that description, otherwise it returns blank since they can't be combined (and to save space). | |||
--- | |||
---@param depositsList table | |||
---@return string the one or shared description in the list, or "" if they are varied | |||
local function writeDescriptionForDeposits(depositsList) | |||
--Heuristic: If there are more then 2, then there's no way all the descriptions match. | |||
if #depositsList > 2 then | |||
return "" | |||
end | |||
local descriptions = {} | |||
for _, deposit in ipairs(depositsList) do | |||
local desc = GladeResourcesData.getDepositDescription(deposit) | |||
descriptions[desc] = true | |||
end | |||
if #descriptions > 1 then | |||
return "" | |||
else | |||
local key, value = next(descriptions) | |||
return key | |||
end | |||
end | end | ||
| Line 88: | Line 150: | ||
---@param depositsList table list of deposits from GladeResourcesData | ---@param depositsList table list of deposits from GladeResourcesData | ||
---@param maxBonuses number slots for bonus resources | ---@param maxBonuses number slots for bonus resources | ||
---@param | ---@param title string title for the whole panel | ||
---@param displayOverride string override code, if any | ---@param displayOverride string override code, if any | ||
---@return string wiki markup | ---@return string wiki markup | ||
local function renderViewStart(depositsList, maxBonuses, | local function renderViewStart(depositsList, maxBonuses, title, displayOverride) | ||
displayOverride = displayOverride --TODO write the other view modes | displayOverride = displayOverride --TODO write the other view modes | ||
local description = writeDescriptionForDeposits(depositsList) | |||
local caption = writeCaptionForDeposits(depositsList) | |||
local numDeposits = #depositsList | local numDeposits = #depositsList | ||
| Line 100: | Line 164: | ||
title = VIEW_TEMPLATE_START, | title = VIEW_TEMPLATE_START, | ||
args = { | args = { | ||
[VIEW_ARG_HEAD] = | [VIEW_ARG_HEAD] = title, | ||
[VIEW_ARG_DESC] = | [VIEW_ARG_DESC] = description, | ||
[VIEW_ARG_CAPTION] = caption, | [VIEW_ARG_CAPTION] = caption, | ||
[VIEW_ARG_NUM_DEPOSITS] = numDeposits, | [VIEW_ARG_NUM_DEPOSITS] = numDeposits, | ||
| Line 254: | Line 318: | ||
} | } | ||
return wikiMarkup | return wikiMarkup | ||
end | end | ||
| Line 296: | Line 333: | ||
frame = mwFrame | frame = mwFrame | ||
local requiredProduct = frame.args.product | local requiredProduct = frame.args.product --TODO these are currently IDs, they need to be names | ||
local requiredBonus = frame.args.bonus | local requiredBonus = frame.args.bonus | ||
local requiredBuilding = frame.args.building | local requiredBuilding = frame.args.building | ||
| Line 313: | Line 350: | ||
local depositsList = {} | local depositsList = {} | ||
local title = "" | |||
if requiredProduct and requiredBonus then | if requiredProduct and requiredBonus then | ||
if requiredProduct ~= requiredBonus then | if requiredProduct ~= requiredBonus then | ||
return ERROR_MESSAGE_PRODUCT_AND_BONUS_BUT_NOT_MATCH | return ERROR_MESSAGE_PRODUCT_AND_BONUS_BUT_NOT_MATCH | ||
else | else | ||
depositsList = GladeResourcesData. | depositsList = GladeResourcesData.getAllDepositsWherePrimaryOrBonusResource(requiredProduct) | ||
end | end | ||
title = #depositsList .. " Deposits for " .. requiredProduct .. " or as a bonus." | |||
elseif requiredProduct then | elseif requiredProduct then | ||
depositsList = GladeResourcesData.getAllDepositsWherePrimaryResource(requiredProduct) | depositsList = GladeResourcesData.getAllDepositsWherePrimaryResource(requiredProduct) | ||
title = #depositsList .. " Deposits for " .. requiredProduct .. "." | |||
elseif requiredBonus then | elseif requiredBonus then | ||
depositsList = GladeResourcesData.getAllDepositsWhereBonusResource(requiredBonus) | depositsList = GladeResourcesData.getAllDepositsWhereBonusResource(requiredBonus) | ||
title = #depositsList .. " Deposits where " .. requiredBonus .. " is a bonus." | |||
end | end | ||
local maxBonuses = countMaxBonusesInList(depositsList) | local maxBonuses = countMaxBonusesInList(depositsList) | ||
local wikiMarkup = "" | local wikiMarkup = "" | ||
wikiMarkup = wikiMarkup .. renderViewStart(depositsList, maxBonuses, | wikiMarkup = wikiMarkup .. renderViewStart(depositsList, maxBonuses, title, displayOverride) | ||
wikiMarkup = wikiMarkup .. renderViewAllRows(depositsList, maxBonuses, displayOverride) | wikiMarkup = wikiMarkup .. renderViewAllRows(depositsList, maxBonuses, displayOverride) | ||
wikiMarkup = wikiMarkup .. renderViewEnd(displayOverride) | wikiMarkup = wikiMarkup .. renderViewEnd(displayOverride) | ||
Revision as of 02:29, 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 searching for a good as both a main and bonus resource, the parameters 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
---Tallies up the categories and concatenates them. Also makes them plural if there are more than one of a particular category.
---
---@param depositsList table
---@return string the categories among the deposits
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
---@private
---Tallies up the descriptions. If there's exactly one, it returns that description, otherwise it returns blank since they can't be combined (and to save space).
---
---@param depositsList table
---@return string the one or shared description in the list, or "" if they are varied
local function writeDescriptionForDeposits(depositsList)
--Heuristic: If there are more then 2, then there's no way all the descriptions match.
if #depositsList > 2 then
return ""
end
local descriptions = {}
for _, deposit in ipairs(depositsList) do
local desc = GladeResourcesData.getDepositDescription(deposit)
descriptions[desc] = true
end
if #descriptions > 1 then
return ""
else
local key, value = next(descriptions)
return key
end
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 title string title for the whole panel
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderViewStart(depositsList, maxBonuses, title, displayOverride)
displayOverride = displayOverride --TODO write the other view modes
local description = writeDescriptionForDeposits(depositsList)
local caption = writeCaptionForDeposits(depositsList)
local numDeposits = #depositsList
local wikiMarkup = frame:expandTemplate{
title = VIEW_TEMPLATE_START,
args = {
[VIEW_ARG_HEAD] = title,
[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
--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 --TODO these are currently IDs, they need to be names
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 = {}
local title = ""
if requiredProduct and requiredBonus then
if requiredProduct ~= requiredBonus then
return ERROR_MESSAGE_PRODUCT_AND_BONUS_BUT_NOT_MATCH
else
depositsList = GladeResourcesData.getAllDepositsWherePrimaryOrBonusResource(requiredProduct)
end
title = #depositsList .. " Deposits for " .. requiredProduct .. " or as a bonus."
elseif requiredProduct then
depositsList = GladeResourcesData.getAllDepositsWherePrimaryResource(requiredProduct)
title = #depositsList .. " Deposits for " .. requiredProduct .. "."
elseif requiredBonus then
depositsList = GladeResourcesData.getAllDepositsWhereBonusResource(requiredBonus)
title = #depositsList .. " Deposits where " .. requiredBonus .. " is a bonus."
end
local maxBonuses = countMaxBonusesInList(depositsList)
local wikiMarkup = ""
wikiMarkup = wikiMarkup .. renderViewStart(depositsList, maxBonuses, title, displayOverride)
wikiMarkup = wikiMarkup .. renderViewAllRows(depositsList, maxBonuses, displayOverride)
wikiMarkup = wikiMarkup .. renderViewEnd(displayOverride)
return wikiMarkup
end
--endregion
return GladeResourcesController


