Module:TournamentSection

-- tournamentSection local m_cargo = require('Module:CargoUtil') local m_util = require('Module:Util') local util_html = require('Module:HTMLUtil') local m_form = require('Module:FormUtil') local Team = require('Module:Team').team local settings = mw.loadData('Module:TournamentSectionSettings')

local p = {}

function p.doQuery(thissection, limit) local query = { tables = 'GameSchedule', fields = table.concat({			'Team1=Team1',			'Team2=Team2',			'Round=Round',			'DateTime=DateTime',			'TimeEntered=TimeEntered',			'Tournament=Tournament',			'ShownName=ShownName',			'Winner=Winner',			'Stream=Stream'		},','), }	-- since you can't edit tables from mw.loadData for k, v in pairs(settings[thissection].cargo) do		query[k] = v	end query.limit = limit if thissection == 'Events' then query.where = m_cargo.makeMinMaxQuery(query, '_pageName', 'DateTime', 'MIN') end return mw.ext.cargo.query(query.tables, query.fields, query) end

function p.makeTogglers(thissection) -- creates the top row of the table where you can toggle between the three options -- this is called once per section local tbl = mw.html.create ul = tbl:tag('div') :addClass('tabs-March2014') :tag('ul') for _, section in ipairs(settings.sections) do		li = ul:tag('li') li:addClass(settings[section].toggles.toggler) if section == thissection then li:attr('id','current') end li:wikitext(settings[section].title):done end return tbl end

function p.makeSection(thissection, result) -- start table local tbl = mw.html.create :tag('table') :css({ ['text-align'] = 'center', display = 'inline-table', margin = '0' }) -- table headings tr = tbl:tag('tr') for i = 1, 3 do		tr:tag('th'):css(settings.sectionstyles[i]) :wikitext(settings[thissection].sectiontitles[i]) end -- rest of the table for n, row in ipairs(result) do		row.N = n		tbl:node(p.makeRow(row, thissection)) end return tbl end

function p.makeRow(row, thissection) local formatted = p.parseData(row) local tbl = mw.html.create('tr') for _, item in ipairs(settings[thissection].fields) do		tbl:tag('td') :node(formatted[item]) :done end return tbl end

function p.parseData(row) local formatted = { tournament = p.formatTournament(row.Tournament, row.ShownName), teams = p.formatTeams(row.Team1, row.Team2, row.Round), winner = p.formatWinner(row), countdown = p.formatCountdown(row), }	return formatted end

function p.formatTournament(tournament, shownname) local link = string.format('%s ',		tournament or ,		shownname or ,		m_form.makeBaseQueryURLFromArgs( 'SpoilerFreeSchedule', 'SFS', { tournament } ).full	) return util_html.makeNodeFromWikitext(link) end

function p.formatTeams(team1, team2, round) local tbl = mw.html.create('table') :css({ margin = '-3px -1px', width = '100%', ['table-layout'] = 'fixed' }) if team1 .. team2 == '' then tbl:wikitext(string.format("%s",round)) else tbl:node(util_html.vsAlign( Team{team1,'leftshortlinked'}, Team{team2,'rightshortlinked'} ))	end return tbl end

function p.formatWinner(row) if row.Winner == '0' then return' DRAW ' elseif row.Winner == '-1' then return 'FFs' else return util_html.makeNodeFromWikitext(Team{row['Team' .. row.Winner], 'onlyimagelinked', size = '45px'}) end end

function p.formatCountdown(row) local lang = mw.getLanguage('en') local tbl = mw.html.create if row.TimeEntered ~= 'true' then tbl:wikitext(lang:formatDate('d M',row.DateTime)) else year, month, day, hour, min, sec = string.match(row.DateTime, "(%d%d%d%d)-(%d%d)-(%d%d) (%d%d):(%d%d):(%d%d)") gametime = { year = year, month = month, day = day, hour = hour, min = min, sec = sec} if os.difftime(os.time(gametime),os.time) > 0 then tbl:tag('span') :attr('data-end','remove') :attr('data-options','no-leading-zeros') :attr('data-toggle','.post-countdown' .. row.N)				:addClass('countdown') :css({ display = 'none'}) :tag('span') :addClass('countdowndate') :wikitext(lang:formatDate("j F Y H:i:s",row.DateTime) .. ' +0000') :done :done end end if row.Stream == '' then return tbl else link = string.format('[%s %s]',			row.Stream,			tostring(tbl)			) local linktbl = mw.html.create('span'):addClass('plainlinks'):wikitext(link) return linktbl end end

function p.main(frame) if frame == mw.getCurrentFrame then args = require('Module:ProcessArgs').merge(true) else frame = mw.getCurrentFrame end local limit = args.limit or 20 local tbl = mw.html.create for _, section in ipairs(settings.sections) do		local result = p.doQuery(section, limit) tbl:tag('div') :addClass(settings[section].toggles.toggle) :node(p.makeTogglers(section)) :wikitext(string.format(settings[section].sentence,limit)) :node(p.makeSection(section, result)) :done end return tostring(tbl) end

return p