Module:PrizePool: Difference between revisions

From TwogPedia
(Created page with "local getArgs = require('Module:Arguments').getArgs local PrizePool = {} 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...")
 
mNo edit summary
Line 19: Line 19:
local participantCell = mw.html.create('th'):wikitext('Participant')
local participantCell = mw.html.create('th'):wikitext('Participant')
tableContainer:node(titleRow:node(usdCell):node(participantCell):node(joindateCell))
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
local columns = args.columns and mw.text.split(args.columns, ',') or nil
Line 85: Line 90:
if row.place then  
if row.place then  
local placeSplit = mw.text.split(row.place, '-')
local placeSplit = mw.text.split(row.place, '-')
rowSpan = tonumber(placeSplit[2]) - tonumber(placeSplit[1]) + 1
if #placeSplit > 1 then
rowSpan = tonumber(placeSplit[2]) - tonumber(placeSplit[1]) + 1
end
placeOffset = placeOffset + rowSpan - 1
placeOffset = placeOffset + rowSpan - 1
end
end
Line 108: Line 116:
tableRow:node(rowUSD)
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   
if row.place then   
Line 156: Line 169:
end
end
end
end
mw.log('!!!!!!!!')
 
mw.log(players)
-- Save prize to db
-- 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}}
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}}

Revision as of 21:39, 24 October 2023

Documentation for this module may be created at Module:PrizePool/doc

local getArgs = require('Module:Arguments').getArgs
local PrizePool = {}
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('Participant')
		
	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 = nil
	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)
	
	if args.currency then
		local tables = 'Conversion'
		local fields = 'rate'
		local cargoArgs = {
			where = 'currency="' .. currency .. '" AND date="' .. date .. '"'
		}
		local results = cargo.query(tables, fields, cargoArgs)

		if #results == 0 then
			local conversion = mw.ext.externaldata.getWebData({
				format = 'json',
			    url = "https://api.exchangerate.host/convert?from=" .. currency .. "&to=USD&date=" .. date
			})

			if conversion.success then
				conversionRate = conversion.info
				frame:callParserFunction{name = '#cargo_store:', args = {_table = 'Conversion', currency = currency, rate = conversion.info, date = date}}
			end
		else 
			conversionRate = results[1].rate
		end
	end
	
	local placeOffset = 0
	while (args[i] ~= nil) do
		local row = mw.text.jsonDecode(args[i])
		
		local prizeString = 'TBD'
		local prizeInt
		
		if 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 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'):wikitext(row.place or i + placeOffset)
	
		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] .. '|' .. string.gsub(participants[1], gameCategory .. '/', '') .. ']]')
						tableRow:node(participantCell)
					else 
						local extraRow = mw.html.create('tr'):addClass('bodyRow')
						
						extraRow:node(mw.html.create('td'):wikitext('[[' .. participants[j] ..  '|' .. string.gsub( participants[j], gameCategory .. '/', '') .. ']]'))
						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)
				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'):wikitext(rowColumns[j]))
					j = j + 1
				end
			else
				for j = 1, #columns do
					tableRow:node(mw.html.create('td'):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