Module:EmberFantasy/sandbox/Slottable

Documentation for this module may be created at Module:EmberFantasy/sandbox/Slottable/doc

-- <pre>
require('Module:Mw.html extension')

local p = {}

local format = require('Module:Format equipment stat').format --for formatting the stats with + and - symbols on output

local p2pIcon = '[[File:Member icon.png|frameless|link=Pay-to-play]]' --these icons are for the later members/f2p stars
local f2pIcon = '[[File:Free-to-play icon.png|frameless|link=Free-to-play]]'

local stats = {'astab', 'aslash', 'acrush', 'amagic', 'arange', 'dstab', 'dslash', 'dcrush', 'dmagic', 'drange', 'str', 'rstr', 'mdmg', 'prayer'}

function p.main(frame)
	local args = frame:getParent().args --get the input from the template
	local slot = string.lower(tostring(args['slot'])) --take the slot input as the slot we want the table for
	local mems = args['mems']
	local dmm = args['dmm']
	local beta = args['beta']
	local data = getData(slot,mems,dmm,beta) --this takes the slot and sends it to our getData function (seen below) which gives us back a data table

	local restbl = mw.html.create('table') --start making our results table, beginninig with the header
	restbl:addClass('wikitable align-center-1 sortable align-center')
		:tag('tr')
			:tag('th'):addClass('unsortable'):wikitext(''):done()
			:tag('th'):wikitext('Name'):done()
			:tag('th'):wikitext('Members'):done()
			:tag('th'):wikitext('[[File:White dagger.png|Stab attack]]') :done()
			:tag('th'):wikitext('[[File:White scimitar.png|Slash attack]]') :done()
			:tag('th'):wikitext('[[File:White warhammer.png|Crush attack]]') :done()
			:tag('th'):wikitext('[[File:Magic icon.png|Magic attack]]') :done()
			:tag('th'):wikitext('[[File:Ranged icon.png|Ranged attack]]') :done()
			:tag('th'):wikitext('[[File:White dagger.png|Stab defence]]<sup>[[File:Defence icon.png]]</sup>') :done()
			:tag('th'):wikitext('[[File:White scimitar.png|Slash defence]]<sup>[[File:Defence icon.png]]</sup>') :done()
			:tag('th'):wikitext('[[File:White warhammer.png|Crush defence]]<sup>[[File:Defence icon.png]]</sup>') :done()
			:tag('th'):wikitext('[[File:Magic icon.png|Magic defence]]<sup>[[File:Defence icon.png]]</sup>') :done()
			:tag('th'):wikitext('[[File:Ranged icon.png|Ranged defence]]<sup>[[File:Defence icon.png]]</sup>') :done()
			:tag('th'):wikitext('[[File:Strength icon.png|Strength]]') :done()
			:tag('th'):wikitext('[[File:Ranged Strength icon.png|Ranged Strength]]') :done()
			:tag('th'):wikitext('[[File:Magic Damage icon.png|Magic Damage]]') :done()
			:tag('th'):wikitext('[[File:Prayer icon.png|Prayer]]') :done()
			:tag('th'):wikitext('Weight') :done()
		:done()

	-- Create the rows for the output table
	for _, item in ipairs(data) do    --for each row of data, we take it and split it up, then put it into our table row. We also format them with + and - symbols here
		local row = restbl:tag('tr')
			:tag('td'):wikitext(item.image):done()
			:tag('td'):wikitext(item.name):done()
			:tag('td'):wikitext(item.members):done()
		for _, stat in ipairs(stats) do
			local value = item[stat]
			local value_num = tonumber(value)
			row:tag('td'):wikitext(format(value))
				:addClassIf(value_num and (value_num > 0), 'table-positive')
				:addClassIf(value_num and (value_num < 0), 'table-negative')
		end
		row:tag('td'):wikitext(item.weight):done()
	end

	return tostring(restbl) --returns the table back to the template to be put onto the page
end

