Module:PerksData/Data: Difference between revisions

From Against the Storm Official Wiki
No edit summary
No edit summary
Line 2: Line 2:


local DATA_TEMPLATE_NAME = "Template:Perks_csv"
local DATA_TEMPLATE_NAME = "Template:Perks_csv"
local INDEX_KEY = "key"
local INDEX_ID = "id"
local INDEX_NAME = "displayName"
local INDEX_DESCRIPTION = "description"
local INDEX_ICON_FILENAME = "iconFilename"
local INDEX_SOURCES = "sources"
local INDEX_RARITY = "rarity"
local INDEX_PRICE = "price"


---
---

Revision as of 07:21, 31 August 2025

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

local CsvUtils = require("Module:CsvUtils")

local DATA_TEMPLATE_NAME = "Template:Perks_csv"

local INDEX_KEY = "key"
local INDEX_ID = "id"
local INDEX_NAME = "displayName"
local INDEX_DESCRIPTION = "description"
local INDEX_ICON_FILENAME = "iconFilename"
local INDEX_SOURCES = "sources"
local INDEX_RARITY = "rarity"
local INDEX_PRICE = "price"

---
--- Transforms the oldPerksTable returned from CSV processing to be more
--- conducive to member functions looking up data. Essentially, we convert the
--- text strings into tables with the same base name and an index.
---
---@param originalPerksTable table of CSV-based table, with a header row, data rows
---@return table better structured with IDs as keys
local function restructurePerksTable(originalPerksTable)

	-- A few constants we'll need only within this function.
	local DATA_ROWS = 2
	local INDEX_OLD_ID = 1
	local INDEX_OLD_NAME = 2
	local INDEX_OLD_DESCRIPTION = 3
	local INDEX_OLD_ICON_FILENAME = 4
	local INDEX_OLD_SOURCES = 5
	local INDEX_OLD_RARITY = 6
	local INDEX_OLD_PRICE = 7

	perksIDs = {}
	mapNamesToIDs = {}

	local newPerksTable = {}
	for _, originalPerk in ipairs(originalPerksTable[DATA_ROWS]) do

		-- Copy over the content, mapping unhelpful indexes into header keys
		local newPerk = {}
		newPerk[INDEX_KEY] = string.lower(originalPerk[INDEX_OLD_ID])
		newPerk[INDEX_ID] = originalPerk[INDEX_OLD_ID]
		newPerk[INDEX_NAME] = originalPerk[INDEX_OLD_NAME]
		newPerk[INDEX_DESCRIPTION] = cleanDescription(originalPerk[INDEX_OLD_DESCRIPTION])
		newPerk[INDEX_ICON_FILENAME] = originalPerk[INDEX_OLD_ICON_FILENAME] .. ".png"
		newPerk[INDEX_RARITY] = originalPerk[INDEX_OLD_RARITY]
		newPerk[INDEX_PRICE] = originalPerk[INDEX_OLD_PRICE] / VALUE_DIVISOR

		newPerk[INDEX_SOURCES] = makeSourcesSubtable(originalPerk[INDEX_OLD_SOURCES])

		newPerksTable[ newPerk[INDEX_KEY] ] = newPerk

		table.insert(perksIDs, newPerk[INDEX_ID])

		-- Also populate the map for looking up IDs with display names
		if not mapNamesToIDs[ newPerk[INDEX_NAME] ] then
			mapNamesToIDs[ newPerk[INDEX_NAME] ] = {}
		end
		table.insert(mapNamesToIDs[ newPerk[INDEX_NAME] ], newPerk[INDEX_ID])
	end

	return newPerksTable
end

return restructurePerksTable(CsvUtils.luaTableFromCSV(CsvUtils.extractCSV(DATA_TEMPLATE_NAME)))