(revert) |
m (switch to Module:ProcessArgs) |
||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
− | local argSpecified = require('Module:Util').argSpecified |
||
local tableColors = { |
local tableColors = { |
||
Line 33: | Line 32: | ||
currentPool.rolls = args['rolls'..poolNumber] |
currentPool.rolls = args['rolls'..poolNumber] |
||
− | if |
+ | if args['bonusrolls'..poolNumber] then |
currentPool.bonusRolls = args['bonusrolls'..poolNumber] |
currentPool.bonusRolls = args['bonusrolls'..poolNumber] |
||
else |
else |
||
Line 39: | Line 38: | ||
end |
end |
||
− | if |
+ | if args['notes'..poolNumber] then |
currentPool.poolNotes = args['notes'..poolNumber] |
currentPool.poolNotes = args['notes'..poolNumber] |
||
end |
end |
||
Line 236: | Line 235: | ||
function p.lootTable(frame) |
function p.lootTable(frame) |
||
− | + | local args = frame |
|
+ | if frame == mw.getCurrentFrame() then |
||
− | args = frame:getParent().args |
||
+ | args = require('Module:ProcessArgs').merge( true ) |
||
+ | else |
||
+ | frame = mw.getCurrentFrame() |
||
+ | end |
||
local numberColumns = 4 |
local numberColumns = 4 |
Revision as of 01:50, 3 June 2021
This module implements Template:LootTable.
local p = {}
local tableColors = {
'#EEEEEE',
'#feefcd',
'#e8f3d8',
'#eae9e1',
'#f9e6ff',
'#f6ffcc',
'#e6ffee',
'#ffe6e6',
'#e6fcff',
'#f2f2f2'
}
-- parse actual loot info; currently supports up to 10 "pool=" arguments, but number can be increased if needed
local function parseLoot(args)
local allPools = {} -- stores each pool as a nested table
local poolWeights = {} -- stores weights for each pool separately
-- determine if these columns have content and should show
allPools.hasNotes = false
allPools.hasLevel = false
allPools.hasXP = false
for poolNumber=1, 10 do
local poolArg = args['pool'..poolNumber]
if poolArg then
local currentPool = {}
currentPool.items = {}
currentPool.poolNumber = poolNumber
currentPool.rolls = args['rolls'..poolNumber]
if args['bonusrolls'..poolNumber] then
currentPool.bonusRolls = args['bonusrolls'..poolNumber]
else
currentPool.bonusRolls = '0'
end
if args['notes'..poolNumber] then
currentPool.poolNotes = args['notes'..poolNumber]
end
poolArg = poolArg .. '\n' -- allow last line to be matched like the rest
-- parse input of pool parameter
for line in mw.ustring.gmatch(poolArg, '[^\r\n]+[\r\n]') do -- split on newline
--local splitLine = {}
local currentItem = {}
for key, value in mw.ustring.gmatch(line, '([%a]+)%s*:%s*(.-)%s*[;\r\n]') do
if value ~= '' then
currentItem[key:lower()] = value
end
end
-- convert weight to number; becomes nil if conversion fails
if currentItem.weight then
currentItem.weight = tonumber(currentItem.weight)
end
-- if converted to number successfully
if currentItem.weight then
if not poolWeights[poolNumber] then
poolWeights[poolNumber] = currentItem.weight
else
poolWeights[poolNumber] = poolWeights[poolNumber] + currentItem.weight
end
end
if currentItem.level then
allPools.hasLevel = true
end
if currentItem.xp then
allPools.hasXP = true
end
if currentItem.notes then
allPools.hasNotes = true
end
table.insert(currentPool.items, currentItem)
end
table.insert(allPools, currentPool)
end
end
return allPools, poolWeights
end
-- create output for parsed input
local function createLootOutput(allPools, poolWeights, numberColumns, hasLuckRolls)
local output = {}
for _, currentPool in ipairs(allPools) do
local poolNumber = currentPool.poolNumber
local color = tableColors[poolNumber]
if not color then
color = ''
else
color = 'background-color:' .. color .. ';'
end
for _, currentItem in ipairs(currentPool.items) do
local itemLink = ''
if currentItem.item == nil or mw.ustring.lower(currentItem.item) == 'nothing' then
itemLink = 'Nothing'
else
local page = currentItem.item
local itemName = currentItem.item
-- strip "mcw:" from imagelink and displayed name, while allowing article link to work normally
if itemName:sub(1, 4) == 'mcw:' then
itemName = itemName:sub(5)
end
local image = itemName .. '.png'
if itemName ~= "Gold Trophy" and itemName ~= "Ornate Trophy" and itemName:find('Trophy') then
image = 'Trophy.png'
page = 'Trophy'
end
if currentItem.image then
image = currentItem.image
end
local imagesize = currentItem.imagesize or '32px'
local imageLink = '[[File:' .. image .. '|' .. imagesize .. '|link=' .. page .. ']] '
if currentItem.image and currentItem.image:lower() == 'none' then
imageLink = ''
end
itemLink = imageLink .. '[[' .. page .. '|' .. itemName .. ']]'
end
local quantity = currentItem.quantity or '–'
local looting
if hasLuckRolls then
if currentItem.looting then
looting = '+' .. currentItem.looting .. ' per level'
else
looting = '–'
end
end
local chance
if currentItem.weight and poolWeights[poolNumber] then -- weight exists and is not 0
chance = string.format("%.1f", (currentItem.weight / poolWeights[poolNumber]) * 100) .. '%'
else
chance = '–'
end
local level = ''
if currentItem.level then
level = ' || ' .. currentItem.level
elseif allPools.hasLevel then
level = ' || '
end
local xp = ''
if currentItem.xp then
xp = ' || ' .. currentItem.xp
elseif allPools.hasXP then
xp = ' || '
end
local notes = ''
if currentItem.notes then
notes = ' || style="text-align:left;" | ' .. currentItem.notes
elseif allPools.hasNotes then
notes = ' || '
end
table.insert(output, '|- style="text-align:center;' .. color .. '"')
if hasLuckRolls then
table.insert(output, '| ' .. itemLink .. ' || ' .. quantity .. ' || '
.. looting .. ' || ' .. chance .. level .. xp .. notes)
else
table.insert(output, '| ' .. itemLink .. ' || ' .. quantity .. ' || '
.. chance .. level .. xp .. notes)
end
end
-- if either rolls or notes is specified, create a new row for it at the bottom of the pool
if currentPool.rolls or currentPool.poolNotes then
if color == '' then
table.insert(output, '|-')
else
table.insert(output, '|- style="' .. color .. '"')
end
local poolText = '| colspan = ' .. numberColumns .. ' | '
-- display info about pool rolls
if currentPool.rolls then
poolText = poolText .. 'The above pool is rolled '
if currentPool.rolls == '1' then
poolText = poolText .. '1 time'
else
poolText = poolText .. currentPool.rolls .. ' times'
end
if currentPool.bonusRolls ~= '0' then
poolText = poolText .. ', with an additional '
if currentPool.bonusRolls == '1' then
poolText = poolText .. 'roll per point of Luck'
else
poolText = poolText .. currentPool.bonusRolls .. ' rolls per point of Luck'
end
end
poolText = poolText .. '.'
end
-- display additional notes about the entire pool
if currentPool.poolNotes then
if currentPool.rolls then
poolText = poolText .. '<br>'
end
poolText = poolText .. currentPool.poolNotes
end
table.insert(output, poolText)
end
end
return table.concat(output, '\n')
end
function p.lootTable(frame)
local args = frame
if frame == mw.getCurrentFrame() then
args = require('Module:ProcessArgs').merge( true )
else
frame = mw.getCurrentFrame()
end
local numberColumns = 4
local output = {} -- list of strings to be concatenated into final output
local allPools, poolWeights = parseLoot(args)
local title = ''
local thirdColumn = ''
local hasLuckRolls = true
if args.type == 'chest' then
title = 'Chest loot'
hasLuckRolls = false
numberColumns = numberColumns - 1
elseif args.type == 'generic' then
title = 'Rewards'
hasLuckRolls = false
numberColumns = numberColumns - 1
else
title = 'Unique drops'
thirdColumn = frame:expandTemplate{title = 'tooltip', args = {'Looting', 'The amount of extra items obtainable with the Looting enchantment.'}}
end
if args.title then
title = args.title
end
if args.otherlooting then
thirdColumn = args.otherlooting
end
local columns = '|-\n! Item !! Quantity !! '
if hasLuckRolls then
columns = columns .. thirdColumn .. ' !! '
end
columns = columns .. 'Chance'
if allPools.hasLevel then
columns = columns .. ' !! Level required'
numberColumns = numberColumns + 1
end
if allPools.hasXP then
columns = columns .. ' !! XP'
numberColumns = numberColumns + 1
end
if allPools.hasNotes then
columns = columns .. ' !! Notes'
numberColumns = numberColumns + 1
end
-- begin creating final output
table.insert(output, '{| class = "wikitable"\n|-')
table.insert(output, '! colspan = ' .. numberColumns .. ' | ' .. title)
table.insert(output, columns .. '\n|-')
lootOutput = createLootOutput(allPools, poolWeights, numberColumns, hasLuckRolls)
table.insert(output, lootOutput)
table.insert(output, '|}')
return table.concat(output, '\n')
end
return p;