Module:Equipment/Recommended

From Melvor Idle
Revision as of 17:11, 25 January 2022 by Kaimbe (talk | contribs)

Documentation for this module may be created at Module:Equipment/Recommended/doc

-- Copied from: https://oldschool.runescape.wiki/w/Module:Recommended Equipment
-- Released under: https://creativecommons.org/licenses/by-nc-sa/3.0/ 

local p = {}

local params = require('Module:Shared/Paramtest')
local icons = require("Module:Icons")

local slots = {
	{ placement = 1, name = 'helm', icon = 'Slot_head', txt = 'Head', link = 'Head slot table' },
	{ placement = 2, name = 'body', icon = 'Slot_chest', txt = 'Body', link = 'Body slot table' },
	{ placement = 3, name = 'legs', icon = 'Slot_legs', txt = 'Legs', link = 'Legs slot table' },
	{ placement = 4, name = 'boots', icon = 'Slot_feet', txt = 'Boots', link = 'Feet slot table' },
	{ placement = 5, name = 'gloves', icon = 'Slot_hands', txt = 'Gloves', link = 'Hand slot table' },
	
	{ placement = 6, name = 'cape', icon = 'Slot_back', txt = 'Back', link = 'Cape slot table' },
	{ placement = 7, name = 'neck', icon = 'Slot_neck', txt = 'Neck', link = 'Neck slot table' },
	{ placement = 8, name = 'ring', icon = 'Slot_ring', txt = 'Ring', link = 'Ring slot table' },
	
	{ placement = 9, name = 'weapon', icon = 'Slot_weapon', txt = 'Weapon', link = 'Weapon slot table' },
	{ placement = 10, name = 'shield', icon = 'Slot_shield', txt = 'Shield', link = 'Shield slot table' },
	{ placement = 11, name = 'ammo', icon = 'Slot_ammo', txt = 'Ammo/Spell', link = 'Ammunition slot table' },
	
	{ placement = 12, name = 'passive', icon = 'Slot_passive', txt = 'Passive', link = '' },
	{ placement = 13, name = 'familiar1', icon = 'Slot_summon', txt = 'Familiar', link = '' },
	{ placement = 14, name = 'familiar2', icon = 'Slot_summon', txt = 'Familiar', link = '' }
	
}

function p.main(frame)
	local args = frame:getParent().args	
	-- Dynamic colspan and N/A generation value
	local greatest_row_size = 0
	-- Number of choices each slot can have
	local number_of_possible_choices = 5
	-- Have to sort this table or else the order is messed up when you use it
	table.sort(slots, function(a, b) return a.placement < b.placement end)
	
	local function make_row(slot, data)
		local tr = mw.html.create('tr')
		tr:tag('td'):attr('style', 'padding-bottom: 4px;'):wikitext(string.format('[[File:%s.png|%s|link=%s|36px]]', slot.icon, slot.txt, slot.link))
		for _, v in ipairs(data) do
			local itemicon = icons.Icon({v, img=v, type='item'})
			tr:tag('td'):attr('style', 'padding-left: 12px;'):wikitext(itemicon)
		end
		
		-- If the data size is smaller than GRS, then we need to fill up the remaining td's with N/As
		if #data < greatest_row_size then
			local difference = greatest_row_size - #data
			for i = 1, difference, 1 do
				tr:tag('td'):addClass('table-na'):wikitext('')	
			end
		end
		return tr
	end
	
	-- Find the greatest row count
	for _, v in next, slots, nil do
		local grs = 0
		for i = 1, number_of_possible_choices, 1 do
			local check = args[v.name .. i]
			if check and params.has_content(check) then
				grs = grs + 1	
			end
		end
		
		if greatest_row_size < grs then
			greatest_row_size = grs	
		end
	end
	
	local parent = mw.html.create('table')
	-- If setname is passed in, apply it above the table
	if args.setname and params.has_content(args.setname) then
		parent:tag('caption'):wikitext(string.format('Recommended equipment for %s', args.setname))
	end

	if args.noheader == nil then
		parent:addClass('wikitable')
		parent:tag('tr')
			:tag('th'):wikitext('Slot'):done()
			:tag('th'):attr('colspan', greatest_row_size):wikitext('Item (most effective → least effective)'):done()
	end
		
	for _, v in next, slots, nil do
		local row_data = {}
		for i = 1, number_of_possible_choices, 1 do
			local gear = args[v.name .. i]
			if gear and params.has_content(gear) then
				table.insert(row_data, gear)
			end
		end
		
		if #row_data > 0 or args.showall then
			parent:node(make_row(v, row_data))
		end
	end
		
	return tostring(parent)
end

return p