diff --git a/components/match2/wikis/wildrift/legacy/match_group_legacy_default.lua b/components/match2/wikis/wildrift/legacy/match_group_legacy_default.lua new file mode 100644 index 00000000000..b6d185c4e87 --- /dev/null +++ b/components/match2/wikis/wildrift/legacy/match_group_legacy_default.lua @@ -0,0 +1,203 @@ +--- +-- @Liquipedia +-- wiki=wildrift +-- page=Module:MatchGroup/Legacy/Default +-- +-- Please see https://github.com/Liquipedia/Lua-Modules to contribute +-- + +local MatchGroupLegacyDefault = {} + +local String = require('Module:StringUtils') +local Logic = require('Module:Logic') + +local MAX_NUM_OPPONENTS = 2 +local MAX_NUM_MAPS = 9 + +local roundData +function MatchGroupLegacyDefault.get(templateid, bracketType) + local lowerHeader = {} + local matches = mw.ext.Brackets.getCommonsBracketTemplate(templateid) + + assert(type(matches) == 'table') + local bracketData = {} + roundData = roundData or {} + local lastRound = 0 + for _, match in ipairs(matches) do + bracketData, lastRound, lowerHeader = MatchGroupLegacyDefault._getMatchMapping(match, bracketData, + bracketType, lowerHeader) + end + + for n = 1, lastRound do + bracketData['R' .. n .. 'M1header'] = 'R' .. n + if lowerHeader[n] then + bracketData['R' .. n .. 'M' .. lowerHeader[n] .. 'header'] = 'L' .. n + end + end + + -- add reference for map mappings + bracketData['$$map'] = { + ['$notEmpty$'] = 'map$1$', + map = 'map$1$', + team1side = 'map$1$team1side', + t1c1 = 'map$1$t1c1', + t1c2 = 'map$1$t1c2', + t1c3 = 'map$1$t1c3', + t1c4 = 'map$1$t1c4', + t1c5 = 'map$1$t1c5', + t1b1 = 'map$1$t1b1', + t1b2 = 'map$1$t1b2', + t1b3 = 'map$1$t1b3', + t1b4 = 'map$1$t1b4', + t1b5 = 'map$1$t1b5', + team2side = 'map$1$team2side', + t2c1 = 'map$1$t2c1', + t2c2 = 'map$1$t2c2', + t2c3 = 'map$1$t2c3', + t2c4 = 'map$1$t2c4', + t2c5 = 'map$1$t2c5', + t2b1 = 'map$1$t2b1', + t2b2 = 'map$1$t2b2', + t2b3 = 'map$1$t2b3', + t2b4 = 'map$1$t2b4', + t2b5 = 'map$1$t2b5', + length = 'map$1$length', + winner = 'map$1$winner', + vod = 'map$1$vod' + } + + return bracketData +end + +function MatchGroupLegacyDefault._readOpponent(prefix, scoreKey, bracketType) + return { + ['type'] = 'type', + template = prefix .. 'team', + score = prefix .. scoreKey, + name = prefix, + displayname = prefix .. 'display', + flag = prefix .. 'flag', + win = prefix .. 'win', + ['$notEmpty$'] = bracketType == 'team' and (prefix .. 'team') or prefix + } +end + +--the following variable gets mutaded by each p._getMatchMapping +--it is needed as a basis for the next call +local _lastRound +function MatchGroupLegacyDefault._getMatchMapping(match, bracketData, bracketType, lowerHeader) + local id = String.split(match.match2id, '_')[2] or match.match2id + --remove 0's and dashes from the match param + --e.g. R01-M001 --> R1M1 + id = id:gsub('0*([1-9])', '%1'):gsub('%-', '') + local bd = match.match2bracketdata + + local roundNum + local round + local reset = false + if id == 'RxMTP' then + round = _lastRound + elseif id == 'RxMBR' then + round = _lastRound + round.G = round.G - 2 + round.W = round.W - 2 + round.D = round.D - 2 + reset = true + else + roundNum = id:match('R%d*'):gsub('R', '') + roundNum = tonumber(roundNum) + round = roundData[roundNum] or { R = roundNum, G = 0, D = 1, W = 1 } + end + round.G = round.G + 1 + + --if bd.header starts with '!l' + if string.match(bd.header or '', '^!l') then + lowerHeader[roundNum or ''] = round.G + end + + local opponents = {} + local finished = {} + local scoreKey = (reset and 'score2' or 'score') + for opponentIndex = 1, MAX_NUM_OPPONENTS do + local prefix + if not reset and + (Logic.isEmpty(bd.toupper) and opponentIndex == 1 or + Logic.isEmpty(bd.tolower) and opponentIndex == 2) then + + prefix = 'R' .. round.R .. 'D' .. round.D + round.D = round.D + 1 + else + prefix = 'R' .. round.R .. 'W' .. round.W + round.W = round.W + 1 + end + + opponents[opponentIndex] = MatchGroupLegacyDefault._readOpponent(prefix, scoreKey, bracketType) + finished[opponentIndex] = prefix .. 'win' + end + + match = { + opponent1 = opponents[1], + opponent2 = opponents[2], + finished = finished[1] .. '|' .. finished[2], + -- reference to variables that shall be flattened + ['$flatten$'] = { 'R' .. round.R .. 'G' .. round.G .. 'details' } + } + + bracketData[id] = MatchGroupLegacyDefault.addMaps(match) + _lastRound = round + roundData[round.R] = round + + return bracketData, round.R, lowerHeader +end + +--[[ +custom mappings are used to overwrite the default mappings +in the cases where the default mappings do not fit the +parameter format of the old bracket +]]-- + +--this can be used for custom mappings too +function MatchGroupLegacyDefault.addMaps(match) + for mapIndex = 1, MAX_NUM_MAPS do + match['map' .. mapIndex] = { + ['$ref$'] = 'map', + ['$1$'] = mapIndex + } + end + return match +end + +--this is for custom mappings +function MatchGroupLegacyDefault.matchMappingFromCustom(data, bracketType) + --[[ + data has the form { + opp1, -- e.g. R1D1 + opp2, -- e.g. R1D20 + details, -- e.g. R1G5 + } + ]]-- + bracketType = bracketType or 'team' + + local mapping = { + ['$flatten$'] = {data.details .. 'details'}, + ['finished'] = data.opp1 .. 'win|' .. data.opp2 .. 'win', + opponent1 = MatchGroupLegacyDefault._readOpponent(data.opp1, 'score', bracketType), + opponent2 = MatchGroupLegacyDefault._readOpponent(data.opp2, 'score', bracketType), + } + mapping = MatchGroupLegacyDefault.addMaps(mapping) + + return mapping +end + +--this is for custom mappings for Reset finals matches +--it switches score2 into the place of score +--and sets flatten to nil +function MatchGroupLegacyDefault.matchResetMappingFromCustom(mapping) + local mappingReset = mw.clone(mapping) + mappingReset.opponent1.score = mapping.opponent1.score .. '2' + mappingReset.opponent2.score = mapping.opponent2.score .. '2' + mappingReset['$flatten$'] = nil + return mappingReset +end + +return MatchGroupLegacyDefault diff --git a/components/match2/wikis/wildrift/legacy/match_maps_legacy.lua b/components/match2/wikis/wildrift/legacy/match_maps_legacy.lua new file mode 100644 index 00000000000..c3347f84db4 --- /dev/null +++ b/components/match2/wikis/wildrift/legacy/match_maps_legacy.lua @@ -0,0 +1,298 @@ +--- +-- @Liquipedia +-- wiki=wildrift +-- page=Module:MatchMaps/Legacy +-- +-- Please see https://github.com/Liquipedia/Lua-Modules to contribute +-- + +local MatchMapsLegacy = {} + +local Arguments = require('Module:Arguments') +local Array = require('Module:Array') +local Logic = require('Module:Logic') +local Lua = require('Module:Lua') +local Json = require('Module:Json') +local MatchGroup = require('Module:MatchGroup') +local PageVariableNamespace = require('Module:PageVariableNamespace') +local Table = require('Module:Table') +local Template = require('Module:Template') + +local MatchSubobjects = Lua.import('Module:Match/Subobjects', { requireDevIfEnabled = true }) + +local globalVars = PageVariableNamespace() +local matchlistVars = PageVariableNamespace('LegacyMatchlist') + +local MAX_NUMBER_OF_OPPONENTS = 2 +local MAX_MUMBER_OF_PLAYERS = 5 +local DEFAULT = 'default' +local DEFAULT_WIN = 'W' +local DEFAULT_LOSS = 'L' +local FORFEIT = 'FF' +local TBD = 'tbd' + +---@param args table +---@param details table +---@return table +function MatchMapsLegacy.copyDetailsToArgs(args, details) + for key, value in pairs(details) do + if Logic.isEmpty(args[key]) then + args[key] = value + end + end + args.details = nil + return args +end + +---@param args table +---@param details table +---@return table +function MatchMapsLegacy.setHeaderIfEmpty(args, details) + args.header = args.header or args.date + args.date = details.date or args.date + return args +end + +---@param args table +---@return table +function MatchMapsLegacy.convertOpponents(args) + for index = 1, MAX_NUMBER_OF_OPPONENTS do + local template = args['team' .. index] + if (not template) or template == ' ' then + template = TBD + else + template = string.lower(template) + end + local score + local winner = tonumber(args.winner) + if args.walkover then + if tonumber(args.walkover) ~= 0 then + score = tonumber(args.walkover) == index and DEFAULT_WIN or FORFEIT + end + elseif args['score' .. index] then + score = tonumber(args['score' .. index]) + elseif not args.mapWinnersSet and winner then + score = winner == index and DEFAULT_WIN or DEFAULT_LOSS + end + + if template ~= TBD then + args['opponent' .. index] = { + score = score, + template = template, + type = 'team', + } + end + args['opponent' .. index .. 'literal'] = args['team' .. index .. 'wildrift'] + + args['team' .. index] = nil + args['score' .. index] = nil + end + args.walkover = nil + args.mapWinnersSet = nil + + return args +end + +---@param args table +---@return table +function MatchMapsLegacy.handleLiteralsForOpponents(args) + for index = 1, MAX_NUMBER_OF_OPPONENTS do + if Logic.isEmpty(args['opponent' .. index]) then + args['opponent' .. index] = { + ['type'] = 'literal', template = 'tbd', name = args['opponent' .. index .. 'literal'] + } + end + end + return args +end + +---@param args table +---@param details table +---@return table, table +function MatchMapsLegacy.handleDetails(args, details) + local getMapFromDetails = function (index) + local prefix = 'map' .. index + if not details[prefix] then + return nil + end + local map = {} + map.map = details[prefix] + map.winner = details[prefix .. 'winner'] + --Try to get winner from "MatchMaps" + if Logic.isEmpty(map.winner) then + map.winner = args[prefix .. 'win'] + end + + if details[prefix .. 'length'] and details[prefix .. 'length']:lower() == DEFAULT then + map.walkover = map.winner + end + map.length = details[prefix .. 'length'] + map.vod = details[prefix .. 'vod'] + + for oppIndex = 1, MAX_NUMBER_OF_OPPONENTS do + local side = 'team' .. oppIndex .. 'side' + map[side] = details[prefix .. side] + details[prefix .. side] = nil + for playerIndex = 1, MAX_MUMBER_OF_PLAYERS do + local pick = 't' .. oppIndex .. 'c' .. playerIndex + local ban = 't' .. oppIndex .. 'b' .. playerIndex + map[pick] = details[prefix .. pick] + map[ban] = details[prefix .. ban] + details[prefix .. pick] = nil + details[prefix .. ban] = nil + end + end + details[prefix] = nil + details[prefix .. 'winner'] = nil + details[prefix .. 'length'] = nil + details[prefix .. 'vod'] = nil + return MatchSubobjects.luaGetMap(map) + end + + local getMapOnlyWithWinner = function (index) + if not args['map' .. index .. 'win'] then + return nil + end + return MatchSubobjects.luaGetMap{ + winner = args['map' .. index .. 'win'], + map = DEFAULT + } + end + + Array.mapIndexes(function (index) + local map = getMapFromDetails(index) or getMapOnlyWithWinner(index) + if map and map.winner then + args.mapWinnersSet = true + end + args['map' .. index] = map + args['map' .. index .. 'win'] = nil + details['match' .. index] = nil + return map + end) + return args, details +end + +---@param args table +---@return table +function MatchMapsLegacy.handleLocation(args) + if args.location then + local matchStatus = Logic.readBool(args.finished) and 'was' or 'being' + local locationComment = 'Match ' .. matchStatus .. ' played in ' .. args.location + args.comment = args.comment and (args.comment .. '
' .. locationComment) or locationComment + args.location = nil + end + return args +end + +---@param args table +---@return table +function MatchMapsLegacy.convertMaps(args) + for matchKey, matchJson, matchIndex in Table.iter.pairsByPrefix(args, 'match') do + local map = Json.parse(matchJson) or {} + map.winner = map.win + map.win = nil + map.vod = args['vodgame' .. matchIndex] + local mapKey = 'map' .. matchIndex + for key, value in pairs(map) do + args[mapKey .. key] = value + end + args['vodgame' .. matchIndex] = nil + args[mapKey] = DEFAULT + args[matchKey] = nil + end + return args +end + +-- invoked by Template:BracketMatchSummary +---@param frame Frame +---@return string +function MatchMapsLegacy.convertBracketMatchSummary(frame) + local args = Arguments.getArgs(frame) + args = MatchMapsLegacy.handleLocation(args) + args = MatchMapsLegacy.convertMaps(args) + return Json.stringify(args) +end + +-- invoked by Template:MatchMapsLua +---@param frame Frame +---@return Html +function MatchMapsLegacy.convertMatch(frame) + local args = Arguments.getArgs(frame) + local details = Json.parseIfString(args.details or '{}') + + args, details = MatchMapsLegacy.handleDetails(args, details) + args = MatchMapsLegacy.convertOpponents(args) + args = MatchMapsLegacy.handleLiteralsForOpponents(args) + args = MatchMapsLegacy.setHeaderIfEmpty(args, details) + args = MatchMapsLegacy.copyDetailsToArgs(args, details) + + Template.stashReturnValue(args, 'LegacyMatchlist') + return mw.html.create('div'):css('display', 'none') +end + +-- invoked by Template:MatchListStart +---@param frame Frame +function MatchMapsLegacy.matchListStart(frame) + local args = Arguments.getArgs(frame) + + local store = Logic.nilOr( + Logic.readBoolOrNil(args.store), + not Logic.readBool(globalVars:get('disable_LPDB_storage')) + ) + local matchsection = Logic.nilOr(args.lpdb_title, args.title) + if Logic.readBoolOrNil(matchsection) ~= false then + matchlistVars:set('matchsection', matchsection) + end + + matchlistVars:set('store', tostring(store)) + matchlistVars:set('bracketid', args.id) + matchlistVars:set('matchListTitle', args.title or args[1] or 'Match List') + matchlistVars:set('width', args.width or '300px') + matchlistVars:set('hide', args.hide or 'true') + matchlistVars:set('patch', args.patch) + globalVars:set('islegacy', 'true') +end + +-- invoked by MatchListEnd +---@return string +function MatchMapsLegacy.matchListEnd() + local bracketId = matchlistVars:get('bracketid') + assert(bracketId, 'Missing id') + + local store = Logic.readBool(matchlistVars:get('store')) + local hide = Logic.readBool(matchlistVars:get('hide')) + + local args = { + isLegacy = true, + id = bracketId, + store = store, + noDuplicateCheck = not store, + collapsed = hide, + attached = hide, + title = matchlistVars:get('matchListTitle'), + width = matchlistVars:get('width'), + matchsection = matchlistVars:get('matchsection'), + patch = matchlistVars:get('patch') + } + + local matches = Template.retrieveReturnValues('LegacyMatchlist') + + Array.forEach(matches, function(match, matchIndex) + args['M' .. matchIndex .. 'header'] = match.header + match.header = nil + args['M' .. matchIndex] = Json.stringify(match) + end) + + matchlistVars:delete('store') + matchlistVars:delete('bracketid') + matchlistVars:delete('matchListTitle') + matchlistVars:delete('width') + matchlistVars:delete('hide') + matchlistVars:delete('patch') + matchlistVars:delete('matchsection') + globalVars:delete('islegacy') + + return MatchGroup.MatchList(args) +end + +return MatchMapsLegacy