function getData(slotName,mems,dmm,beta) --so this is the function that takes the slot name and gets all the data for the items
	--first we set up our SMW query
	local q = {
		'[[Equipment slot::'..slotName..']]', --we want everything that matches the slot we called, and then all the attached data below
		'?Is members only',
		'?Stab attack bonus',
		'?Slash attack bonus',
		'?Crush attack bonus',
		'?Magic attack bonus',
		'?Range attack bonus',
		'?Stab defence bonus',
		'?Slash defence bonus',
		'?Crush defence bonus',
		'?Magic defence bonus',
		'?Range defence bonus',
		'?Strength bonus',
		'?Ranged Strength bonus',
		'?Magic Damage bonus',
		'?Prayer bonus',
		'?Category',
		'?Image',
		'?Weight',
		limit = 1000,
		order = 'ascending'
	}
	local smwdata = mw.smw.ask(q) --this now asks the smw for all the data, and saves all of it in smwdata
	local data = {} --setting this table up to insert processed data into

	for _, item in ipairs(smwdata) do --for each item we found with our smw query
		local process = true
		local dmmcat = false
		local betacat = false
		if type(item['Category']) == 'table' then
			for _, category in ipairs(item['Category']) do
				if category == "[[:Category:Deadman seasonal items|Deadman seasonal items]]" then
					dmmcat = true
				elseif category == "[[:Category:Beta items|Beta items]]" then
					betacat = true
				end
			end
		end
		if mems == 'Members' then
			if not item['Is members only'] then
				process = false
			end
		end
		if mems == 'F2P' then
			if item['Is members only'] then
				process = false
			end
		end
		if dmm == 'No' then
			if dmmcat == true then
				process = false
			end
		end
		if beta == 'No' then
			if betacat == true then
				process = false
			end
		end
		if process then
			local dataline = processData(item) --we process it (seen below) and then save it as a line
			table.insert(data, dataline) --and then we insert that line into our data table to be returned
		end
	end

	return data --and once we've processed all the data we send the data table back up to main for the formatting
end

function processData(item) --this breaks up the smwdata bit into manageable little bites
	local name = item[1] or '' --this gets the item name, which by default is in the first position of the query results (lua starts at 1, not 0)
	local _name = string.match(name, '|(.-)%]')
	
	local members = item['Is members only'] --members we have to do a bit extra for
	if members == true then --if it is members, we use the members star
		members = p2pIcon
	elseif members == false then --if not, use f2p star
		members = f2pIcon
	else
		members = ''
	end
	
	local image = item['Image'] or ''
	if type(image) == 'table' then
		image = image[1] -- take the first image available
	end
	image = string.match(image, '[Ff]ile:.-%.png') or ''
	if image ~= '' then
		image = string.format('[[%s|link=%s]]', image, _name)
	end
	
	local keyset=""
	local n=0
	
	for k,v in pairs(item) do
	  n=n+1
	  keyset = keyset .. k .. " (" .. tostring(v) ..")" .. ":"
	end

	return { --now we return the processed data for this item back to the getData function, which sends us another item and repeats until we've done them all
		name = name,
		image = image,
		members = members,
		astab = item['Stab attack bonus'] or '',
		aslash = item['Slash attack bonus'] or '',
		acrush = item['Crush attack bonus'] or '',
		amagic = item['Magic attack bonus'] or '',
		arange = item['Range attack bonus'] or '',
		dstab = item['Stab defence bonus'] or '',
		dslash = item['Slash defence bonus'] or '',
		dcrush = item['Crush defence bonus'] or '',
		dmagic = item['Magic defence bonus'] or '',
		drange = item['Range defence bonus'] or '',
		str = item['Strength bonus'] or '',
		rstr = item['Ranged Strength bonus'] or '',
		mdmg = item['Magic Damage bonus'] or keyset,
		prayer = item['Prayer bonus'] or '',
		weight = item['Weight'] or '',
	}
end
p.getData = getData
return p