Module:Matches Tournament

--- -- @author Vogan for Liquipedia --

local p = {} -- p stands for package

local MatchesBuilder = require('Module:MatchesBuilder') local htmlCreate = mw.html.create

local LIMIT = 5 local DATA_SORT = 'date asc' local DATA_GROUP = 'objectname asc' local PAGENAME_IS_SPECIFIED = False

function p.get(frame) local outerDiv = htmlCreate('div') outerDiv:attr('class', 'fo-nttax-infobox-wrapper') local contentDiv = p.create_matches_header

local func_tournament = p.create_tournament

local builder = MatchesBuilder:new local pagename = p.get_pagename(frame.args) builder.init(p.get_tournament_data(pagename, frame.args.limit or LIMIT)) builder.set_tournament_function(func_tournament) local matches = builder.build matches.get_matches(contentDiv)

outerDiv:node(contentDiv) return outerDiv end

--- Retrieves data from LPDB for a certain tournament -- @param pagename: pagename of the tournament, limit number of upcoming matches function p.get_tournament_data(pagename, limit) local queryString = 'opponent1, opponent2, date,' .. ' stream, pagename, tournament, objectname, icon, extradata' local conditions = pagename .. ' AND date::>' ..       os.date("%Y-%m-%d",os.time - (24*60*60)) .. ' AND ' .. 'finished::0 AND dateexact::1' mw.log(conditions) local data = mw.ext.LiquipediaDB.lpdb('match', {       limit = limit,        order = DATA_SORT,        conditions = conditions,        query = queryString,        groupby = DATA_GROUP    }) return data end

function p.get_pagename(args) local pagename if args[1] ~= nil then PAGENAME_IS_SPECIFIED = True pagename = args[1] pagename = pagename:gsub(' ', '_') -- Else it doesn't work local pagenameString = '(pagename::' .. pagename ..        local i = 2        while(args[i] ~= nil and args[i] ~= ) do            pagenameString = pagenameString .. ' OR pagename::' .. (args[i] or ):gsub(' ', '_') ..             i = i + 1        end        pagenameString = pagenameString .. ')' mw.log(pagenameString) return pagenameString else pagename = tostring(mw.title.getCurrentTitle) pagename = pagename:gsub(' ', '_') -- Else it doesn't work local pagenameString = 'pagename::' .. pagename .. '' return pagenameString end end

--- Override function p.create_tournament(item) local div = htmlCreate('div') div:attr('style', 'width:180px; float:right; white-space:nowrap;')

div:node(p.get_tournament_name(item)) return div end

--- Override function p.get_tournament_name(item) local name = htmlCreate('div') name:attr('style', 'overflow:hidden; text-overflow:ellipsis; ' ..       'max-width: 170px; vertical-align:middle; white-space:nowrap; ' ..        'font-size:11px; height:16px; margin-top:3px; text-align:right;') name:wikitext(p.get_tournament_link(item)) return name end

--- Override function p.get_tournament_link(item) local extradata = item.extradata if extradata ~= nil and extradata.matchsection and extradata.matchsection ~= '' then return  .. extradata.matchsection ..  elseif item.tournament ~= nil then return item.tournament elseif PAGENAME_IS_SPECIFIED then local split_pagename = split(item.pagename, '/') return  .. split_pagename[#split_pagename] ..  else return item.pagename end end

function p.create_matches_header local contentDiv = htmlCreate('div') contentDiv:attr('class', 'fo-nttax-infobox wiki-bordercolor-light')

local div = htmlCreate('div')

local infoboxHeader = htmlCreate('div') infoboxHeader:attr('class', 'infobox-header wiki-backgroundcolor-light') infoboxHeader:wikitext('Upcoming Matches')

div:node(infoboxHeader) contentDiv:node(div) return contentDiv end

-- https://stackoverflow.com/a/7615129 function split(inputstr, sep) if sep == nil then sep = "%s" end local t={} ; i=1 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do       t[i] = str i = i + 1 end return t end

return p