Module:MatchesUtil

local util_args = require('Module:ArgsUtil') local util_table = require('Module:TableUtil') local util_time = require('Module:TimeUtil') local util_vars = require('Module:VarsUtil')

local TIME_UNTIL_AUTO_NEXT_TAB = 2 * 24 * 60 * 60 local PERCENT_UNTIL_AUTO_NEXT_TAB = 0.5 local TIME_TO_RETAIN_LAST_TAB_ACTIVE = 2 * 24 * 60 * 60

local p = {} local h = {}

function p.addCustomClass(row, class) class = util_table.guaranteeTable(class) class[#class+1] = row.customClass row.customClass = util_table.concat(class, ' ') end

function p.printCustomClass(tr, row) tr:addClass(row.customClass) end --- function p.determineThis(matchData, this) if this then return tonumber(this) end if #matchData == 0 then return nil end if not h.firstWinnerInTab(matchData[1]) then return 1 end if h.lastWinnerInTab(matchData[#matchData]) then return h.thisIfTournamentIsOver(matchData[#matchData], #matchData) end for i = 1, #matchData do		if not h.lastWinnerInTab(matchData[i]) then return i		else local potentialThis = h.setThisInBetweenWeeks(matchData[i], matchData[i+1], i)			if potentialThis then return potentialThis end end end end

function h.thisIfTournamentIsOver(tab, n)	local lastTime = h.lastTimeInTab(tab) local now = os.time if now > lastTime + TIME_TO_RETAIN_LAST_TAB_ACTIVE then return nil end return n end

function h.firstWinnerInTab(tab) if not tab[1] then return nil end return tab[1].Winner end

function h.lastWinnerInTab(tab) if not tab[1] then return nil end return tab[#tab].Winner end

function h.setThisInBetweenWeeks(lastTab, nextTab, i)	if h.firstWinnerInTab(nextTab) then return false end local lastTime = h.lastTimeInTab(lastTab) local nextTime = h.firstTimeInTab(nextTab) if not lastTime or not nextTime then return nil end local now = os.time if now > lastTime * PERCENT_UNTIL_AUTO_NEXT_TAB + nextTime * (1 - PERCENT_UNTIL_AUTO_NEXT_TAB) then return i + 1 elseif now > lastTime + TIME_UNTIL_AUTO_NEXT_TAB then return i + 1 else return i	end end

function h.firstTimeInTab(tab) return util_time.unixNumber(tab[1].UTC) end

function h.lastTimeInTab(tab) return util_time.unixNumber(tab[#tab].UTC) end --- function p.getPageAndTabDict(matchData) local tbl = {} for i, tab in ipairs(matchData) do		tbl[i] = tab[1].PageAndTab end return util_table.hash(tbl) end

return p