Module:Recommended equipment: Difference between revisions

From Heavenly Wiki
Jump to navigation Jump to search
Created page with "local p = {} local paramTest = require('Module:Paramtest') local onMain = require('Module:Mainonly').on_main local yesNo = require('Module:Yesno') local SlotInfo = { { name = 'head', icon = 'Head slot', txt = 'Head', link = 'Head slot table' }, { name = 'neck', icon = 'Neck slot', txt = 'Neck', link = 'Neck slot table' }, { name = 'cape', icon = 'Cape slot', txt = 'Back', link = 'Cape slot table' }, { name = 'body', icon = 'Body slot', txt = 'Body', link = 'Body sl..."
 
No edit summary
 
Line 4: Line 4:
local onMain = require('Module:Mainonly').on_main
local onMain = require('Module:Mainonly').on_main
local yesNo = require('Module:Yesno')
local yesNo = require('Module:Yesno')
-- Add error checking for SMW
local smw = mw.smw
if not smw then
    mw.log('SMW Lua bridge not available')
    smw = {
        set = function() return nil end  -- Stub function that does nothing
    }
end


local SlotInfo = {
local SlotInfo = {
{ name = 'head', icon = 'Head slot', txt = 'Head', link = 'Head slot table' },
    { name = 'head', icon = 'Head slot', txt = 'Head', link = 'Head slot table' },
{ name = 'neck', icon = 'Neck slot', txt = 'Neck', link = 'Neck slot table' },
    { name = 'neck', icon = 'Neck slot', txt = 'Neck', link = 'Neck slot table' },
{ name = 'cape', icon = 'Cape slot', txt = 'Back', link = 'Cape slot table' },
    { name = 'cape', icon = 'Cape slot', txt = 'Back', link = 'Cape slot table' },
{ name = 'body', icon = 'Body slot', txt = 'Body', link = 'Body slot table' },
    { name = 'body', icon = 'Body slot', txt = 'Body', link = 'Body slot table' },
{ name = 'legs', icon = 'Legs slot', txt = 'Legs', link = 'Legs slot table' },
    { name = 'legs', icon = 'Legs slot', txt = 'Legs', link = 'Legs slot table' },
{ name = 'weapon', icon = 'Weapon slot', txt = 'Weapon', link = 'Weapon slot table' },
    { name = 'weapon', icon = 'Weapon slot', txt = 'Weapon', link = 'Weapon slot table' },
{ name = 'shield', icon = 'Shield slot', txt = 'Shield', link = 'Shield slot table' },
    { name = 'shield', icon = 'Shield slot', txt = 'Shield', link = 'Shield slot table' },
{ name = '2h', icon = '2h slot', txt = 'Two-handed weapon', link = 'Two-handed slot table' },
    { name = '2h', icon = '2h slot', txt = 'Two-handed weapon', link = 'Two-handed slot table' },
{ name = 'ammo', icon = 'Ammo slot', txt = 'Ammo/Spell', link = 'Ammunition slot table' },
    { name = 'ammo', icon = 'Ammo slot', txt = 'Ammo/Spell', link = 'Ammunition slot table' },
{ name = 'hands', icon = 'Hands slot', txt = 'hands', link = 'Hand slot table' },
    { name = 'hands', icon = 'Hands slot', txt = 'hands', link = 'Hand slot table' },
{ name = 'feet', icon = 'Feet slot', txt = 'Boots', link = 'Feet slot table' },
    { name = 'feet', icon = 'Feet slot', txt = 'Boots', link = 'Feet slot table' },
{ name = 'ring', icon = 'Ring slot', txt = 'Ring', link = 'Ring slot table' },
    { name = 'ring', icon = 'Ring slot', txt = 'Ring', link = 'Ring slot table' },
{ name = 'special', icon = 'Special attack orb', txt = 'Special attack', link = 'Special attacks' },
    { name = 'special', icon = 'Special attack orb', txt = 'Special attack', link = 'Special attacks' },
}
}


function buildRow(slot, rowData, largestRowSize)
function buildRow(slot, rowData, largestRowSize)
local row = mw.html.create('tr')
    local row = mw.html.create('tr')
row:tag('td'):wikitext('[[File:' .. slot.icon .. '.png|' .. slot.txt .. '|link=' .. slot.link .. ']]')
    row:tag('td'):wikitext('[[File:' .. slot.icon .. '.png|' .. slot.txt .. '|link=' .. slot.link .. ']]')
for _, cellData in ipairs(rowData) do
    for _, cellData in ipairs(rowData) do
