Module:EncounterTable

From Caves of Qud Wiki
Revision as of 17:12, 12 November 2019 by Teamtoto (talk | contribs) (Created page with "local p = {} function p.splitArgs(input, fieldlist, sep) if not input or input == '' then return end sep = sep or '%s*;;;%s*' local result = {} local inputTbl...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

local p = {}

function p.splitArgs(input, fieldlist, sep)
    if not input or input == '' then return end
    sep = sep or '%s*;;;%s*'
    local result = {}
    local inputTbl = util_text.split(input,sep)
    for i, v in ipairs(fieldlist) do
        if not inputTbl[i] then
            error(('Missing parameter %s - maybe wrong child template?'):format(v))
        end
        if inputTbl[i] ~= '' then 
            result[v] = inputTbl[i]
        end
    end
    return result
end

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


function p.main(frame)
 local result = {}
 local totalWeight = 0
 for i, row in frame.args do
  local newrow = p.splitEncounterTableArgs(row)
  result[i] = newrow
  totalWeight = totalWeight + tonumber(newrow['weight'])
 end

--second pass to calculate chance--
  for i, row in result do
    result['chance'] = row['weight']/totalWeight
  end
--format table. 
  return p.formatTable(result)
end

function p.formatTable(table)
--format the full table. only item OR table can be defined at once.
  return "place holder"
end

return p