Module:PrizePool
From TwogPedia
Documentation for this module may be created at Module:PrizePool/doc
local getArgs = require('Module:Arguments').getArgs
local PrizePool = {}
local funcs = require('Module:Functions')
local getTeamLogo = require('Module:Functions').getTeamLogo
local getTeamDisplaytitle = funcs.getTeamDisplaytitle
local VariablesLua = mw.ext.VariablesLua
local cargo = mw.ext.cargo
function PrizePool.main(frame)
local args = getArgs(frame)
local tableContainer = mw.html.create('table'):addClass('striped-table')
local titleRow = mw.html.create('tr')
titleRow:addClass('headerRow')
local placementCell = mw.html.create('th'):wikitext('Place')
titleRow:node(placementCell)
local currency = args.currency and string.upper(args.currency) or nil
if args.currency then titleRow:node(mw.html.create('th'):wikitext(currency)) end
local usdCell = mw.html.create('th'):wikitext('USD')
local participantCell = mw.html.create('th'):wikitext('Team')
tableContainer:node(titleRow:node(usdCell))
-- If prize percentage
if args.percent then
titleRow:node(mw.html.create('th'):wikitext('Percent'))
end
titleRow:node(participantCell):node(joindateCell)
local columns = args.columns and mw.text.split(args.columns, ',') or nil
if columns then
for i = 1, #columns do
titleRow:node(mw.html.create('th'):wikitext(columns[i]))
i = i + 1
end
end
local i = 1
-- non-USD currency things
local conversionRate = args.rate or 1
local startDate = VariablesLua.varexists('startDate') and VariablesLua.var('startDate', nil) or nil
local endDate = VariablesLua.varexists('endDate') and VariablesLua.var('endDate', nil) or nil
local currentDate = os.date("%Y-%m-%d", os.time())
local date = os.date(endDate or startDate) > os.date(currentDate) and currentDate or (endDate or startDate)
local placeOffset = 0
while (args[i] ~= nil) do
local row = mw.text.jsonDecode(args[i])
local prizeString = 'TBD'
local prizeInt
if args.prize and row.percent and not row.prize then
prizeString = args.prize:gsub(",", "")
prizeInt = math.floor(tonumber(prizeString) / 100 * tonumber(row.percent))
elseif row.prize then
prizeString = row.prize:gsub(",", "")
prizeInt = tonumber(prizeString)
end
local bgClass = row.bg or nil
if not bgClass and (i == 1 and not row.place) or (row.place and string.sub(row.place, 1, string.len('1-')) == '1-') then
bgClass = 'gold'
elseif not bgClass and (i == 2 and not row.place) or (row.place and string.sub(row.place, 1, string.len('2-')) == '2-') then
bgClass = 'silver'
elseif not bgClass and (i == 3 and not row.place) or (row.place and string.sub(row.place, 1, string.len('3-')) == '3-') then
bgClass = 'bronze'
end
local iconText = ''
if bgClass == 'gold' then
iconText = '[[File:Gold-icon.png|15px|alt=Gold Medal]]'
elseif bgClass == 'silver' then
iconText = '[[File:Silver-icon.png|15px|alt=Silver Medal]]'
elseif bgClass == 'bronze' then
iconText = '[[File:Bronze-icon.png|15px|alt=Bronze Medal]]'
end
local rowSpan = 1
if row.place then
local placeSplit = mw.text.split(row.place, '-')
if #placeSplit > 1 then
rowSpan = tonumber(placeSplit[2]) - tonumber(placeSplit[1]) + 1
end
placeOffset = placeOffset + rowSpan - 1
end
local tableRow = mw.html.create('tr'):addClass('bodyRow'):addClass(bgClass)
local placement = mw.html.create('td')
if bgClass == 'gold' or bgClass == 'silver' or bgClass == 'bronze' then
placement:wikitext(iconText)
else
placement:wikitext(row.place or i + placeOffset)
end
tableRow:node(placement)
local rowUSD = mw.html.create('td')
if args.currency then
local currencyCell = mw.html.create('td'):wikitext('$' .. prizeToString(prizeInt))
rowUSD:wikitext('$' .. (conversionRate and prizeToString(math.floor(conversionRate * prizeInt)) or 'TBD'))
if row.place then currencyCell:attr('rowspan', rowSpan) end
tableRow:node(currencyCell)
else
rowUSD:wikitext('$' .. prizeToString(prizeInt))
end
tableRow:node(rowUSD)
-- If table has percent and row has percent
if args.percent and row.percent then
tableRow:node(mw.html.create('td'):attr('rowspan', rowSpan):wikitext(row.percent .. '%'))
end
if row.place then
placement:attr('rowspan', rowSpan)
rowUSD:attr('rowspan', rowSpan)
end
tableContainer:node(tableRow)
if row.participants then
local participants = mw.text.split(row.participants, ',')
local currentTitle = mw.title.getCurrentTitle().text
local gameCategory = mw.text.split(currentTitle, '/')[1]
for j = 1, #participants do
-- Check if shorthand of a team was used
local tables = 'Teams'
local fields = '_pageName, shorthand'
local cargoArgs = {
where = '_pageName LIKE "' .. gameCategory .. '/%" AND shorthand = "' .. participants[j] .. '"'
}
local results = cargo.query(tables, fields, cargoArgs)
if #results > 0 then participants[j] = results[1]._pageName else participants[j] = gameCategory .. '/' .. mw.text.trim(participants[j]) end
j = j + 1
end
if #participants > 0 then
local players = nil
for j = 1, #participants do
-- If a team tournament, then go through teams to get lists of their players to enter into the prize table
if args.solo == nil then
local tables = 'Participants'
local fields = 'players'
local cargoArgs = {
where = '_pageName="' .. currentTitle .. '" AND team = "' .. participants[j] .. '"'
}
local results = cargo.query(tables, fields, cargoArgs)
if #results > 0 then players = results[1].players end
end
if j == 1 then
-- local participantCell = mw.html.create("td"):wikitext('[[' .. participants[1] .. '|' .. getTeamDisplaytitle(participants[1], VariablesLua.var('endDate')) .. ']]')
-- tableRow:node(participantCell)
local participantCell = mw.html.create("td")
local teamContainer = mw.html.create("div"):attr("style", "display: flex; align-items: center; gap: 10px;")
local teamLogo = getTeamLogo(participants[1], gameCategory, "25px")
local teamName = mw.html.create("span"):wikitext("[[" .. participants[1] .. "|" .. getTeamDisplaytitle(participants[1], VariablesLua.var("endDate")) .. "]]")
teamContainer:node(teamLogo):node(teamName)
participantCell:node(teamContainer)
tableRow:node(participantCell)
else
local extraRow = mw.html.create('tr'):addClass('bodyRow'):addClass(bgClass)
extraRow:node(mw.html.create('td'):wikitext('[[' .. participants[j] .. '|' .. getTeamDisplaytitle(participants[j], VariablesLua.var('endDate')) .. ']]'))
tableContainer:node(extraRow)
end
end
-- Save prize to db
frame:callParserFunction{name = '#cargo_store:', args = {_table = 'Prizes', placement = row.place or i, prize = args.currency and conversionRate * prizeInt or prizeInt, localPrize = args.currency and prizeInt or nil, currency = currency, teams = not args.solo and table.concat(participants, ','), players = args.solo and table.concat(participants, ',') or players}}
end
else
for j = 1, rowSpan do
if j == 1 then
-- local participantCell = mw.html.create("td"):wikitext('TBD')
-- tableRow:node(participantCell)
local participantCell = mw.html.create("td")
local teamContainer = mw.html.create("div"):attr("style", "display: flex; align-items: center; gap: 10px;")
local teamLogo, teamName
if participants and participants[1] then
teamLogo = getTeamLogo(participants[1], gameCategory, "25px")
teamName = mw.html.create("span"):wikitext("[[" .. participants[1] .. "|" .. getTeamDisplaytitle(participants[1], VariablesLua.var("endDate")) .. "]]")
else
teamLogo = ''
teamName = mw.html.create("span"):wikitext("TBD")
end
teamContainer:node(teamLogo):node(teamName)
participantCell:node(teamContainer)
tableRow:node(participantCell)
else
local extraRow = mw.html.create('tr'):addClass('bodyRow')
extraRow:node(mw.html.create('td'):wikitext('TBD'))
tableContainer:node(extraRow)
end
j = j + 1
end
end
if columns then
local rowColumns = row.columns and mw.text.split(row.columns, ',') or nil
if rowColumns then
for j = 1, #rowColumns do
tableRow:node(mw.html.create('td'):attr('rowspan', rowSpan):wikitext(rowColumns[j]))
j = j + 1
end
else
for j = 1, #columns do
tableRow:node(mw.html.create('td'):attr('rowspan', rowSpan):wikitext('-'))
j = j + 1
end
end
end
i = i + 1
end
return tableContainer
end
function prizeToString(nr)
if nr == nil then return 'TBD' end
local prize = ''
local reversed = string.reverse(tostring(nr))
for k = 1, #reversed do
prize = reversed:sub(k, k) .. prize
if k % 3 == 0 and k < #reversed then prize = ',' .. prize end
k = k + 1
end
return prize
end
return PrizePool
No categories