Skip to content

Commit

Permalink
Add Match Legacy for Heroes (#3374)
Browse files Browse the repository at this point in the history
* Match Legacy for Heroes

* Update match_legacy.lua
  • Loading branch information
Hesketh2 authored Oct 16, 2023
1 parent b141997 commit 5bfd291
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions components/match2/wikis/heroes/match_legacy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
-- @Liquipedia
-- wiki=heroes
-- page=Module:Match/Legacy
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--

local MatchLegacy = {}

local Json = require('Module:Json')
local Logic = require('Module:Logic')
local String = require('Module:StringUtils')
local Table = require('Module:Table')
local Opponent = require('Module:Opponent')

local _GAME_EXTRADATA_CONVERTER = {
ban = 'b',
champion = 'h',
}

function MatchLegacy.storeMatch(match2, options)
local match = MatchLegacy._convertParameters(match2)

if options.storeMatch1 then
match.games = MatchLegacy.storeGames(match, match2)

return mw.ext.LiquipediaDB.lpdb_match(
'legacymatch_' .. match2.match2id,
match
)
end
end

function MatchLegacy._convertParameters(match2)
local match = Table.deepCopy(match2)
for key, _ in pairs(match) do
if String.startsWith(key, 'match2') then
match[key] = nil
end
end
match.links = nil

if Logic.isNotEmpty(match.walkover) then
match.resulttype = match.walkover
match.walkover = match.winner
end

match.staticid = match2.match2id

-- Handle extradata fields
match.extradata = {}
local extradata = Json.parseIfString(match2.extradata)
match.extradata.gamecount = match2.bestof ~= 0 and tostring(match2.bestof) or ''
match.extradata.matchsection = extradata.matchsection
match.extradata.comment = extradata.comment

match.extradata = mw.ext.LiquipediaDB.lpdb_create_json(match.extradata)

-- Handle Opponents
local handleOpponent = function (index)
local prefix = 'opponent'..index
local opponent = match2.match2opponents[index] or {}
local opponentmatch2players = opponent.match2players or {}
if opponent.type == Opponent.team then
match[prefix] = opponent.name
match[prefix..'score'] = (tonumber(opponent.score) or 0) > 0 and opponent.score or 0
local opponentplayers = {}
for i = 1, 10 do
local player = opponentmatch2players[i] or {}
opponentplayers['p' .. i] = player.name or ''
opponentplayers['p' .. i .. 'flag'] = player.flag or ''
opponentplayers['p' .. i .. 'dn'] = player.displayname or ''
end
match[prefix..'players'] = mw.ext.LiquipediaDB.lpdb_create_json(opponentplayers)
elseif opponent.type == Opponent.solo then
local player = opponentmatch2players[1] or {}
match[prefix] = player.name
match[prefix..'score'] = (tonumber(opponent.score) or 0) > 0 and opponent.score or 0
match[prefix..'flag'] = player.flag
elseif opponent.type == Opponent.literal then
match[prefix] = 'TBD'
end
end

handleOpponent(1)
handleOpponent(2)

return match
end

function MatchLegacy.storeGames(match, match2)
local games = {}
for gameIndex, game2 in ipairs(match2.match2games or {}) do
local game = Table.deepCopy(game2)

-- Extradata
local extradata = Json.parseIfString(game2.extradata)
game.extradata = {}
game.extradata.gamenumber = gameIndex
game.extradata.team1side = extradata.team1side
game.extradata.team2side = extradata.team2side

local parameterType, teamIndex, parameterIndex
for key, item in pairs(extradata) do
teamIndex, parameterType, parameterIndex = string.match(key, 'team(%d)(%a+)(%d)')
parameterType = _GAME_EXTRADATA_CONVERTER[parameterType or '']
if parameterType then
game.extradata['t' .. teamIndex .. parameterType .. parameterIndex] = item
end
end

game.extradata = mw.ext.LiquipediaDB.lpdb_create_json(game.extradata)

-- Other stuff
game.opponent1 = match.opponent1
game.opponent2 = match.opponent2
game.opponent1flag = match.opponent1flag
game.opponent2flag = match.opponent2flag
game.date = match.date
local winner = tonumber(game.winner) or 0
game.opponent1score = winner == 1 and 1 or 0
game.opponent2score = winner == 2 and 1 or 0
local res = mw.ext.LiquipediaDB.lpdb_game(
'legacygame_' .. match2.match2id .. '_' .. gameIndex,
game
)
table.insert(games, res)
end
return table.concat(games)
end

return MatchLegacy

0 comments on commit 5bfd291

Please sign in to comment.