Module:UserPredictions

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_esports = require('Module:EsportsUtil') local util_matches = require('Module:MatchesUtil') local util_predictions = require('Module:PredictionsUtil') local util_table = require('Module:TableUtil') local util_text = require('Module:TextUtil') local util_title = require('Module:TitleUtil') local util_vars = require('Module:VarsUtil') local i18n = require('Module:i18nUtil')

local MatchList = require('Module:MatchListAbstract')

local OVERVIEW_PAGE local USER

local h = {}

local p = {} function p.main(frame) local args = util_args.merge i18n.init('MatchList', 'UserPredictions') OVERVIEW_PAGE = util_predictions.getOverviewPage(args.page, 'User') USER = h.getUser local matchData = MatchList:getMatchData(OVERVIEW_PAGE, args) local order = h.getOrder(args, matchData) local predictionData = h.getPredictionData(order, args, matchData) util_predictions.addPredictionsToData(predictionData, matchData) util_predictions.addPredictionTotalsToData(matchData) h.storeCargo(matchData, predictionData) local hasBO2 = h.hasBO2(matchData) return h.makeOutput(matchData, hasBO2, args), util_predictions.displayTitle(OVERVIEW_PAGE) end

function h.getUser return util_title.titleparts(nil, 1, -1) end

function h.getOrder(args, matchData) -- order is a param stored denoting the initial order of the data, prior to any reschedules happening -- outside of legacy support, order is guaranteed to be present for every single match -- if a match was changed from one tab (week) to another, then _tab will also be defined -- we will store everything according to its initial order, and then re-parse to current order on querying -- so here we make a lookup table going from current order to initial order, for store -- both this display & the queried display do their own parsing to get current order -- keep in mind that the current order at save may be different from the realtime current order -- this is undoing the reordering that was present AT THE LAST TIME OF PAGE SAVE local ret = {} h.addOrdersFromArgs(ret, args, util_matches.getPageAndTabDict(matchData)) h.addOrdersFromMatchData(ret, matchData) return ret end

function h.addOrdersFromArgs(ret, args, pageAndTabToTab) for k, v in pairs(args) do		if k:match('_order') then t, r = k:match('t(%d+)_r(%d+)_order') local thistab = h.getThisTab(args, t, r, pageAndTabToTab) ret[('%s_%s'):format(thistab, tonumber(v))] = { tab = t, row = r } end end end

function h.addOrdersFromMatchData(ret, matchData) -- legacy support for predictions made before _order was a guaranteed input for every single arg if next(ret) then return end for i, tab in ipairs(matchData) do		for r, row in ipairs(tab) do			ret[('%s_%s'):format(i, r)] = { tab = i, row = r } end end end

function h.getThisTab(args, t, r, pageAndTabToTab) local key = 't' .. t .. '_r' .. r .. '_tab' if args[key] then return pageAndTabToTab[args[key]] end return t end

function h.initTab(tbl, t)	if tbl[t] then return end tbl[t] = {} end

function h.storeCargo(matchData, predictionData) local data = { _table = 'UserPredictions', OverviewPage = OVERVIEW_PAGE, User = USER, NumberOfPredictions = matchData.predictionTotals.made, NumberCorrect = matchData.predictionTotals.right, NumberOver = matchData.predictionTotals.over, NumberMadeAndOver = matchData.predictionTotals.madeAndOver, PredictionList = h.concatPredictionData(predictionData), }	util_cargo.store(data) end

function h.concatPredictionData(predictionData) local tbl = {} for i, row in ipairs(predictionData) do		tbl[i] = util_table.concat(row, ',') end return util_table.concat(tbl,';') end

function h.getPredictionData(order, args, matchData) local predictionsData = {} for i, matchesTab in ipairs(matchData) do		h.initTab(predictionsData, i)		h.addRowPredictionData(predictionsData, matchesTab, order, args) end return predictionsData end

function h.addRowPredictionData(predictionsData, matchesTab, order, args) for r, row in ipairs(matchesTab) do		local index = h.getArgIndexFromOrder(matchesTab.index, r, order) predictionsData[matchesTab.index][r] = index and args[index] or '' end end

function h.getArgIndexFromOrder(i, r, order) local sourceMatch = order[('%s_%s'):format(i, r)] if not sourceMatch then return nil end return 't' .. sourceMatch.tab .. '_r' .. sourceMatch.row end

function h.hasBO2(matchData) for i, tab in ipairs(matchData) do		for j, row in ipairs(tab) do			if row.BestOf == 2 then return true end end end return false end

function h.makeOutput(matchData, hasBO2, args) args.tabwrapperclass = h.getTabWrapperClass(hasBO2) local output = mw.html.create('div'):addClass('user-predictions-page') h.printPageList(output) output:node(MatchList:makeOutput(matchData, args)) return output end

function h.printPageList(output) local ul = output:tag('ul') ul:tag('li'):wikitext(util_text.intLink(OVERVIEW_PAGE, i18n.print('viewOverview'))) h.refreshUserPage(ul:tag('li')) util_predictions.linkToGeneratedPage(ul:tag('li'), 'Leaderboard', OVERVIEW_PAGE) util_predictions.linkToGeneratedPage(ul:tag('li'), 'UserOverview', USER) h.printGroupList(ul:tag('li')) output:tag('hr') end

function h.refreshUserPage(li) li:tag('div') :attr('id', 'predictions-reload-user-data') :addClass('user-predictions-action-link') :wikitext(i18n.print('userRefresh')) end

function h.printGroupList(li) local user = USER local data = h.getGroupData(user) if next(data) then h.printExistingGroups(li, data) else h.printNoGroups(li) end end

function h.getGroupData(user) local query = h.getGroupQuery(user) return util_cargo.getOrderedList(query, 'GroupName') end

function h.getGroupQuery(user) local tbl = { tables = 'UserPredictionGroups', where = h.getGroupWhere(user), fields = 'GroupName', orderBy = 'NumberOfMembers', }	return tbl end

function h.getGroupWhere(user) local tbl = { util_cargo.fakeHolds('Members', user), ('OverviewPage="%s"'):format(OVERVIEW_PAGE) }	return util_cargo.concatWhere(tbl) end

function h.printExistingGroups(li, data) li:wikitext(i18n.print('groups')) local ul = li:tag('ul') for _, v in ipairs(data) do		ul:tag('li') :wikitext(util_text.intLink(h.linkToGroup(v), v)) end h.printCreateOrJoinGroup(ul) end

function h.linkToGroup(v) return ('Predictions:%s/Group/%s'):format(OVERVIEW_PAGE, v) end

function h.printNoGroups(li) li:wikitext(i18n.print('noGroups')) local ul = li:tag('ul') h.printCreateOrJoinGroup(ul) end

function h.printCreateOrJoinGroup(ul) ul:tag('li'):tag('div') :attr('id', 'predictions-join-or-create') :addClass('user-predictions-action-link') :wikitext(i18n.print('newOrJoinGroup')) end

function h.getTabWrapperClass(hasBO2) local tbl = { 'user-predictions-wrapper', hasBO2 and 'user-predictions-allow-draws' }	return util_table.concat(tbl, ' ') end

return p