row:tag('td'):wikitext(cellData)
        row:tag('td'):wikitext(cellData)
end
    end
   
-- If there are any rows that empty compared to other rows fill up the remaining td's with N/As
    -- If there are any rows that empty compared to other rows fill up the remaining td's with N/As
for i = 1, largestRowSize - #rowData, 1 do
    for i = 1, largestRowSize - #rowData, 1 do
row:tag('td'):addClass('table-na'):wikitext('N/A')
        row:tag('td'):addClass('table-na'):wikitext('N/A')  
end
    end
return row
    return row
end
end


function p._main(args)
function p._main(args)
   
-- Tracking for colspan and N/A cells
    -- Tracking for colspan and N/A cells
local largestRowSize = 0
    local largestRowSize = 0
-- Intended as a hidden setting, use sparingly
    -- Intended as a hidden setting, use sparingly
local maxRowsPerSlot = paramTest.default_to(args.rows, 5)
    local maxRowsPerSlot = paramTest.default_to(args.rows, 5)
   
-- Find the greatest row count
    -- Find the greatest row count
for i, slot in ipairs(SlotInfo) do
    for i, slot in ipairs(SlotInfo) do
local slotRowSize = 0
        local slotRowSize = 0
       
for i = 1, maxRowsPerSlot, 1 do
        for i = 1, maxRowsPerSlot, 1 do
if(paramTest.has_content(args[slot.name .. i])) then
            if(paramTest.has_content(args[slot.name .. i])) then
slotRowSize = i
                slotRowSize = i
end
            end
end
        end
       
if(largestRowSize < slotRowSize) then
        if(largestRowSize < slotRowSize) then
largestRowSize = slotRowSize
            largestRowSize = slotRowSize  
end
        end
if(largestRowSize == maxRowsPerSlot) then
        if(largestRowSize == maxRowsPerSlot) then
break
            break
end
        end
end
    end
   
local ret = mw.html.create('table'):addClass('wikitable sticky-header')
    local ret = mw.html.create('table'):addClass('wikitable sticky-header')
   
if(paramTest.has_content(args.style)) then
    if(paramTest.has_content(args.style)) then
ret:tag('caption'):wikitext('Recommended equipment for ' .. args.style)
        ret:tag('caption'):wikitext('Recommended equipment for ' .. args.style)
end
    end
   
ret:tag('tr')
    ret:tag('tr')
:tag('th'):wikitext('Slot'):done()
        :tag('th'):wikitext('Slot'):done()
:tag('th'):attr('colspan', largestRowSize):wikitext('Item (most effective → least effective)'):done()
        :tag('th'):attr('colspan', largestRowSize):wikitext('Item (most effective → least effective)'):done()
   
   
local slotInputs = {}
    local slotInputs = {}
for _, slot in ipairs(SlotInfo) do
    for _, slot in ipairs(SlotInfo) do
local slotRow = {}
        local slotRow = {}
for i = 1, maxRowsPerSlot, 1 do
        for i = 1, maxRowsPerSlot, 1 do
local gear = args[slot.name .. i]
            local gear = args[slot.name .. i]
if(paramTest.has_content(gear)) then
            if(paramTest.has_content(gear)) then
table.insert(slotRow, gear)
                table.insert(slotRow, gear)
end
            end
end
        end
       
