Module:Country representation

local p = {} -- p stands for package

-- Integer win rate %, with no wins replaced by 0% rather than nan function p.calcRatio(num, total) return (string.gsub(math.floor((100 * num / (total))+0.5),"-nan","0")) end

function p.getSMWResults(frame, tournaments) local SMWConditions = '|'			end			SMWConditions = SMWConditions .. item		end 	else   		SMWConditions = SMWConditions .. frame.args.tournament  	end	SMWConditions = SMWConditions .. ' has mvptable flag::+ has player pages::+' SMWConditions = SMWConditions .. '|?has mvptable flag=flag|?has player pages=players|mainlabel=page|limit=1000' --mw.log(SMWConditions) local queryResult = mw.smw.getQueryResult( SMWConditions ) local output = {} --mw.logObject(queryResult) local count = 0 if type( queryResult ) == "table" then for key, item in pairs( queryResult.results ) do	 		local playerList = {} for _, player in pairs( item.printouts.players ) do	 			table.insert(playerList, {name = player.displaytitle ~= '' and player.displaytitle or player.fulltext, page = player.fulltext}) count = count + 1 end table.sort(playerList, function(item1, item2) return item1.page < item2.page end) local flag = item.printouts.flag[1] if type(flag) == 'table' then flag = flag.fulltext end table.insert(output, {		 		flag = flag,				players = playerList		  	}) end end return output, count end

function p.getLPDBResults(args, tournaments) args.staff = args.staff == 'show' and true or false if args.noCountry == 'hide' then args.noCountry = false else args.noCountry = true end local LPDBconditions = '(pagename::' .. tournaments[1] ..  	for i=2, #tournaments do		LPDBconditions = LPDBconditions .. ' OR pagename::' .. tournaments[i] .. 	end  	LPDBconditions = LPDBconditions .. ')' local queryResult = mw.ext.LiquipediaDB.lpdb('placement', {       limit = 5000,        offset = 0,        conditions = LPDBconditions,        query = 'players',    }) local output = {} local playerCache = {} local count = 0 local MAXPLAYERS = tonumber(args.maxPlayers or '5') for _, team in pairs(queryResult or {})do if team.players then for i = 1, MAXPLAYERS do               local res = p.handlePlayer("player", team.players, i, output, playerCache, args.noCountry) count = count + (res and 1 or 0) end if (args.staff or ) ~=  then for i = 1, 5 do                   local res = p.handlePlayer("coach", team.players, i, output, playerCache, args.noCountry) count = count + (res and 1 or 0) end end end end output = removeKeys(output) return output, count end

function p.handlePlayer(prefix, data, i, output, playerCache, showNoCountry) local playerPage = data[prefix..i]	local playerDisplay = data[prefix..i.."dn"] local flag = data[prefix..i.."flag"] if playerPage and playerPage ~= "" and flag and (showNoCountry or flag ~= "") then if not playerDisplay then playerDisplay = playerPage end if not output[flag] then output[flag] = {flag = flag, players = {}} end if not playerCache[playerPage] then table.insert(output[flag].players, {page = playerPage, name = playerDisplay}) table.sort(output[flag].players, function(item1, item2) return string.lower(item1.name) < string.lower(item2.name) end) playerCache[playerPage] = true return true end end return false end

function removeKeys(list) local output = {} for _,v in pairs(list) do       table.insert(output, v)    end return output end

function p.get(frame, args, data) local tableWrapper, numPlayers = '', 1 local tournaments, flagList = {}, {} if not args then args = require('Module:Arguments').getArgs(frame) end -- parse parameters tournamentX for key, item in pairs(args) do		if item == '' or item == '\n' then args[key] = nil end if type(key) == 'string' and item ~= '' then -- tournamentX if key:match('^tournament(%d*)$') then item = string.gsub(item,' ','_') table.insert(tournaments, item) end end end if not next(tournaments) then local page = string.gsub(mw.title.getCurrentTitle.text,' ','_') table.insert(tournaments, page) end

if (args.type or '') == 'LPDB' then flagList, numPlayers = p.getLPDBResults(args, tournaments) else flagList, numPlayers = p.getSMWResults(frame, tournaments) end table.sort(flagList, function(item1, item2) return #item1.players > #item2.players or (#item1.players == #item2.players and item1.flag < item2.flag) end) --mw.logObject(flagList) tableWrapper = mw.html.create('table') :addClass('sortable wikitable') local row = tableWrapper:tag('tr') row:tag('th'):wikitext('#'):addClass('unsortable') row:tag('th'):wikitext('Country / Region') row:tag('th'):wikitext('Representation') row:tag('th'):wikitext('Players'):addClass('unsortable') row:done local rank = 0 local rankbuffer = 0 local playernum for key, item in pairs(flagList) do	 	if #item.players == playernum then rank = rank rankbuffer = rankbuffer + 1 else rank = rank + rankbuffer + 1 rankbuffer = 0 end playernum = #item.players row = tableWrapper:tag('tr') row:tag('td'):wikitext(rank):css('text-align','right') row:tag('td'):wikitext(getFlag(frame, item.flag)) row:tag('td'):wikitext(#item.players .. ' / ' .. numPlayers .. ' (' .. p.calcRatio(#item.players,numPlayers) .. '%)'):css('text-align','right') local playerList = {} for _, player in pairs(item.players) do table.insert(playerList,  .. player.name .. ) end row:tag('td'):wikitext(table.concat(playerList,', ')) row:done end local output = ' ' .. tostring(tableWrapper) .. ' '   	return output end

function getFlag(frame, flag) if flag then if type(flag) == 'table' then flag = flag.fulltext end local result, icon = pcall(frame.expandTemplate, frame, {title = 'Flag/' .. flag:lower}) if result then return ' ' .. icon .. ' ' .. flag .. ' '		else return '' .. ' ' .. flag end end end

return p