Module:Game

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_esports = require('Module:EsportsUtil') local util_html = require('Module:HtmlUtil') local util_table = require('Module:TableUtil') local util_text = require('Module:TextUtil') local i18n = require('Module:i18nUtil')

local m_currency = require('Module:Currency') local m_team = require('Module:Team')

local HEADINGS = { 'Place', 'Player', classes = { Place = 'gameblock-place', Player = 'gameblock-player', Points = 'gameblock-points', Prize = 'gameblock-prize', } } local LINE_ARG_ORDER = { 'player', 'flag', 'prize', 'points', 'team' } local CURRENCY local TEAM_MANDATORY = false local FLAG_MANDATORY = false

local h = {}

local p = {} function p.main(frame) local args = util_args.merge(true) i18n.init('Game') h.setPreferencesFromArgs(args) local processed = h.getProcessedArgs(args) h.setCols(processed) return h.makeOutput(processed, args) end

function h.setPreferencesFromArgs(args) if args.currency then CURRENCY = args.currency end TEAM_MANDATORY = util_args.castAsBool(args.requireteam) FLAG_MANDATORY = util_args.castAsBool(args.requireflag) end

function h.getProcessedArgs(args) local processed = util_args.splitAllArgs(args, LINE_ARG_ORDER) util_table.mapRowsInPlace(processed, h.processRow) return processed end

function h.processRow(row) row.Player = h.getPlayerDisplay(row) row.Prize = h.getPrizeDisplay(row.prize) row.Points = row.points row.Place = row.place or row.index end

function h.getPlayerDisplay(row) if TEAM_MANDATORY and FLAG_MANDATORY then return util_esports.teamFlagPlayerMandatory(row) elseif TEAM_MANDATORY then return util_esports.teamPlayerMandatory(row.team, row.player) elseif FLAG_MANDATORY then return util_esports.flagPlayerMandatory(row) end return util_esports.playerMarkup(row) end

function h.getPrizeDisplay(prize) if not prize then return end if not tonumber(prize) then return end return m_currency.short(prize, CURRENCY) end

function h.setCols(processed) h.addColumnIfPresent('Points', processed) h.addColumnIfPresent('Prize', processed) end

function h.addColumnIfPresent(col, processed) for _, row in ipairs(processed) do		if row[col] then HEADINGS[#HEADINGS+1] = col return end end end

function h.makeOutput(processed, args) local output = mw.html.create('table') :addClass('wikitable') :addClass('gameblock') h.printHeading(output, args.name) util_html.printHeaderFromI18n(output, HEADINGS) util_html.printRowsByList(output, processed, HEADINGS) return output end

function h.printHeading(output, name) if not name then return end util_html.printColspanHeader(output, name, #HEADINGS) end

return p