Module:Interactables

From Risk of Rain 2 Wiki
Jump to navigation Jump to search

Interactables stores details of Risk of Rain 2' interactables

Documentation

Package items

interactables.CategoryGallery(frame) (function)
Builds a gallery of all interactables of a specific category.
Parameter: frame Frame object w/ the first argument being the category name (table)
Returns: Preprocessed wikitext of resultant gallery (string)
interactables.AllInteractablesTable(frame) (function)
Builds a wikitable of all interactables
Parameter: frame Frame object (table)
Returns: Preprocessed wikitext of resultant wikitable (string)
Usage: p.BuildAllInteractablesTable(mw.getCurrentFrame()) or {{#invoke:Interactables|AllInteractablesTable}}


---	'''Interactables''' stores details of Risk of Rain 2' interactables
--	
--	@module		interactables
--	@alias		p
--	@author		[[User:GChinchi|GChinchi]]
--	@require	[[Module:Interactables/Data]]
--	@release	stable
--<nowiki>

local interactables = mw.loadData('Module:Interactables/Data').interactables

p = {}
table.unpack = unpack	-- pre-Lua 5.3 table library does not have unpack function

local galleryElement = "<gallery class=\"nobackground\" widths=\"%d\" heights=\"%d\">"
local galleryRow = "File:%s|link=%s|[[%s|%s]]"

--- Custom function for the 'Chests & Loot' category
local function ChestGallery(frame)
	local gallery = { galleryElement:format(128, 128) }
	table.insert(gallery, galleryRow:format(
		"Chest & Equipment Barrel.png",
		"Chests",
		"Chests", "Chests & Equipment"
	))
	--[[
		We want to supress variants of the same interactable, so all used
		links are cached. We "blacklist" the Lunar Scavenger Backpack,
		so that it does not show along with the normal one.
	]]--
	local links = {
		["Twisted Scavenger"] = true
	}
	for _, category in ipairs({ "Chests", "Barrels", "3D Printers", "Scrapper" }) do
		for i in pairs(interactables) do
			local value = interactables[i]
			if value.Category == category and (value.NameLink == nil or not string.find(value.NameLink, "Chests#")) then
				local link = value.NameLink or value.Name
				if links[link] == nil then
					table.insert(gallery, galleryRow:format(
						value.ImageLink or (value.Name .. ".png"),
						link,
						link, value.NameAlt or value.Name
					))
					links[link] = true
				end
			end
		end
	end
	table.insert(gallery, "</gallery>")
	return frame:preprocess(table.concat(gallery, "\n"))
end

---	Builds a gallery of all interactables of a specific category.
--	@function		p.CategoryGallery
--	@param			{table} frame Frame object w/ the first argument being the category name
--	@return			{string} Preprocessed wikitext of resultant gallery
function p.CategoryGallery(frame)
	local category = frame.args[1]
	if category == "Chests & Loot" then
		return ChestGallery(frame)
	end
	local size = { 128, 128 }
	-- Some galleries look better in different sizes
	if category == "Shrines" or category == "Portals" then
		size = { 128, 192 }
	elseif category == "Charging Zones" then
		size = { 192, 192 }
	end
	local gallery = { galleryElement:format(table.unpack(size)) }
	--[[
		We want to supress variants of the same interactable, so all used
		links are cached. While the white Cauldron links to the Commencement article,
		it is conceptually the same as the ones that link to the Bazaar Between Time,
		so we preemptively cache it.
	]]--
	local links = {
		["Commencement#Cauldrons & Lunar Pods"] = true
	}
	for i in pairs(interactables) do
		local value = interactables[i]
		if value.Category == category then
			local link = value.NameLink or value.Name
			if links[link] == nil then
				table.insert(gallery, galleryRow:format(
					value.ImageLink or (value.Name .. ".png"),
					link,
					link, value.NameAlt or value.Name
				))
				if value.Location ~= nil then
					table.insert(gallery, "<br>" .. table.concat(value.Location, "<br>"))
				end
				-- We want all pillars to show in the gallery
				if link ~= "Commencement#Releasing the Rescue Ship" then
				    links[link] = true
				end
			end
		end
	end
	table.insert(gallery, "</gallery>")
	return frame:preprocess(table.concat(gallery, "\n"))
end


local table_head = [=[
{| class="wikitable sortable"
! data-sort-type="string" | Name
! data-sort-type="string" | Category
! data-sort-type="string" | Cost Type
! data-sort-type="string" | Cost Value
! data-sort-type="string" | Effect
]=]

local table_row_item = [=[
|-
| data-sort-value="%s" | [[%s|%s]]
| %s
| %s
| %s
| %s
]=]

---	Builds a wikitable of all interactables
--	@function		p.AllInteractablesTable
--	@param			{table} frame Frame object
--	@return			{string} Preprocessed wikitext of resultant wikitable
--	@usage			p.BuildAllInteractablesTable(mw.getCurrentFrame()) or {{#invoke:Interactables|AllInteractablesTable}}
function p.AllInteractablesTable(frame)
	local categoryNames = { "Chests", "Barrels", "3D Printers", "Scrapper", "Drones", "Shrines" }
	local categories = {}
	for i, name in ipairs(categoryNames) do
		categories[name] = {}
	end
    for i in pairs(interactables) do
		local value = interactables[i]
		if value.Category ~= nil then
			if categories[value.Category] == nil then
				table.insert(categoryNames, value.Category)
				categories[value.Category] = {}
			end
			table.insert(categories[value.Category], value)
		end
	end
	local wikitable = { table_head }
	for i, category in pairs(categoryNames) do
		local entries = categories[category]
		for i = 1, #entries do
			local entry = entries[i]
			local purchaseType = "-"
			if entry.Purchase and entry.Purchase.Type then
				purchaseType = entry.Purchase.Type
			end
			table.insert(wikitable, table_row_item:format(
				entry.Name, entry.NameLink or entry.Name, entry.Name,
				category,
				purchaseType,
				entry.Purchase and entry.Purchase.Cost or "-",
				entry.Effect
			))
		end
	end
	table.insert(wikitable, "|}")
	return frame:preprocess(table.concat(wikitable, ""))
end

return p
--</nowiki>