Module:Bracket matches table

local p = {} local getArgs = require('Module:Arguments').getArgs local countdown = require('Module:Countdown')._create local lang = mw.language.new('en')

-- If run in abbreviated roundnames mode local abbreviations = { ["Upper Bracket"] = "UB", ["Lower Bracket"] = "LB", --["Bracket"] = "", }

function p.get(frame) args = getArgs(frame) local tournament = args.tournament or mw.title.getCurrentTitle.prefixedText tournament = string.gsub(tournament,'%s','_') local bestof = args.bestof or 'gamecount' local LPDBConditions = 'pagename::' .. tournament .. ' AND status::' LPDBConditions = LPDBConditions .. ' AND date::>1970-01-01' if args.sdate then LPDBConditions = LPDBConditions .. ' AND (date::>' .. args.sdate .. ' OR date::' .. args.sdate .. ')' end if args.edate then LPDBConditions = LPDBConditions .. ' AND (date::<' .. args.edate .. ' OR date::' .. args.edate .. ')' end if args.matchsection then LPDBConditions = LPDBConditions .. ' AND extradata_matchsection::' .. args.matchsection .. '' end local data = mw.ext.LiquipediaDB.lpdb('match', {		limit = tonumber(args.limit) or 10000,	 	offset = 0,	  	order = 'date asc',	  	conditions = LPDBConditions,	}) local output = '' output = mw.html.create('div') :addClass('table-responsive') :css('margin-bottom', '10px') local tableOutput = output:tag('table') :addClass('wikitable wikitable-striped sortable') :addClass('match-card') :cssText(args.tablestyle) local row = tableOutput:tag('tr') :addClass('HeaderRow') local cell

cell = row:tag('th') :addClass('divCell') :attr('data-sort-type','isoDate') :wikitext('Date') if args.hideround ~= 'true' then cell = row:tag('th') :addClass('divCell') :addClass('unsortable') :wikitext('Round') end cell = row:tag('th') :addClass('divCell') :wikitext('Team') cell = row:tag('th') :addClass('divCell') :attr('width','50') :wikitext('Score') cell = row:tag('th') :addClass('divCell') :wikitext('Vs. Team')

if type(data) == 'table' then local formatdate for _, item in ipairs(data) do			local roundHeader = (item.header ~= '') and item.header or item.extradata.matchround row = tableOutput:tag('tr') :addClass('Match') cell = row:tag('td') :addClass('Date') if item.dateexact == '1' then formatdate = lang:formatDate('F j, Y', item.date) .. ' - ' .. item.date:match('%s(%d+:%d+):%d+$') if item.finished ~= '1' and args.countdown == 'true' then cell:wikitext(mw.getCurrentFrame:expandTemplate{ title = 'HiddenSort', args = {item.date} } .. countdown{date = formatdate, rawcountdown= 'true', twitch = item.stream.twitch}) else cell:wikitext(mw.getCurrentFrame:expandTemplate{ title = 'HiddenSort', args = {item.date} } .. countdown{date = formatdate, rawdatetime = 'true'}) end elseif args.dateexact == 'true' then cell:css('text-align', 'center'):css('font-style', 'italic'):wikitext(mw.getCurrentFrame:expandTemplate{ title = 'HiddenSort', args = {item.date} } .. 'To be announced') else formatdate = lang:formatDate('F j, Y', item.date) cell:wikitext(formatdate) end if args.hideround ~= 'true' then cell = row:tag('td') :addClass('Round') if roundHeader ~= nil and roundHeader ~= '' then if args.shortedroundnames == 'true' then for k,v in pairs(abbreviations) do							roundHeader = roundHeader:gsub(k, v)						end end cell:wikitext(roundHeader) end end cell = row:tag('td') :addClass('TeamLeft') if item.opponent1 == 'TBD' then cell:wikitext(frame:expandTemplate{ title = 'Abbr/TBD' }) else cell:wikitext(mw.ext.TeamTemplate.team2short(item.opponent1, item.date)) end cell = row:tag('td') :addClass('Score') if item.finished == '1' then if item.winner == '1' then if item.resulttype == 'ff' then cell:wikitext('\'\'\'W\'\'\' : FF') elseif item.resulttype == 'dq' then cell:wikitext('\'\'\'W\'\'\' : DQ') else cell:wikitext('\'\'\'' .. item.opponent1score .. '\'\'\' : ' .. item.opponent2score) end elseif item.winner == '2' then if item.resulttype == 'ff' then cell:wikitext('FF : \'\'\'W\'\'\'') elseif item.resulttype == 'dq' then cell:wikitext('DQ : \'\'\'W\'\'\'') else cell:wikitext(item.opponent1score .. ' : \'\'\ .. item.opponent2score .. '\'\'\) end else cell:wikitext(item.opponent1score .. ' : ' .. item.opponent2score) end -- add score for ongoing games elseif os.time >= parseDateTime(item.date) and item.dateexact == '1' then cell:wikitext(item.opponent1score .. ' : ' .. item.opponent2score) -- gamecount might never be '' and at least '0' have to double check that elseif item.extradata and item.extradata[bestof] ~= nil and item.extradata[bestof] ~= '' and item.extradata[bestof] ~= '0' then cell:css('padding','1px 5px') cell:wikitext(' vs. (' .. safeExpand(mw.getCurrentFrame, 'Abbr/Bo' .. item.extradata[bestof], {}) .. ') ')		 	else cell:wikitext('vs.') end cell = row:tag('td') :addClass('TeamRight') if item.opponent2 == 'TBD' then cell:wikitext(frame:expandTemplate{ title = 'Abbr/TBD' }) else cell:wikitext(mw.ext.TeamTemplate.teamshort(item.opponent2, item.date)) end end end return output end

function safeExpand(frame, templateTitle, templateArgs) local result, value = pcall(frame.expandTemplate, frame, {title = templateTitle, args = templateArgs}) if result then return value else return 'Template:' .. templateTitle .. '' end end

function parseDateTime(str) local y, m, d, h, min, s = str:match("(%d%d%d%d)-?(%d?%d?)-?(%d?%d?) (%d?%d?):(%d?%d?):(%d?%d?)$") -- fallback to now if y == nil then return os.time end -- defaults if m == '' then m = 1 end if d == '' then d = 1 end if h == '' then h = 0 end if min == '' then min = 0 end if s == '' then s = 0 end -- Adjust time based on server timezone offset from UTC local offset = os.time(os.date("*t")) - os.time(os.date("!*t")) -- create time local localTime = os.time{year=y, month=m, day=d, hour=h, min=min, sec=s} -- This will take our UTC timestamp and put it into localtime without converting... return localTime + offset -- "Convert" back to UTC end

return p