if(#slotRow > 0) then
        if(#slotRow > 0) then
ret:node(buildRow(slot, slotRow, largestRowSize))
            ret:node(buildRow(slot, slotRow, largestRowSize))
slotInputs[slot.name] = slotRow
            slotInputs[slot.name] = slotRow
end
        end
end
    end
   
local useSmw = yesNo(args.smw or '', true)
    local useSmw = yesNo(args.smw or '', true)
if(useSmw and onMain()) then
    if(useSmw and onMain() and smw) then
local jsonObject = { ['Recommended Equipment'] = slotInputs, style = args.style}
        local success, err = pcall(function()
local smwmap = {
            local jsonObject = { ['Recommended Equipment'] = slotInputs, style = args.style}
['Recommended Equipment JSON'] = mw.text.jsonEncode(jsonObject),
            local smwmap = {
}
                ['Recommended Equipment JSON'] = mw.text.jsonEncode(jsonObject),
mw.smw.set(smwmap)
            }
end
            smw.set(smwmap)
        end)
return ret
        if not success then
            mw.log('SMW error: ' .. (err or 'unknown error'))
        end
    end
   
    return ret
end
end


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


return p
return p

Latest revision as of 00:38, 3 May 2025

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

local p = {}

local paramTest = require('Module:Paramtest')
local onMain = require('Module:Mainonly').on_main
local yesNo = require('Module:Yesno')

-- Add error checking for SMW
local smw = mw.smw
if not smw then
    mw.log('SMW Lua bridge not available')
    smw = {
        set = function() return nil end  -- Stub function that does nothing
    }
end

local SlotInfo = {
    { name = 'head', icon = 'Head slot', txt = 'Head', link = 'Head slot table' },
    { name = 'neck', icon = 'Neck slot', txt = 'Neck', link = 'Neck slot table' },
    { name = 'cape', icon = 'Cape slot', txt = 'Back', link = 'Cape slot table' },
    { name = 'body', icon = 'Body slot', txt = 'Body', link = 'Body slot table' },
    { name = 'legs', icon = 'Legs slot', txt = 'Legs', link = 'Legs slot table' },
    { name = 'weapon', icon = 'Weapon slot', txt = 'Weapon', link = 'Weapon slot table' },
    { name = 'shield', icon = 'Shield slot', txt = 'Shield', link = 'Shield slot table' },
    { name = '2h', icon = '2h slot', txt = 'Two-handed weapon', link = 'Two-handed slot table' },
    { name = 'ammo', icon = 'Ammo slot', txt = 'Ammo/Spell', link = 'Ammunition slot table' },
    { name = 'hands', icon = 'Hands slot', txt = 'hands', link = 'Hand slot table' },
    { name = 'feet', icon = 'Feet slot', txt = 'Boots', link = 'Feet slot table' },
    { name = 'ring', icon = 'Ring slot', txt = 'Ring', link = 'Ring slot table' },
    { name = 'special', icon = 'Special attack orb', txt = 'Special attack', link = 'Special attacks' },
}

function buildRow(slot, rowData, largestRowSize)
    local row = mw.html.create('tr')
    row:tag('td'):wikitext('[[File:' .. slot.icon .. '.png|' .. slot.txt .. '|link=' .. slot.link .. ']]')
    for _, cellData in ipairs(rowData) do
        row:tag('td'):wikitext(cellData)
    end
    
    -- If there are any rows that empty compared to other rows fill up the remaining td's with N/As
    for i = 1, largestRowSize - #rowData, 1 do
        row:tag('td'):addClass('table-na'):wikitext('N/A')    
    end
    return row
end

function p._main(args)
    
    -- Tracking for colspan and N/A cells
    local largestRowSize = 0
    -- Intended as a hidden setting, use sparingly
    local maxRowsPerSlot = paramTest.default_to(args.rows, 5)
    
    -- Find the greatest row count
    for i, slot in ipairs(SlotInfo) do
        local slotRowSize = 0
        
        for i = 1, maxRowsPerSlot, 1 do
            if(paramTest.has_content(args[slot.name .. i])) then
                slotRowSize = i
            end
        end
        
        if(largestRowSize < slotRowSize) then
            largestRowSize = slotRowSize    
        end
        if(largestRowSize == maxRowsPerSlot) then
            break
        end
    end
    
    local ret = mw.html.create('table'):addClass('wikitable sticky-header')
    
    if(paramTest.has_content(args.style)) then
        ret:tag('caption'):wikitext('Recommended equipment for ' .. args.style)
    end
    
    ret:tag('tr')
        :tag('th'):wikitext('Slot'):done()
        :tag('th'):attr('colspan', largestRowSize):wikitext('Item (most effective → least effective)'):done()
    
    
    local slotInputs = {}
    for _, slot in ipairs(SlotInfo) do
        local slotRow = {}
        for i = 1, maxRowsPerSlot, 1 do
            local gear = args[slot.name .. i]
            if(paramTest.has_content(gear)) then
                table.insert(slotRow, gear)
            end
        end
        
        if(#slotRow > 0) then
            ret:node(buildRow(slot, slotRow, largestRowSize))
            slotInputs[slot.name] = slotRow
        end
    end
    
    local useSmw = yesNo(args.smw or '', true)
    if(useSmw and onMain() and smw) then
        local success, err = pcall(function()
            local jsonObject = { ['Recommended Equipment'] = slotInputs, style = args.style}
            local smwmap = {
                ['Recommended Equipment JSON'] = mw.text.jsonEncode(jsonObject),
            }
            smw.set(smwmap)
        end)
        if not success then
            mw.log('SMW error: ' .. (err or 'unknown error'))
        end
    end
    
    return ret
end

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

return p