Module:Sandbox/User:Fjara/Sandbox/Crane

From Illerai
Jump to navigation Jump to search

Documentation for this module may be created at Module:Sandbox/User:Fjara/Sandbox/Crane/doc

local p = {}

--SpellReq (RuneReq's counterpart)
--	How to differentiate same spelling names like Ape Atoll Teleport (standard) vs Ape Atoll Teleport (Arceuus)

local paramTest = require('Module:Paramtest')
local yesNo = require('Module:Yesno')

-- Create a span to list staff requirements, runes required, bolts required, and prayer required
function p.buildOutput(runes, bolts, staff, prayer, runesOnly)
	local ret = ''
	if(staff and paramTest.has_content(staff) and (not runesOnly)) then
		ret = ret .. '[[File:' .. staff .. '.png|20px|' .. staff .. '|link=' .. staff .. ']] '
	end
	for _, rune in ipairs(runes) do
		if(rune.quantity) then
			local hoverText = rune.rune:sub(1, rune.rune:find(' ') - 1) or rune.rune
			ret = ret .. '<sup>' .. rune.quantity .. '</sup>[[File:' .. rune.rune .. '.png|20px|' .. hoverText .. '|link=' .. rune.rune .. ']] '
		end
	end
	if(bolts and paramTest.has_content(bolts) and (not runesOnly)) then
		ret = ret .. '<sup>10</sup>[[File:' .. bolts .. ' 5.png|20px|' .. bolts .. '|link=' .. bolts .. ']] '
	end
	if(prayer and paramTest.has_content(prayer) and (not runesOnly)) then
		ret = ret .. '<sup>' .. prayer .. '</sup>[[File:Prayer icon.png|20px|Prayer points|link=Prayer]]'
	end
	if(ret == '') then
		ret = 'None'
	end
	return '<span style="white-space:nowrap">' .. ret .. '</span>'
end

-- Get Spell JSON for display of spell
function p.loadData(spell)
	local query = {
		'[[' .. spell .. ']]',
		'?=#-',
		'?Spell JSON = json',
		offset = 0,
		limit = 1,
	}
	local t1 = os.clock()
	local smwData = mw.smw.ask(query)
	local t2 = os.clock()
	if((not smwData) or (#smwData == 0)) then
		error('Failed to find spell: ' .. spell ..' - ensure it is spelled correctly. No results from SMW.')
	end
	mw.log(string.format('SMW: Found %i, offset %i, limit %i, time elapsed %.3f ms', #smwData, query.offset, query.limit, (t2 - t1) * 1000))
	
	smwData = mw.text.jsonDecode(smwData[1].json)
	
	-- Format rune quantities identically to buildOutput input parameters
	local runes = {}
	for _, runeData in ipairs(smwData.runes) do
		table.insert(runes, { ['rune'] = runeData.rune, ['quantity'] = runeData.quantity } )
	end
	
	return runes, smwData.bolts, smwData.staff, smwData.prayer
end

function p._main(args)
	-- SMW display flag for disabling display of Prayer, bolts, or staves
	local runesOnly = yesNo(paramTest.default_to(args.runesonly, false))
	-- SMW spell name (page name)
	local spell = paramTest.default_to(args[1], nil)
	
	if(spell and paramTest.has_content(spell)) then
		return p.buildOutput(p.loadData(spell), runesOnly)
	else
		error('Spell was not defined.')
	end
end

function p.main(frame)
	--mw.logObject(frame)
	local args = frame:getParent().args
	return p._main(args)
end

return p