Module:EncounterTable: Difference between revisions

From Caves of Qud Wiki
Jump to navigation Jump to search
(reformatted chance to be a percent)
(change data namespace to Population)
 
(26 intermediate revisions by 3 users not shown)
Line 1: Line 1:
local p = {}
local EncounterTable = {}
local process_args = require('Module:ProcessArgs')


--a lot taken from lol gamepedia
--# Requires
function p.gsplit( text, pattern, plain )
 
if not pattern then pattern = '%s*,%s*' end
local ProcessArgs = require'Module:ProcessArgs'
local s, l = 1, text:len()
local TextUtility = require'Module:Text Utility'
return function ()
 
if s then
--# Interface
local e, n = text:find( pattern, s, plain )
 
local ret
function EncounterTable.splitEncounterTableArgs(row, hideQuantity)
if not e then
    local TABLE_ARGS = {}
ret = text:sub( s )
    if hideQuantity then
s = nil
        TABLE_ARGS = {'table', 'item', 'weight'}
elseif n < e then
    else
-- Empty separator!
        TABLE_ARGS = {'table', 'item', 'quantity', 'weight'}
ret = text:sub( s, e )
    end
if e < l then
    return TextUtility.splitArgs(row, TABLE_ARGS)
s = e + 1
else
s = nil
end
else
ret = e > s and text:sub( s, e - 1 ) or ''
s = n + 1
end
return ret
end
end, nil, nil
end
end


function p.split(text, pattern, plain)
if not text then
return {}
end
local ret = {}
for m in p.gsplit(text, pattern, plain) do
ret[#ret+1] = m
end
return ret
end


function p.splitArgs(input, fieldlist, sep)
function EncounterTable.start(frame)
     if not input or input == '' then return end
    local favilink = require 'Module:Favilink'
     sep = sep or '%s*;;;%s*'
 
    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 result = {}
     local inputTbl = p.split(input,sep)
     local totalWeight = 0
     for i, v in ipairs(fieldlist) do
 
         if not inputTbl[i] then
    for i, row in ipairs(args) do
             error(('Missing parameter %s - maybe wrong child template?'):format(v))
        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 [[Population:' .. linkstr  .. '|' .. row.table .. ']]'
        else
            error'A table and item cannot be specified at the same time!'
         end
         end
         if inputTbl[i] ~= '' then  
 
             result[v] = inputTbl[i]
        -- 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
     end
     end
    return result
end


function p.splitEncounterTableArgs(row)
     return EncounterTable.formatTable(finalTable, bHideQuantity)
    local TABLE_ARGS = { 'table', 'item', 'quantity', 'weight' }
     return p.splitArgs(row, TABLE_ARGS)
end
end


function EncounterTable.formatTable(final, bHideQuantity)
    local TABLE_HEADER = bHideQuantity and
'<tr><th>Item</th><th>Weight</th><th>Chance</th></tr>' or
'<tr><th>Item</th><th>Quantity</th><th>Weight</th><th>Chance</th></tr>'
    local tableRows = ''
    local headerhtml = ''


function p.start(frame)
    for _, entry in ipairs(final) do
args = process_args.merge(true)
          if bHideQuantity then
local tblName = args['name'] or ''
              tableRows = tableRows .. ('<tr><td>%s</td><td>%s</td><td>%s</td></tr>'):format(entry.item, entry.weight, entry.chance)
local result = {}
          else
local totalWeight = 0
              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)
for i, row in ipairs(args) do
          end
  local newrow = p.splitEncounterTableArgs(row)
    end
  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
        finalTable[i]['item'] = '[[' .. row['item'] .. ']]'
      end
    elseif row["item"] == 'none' then
      finalTable[i]['item'] = 'Item from [[EncounterTable:' .. row['table'] .. '|' .. row['table'] .. ']]'
    else
      error('A table and item cannot be specified at the same time!')
    end
    -- dice tooltip quantity
    finalTable[i]['quantity'] = frame:expandTemplate{title='Dice tooltip', args={row['quantity']}}
    --finalTable[i]['quantity'] = row['quantity']
    finalTable[i]['weight'] = row['weight']
  -- calculate final chance
    finalTable[i]['chance'] = string.format("%.2f%%",row['weight']*100/totalWeight)
  end
  return (p.formatTable(finalTable))
end


function p.formatTable(final)
     return ('<table class="wikitable sortable">%s%s</table>'):format(TABLE_HEADER, tableRows)
  local tblheader = '<tr><th>Item</th><th>Quantity</th><th>Weight</th><th>Chance</th></tr>'
  local tr = ''
  for _, v in ipairs(final) do
        tr = tr ..  ' <tr><td>' .. v['item'] .. '</td><td>' .. v['quantity'] .. '</td><td>' .. v['weight'] .. '</td><td>' .. v['chance'] .. '</td></tr>'
  end
     return '<table class="wikitable">' .. tblheader  .. tr .. '</table>'
end
end


return p
return EncounterTable

Latest revision as of 13:42, 20 January 2023


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 [[Population:' .. 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, bHideQuantity)
end

function EncounterTable.formatTable(final, bHideQuantity)
    local TABLE_HEADER = bHideQuantity and 
'<tr><th>Item</th><th>Weight</th><th>Chance</th></tr>' or 
'<tr><th>Item</th><th>Quantity</th><th>Weight</th><th>Chance</th></tr>'
    local tableRows = ''
    local headerhtml = ''

    for _, entry in ipairs(final) do
          if bHideQuantity then
              tableRows = tableRows .. ('<tr><td>%s</td><td>%s</td><td>%s</td></tr>'):format(entry.item, entry.weight, entry.chance)
          else
              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)
          end
    end

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

return EncounterTable