Module:GladeResourcesController: Difference between revisions
From Against the Storm Official Wiki
Now does buildings! and good names instead of IDs |
Now special case of one deposit displays a little differently |
||
| Line 196: | Line 196: | ||
local caption = writeCaptionForDeposits(depositsList) | local caption = writeCaptionForDeposits(depositsList) | ||
local numDeposits = #depositsList | local numDeposits = #depositsList | ||
--Special case where we swap the title out for the name of the one deposit | |||
if numDeposits == 1 then | |||
title = GladeResourcesData.getDepositName(depositsList[1]) | |||
end | |||
local wikiMarkup = frame:expandTemplate{ | local wikiMarkup = frame:expandTemplate{ | ||
| Line 293: | Line 298: | ||
--- | --- | ||
---@param deposit table from GladeResourceData | ---@param deposit table from GladeResourceData | ||
---@param numDeposits number total number of deposits | |||
---@param maxBonuses number slots for bonus resources | ---@param maxBonuses number slots for bonus resources | ||
---@param displayOverride string override code, if any | ---@param displayOverride string override code, if any | ||
---@return string wiki markup | ---@return string wiki markup | ||
local function renderViewOneRow(deposit, maxBonuses, displayOverride) | local function renderViewOneRow(deposit, numDeposits, maxBonuses, displayOverride) | ||
displayOverride = displayOverride --TODO write the other display modes | displayOverride = displayOverride --TODO write the other display modes | ||
--Special case where we take out the name when there's just one deposit. | |||
local depositName = "" | |||
if numDeposits > 1 then | |||
depositName = GladeResourcesData.getDepositName(deposit) | |||
end | |||
local viewArgs = { | local viewArgs = { | ||
[VIEW_ARG_DEPOSIT] = | [VIEW_ARG_DEPOSIT] = depositName, | ||
[VIEW_ARG_CHARGES] = GladeResourcesData.getDepositCharges(deposit), | [VIEW_ARG_CHARGES] = GladeResourcesData.getDepositCharges(deposit), | ||
[VIEW_ARG_PRODUCT] = renderProduct(deposit, displayOverride), | [VIEW_ARG_PRODUCT] = renderProduct(deposit, displayOverride), | ||
| Line 327: | Line 339: | ||
---@return string wiki markup | ---@return string wiki markup | ||
local function renderViewAllRows(depositsList, maxBonuses, displayOverride) | local function renderViewAllRows(depositsList, maxBonuses, displayOverride) | ||
local numDeposits = #depositsList | |||
local wikiMarkup = "" | local wikiMarkup = "" | ||
for _, deposit in ipairs(depositsList) do | for _, deposit in ipairs(depositsList) do | ||
wikiMarkup = wikiMarkup .. "\n" | wikiMarkup = wikiMarkup .. "\n" | ||
wikiMarkup = wikiMarkup .. renderViewOneRow(deposit, maxBonuses, displayOverride) | wikiMarkup = wikiMarkup .. renderViewOneRow(deposit, numDeposits, maxBonuses, displayOverride) | ||
end | end | ||
Revision as of 03:58, 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 BuildingDataProxy = require("Module:BuildingDataProxy")
local DataModelsWithGladeResourcesRecipes = {
require("Module:MinesData"),
require("Module:CampsData"),
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."
local ERROR_MESSAGE_NO_PARAMETERS_GIVEN = "You must specify the name of a product, a bonus resource, or a building—and as it's spelled in the game."
--endregion
--region Private member variables
local frame
--endregion
--region Private methods
---@private
---Uses the specified building ID to go through applicable data models, assembling a list of possible deposits, filtered by the grade of the building.
---
---@param buildingID string a building's ID
---@return table array of deposit records retrieved from GladeResourcesData
local function assembleDepositsFromBuilding(buildingID)
local depositsList = {}
--This isn't as expensive as it looks, because typically only one data model will return something. However, we don't know which it will be in advance.
for _, dataModel in ipairs(DataModelsWithGladeResourcesRecipes) do
local recipeDataArray = dataModel:getIDsAndRecipesWhereBuildingID(buildingID)
if #recipeDataArray > 0 then
local products = {}
for _, recipeData in ipairs(recipeDataArray) do
local id = dataModel:getRecipeProductID(recipeData)
products[id] = dataModel:getRecipeGrade(recipeData)
end
for id, grade in pairs(products) do
local newList = GladeResourcesData.getAllDepositsWherePrimaryResourceAndGrade(id, grade)
for _, deposit in ipairs(newList) do
table.insert(depositsList, deposit)
end
end
end
end
return depositsList
end
---@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
--Use exactly one description unless there are more, in which case return empty.
local firstDescription = next(descriptions)
if firstDescription and not next(descriptions, firstDescription) then
return firstDescription
else
return ""
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
--Special case where we swap the title out for the name of the one deposit
if numDeposits == 1 then
title = GladeResourcesData.getDepositName(depositsList[1])
end
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 numDeposits number total number of deposits
---@param maxBonuses number slots for bonus resources
---@param displayOverride string override code, if any
---@return string wiki markup
local function renderViewOneRow(deposit, numDeposits, maxBonuses, displayOverride)
displayOverride = displayOverride --TODO write the other display modes
--Special case where we take out the name when there's just one deposit.
local depositName = ""
if numDeposits > 1 then
depositName = GladeResourcesData.getDepositName(deposit)
end
local viewArgs = {
[VIEW_ARG_DEPOSIT] = depositName,
[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 numDeposits = #depositsList
local wikiMarkup = ""
for _, deposit in ipairs(depositsList) do
wikiMarkup = wikiMarkup .. "\n"
wikiMarkup = wikiMarkup .. renderViewOneRow(deposit, numDeposits, 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 productName = frame.args.product --TODO these are currently IDs, they need to be names
local bonusName = frame.args.bonus
local buildingName = frame.args.building
local displayOverride = frame.args.display
--Unset blanks back to nil or get their names from their data model
local productID
if productName and productName ~= "" then
productID = GoodsData.getGoodID(productName)
end
local bonusID = nil
if bonusName and bonusName ~= "" then
bonusID = GoodsData.getGoodID(bonusName)
end
local buildingID = BuildingDataProxy.getID(buildingName)
local depositsList = {}
local title = ""
if productID and bonusID then
if productID ~= bonusID then
return ERROR_MESSAGE_PRODUCT_AND_BONUS_BUT_NOT_MATCH
else
depositsList = GladeResourcesData.getAllDepositsWherePrimaryOrBonusResource(productID)
end
title = #depositsList .. " Deposits for " .. productName .. " or as a bonus."
elseif productID then
depositsList = GladeResourcesData.getAllDepositsWherePrimaryResource(productID)
title = #depositsList .. " Deposits for " .. productName .. "."
elseif bonusID then
depositsList = GladeResourcesData.getAllDepositsWhereBonusResource(bonusID)
title = #depositsList .. " Deposits where " .. bonusName .. " is a bonus."
elseif buildingID then
depositsList = assembleDepositsFromBuilding(buildingID)
title = #depositsList .. " Deposits exploited by the " .. buildingName .. "."
else
return ERROR_MESSAGE_NO_PARAMETERS_GIVEN
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


