Module:Sandbox/Skills/Cartography: Difference between revisions

From Melvor Idle
(Sortable tables, switched to mw.html)
(Added table of contents header)
Line 255: Line 255:
end
end
return p._getDiscoveryRewardsTable(item)
return '==POI Discovery Rewards==\n' .. p._getDiscoveryRewardsTable(item)
end
end


Line 263: Line 263:
if POI.discoveryRewards ~= nil and POI.discoveryRewards.items ~= nil then
if POI.discoveryRewards ~= nil and POI.discoveryRewards.items ~= nil then
local item = Items.getItemByID(POI.discoveryRewards.items[1].id)
local item = Items.getItemByID(POI.discoveryRewards.items[1].id)
local resultTable = string.gsub(p.getDiscoveryRewardsTable(item.name), "==POI Discovery Rewards==", "")
table.insert(testTable, p.getDiscoveryRewardsTable(item.name))
table.insert(testTable, resultTable)
end
end
end
end

Revision as of 01:26, 16 March 2024

Documentation for this module may be created at Module:Sandbox/Skills/Cartography/doc

local p = {}

local Shared = require('Module:Shared')
local Constants = require('Module:Constants')
local GameData = require('Module:GameData')
local SkillData = GameData.skillData
local Items = require('Module:Items')
local Common = require('Module:Common')
local Icons = require('Module:Icons')

function p.getPointOfInterestByID(id)
	for i, POI in pairs(SkillData.Cartography.worldMaps[1].pointsOfInterest) do
		if POI.id == id then
			return POI
		end
	end
	return nil
end

function p.getPointOfInterestForDigSite(digSiteID)
	for i, POI in pairs(SkillData.Cartography.worldMaps[1].pointsOfInterest) do
		if POI.digSiteID ~= nil and POI.digSiteID == digSiteID then
			return POI
		end
	end
	return nil
end

function p.getPointOfInterest(name)
	name = string.gsub(name, "%%27", "'")
	name = string.gsub(name, "'", "'")
	for i, POI in pairs(SkillData.Cartography.worldMaps[1].pointsOfInterest) do
		if POI.name == name then
			return POI
		end
	end
	return nil
end

function p.getPointsOfInterest(checkFunc)
	local result = {}
	for i, POI in pairs(SkillData.Cartography.worldMaps[1].pointsOfInterest) do
		if checkFunc(POI) then
			table.insert(result, POI)
		end
	end
	return result
end

function p.getHexByAxial(Q, R)
	--Assume if R is nil that a pair was sent in
	if R == nil then
		R = Q.r
		Q = Q.q
	end
	
	for i, hex in pairs(SkillData.Cartography.worldMaps[1].hexes) do
		local coords = hex.coordinates
		if coords.q == Q and coords.r == R then
			return hex
		end
	end
	return nil
end

function p.getHexByXY(X, Y)
	local Q, R = p.convertXYToAxial(X, Y)
	return p.getHexByAxial(Q, R)
end

function p.convertXYToAxial(X, Y)
	--Assume that if Y is nil that a pair was sent in
	if Y == nil then
		Y = X.y
		X = X.x
	end
	local Q = X
	local R = Y - (X - (X % 2)) / 2
	return Q, R
end

function p.convertAxialToXY(Q, R)
	--Assume if r is nil that a pair was sent in
	if R == nil then
		R = Q.r
		Q = Q.q
	end
	local Y = R + (Q - (Q % 2)) / 2
	local X = Q
	return X, Y
end

function p._getPOIRequirements(POI)
	local Hex = p.getHexByAxial(POI.coords)
	local reqTable = {}
	local HexReqs = Common.getRequirementString(Hex.requirements, '')
	if HexReqs ~= '' then
		table.insert(reqTable, HexReqs)
	end
	if POI.hidden ~= nil then
		local POIReqs = Common.getRequirementString(POI.hidden.requirements, '')
		if POIReqs ~= '' then
			table.insert(reqTable, POIReqs)
		end
	end
	return reqTable
end

function p._getDiscoveryRewards(POI)
	local rewardTable = {}
	if POI.discoveryRewards ~= nil then
		if POI.discoveryRewards.gp ~= nil then
			table.insert(rewardTable, Icons.GP(POI.discoveryRewards.gp))
		end
		if POI.discoveryRewards.sc ~= nil then
			table.insert(rewardTable, Icons.SC(POI.discoveryRewards.sc))
		end
		if POI.discoveryRewards.items ~= nil then
			for j, reward in pairs(POI.discoveryRewards.items) do
				local item = Items.getItemByID(reward.id)
				local qty = reward.quantity
				table.insert(rewardTable, Icons.Icon({item.name, type='item', qty = qty}))
			end
		end
	end
	return rewardTable
end

