-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathRepository.lua
More file actions
166 lines (144 loc) · 6.93 KB
/
Repository.lua
File metadata and controls
166 lines (144 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---
-- @Liquipedia
-- page=Module:TeamParticipants/Repository
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--
local Lua = require('Module:Lua')
local Array = Lua.import('Module:Array')
local FnUtil = Lua.import('Module:FnUtil')
local Json = Lua.import('Module:Json')
local PageVariableNamespace = Lua.import('Module:PageVariableNamespace')
local Table = Lua.import('Module:Table')
local TeamTemplate = Lua.import('Module:TeamTemplate')
local Variables = Lua.import('Module:Variables')
local Opponent = Lua.import('Module:Opponent/Custom')
local prizePoolVars = PageVariableNamespace('PrizePool')
local teamCardsVars = PageVariableNamespace('TeamCards')
local globalVars = PageVariableNamespace()
local TeamParticipantsRepository = {}
--- Save a team participant to lpdb placement table, after merging data from prizepool if exists
---@param participant TeamParticipant
function TeamParticipantsRepository.save(participant)
-- Since we merge data from prizepool and teamparticipants, we need to first fetch the existing record from prizepool
local lpdbData = TeamParticipantsRepository.getPrizepoolRecordForTeam(participant.opponent) or {}
local function generateObjectName()
local team = Opponent.toName(participant.opponent)
local isTbd = Opponent.isTbd(participant.opponent)
if not isTbd then
return 'ranking_' .. mw.ustring.lower(team)
end
-- Ensure names are unique for TBDs
local storageName = 'participant_' .. mw.ustring.lower(team)
local tbdCounter = tonumber(teamCardsVars:get('TBDs')) or 1
storageName = storageName .. '_' .. tbdCounter
teamCardsVars:set('TBDs', tbdCounter + 1)
return storageName
end
-- Use the tournament defaults if no data is provided from prizepool
-- TODO: Refactor in the future to have a util function deal with this, including prizepool, broadcasters, etc.
lpdbData.objectName = lpdbData.objectName or generateObjectName()
lpdbData.tournament = lpdbData.tournament or Variables.varDefault('tournament_name')
lpdbData.parent = lpdbData.parent or Variables.varDefault('tournament_parent')
lpdbData.series = lpdbData.series or Variables.varDefault('tournament_series')
lpdbData.shortname = lpdbData.shortname or Variables.varDefault('tournament_tickername')
lpdbData.mode = lpdbData.mode or Variables.varDefault('tournament_mode')
lpdbData.type = lpdbData.type or Variables.varDefault('tournament_type')
lpdbData.liquipediatier = lpdbData.liquipediatier or Variables.varDefault('tournament_liquipediatier')
lpdbData.liquipediatiertype = lpdbData.liquipediatiertype or Variables.varDefault('tournament_liquipediatiertype')
lpdbData.publishertier = lpdbData.publishertier or Variables.varDefault('tournament_publishertier')
lpdbData.icon = lpdbData.icon or Variables.varDefault('tournament_icon')
lpdbData.icondark = lpdbData.icondark or Variables.varDefault('tournament_icondark')
lpdbData.game = lpdbData.game or Variables.varDefault('tournament_game')
lpdbData.startdate = lpdbData.startdate or Variables.varDefault('tournament_startdate')
lpdbData.date = lpdbData.date or Variables.varDefault('tournament_enddate')
if participant.qualification then
lpdbData.qualifier = participant.qualification.text
if participant.qualification.type == 'tournament' then
lpdbData.qualifierpage = participant.qualification.tournament.pageName
elseif participant.qualification.type == 'external' then
lpdbData.qualifierurl = participant.qualification.url
end
end
lpdbData.extradata = lpdbData.extradata or {}
lpdbData.extradata.opponentaliases = participant.aliases
if participant.potentialQualifiers and #participant.potentialQualifiers > 0 then
local serializedQualifiers = Array.map(participant.potentialQualifiers, Opponent.toName)
lpdbData.extradata.potentialQualifiers = serializedQualifiers
end
-- Remove players that should not be counted for results
local activeOpponent = Table.deepCopy(participant.opponent)
activeOpponent.players = Array.filter(activeOpponent.players or {}, function(player)
return player.extradata.results
end)
-- Add full opponent data for players with results with this team
lpdbData = Table.mergeInto(lpdbData, Opponent.toLpdbStruct(activeOpponent, { setPlayersInTeam = true }))
-- Legacy participant fields
lpdbData = Table.mergeInto(lpdbData, Opponent.toLegacyParticipantData(activeOpponent))
lpdbData.players = lpdbData.opponentplayers
-- Calculate individual prize money (prize money per player on team)
-- Opt to use playerShare over prizepool if available
local prizevalue = lpdbData.extradata and lpdbData.extradata.playershare or lpdbData.prizemoney
if prizevalue then
local filteredPlayers = Array.filter(activeOpponent.players, function(player)
return player.extradata.type ~= 'staff'
end)
local numberOfPlayersOnTeam = math.max(#(filteredPlayers), 1)
lpdbData.individualprizemoney = prizevalue / numberOfPlayersOnTeam
end
mw.ext.LiquipediaDB.lpdb_placement(lpdbData.objectName, Json.stringifySubTables(lpdbData))
end
--- Set wiki variables for team participants to be used in other modules/templates, primarily matches
--- This matches with what HiddenDataBox also does
--- We should change all usages to be more sane structure instead of flat variables in the future
---@param participant TeamParticipant
function TeamParticipantsRepository.setPageVars(participant)
Array.forEach(participant.aliases or {}, function(teamTemplate)
local teamName = TeamTemplate.getPageName(teamTemplate)
if not teamName then
return
end
local teamPrefixes = {
teamName:gsub('_', ' '),
teamName:gsub(' ', '_'),
}
local playerCount, staffCount = 0, 0
Array.forEach(participant.opponent.players or {}, function(player)
local playerPrefix
if player.extradata.type == 'staff' then
staffCount = staffCount + 1
playerPrefix = 'c' .. staffCount
else
playerCount = playerCount + 1
playerPrefix = 'p' .. playerCount
end
Array.forEach(teamPrefixes, function(teamPrefix)
local combinedPrefix = teamPrefix .. '_' .. playerPrefix
globalVars:set(combinedPrefix, player.pageName)
globalVars:set(combinedPrefix .. 'flag', player.flag)
globalVars:set(combinedPrefix .. 'dn', player.displayName)
globalVars:set(combinedPrefix .. 'id', player.apiId)
globalVars:set(combinedPrefix .. 'faction', player.faction)
-- TODO: joindate, leavedate
end)
end)
end)
end
---@param opponent standardOpponent
---@return placement?
function TeamParticipantsRepository.getPrizepoolRecordForTeam(opponent)
if Opponent.isTbd(opponent) then
return
end
local prizepoolRecords = TeamParticipantsRepository.getPrizepoolRecords()
return Array.find(prizepoolRecords, function(record)
return Opponent.same(opponent, Opponent.fromLpdbStruct(record))
end)
end
---@return placement[]
TeamParticipantsRepository.getPrizepoolRecords = FnUtil.memoize(function()
return Array.flatten(Array.mapIndexes(function(prizePoolIndex)
return Json.parseIfString(prizePoolVars:get('placementRecords.' .. prizePoolIndex))
end))
end)
return TeamParticipantsRepository