Module:Attacks/Tables: Difference between revisions

From Melvor Idle
mNo edit summary
(Don't return table when number of attacks is zero)
Line 50: Line 50:


     -- Generate output table
     -- Generate output table
    if Shared.tableCount(spAttTable) == 0 then
    return ''
    end
   
     local resultPart = {}
     local resultPart = {}
     table.insert(resultPart, '{|class="wikitable sortable stickyHeader"')
     table.insert(resultPart, '{|class="wikitable sortable stickyHeader"')
Line 107: Line 111:


     -- Generate output table
     -- Generate output table
    if Shared.tableCount(spAttTable) == 0 then
    return ''
    end
     local resultPart = {}
     local resultPart = {}
     table.insert(resultPart, '{|class="wikitable sortable stickyHeader"')
     table.insert(resultPart, '{|class="wikitable sortable stickyHeader"')

Revision as of 01:39, 9 November 2021

Documentation for this module may be created at Module:Attacks/Tables/doc

local p = {}

local Constants require('Module:Constants')
local Shared = require('Module:Shared')
local Icons = require('Module:Icons')
local Items = require('Module:Items')
local Monsters = require('Module:Monsters')
local Attacks = require('Module:Attacks')

function p._getMonsterSpecialAttackTable(effectDefn)
    local spAttTable = {}
    local attacks = Attacks.getAttacks(function(attack)
                                           if effectDefn == nil then
                                               return true
                                           else
                                               return Attacks.attackHasEffect(attack, effectDefn)
                                           end
                                       end)

    -- Compile table of monsters & chances for each attack
    for i, spAtt in ipairs(attacks) do
        if Shared.tableCount(spAtt.monsters) > 0 then
            spAttTable[i] = {}

            for j, monsterID in ipairs(spAtt.monsters) do
                local monster = Monsters.getMonsterByID(monsterID)
                local overrideChance = (monster.overrideSpecialChances ~= nil and Shared.tableCount(monster.overrideSpecialChances) > 0)
                local attChance = spAtt.defaultChance
                if overrideChance then
                    local attIdx = nil
                    for k, monsterAttack in ipairs(monster.specialAttacks) do
                        local attID = (type(monsterAttack) == 'table' and monsterAttack.id) or monsterAttack
                        if spAtt.id == attID then
                            attIdx = k
                            break
                        end
                    end
                    if attIdx ~= nil then
                        attChance = monster.overrideSpecialChances[attIdx]
                    end
                end

                if spAttTable[i][attChance] == nil then
                    spAttTable[i][attChance] = {}
                end
                table.insert(spAttTable[i][attChance], Icons.Icon({ monster.name, type = 'monster' }))
            end
        end
    end

    -- Generate output table
    if Shared.tableCount(spAttTable) == 0 then
    	return ''
    end
    
    local resultPart = {}
    table.insert(resultPart, '{|class="wikitable sortable stickyHeader"')
    table.insert(resultPart, '\r\n|- class="headerRow-0"')
    table.insert(resultPart, '\r\n!Name!!style="min-width:225px"|Monsters!!Chance!!Effect')

    for i, spAttData in Shared.skpairs(spAttTable) do
        local spAtt = attacks[i]
        local firstRow = true
        local rowsSpanned = Shared.tableCount(spAttData)
        local rowSuffix = ''
        if rowsSpanned > 1 then
            rowSuffix = '|rowspan="' .. rowsSpanned .. '"'
        end
        for chance, iconList in Shared.skpairs(spAttData) do
            table.insert(resultPart, '\r\n|-')
            if firstRow then
                table.insert(resultPart, '\r\n' .. rowSuffix .. '| ' .. spAtt.name)
            end
            table.insert(resultPart, '\r\n|data-sort-value="' .. spAtt.name .. '"| ' .. table.concat(iconList, '<br/>'))
            table.insert(resultPart, '\r\n|data-sort-value="' .. chance .. '"| ' .. Shared.round(chance, 2, 0) .. '%')
            if firstRow then
                table.insert(resultPart, '\r\n' .. rowSuffix .. '| ' .. spAtt.description.monster)
                firstRow = false
            end
        end
    end
    table.insert(resultPart, '\r\n|}')

    return table.concat(resultPart)
end

function p._getItemSpecialAttackTable(effectDefn)
  local spAttTable = {}
  local attacks = Attacks.getAttacks(function(attack)
                                         if effectDefn == nil then
                                             return true
                                         else
                                             return Attacks.attackHasEffect(attack, effectDefn)
                                         end
                                     end)

    -- Compile table of items for each attack
    for i, spAtt in ipairs(attacks) do
        if Shared.tableCount(spAtt.items) > 0 then
            spAttTable[i] = { Icons = {} }

            for j, itemID in ipairs(spAtt.items) do
                local item = Items.getItemByID(itemID)
                if spAttTable[i]['sortName'] == nil then
                    spAttTable[i]['sortName'] = item.name
                end
                table.insert(spAttTable[i]['Icons'], Icons.Icon({ item.name, type = 'item' }))
            end
        end
    end

    -- Generate output table
    if Shared.tableCount(spAttTable) == 0 then
    	return ''
    end

    local resultPart = {}
    table.insert(resultPart, '{|class="wikitable sortable stickyHeader"')
    table.insert(resultPart, '\r\n|- class="headerRow-0"')
    table.insert(resultPart, '\r\n!style="min-width:100px"|Name!!style="min-width:180px"|Weapon(s)!!Chance!!Effect')

    for i, spAttData in Shared.skpairs(spAttTable) do
        local spAtt = attacks[i]
        table.sort(spAttData.Icons, function(a, b) return a < b end)

        table.insert(resultPart, '\r\n|-')
        table.insert(resultPart, '\r\n| ' .. spAtt.name)
        table.insert(resultPart, '\r\n|data-sort-value="' .. spAttData.sortName .. '"| '  .. table.concat(spAttData.Icons, '<br/>'))
        table.insert(resultPart, '\r\n|data-sort-value="' .. spAtt.defaultChance .. '"| ' .. spAtt.defaultChance .. '%')
        table.insert(resultPart, '\r\n| ' .. spAtt.description.player)
    end
    table.insert(resultPart, '\r\n|}')

    return table.concat(resultPart)
end

function p.getSpecialAttackTable(frame)
    local args = frame.args ~= nil and frame.args or frame
    local tableType = args[1]
    local effectName = args[2]
    local effectDefn = nil

    if effectName ~= nil and effectName ~= '' then
        effectDefn = Attacks.effectDefinition[effectName]
        if effectDefn == nil then
            local validEffectNames = {}
            for k, v in pairs(Attacks.effectDefinition) do
                table.insert(validEffectNames, k)
            end
            table.sort(validEffectNames, function(a, b) return a < b end)

            return 'ERROR: Invalid effect name "' .. effectName .. '", must be one of: ' .. table.concat(validEffectNames, ', ') .. '[[Category:Pages with script errors]]'
        end
    end

    if tableType == 'Monster' then
        return p._getMonsterSpecialAttackTable(effectDefn)
    elseif tableType == 'Item' then
        return p._getItemSpecialAttackTable(effectDefn)
    else
        return 'ERROR: Invalid table type, must be one of: Item, Monster[[Category:Pages with script errors]]'
    end
end

return p