Anonymous

Module:Equipment/Recommended: Difference between revisions

From Melvor Idle
Use printError function
(main: Modify to allow passing of plaintext as equipment parameters; consistency of Module variables with other modules)
(Use printError function)
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
-- Copied from: https://oldschool.runescape.wiki/w/Module:Recommended Equipment
-- Adapted from: https://oldschool.runescape.wiki/w/Module:Recommended_equipment
-- Released under: https://creativecommons.org/licenses/by-nc-sa/3.0/  
-- Released under: https://creativecommons.org/licenses/by-nc-sa/3.0/  


Line 5: Line 5:


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


Line 22: Line 23:
{ placement = 11, name = 'ammo', icon = 'Slot_ammo', txt = 'Quiver', link = 'Equipment#Ammunition' },
{ placement = 11, name = 'ammo', icon = 'Slot_ammo', txt = 'Quiver', link = 'Equipment#Ammunition' },
{ placement = 12, name = 'passive', icon = 'Slot_passive', txt = 'Passive', link = 'Combat Passive Slot' },
{ placement = 12, name = 'passive', icon = 'Slot_passive', txt = 'Passive', link = 'Combat Passive Slot', hasStats = false },
{ placement = 13, name = 'familiar1', icon = 'Slot_summon', txt = 'Familiar Left', link = 'Summoning' },
{ placement = 13, name = 'familiar1', icon = 'Slot_summon', txt = 'Familiar Left', link = 'Summoning' },
{ placement = 14, name = 'familiar2', icon = 'Slot_summon', txt = 'Familiar Right', link = 'Summoning' },
{ placement = 14, name = 'familiar2', icon = 'Slot_summon', txt = 'Familiar Right', link = 'Summoning' },
{ placement = 99, name = 'notes', icon = '', txt = "Notes", link = nil }
{ placement = 15, name = 'prayer1', icon = 'Slot_prayer', txt = 'Prayer1', link = 'Prayer', icontype = 'prayer' },
{ placement = 16, name = 'prayer2', icon = 'Slot_prayer', txt = 'Prayer2', link = 'Prayer', icontype = 'prayer' },
{ placement = 99, name = 'notes', icon = '', txt = 'Notes', link = '', hasStats = false }
}
}


Line 36: Line 40:
-- Have to sort this table or else the order is messed up when you use it
-- 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)
table.sort(slots, function(a, b) return a.placement < b.placement end)
local showDR = args.showDR ~= nil and string.lower(args.showDR) == 'true'
local Items, statTotal = nil, 0
if showDR then
-- Include the Items module here if necessary, avoids loading the data
-- if the template does not require it
Items = require('Module:Items')
end
local imgSize = 36
if args.imgSize ~= nil and tonumber(args.imgSize) ~= nil then
imgSize = math.max(0, tonumber(args.imgSize))
elseif showDR then
imgSize = 25
end
local function make_row(slot, data, compact)
local function make_row(slot, data, compact)
local tr = mw.html.create('tr')
local tr = mw.html.create('tr')
local sloticon = tr:tag('td')
local sloticon = tr:tag('td')
sloticon:wikitext(string.format('[[File:%s.png|%s|link=%s|36px]]', slot.icon, slot.txt, slot.link))
sloticon:attr('style', 'text-align: center')
:wikitext(string.format('[[File:%s.png|%s|link=%s|%spx]]', slot.icon, slot.txt, slot.link, imgSize))
if compact then
if compact then
sloticon:attr('style', 'padding-bottom: 4px;')
sloticon:attr('style', 'padding-bottom: 4px;')
end
end
for _, v in ipairs(data) do
for _, v in ipairs(data) do
local itemicon = nil
local itemIcon = nil
if string.find(v, '!', 1, true) then
local isPlain = type(string.find(v, '!', 1, true)) == 'number'
if isPlain then
-- ! prefix, treat as plain text
-- ! prefix, treat as plain text
itemicon = string.sub(v, 2, -1)
itemIcon = string.sub(v, 2, -1)
else
else
-- Otherwise treat as an item icon
-- Otherwise treat as an item icon
itemicon = Icons.Icon({v, img=v, type='item'})
local icontype = slot.icontype or 'item'
itemIcon = Icons.Icon({v, img=v, type=icontype})
end
end
local gearname = tr:tag('td'):wikitext(itemicon)
local gearname = tr:tag('td'):wikitext(itemIcon)
if compact then
if compact then
gearname:attr('style', 'padding-left: 12px;')
gearname:attr('style', 'padding-left: 12px;')
end
-- Attempt to obtain DR value if requested
if showDR then
local hasStats = true
if slot.hasStats ~= nil then
hasStats = slot.hasStats
end
local statValue = 0
if hasStats and not isPlain and Items ~= nil then
local item = Items.getItem(v)
if item ~= nil then
statValue = Items._getItemStat(item, 'damageReduction', true)
end
end
statTotal = statTotal + statValue
tr:tag('td'):wikitext(statValue .. '%')
end
end
end
end
Line 80: Line 117:
greatest_row_size = grs
greatest_row_size = grs
end
end
end
if showDR and greatest_row_size > 1 then
-- showDR option is only compatible if a single column of equipment is to be displayed
return Shared.printError('showDR option is incompatible with more than one item per slot')
end
end
Line 90: Line 132:
local compact = true
local compact = true
if args.noheader == nil then
if args.noheader == nil then
local itemHeader = (greatest_row_size > 1 and 'Item (most effective → least effective)') or 'Item'
compact = false
compact = false
parent:addClass('wikitable')
parent:addClass('wikitable stickyHeader')
parent:tag('tr')
local trHead = parent:tag('tr')
trHead:addClass('headerRow-0')
:tag('th'):wikitext('Slot'):done()
:tag('th'):wikitext('Slot'):done()
:tag('th'):attr('colspan', greatest_row_size):wikitext('Item (most effective → least effective)'):done()
:tag('th'):attr('colspan', greatest_row_size):wikitext(itemHeader):done()
if showDR then
trHead:tag('th'):wikitext('DR%'):done()
end
end
end
Line 110: Line 157:
end
end
end
end
if showDR then
-- Total row for displayed stat
parent:tag('tr')
:tag('th'):attr('colspan', 1 + greatest_row_size):wikitext('Total'):done()
:tag('td'):wikitext(statTotal .. '%'):done()
:done()
end
return tostring(parent) .. '\r\n[[Category:Pages with Equipment Recommendations]]'
return tostring(parent) .. '\r\n[[Category:Pages with Equipment Recommendations]]'
end
end
Line 170: Line 225:
table.insert(row, lefties[i])
table.insert(row, lefties[i])
table.insert(row, righties[i])
table.insert(row, righties[i])
table.insert(row, notes[i])
table.insert(row, '| ' .. notes[i])
table.insert(rows, table.concat(row, "\r\n"))
table.insert(rows, table.concat(row, "\r\n"))
Line 185: Line 240:
familiar22 = 'Octopus',
familiar22 = 'Octopus',
familiar13 = 'Crow',
familiar13 = 'Crow',
familiar23 = 'Bear'
familiar23 = 'Bear',
}} )
}} )
end
end


return p
return p