From 1ec7c7a49aaf5b1eda53c7ad10d18e56e01e2c98 Mon Sep 17 00:00:00 2001 From: hjpalpha <75081997+hjpalpha@users.noreply.github.com> Date: Fri, 13 Oct 2023 10:36:16 +0200 Subject: [PATCH] Add warcraft match2 copy pasta generator (#3321) * Add warcraft match2 copy pasta generator * rename var as per review --- .../get_match_group_copy_paste_wiki.lua | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 components/match2/wikis/warcraft/get_match_group_copy_paste_wiki.lua diff --git a/components/match2/wikis/warcraft/get_match_group_copy_paste_wiki.lua b/components/match2/wikis/warcraft/get_match_group_copy_paste_wiki.lua new file mode 100644 index 00000000000..e4552fc0e44 --- /dev/null +++ b/components/match2/wikis/warcraft/get_match_group_copy_paste_wiki.lua @@ -0,0 +1,135 @@ +--- +-- @Liquipedia +-- wiki=warcrat +-- page=Module:GetMatchGroupCopyPaste/wiki +-- +-- Please see https://github.com/Liquipedia/Lua-Modules to contribute +-- + +local Array = require('Module:Array') +local Logic = require('Module:Logic') +local Lua = require('Module:Lua') +local Table = require('Module:Table') + +local CopyPaste = Lua.import('Module:GetMatchGroupCopyPaste/wiki/Base', {requireDevIfEnabled = true}) + +local Opponent = require('Module:OpponentLibraries').Opponent + +local INDENT = '\t' +local MODE_CONVERSION = { + ['1v1'] = {Opponent.solo}, + ['2v2'] = {Opponent.duo}, + ['3v3'] = {Opponent.trio}, + ['4v4'] = {Opponent.quad}, + ['literal'] = {Opponent.literal}, + ['team - 1v1'] = {Opponent.solo, isTeamMatch = true}, + ['team - 2v2'] = {Opponent.duo, isTeamMatch = true}, + ['team - 3v3'] = {Opponent.trio, isTeamMatch = true}, + ['team - 4v4'] = {Opponent.quad, isTeamMatch = true}, +} +MODE_CONVERSION.default = MODE_CONVERSION['1v1'] + +local WikiCopyPaste = Table.copy(CopyPaste) + +---Returns the cleaned opponent type +---@param mode string +---@return {[1]: OpponentType, isTeamMatch: boolean?} +---@diagnostic disable-next-line: inject-field +function WikiCopyPaste.getMode(mode) + return MODE_CONVERSION[string.lower(mode or '')] or MODE_CONVERSION.default +end + +---Returns the Code for a Match, depending on the input +---@param bestof number +---@param mode {[1]: OpponentType, isTeamMatch: boolean?} +---@param index number +---@param opponents number +---@param args table +---@return string +---@diagnostic disable-next-line: inject-field +function WikiCopyPaste.getMatchCode(bestof, mode, index, opponents, args) + if bestof == 0 then + args.score = true + end + local score = Logic.readBool(args.score) and '|score=' or '' + + local lines = Array.extend( + '{{Match', + index == 1 and (INDENT .. '|bestof=' .. (bestof ~= 0 and bestof or '')) or nil, + Logic.readBool(args.needsWinner) and INDENT .. '|winner=' or nil, + Logic.readBool(args.hasDate) and {INDENT .. '|date=', INDENT .. '|twitch='} or {} + ) + + for opponentIndex = 1, opponents do + table.insert(lines, INDENT .. '|opponent' .. opponentIndex .. '=' .. WikiCopyPaste._getOpponent(mode, score)) + end + + if bestof > 0 then + local submatchBo = tonumber(args.submatch) + Array.forEach(Array.range(1, bestof), function(gameIndex) + local subgroup = '' + if submatchBo then + subgroup = '|subgroup=' .. (math.floor((gameIndex - 1) / submatchBo) + 1) + elseif mode.isTeamMatch and Logic.readBool(args.submatch) then + subgroup = '|subgroup=' + end + table.insert(lines, INDENT .. '|map' .. gameIndex .. '={{Map|map=|winner=' .. subgroup + .. WikiCopyPaste._mapDetails(opponents, mode)) + end) + end + + local vetoes = tonumber(args.vetoes) or 0 + if vetoes > 0 then + Array.forEach(Array.range(1, vetoes), function(vetoIndex) + local prefix = '|veto' .. vetoIndex + table.insert(lines, INDENT .. prefix .. '=' .. prefix .. 'by=') + end) + end + + table.insert(lines, '}}') + + return table.concat(lines, '\n') +end + +---@param opponents number +---@param mode {[1]: OpponentType, isTeamMatch: boolean?} +---@return string +---@diagnostic disable-next-line: inject-field +function WikiCopyPaste._mapDetails(opponents, mode) + if mode[1] == Opponent.literal then return '' end + local lines = {''} + + Array.forEach(Array.range(1, opponents), function(opponentIndex) + Array.forEach(Array.range(1, Opponent.partySize(mode[1])), function(playerIndex) + local prefix = '|t' .. opponentIndex .. 'p' .. playerIndex + local player = mode.isTeamMatch and (prefix .. '=') or '' + table.insert(lines, INDENT .. INDENT .. player .. prefix .. 'heroes=') + end) + end) + + table.insert(lines, INDENT .. '}}') + + return table.concat(lines, '\n') +end + +---Subfunction used to generate the code for the Opponent template, depending on the type of opponent +---@param mode {[1]: OpponentType, isTeamMatch: boolean?} +---@param score string +---@return string +---@diagnostic disable-next-line: inject-field +function WikiCopyPaste._getOpponent(mode, score) + if mode.isTeamMatch then + return '{{TeamOpponent|template=' .. score .. '}}' + elseif mode[1] == Opponent.literal then + return '{{LiteralOpponent|template=' .. score .. '}}' + end + + local players = '' + Array.forEach(Array.range(1, Opponent.partySize(mode[1])), function(playerIndex) + players = players .. '|p' .. playerIndex .. '=' .. '|p' .. playerIndex .. 'race=' + end) + + return '{{' .. mw.getContentLanguage():ucfirst(mode[1]) .. 'Opponent' .. players .. score .. '}}' +end + +return WikiCopyPaste