function p.getPointOfInterestTable(frame)
	local POIs = {}
	local POI_Strings = {}
	local tableStr = {}
	table.insert(tableStr, '{| class="wikitable sortable lighttable"')
	table.insert(tableStr, '\r\n|-\r\n!colspan="2"|Name!!Type!!X!!Y!!Requirements!!Discovery Rewards!!Discovery Modifiers!!Active Effect')
	
	for i, POI in pairs(SkillData.Cartography.worldMaps[1].pointsOfInterest) do
		table.insert(POIs, POI)
	end
	
	table.sort(POIs, function(a, b)
						local aX, aY = p.convertAxialToXY(a.coords)
						local bX, bY = p.convertAxialToXY(b.coords)
						if aX ~= bX then
							return aX < bX
						else
							return aY < bY
						end
					 end)
	
	for i, POI in pairs(POIs) do
		local X, Y = p.convertAxialToXY(POI.coords)
		table.insert(POI_Strings, POI.name..' ('..X..', '..Y..')')
		
		table.insert(tableStr,'\r\n|-\r\n|')
		table.insert(tableStr, Icons.Icon({POI.name, type='poi', notext='true', nolink='true', size='50'}))
		table.insert(tableStr, '||id="'..string.gsub(POI.name,' ', '')..'"|')
		--Link Dig Sites
		if POI.type == 'DigSite' then
			table.insert(tableStr, '[['..POI.name..']]')
		else
			table.insert(tableStr, POI.name)
		end
		local POIType = POI.type
		if POIType == 'DigSite' then
			POIType = 'Dig&nbsp;Site'
		else
			if POI.activeModifiers ~= nil then
				POIType = 'Active'
			elseif POI.fastTravel ~= nil then
				POIType = 'Port'
			end
		end
		table.insert(tableStr, '||'..POIType)
		table.insert(tableStr, '||'..X)
		table.insert(tableStr, '||'..Y)
		
		--Add Requirements
		table.insert(tableStr, '\r\n|')
		local reqTable= p._getPOIRequirements(POI)
		table.insert(tableStr, table.concat(reqTable, '<br/>'))
		
		--Add Discovery Rewards
		table.insert(tableStr, '\r\n|')
		local rewardTable = p._getDiscoveryRewards(POI)
		table.insert(tableStr, table.concat(rewardTable,'<br/>'))
		
		--Add Discovery Modifiers
		table.insert(tableStr, '\r\n|')
		if POI.discoveryModifiers ~= nil then
			table.insert(tableStr, Constants.getModifiersText(POI.discoveryModifiers.modifiers)..' (for '..POI.discoveryModifiers.moves..' travels)')
		end
		
		--Add Active Modifiers
		table.insert(tableStr, '\r\n|')
		if POI.activeModifiers ~= nil then
			table.insert(tableStr, Constants.getModifiersText(POI.activeModifiers))
		end
	end
	
	table.insert(tableStr, '\r\n|}')
	
	return table.concat(tableStr, '')
end

function p._getDiscoveryRewardsTable(item)
	local POIs = {}
	for i, POI in pairs(SkillData.Cartography.worldMaps[1].pointsOfInterest) do
		if POI.discoveryRewards ~= nil and POI.discoveryRewards.items ~= nil then
			for i, reward in pairs(POI.discoveryRewards.items) do
				if reward.id == item.id then
					table.insert(POIs, POI)
				end
			end
		end
	end

	table.sort(POIs, function(a, b)
		local aX, aY = p.convertAxialToXY(a.coords)
		local bX, bY = p.convertAxialToXY(b.coords)
		if aX ~= bX then
			return aX < bX
		else
			return aY < bY
		end
	end)

	-- Build the table
	local resultTable = mw.html.create('table')
	resultTable:addClass('wikitable'):addClass('sortable')
	resultTable:tag('tr'):addClass('headerRow-0')
		:tag('th'):wikitext('Point of Interest'):done()
		:tag('th'):wikitext('X'):done()
		:tag('th'):wikitext('Y'):done()
		:tag('th'):wikitext('Requirements'):done()
		:tag('th'):wikitext('Discovery Rewards'):done()

	for _, POI in ipairs(POIs) do
		local X, Y = p.convertAxialToXY(POI.coords)
		local tr = mw.html.create('tr')
		tr:tag('td'):wikitext(Icons.Icon({POI.name, type='poi'}))
		tr:tag('td'):wikitext(X)
		tr:tag('td'):wikitext(Y)
		tr:tag('td'):wikitext(table.concat(p._getPOIRequirements(POI), '<br/>'))
		tr:tag('td'):wikitext(table.concat(p._getDiscoveryRewards(POI), '<br/>'))
		resultTable:node(tr)
	end
	return tostring(resultTable)
end

function p.getDiscoveryRewardsTable(frame)
	local itemName = frame.args ~= nil and frame.args[1] or frame
	local item = Items.getItem(itemName)
	if item == nil then
		return Shared.printError('No item named "' .. itemName .. '" exists in the data module')
	end
	
	return '==POI Discovery Rewards==\n' .. p._getDiscoveryRewardsTable(item)
end

function p.testDiscoveryRewards()
	local testTable = {}
	for i, POI in pairs(SkillData.Cartography.worldMaps[1].pointsOfInterest) do
		if POI.discoveryRewards ~= nil and POI.discoveryRewards.items ~= nil then
			local item = Items.getItemByID(POI.discoveryRewards.items[1].id)
			table.insert(testTable, p.getDiscoveryRewardsTable(item.name))
		end
	end
	return table.concat(testTable)
end

function p.test()
	return p.getPointOfInterestTable()
end

return p