Module:PredictionsUtil

local util_cargo = require('Module:CargoUtil') local util_esports = require('Module:EsportsUtil') local util_matches = require('Module:MatchesUtil') 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 p = {} local h = {}

function p.getOverviewPage(page, pageType) if page then return util_esports.getOverviewPage(page) end local tbl = util_title.titleTable local i = util_table.keyOf(tbl, pageType) return util_title.titleslice(nil, 1, i - 1) end

--- function p.addPredictionsToData(predictionData, matchData) local pageAndTabToTab = util_matches.getPageAndTabDict(matchData) util_vars.log(predictionData[1]) for i, tab in ipairs(matchData) do		for r, row in ipairs(tab) do			local index = tonumber(row.InitialN_MatchInTab or r)			local tabIndex = h.getTabIndex(pageAndTabToTab, row, i)			-- if not predictionData[tabIndex][index] then -- 	error(('Missing index %s in tab %s'):format(index, tabIndex)) -- end if not predictionData[tabIndex] then error(('Missing tab %s'):format(tabIndex)) end row.Prediction = tonumber(predictionData[tabIndex][index]) h.addPredictionValueClassToRow(row) h.addPredictionResultClassToRow(row) end end end

function h.getTabIndex(pageAndTabToTab, row, i)	return pageAndTabToTab[row.InitialPageAndTab or row.PageAndTab] end

function h.addPredictionValueClassToRow(row) if not row.Prediction then return end util_matches.addCustomClass(row, 'ml-prediction-' .. row.Prediction) end

function h.addPredictionResultClassToRow(row) if not (row.Prediction and row.Winner) then return end if row.Prediction == row.Winner then util_matches.addCustomClass(row, 'prediction-row-right') else util_matches.addCustomClass(row, 'prediction-row-wrong') end end

function p.addPredictionTotalsToData(matchData) for i, tab in ipairs(matchData) do		tab.predictionTotals = h.getPredictionTotalsForTab(tab, i)	end matchData.predictionTotals = h.countOverallTotals(matchData) end

function h.getPredictionTotalsForTab(tab, i)	local ret = { made = 0, right = 0, over = 0, madeAndOver = 0, }	for j, row in ipairs(tab) do row = util_table.guaranteeTable(row) -- not sure if thisll work? ret.made = ret.made + (row.Prediction and 1 or 0) ret.over = ret.over + (row.Winner and 1 or 0) ret.madeAndOver = ret.madeAndOver + (row.Winner and row.Prediction and 1 or 0) ret.right = ret.right + (row.Prediction and row.Prediction == row.Winner and 1 or 0) end return ret end

function h.countOverallTotals(matchData) local ret = { made = 0, right = 0, madeAndOver = 0, over = 0 } for k, _ in pairs(ret) do		ret[k] = h.countOneOverallTotal(matchData, k)	end return ret end

function h.countOneOverallTotal(matchData, totalType) local total = 0 for _, tab in ipairs(matchData) do		total = total + tab.predictionTotals[totalType] end return total end --- function p.displayTitle(overviewPage) local eventName = h.getEventName(overviewPage) local pageType = h.getPageType local subject = h.getSubject(pageType) local display = h.concatDisplay(eventName, pageType, subject) return mw.getCurrentFrame:preprocess((''):format(display)) end --- function h.getPageType if util_title.titleparts(nil, 1, -1) == 'Leaderboard' then return 'Leaderboard' end return util_title.titleparts(nil, 1, -2) end

function h.getEventName(overviewPage) local query = h.getEventNameQuery(overviewPage) return util_cargo.getOneResult(query) or overviewPage end

function h.getEventNameQuery(overviewPage) local tbl = { tables = 'Tournaments', fields = 'Name', where = ('_pageName="%s"'):format(overviewPage) }	return tbl end

function h.getSubject(pageType) if pageType == 'Leaderboard' then return nil else return util_title.titleparts(nil, 1, -1) end end

function h.concatDisplay(eventName, pageType, subject) local subjectDisplay = subject and ' - ' .. subject or '' return ('Predictions:%s - %s%s'):format(eventName, pageType, subjectDisplay) end

-- local TARGET_DATA = { Leaderboard = { text = 'UserPredictionsLeaderboard', title = '%s/Leaderboard' },	UserOverview = { text = 'UserPredictionsUser', title = 'User/%s' } }

function p.linkToGeneratedPage(li, pageType, value) local target = TARGET_DATA[pageType].title:format(value) if mw.title.makeTitle('Predictions', target).exists then h.printTargetLink(li, pageType, target) else h.printCreateTargetButton(li, pageType, target) end end

function h.printTargetLink(li, pageType, target) li:wikitext(util_text.intLink('Predictions:' .. target, i18n.print('view' .. pageType))) end

function h.printCreateTargetButton(li, pageType, target) li:wikitext(i18n.print('create' .. pageType), ' ') li:tag('span') :attr('data-target-text', TARGET_DATA[pageType].text) :attr('data-target-title', 'Predictions:' .. target) :addClass('predictions-create-page') :addClass('user-predictions-action-link') :wikitext(i18n.print('createAction')) end --

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

function p.printRefreshTargetButton(li, target) li:tag('div') :addClass('predictions-refresh-target') :addClass('prediction-action') :attr('data-refresh-target', target) end

return p