Module:RuneReq
Jump to navigation
Jump to search
Documentation for this module may be created at Module:RuneReq/doc
local p = {}
local paramTest = require('Module:Paramtest')
local onMain = require('Module:Mainonly').on_main
local yesNo = require('Module:Yesno')
local trim = mw.text.trim
local jsonDecode = mw.text.jsonDecode
-- Get Spell JSON from specific spell page
function p.loadData(spell)
local query = {
'[[' .. spell .. ']]',
'[[Spell JSON::+]]',
'?=#-',
'?Spell JSON = json',
offset = 0,
limit = 500,
}
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 itemCount = {}
for _, runeData in ipairs(smwData.runes) do
table.insert(itemCount, { runeData.rune, runeData.quantity } )
end
return itemCount, smwData.bolts, smwData.staff, smwData.prayer
end
function p.setSMW(itemCount, bolts, staff, prayer)
local jsonObject = { staff = staff, bolts = bolts, prayer = prayer, runes = {} }
local materialNames = {}
for _, rune in ipairs(itemCount) do
if(rune[2] > 0) then
table.insert(jsonObject.runes, {rune = rune[1], quantity = rune[2]})
table.insert(materialNames, rune[1])
end
end
if(bolts ~= nil) then
table.insert(materialNames, bolts)
end
local smwMap = {
['Uses material'] = materialNames,
['Spell JSON'] = mw.text.jsonEncode(jsonObject),
}
for _, v in pairs(smwMap) do
-- Trim {{!}}foo
if(type(v) == 'table') then
for j, w in ipairs(v) do
v[j] = mw.text.split(w, '|')[1]
end
end
end
mw.smw.set(smwMap)
end
function p.buildOutput(itemCount, bolts, staff, prayer)
local ret = ''
if(staff ~= nil) then
if(not staff:match('staff')) then
staff = trim(staff) .. ' staff'
end
ret = ret .. '[[File:' .. staff .. '.png|20px|' .. staff .. '|link=' .. staff .. ']] '
end
for _, rune in ipairs(itemCount) do
if(rune[2] > 0) then
local hoverText = rune[1]
if(rune[1]:match('rune')) then
hoverText = rune[1]:sub(1, rune[1]:find(' ') - 1)
end
ret = ret .. '<sup>' .. rune[2] .. '</sup>[[File:' .. rune[1] .. '.png|20px|' .. hoverText .. '|link=' .. rune[1] .. ']] '
end
end
if(bolts ~= nil) then
if(not bolts:match('bolts')) then
bolts = trim(bolts) .. ' bolts'
end
ret = ret .. '<sup>10</sup>[[File:' .. bolts .. ' 5.png|20px|' .. bolts .. '|link=' .. bolts .. ']] '
end
if(prayer) 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
function p._main(args)
local smw = yesNo(paramTest.default_to(args.smw, false))
-- Ouput everything based on a spell using smw
local spell = paramTest.default_to(args.spell, nil)
local data = {}
if(spell ~= nil) then
return p.buildOutput(p.loadData(spell))
end
-- To reserve input order, the only option is unanmed args with |air\3
local itemCount = { { 'Law rune', paramTest.default_to(tonumber(args.law), 0) },
{ 'Soul rune', paramTest.default_to(tonumber(args.soul), 0) },
{ 'Astral rune', paramTest.default_to(tonumber(args.astral), 0) },
{ 'Cosmic rune', paramTest.default_to(tonumber(args.cosmic), 0) },
{ 'Body rune', paramTest.default_to(tonumber(args.body), 0) },
{ 'Nature rune', paramTest.default_to(tonumber(args.nature), 0) },
{ 'Death rune', paramTest.default_to(tonumber(args.death), 0) },
{ 'Chaos rune', paramTest.default_to(tonumber(args.chaos), 0) },
{ 'Mind rune', paramTest.default_to(tonumber(args.mind), 0) },
{ 'Blood rune', paramTest.default_to(tonumber(args.blood), 0) },
{ 'Wrath rune', paramTest.default_to(tonumber(args.wrath), 0) },
{ 'Water rune', paramTest.default_to(tonumber(args.water), 0) },
{ 'Earth rune', paramTest.default_to(tonumber(args.earth), 0) },
{ 'Air rune' , paramTest.default_to(tonumber(args.air), 0) },
{ 'Fire rune', paramTest.default_to(tonumber(args.fire), 0) },
{ 'Banana', paramTest.default_to(tonumber(args.banana), 0) },
{ 'Unpowered orb', paramTest.default_to(tonumber(args.orb), 0) },
{ 'Basalt', paramTest.default_to(tonumber(args.basalt), 0) },
{ 'Efh salt', paramTest.default_to(tonumber(args['efh salt']), 0) },
{ 'Te salt', paramTest.default_to(tonumber(args['te salt']), 0) },
{ 'Urt salt', paramTest.default_to(tonumber(args['urt salt']), 0) },
}
local bolts = paramTest.default_to(args.bolts, nil)
local staff = paramTest.default_to(args.staff, nil)
local prayer = paramTest.default_to(args.prayer, nil)
if(smw and onMain()) then
p.setSMW(itemCount, bolts, staff, prayer)
end
return p.buildOutput(itemCount, bolts, staff, prayer)
end
function p.main(frame)
--mw.logObject(frame)
local args = frame:getParent().args
return p._main(args)
end
return p