Module:EncounterTable: Difference between revisions

From Caves of Qud Wiki
Jump to navigation Jump to search
(add favilink support?)
(add option to hide quanitty)
Line 8: Line 8:
--# Interface
--# Interface


function EncounterTable.splitEncounterTableArgs(row)
function EncounterTable.splitEncounterTableArgs(row, hideQuantity)
     local TABLE_ARGS = {'table', 'item', 'quantity', 'weight'}
     local TABLE_ARGS = {}
    if hideQuantity then
        TABLE_ARGS = {'table', 'item', 'weight'}
    else
        TABLE_ARGS = {'table', 'item', 'quantity', 'weight'}
    end
     return TextUtility.splitArgs(row, TABLE_ARGS)
     return TextUtility.splitArgs(row, TABLE_ARGS)
end
end
Line 21: Line 26:
     local pick = args.roll or 'once'
     local pick = args.roll or 'once'
     local bFavilinkId = false
     local bFavilinkId = false
    local bHideQuantity = (args.hidequantity ~= nil)
     if args.favilinkid == 'yes' then bFavilinkId = true end
     if args.favilinkid == 'yes' then bFavilinkId = true end
     local bSamePageLink = false
     local bSamePageLink = false
Line 69: Line 75:


         -- dice tooltip quantity
         -- dice tooltip quantity
 
        if not bHideQuantity then
        finalTable[i].quantity = frame:expandTemplate{
            finalTable[i].quantity = frame:expandTemplate{
            title = 'Dice tooltip',
                title = 'Dice tooltip',
            args = {row.quantity},
                args = {row.quantity},
        }
            }
 
        end
         --finalTable[i].quantity = row.quantity
         --finalTable[i].quantity = row.quantity
         finalTable[i].weight = row.weight
         finalTable[i].weight = row.weight
Line 95: Line 101:
     local TABLE_HEADER = '<tr><th>Item</th><th>Quantity</th><th>Weight</th><th>Chance</th></tr>'
     local TABLE_HEADER = '<tr><th>Item</th><th>Quantity</th><th>Weight</th><th>Chance</th></tr>'
     local tableRows = ''
     local tableRows = ''
 
    local headerhtml = ''
    if bHideQuantity then
        headerhtml = ('<tr><td>%s</td><td>%s</td><td>%s</td></tr>'):format(entry.item, entry.weight, entry.chance)
    else
        headerhtml = ('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>'):format(entry.item, entry.quantity, entry.weight, entry.chance)
    end
     for _, entry in ipairs(final) do
     for _, entry in ipairs(final) do
         tableRows = tableRows .. ('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>'):format(entry.item, entry.quantity, entry.weight, entry.chance)
         tableRows = tableRows .. headerhtml
     end
     end



Revision as of 04:34, 20 November 2021


local EncounterTable = {}

--# Requires

local ProcessArgs = require'Module:ProcessArgs'
local TextUtility = require'Module:Text Utility'

--# Interface

function EncounterTable.splitEncounterTableArgs(row, hideQuantity)
    local TABLE_ARGS = {}
    if hideQuantity then
         TABLE_ARGS = {'table', 'item', 'weight'}
    else
         TABLE_ARGS = {'table', 'item', 'quantity', 'weight'}
    end
    return TextUtility.splitArgs(row, TABLE_ARGS)
end


function EncounterTable.start(frame)
    local favilink = require 'Module:Favilink'

    local args = ProcessArgs.merge(true)
    local tblName = args.name or ''
    local pick = args.roll or 'once'
    local bFavilinkId = false
    local bHideQuantity = (args.hidequantity ~= nil)
    if args.favilinkid == 'yes' then bFavilinkId = true end
    local bSamePageLink = false
    if args.samepage == 'yes' then bSamePageLink = true end
    local result = {}
    local totalWeight = 0

    for i, row in ipairs(args) do
        local newrow = EncounterTable.splitEncounterTableArgs(row)
        result[i] = newrow
        totalWeight = totalWeight + tonumber(newrow.weight)
    end
 
    -- format table. 

    local finalTable = {}
    local str = ''

    for i, row in ipairs(result) do
        finalTable[i] = {}

        if row.table == 'none' then
            if row.item == 'none' then
                error'A table or item must be specified!'
            else
                if bFavilinkId then
                    finalTable[i].item = favilink.modulefavilink(row.item, 'ObjectID')
                else
                    finalTable[i].item = '[[' .. row.item .. ']]'
                end
            end
        elseif row.item == 'none' then 
            local linkstr = ''
            maintable = row.table:match'^(.+)%s+%w*$'
            if bSamePageLink then
                maintable = mw.title.getCurrentTitle().text
                linkstr = maintable .. '#' .. row.table
            elseif maintable ~= nil and maintable ~= '' then
                linkstr = maintable .. '#' .. row.table
            else
                linkstr = row.table
            end

            finalTable[i].item = 'Item from [[Data:' .. linkstr  .. '|' .. row.table .. ']]'
        else
            error'A table and item cannot be specified at the same time!'
        end

        -- dice tooltip quantity
        if not bHideQuantity then
            finalTable[i].quantity = frame:expandTemplate{
                title = 'Dice tooltip',
                args = {row.quantity},
            }
        end
        --finalTable[i].quantity = row.quantity
        finalTable[i].weight = row.weight

        -- calculate final chance

        if pick == 'once' then
            finalTable[i].chance = ('%.2f%%'):format(row.weight * 100 / totalWeight)
        elseif pick == 'each' then
            finalTable[i].chance = ('%.2f%%'):format(row.weight)
        else
            error'"roll" parameter takes only "once" or "each"!'
        end
    end

    return EncounterTable.formatTable(finalTable)
end

function EncounterTable.formatTable(final)
    local TABLE_HEADER = '<tr><th>Item</th><th>Quantity</th><th>Weight</th><th>Chance</th></tr>'
    local tableRows = ''
    local headerhtml = ''
    if bHideQuantity then
         headerhtml = ('<tr><td>%s</td><td>%s</td><td>%s</td></tr>'):format(entry.item, entry.weight, entry.chance)
    else
         headerhtml = ('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>'):format(entry.item, entry.quantity, entry.weight, entry.chance)
    end
    for _, entry in ipairs(final) do
        tableRows = tableRows .. headerhtml
    end

    return ('<table class="wikitable sortable">%s%s</table>'):format(TABLE_HEADER, tableRows)
end

return EncounterTable