Skip to content

Commit d14610b

Browse files
authored
feat: Use StandardTournament in TournamentsListing (#7314)
* Annotate LpdbQueryParams * Extend standardTournament with used values * Adjust to use StandardTournament * Use annotation in mock * Use fullname, correct fields in conditions * Fix limit handling * Fix limit handling * chore: update visual snapshots * Add organizers * More uses of StandardTournament * Fix invalid tiertype condition * chore: update visual snapshots * Linter --------- Co-authored-by: mbergen <16326643+mbergen@users.noreply.github.com>
1 parent bd79ae4 commit d14610b

5 files changed

Lines changed: 64 additions & 46 deletions

File tree

lua/wikis/commons/Lpdb.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ function Lpdb.isStorageDisabled()
3131
return Logic.readBool(Variables.varDefault('disable_LPDB_storage'))
3232
end
3333

34+
---@class LpdbQueryParameters
35+
---@field conditions string?
36+
---@field offset integer?
37+
---@field limit integer?
38+
---@field query string?
39+
---@field groupby string?
40+
---@field order string?
41+
3442
-- Executes a mass query.
3543
--[==[
3644
Loops LPDB queries to e.g.
@@ -67,7 +75,7 @@ example:
6775
]==]
6876
---@generic T
6977
---@param tableName `T`
70-
---@param queryParameters table
78+
---@param queryParameters LpdbQueryParameters
7179
---@param itemChecker fun(item: T): boolean?
7280
---@param limit number?
7381
function Lpdb.executeMassQuery(tableName, queryParameters, itemChecker, limit)

lua/wikis/commons/Mock/Lpdb.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ dbStructure.squadplayer = {
131131
---- groupby
132132
---@generic T:LpdbBaseData
133133
---@param dbTable `T`
134-
---@param parameters table
134+
---@param parameters LpdbQueryParameters
135135
---@return T[]
136136
function mockLpdb.lpdb(dbTable, parameters)
137137
local lpdbData = mockLpdb._getMockData(dbTable)

lua/wikis/commons/Tournament.lua

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,40 @@ local TOURNAMENT_PHASE = {
4141
---@class StandardTournament: StandardTournamentPartial
4242
---@field startDate {year: integer, month: integer?, day: integer?, timestamp: integer?}?
4343
---@field endDate {year: integer, month: integer?, day: integer?, timestamp: integer?}?
44+
---@field locations table
4445
---@field region string?
46+
---@field type string
4547
---@field featured boolean
4648
---@field status string?
4749
---@field phase TournamentPhase
4850
---@field tierOptions table
51+
---@field prizepool number
52+
---@field participantsNumber number
4953
---@field extradata table
54+
---@field organizers table
5055
---@field isHighlighted fun(self: StandardTournament, options?: table): boolean
5156

5257
---@param conditions string|AbstractConditionNode?
5358
---@param filterTournament? fun(tournament: StandardTournament): boolean
59+
---@param queryProps LpdbQueryParameters?
5460
---@return StandardTournament[]
55-
function Tournament.getAllTournaments(conditions, filterTournament)
61+
function Tournament.getAllTournaments(conditions, filterTournament, queryProps)
5662
local tournaments = {}
63+
local limit = queryProps and Table.extract(queryProps, 'limit') or nil
5764
Lpdb.executeMassQuery(
5865
'tournament',
59-
{
66+
Table.merge({
6067
conditions = conditions and tostring(conditions),
6168
order = 'sortdate desc',
6269
limit = 1000,
63-
},
70+
}, queryProps),
6471
function(record)
6572
local tournament = Tournament.tournamentFromRecord(record)
6673
if not filterTournament or filterTournament(tournament) then
6774
table.insert(tournaments, tournament)
6875
end
69-
end
76+
end,
77+
limit
7078
)
7179
return tournaments
7280
end
@@ -134,13 +142,18 @@ function Tournament.tournamentFromRecord(record)
134142
liquipediaTier = Tier.toIdentifier(tier),
135143
liquipediaTierType = Tier.toIdentifier(tierType) --[[ @as string? ]],
136144
publisherTier = record.publishertier,
145+
locations = record.locations,
137146
region = (record.locations or {}).region1,
147+
type = record.type,
138148
status = record.status,
139149
icon = record.icon,
140150
iconDark = record.icondark,
141151
series = record.series,
142152
game = record.game,
143153
tierOptions = tierOptions,
154+
prizepool = record.prizepool,
155+
participantsNumber = record.participantsnumber,
156+
organizers = record.organizers,
144157
extradata = extradata,
145158
}
146159

@@ -171,9 +184,15 @@ function Tournament.calculatePhase(tournament)
171184
return TOURNAMENT_PHASE.FINISHED
172185
end
173186

187+
---@class DateRecord
188+
---@field year integer
189+
---@field month integer?
190+
---@field day integer?
191+
---@field timestamp integer?
192+
174193
--- This function parses fuzzy dates into a structured format.
175194
---@param dateRecord string? # date in the format of `YYYY-MM-DD`, with `-MM-DD` optional.
176-
---@return {year: integer, month: integer?, day: integer?, timestamp: integer?}?
195+
---@return DateRecord?
177196
function Tournament.parseDateRecord(dateRecord)
178197
if not dateRecord then
179198
return nil

lua/wikis/commons/TournamentsListing/CardList.lua

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ local LeagueIcon = Lua.import('Module:LeagueIcon')
2121
local Region = Lua.import('Module:Region')
2222
local String = Lua.import('Module:StringUtils')
2323
local Table = Lua.import('Module:Table')
24+
local Tournament = Lua.import('Module:Tournament')
2425

2526
local Opponent = Lua.import('Module:Opponent/Custom')
2627
local OpponentDisplay = Lua.import('Module:OpponentDisplay/Custom')
2728

2829
local Conditions = Lua.import('Module:TournamentsListing/Conditions')
29-
local HighlightConditions = Lua.import('Module:HighlightConditions')
3030
local Tier = Lua.import('Module:Tier/Custom')
3131

3232
local TableWidgets = Lua.import('Module:Widget/Table2/All')
@@ -56,6 +56,7 @@ local DEFAULT_LIMIT = 5000
5656
---@field showRank boolean
5757
---@field noLis boolean
5858
---@field offset number
59+
---@field limit number
5960
---@field allowedPlacements string[]
6061
---@field dynamicPlacements boolean
6162
---@field onlyHighlightOnValue string?
@@ -123,6 +124,7 @@ function BaseTournamentsListing:readConfig()
123124
showRank = Logic.readBool(Logic.nilOr(args.showRank)),
124125
noLis = Logic.readBool(args.noLis),
125126
offset = tonumber(args.offset) or 0,
127+
limit = tonumber(args.limit) or DEFAULT_LIMIT,
126128
allowedPlacements = self:_allowedPlacements(),
127129
dynamicPlacements = Logic.readBool(args.dynamicPlacements),
128130
onlyHighlightOnValue = args.onlyHighlightOnValue,
@@ -138,22 +140,19 @@ end
138140

139141
---@return self
140142
function BaseTournamentsListing:create()
141-
local data = self.args.data or self:_query()
143+
local data = self.args.data and Array.map(self.args.data, Tournament.tournamentFromRecord) or self:_query()
142144
if Table.isNotEmpty(data) then
143145
self.data = data
144146
end
145147

146148
return self
147149
end
148150

149-
---@return table
151+
---@return StandardTournament[]
150152
function BaseTournamentsListing:_query()
151-
return mw.ext.LiquipediaDB.lpdb('tournament', {
152-
conditions = self:buildConditions(),
153-
query = 'pagename, name, icon, icondark, organizers, startdate, enddate, status, locations, series, '
154-
.. 'prizepool, participantsnumber, game, liquipediatier, liquipediatiertype, extradata, publishertier, type',
153+
return Tournament.getAllTournaments(self:buildConditions(), nil, {
155154
order = self.args.order,
156-
limit = self.args.limit or DEFAULT_LIMIT,
155+
limit = self.config.limit,
157156
offset = self.config.offset,
158157
})
159158
end
@@ -248,20 +247,20 @@ function BaseTournamentsListing:_header()
248247
end
249248

250249
---@private
251-
---@param tournamentData table
250+
---@param tournamentData StandardTournament
252251
---@return Widget
253252
function BaseTournamentsListing:_row(tournamentData)
254253
local config = self.config
255254

256-
local highlight = config.showHighlight and self:getHighlightClass(tournamentData) or nil
255+
local highlight = config.showHighlight and tournamentData:isHighlighted(self.config) or nil
257256
local status = tournamentData.status and tournamentData.status:lower()
258257

259258
if config.showRank then
260259
self:_calculateRank(tonumber(tournamentData.prizepool) or 0)
261260
end
262261

263262
local prizeValue = tonumber(tournamentData.prizepool) or 0
264-
local participantNumber = tonumber(tournamentData.participantsnumber) or -1
263+
local participantNumber = tonumber(tournamentData.participantsNumber) or -1
265264

266265
local placements = self:_fetchPlacementData(tournamentData)
267266

@@ -277,13 +276,13 @@ function BaseTournamentsListing:_row(tournamentData)
277276
} or nil,
278277
TableWidgets.Cell{
279278
attributes = {
280-
['data-sort-value'] = tournamentData.name
279+
['data-sort-value'] = tournamentData.fullName
281280
},
282281
children = LeagueIcon.display{
283282
icon = tournamentData.icon,
284-
iconDark = tournamentData.icondark,
285-
link = tournamentData.parent,
286-
name = tournamentData.name,
283+
iconDark = tournamentData.iconDark,
284+
link = tournamentData.pageName,
285+
name = tournamentData.fullName,
287286
options = {noTemplate = true},
288287
}
289288
},
@@ -292,11 +291,11 @@ function BaseTournamentsListing:_row(tournamentData)
292291
['text-decoration'] = status == CANCELLED and 'line-through' or nil,
293292
},
294293
attributes = {
295-
['data-sort-value'] = tournamentData.name,
294+
['data-sort-value'] = tournamentData.fullName,
296295
},
297296
children = LinkWidget{
298-
children = tournamentData.name,
299-
link = tournamentData.pagename,
297+
children = tournamentData.fullName,
298+
link = tournamentData.pageName,
300299
}
301300
},
302301
config.showOrganizer
@@ -309,7 +308,7 @@ function BaseTournamentsListing:_row(tournamentData)
309308
css = {
310309
['font-style'] = (status == POSTPONED or status == DELAYED) and 'italic' or nil,
311310
},
312-
children = BaseTournamentsListing._dateDisplay(tournamentData.startdate, tournamentData.enddate, status)
311+
children = BaseTournamentsListing._dateDisplay(tournamentData.startDate, tournamentData.endDate, status)
313312
},
314313
TableWidgets.Cell{
315314
children = prizeValue > 0
@@ -378,7 +377,7 @@ function BaseTournamentsListing:_calculateRank(prize)
378377
end
379378

380379
---@private
381-
---@param tournamentData table
380+
---@param tournamentData StandardTournament
382381
---@return string[]
383382
function BaseTournamentsListing._organizerDisplay(tournamentData)
384383
local organizers = Logic.emptyOr(tournamentData.organizers) or {}
@@ -446,8 +445,8 @@ function BaseTournamentsListing._displayLocation(locationData, locationIndex)
446445
end
447446

448447
---@private
449-
---@param startDate string
450-
---@param endDate string
448+
---@param startDate DateRecord
449+
---@param endDate DateRecord
451450
---@param status string?
452451
---@return Widget|string
453452
function BaseTournamentsListing._dateDisplay(startDate, endDate, status)
@@ -459,7 +458,7 @@ function BaseTournamentsListing._dateDisplay(startDate, endDate, status)
459458
end
460459

461460
---@private
462-
---@param tournamentData table
461+
---@param tournamentData StandardTournament
463462
---@return {qualified: table[]?, [1]: table[]?, [2]: table[]?}
464463
function BaseTournamentsListing:_fetchPlacementData(tournamentData)
465464
local placements = {}
@@ -501,7 +500,7 @@ function BaseTournamentsListing:_fetchPlacementData(tournamentData)
501500

502501
local opponent = Opponent.fromLpdbStruct(item)
503502
if not opponent then
504-
mw.logObject({pageName = tournamentData.pagename, place = item.placement}, 'Invalid Prize Pool Data returned from')
503+
mw.logObject({pageName = tournamentData.pageName, place = item.placement}, 'Invalid Prize Pool Data returned from')
505504
elseif Opponent.isTbd(opponent) then
506505
opponent = Opponent.tbd(Opponent.team)
507506
end
@@ -531,26 +530,18 @@ function BaseTournamentsListing.participantsNumber(number)
531530
return LANG:formatNum(number)
532531
end
533532

534-
-- overwritable in case wikis want several highlight options
535-
---@protected
536-
---@param tournamentData table
537-
---@return boolean
538-
function BaseTournamentsListing:getHighlightClass(tournamentData)
539-
return HighlightConditions.tournament(tournamentData, self.config)
540-
end
541-
542-
---@param tournamentData table
533+
---@param tournamentData StandardTournament
543534
---@return string?
544535
function BaseTournamentsListing:displayTier(tournamentData)
545-
local tier, tierType, options = Tier.parseFromQueryData(tournamentData)
536+
local options = tournamentData.tierOptions
546537
options.link = true
547538
if self.config.onlyTierTypeIfBoth then
548539
options.onlyTierTypeIfBoth = true
549540
else
550541
options.tierTypeShort = true
551542
end
552543

553-
return Tier.display(tier, tierType, options)
544+
return Tier.display(tournamentData.liquipediaTier, tournamentData.liquipediaTierType, options)
554545
end
555546

556547
return BaseTournamentsListing

lua/wikis/commons/TournamentsListing/Conditions.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,15 @@ function TournamentsListingConditions.base(args)
147147
return conditions
148148
end
149149

150-
---@param tournamentData table
150+
---@param tournamentData StandardTournament
151151
---@param config table
152152
---@return string
153153
function TournamentsListingConditions.placeConditions(tournamentData, config)
154154
local conditions = ConditionTree(BooleanOperator.all)
155155
:add{
156-
ConditionNode(ColumnName('liquipediatier'), Comparator.eq, tournamentData.liquipediatier),
157-
ConditionNode(ColumnName('liquipediatiertype'), Comparator.eq, tournamentData.liquipediatiertype),
158-
ConditionNode(ColumnName(config.useParent and 'parent' or 'pagename'), Comparator.eq, tournamentData.pagename),
156+
ConditionNode(ColumnName('liquipediatier'), Comparator.eq, tournamentData.liquipediaTier),
157+
ConditionNode(ColumnName('liquipediatiertype'), Comparator.eq, tournamentData.liquipediaTierType or ''),
158+
ConditionNode(ColumnName(config.useParent and 'parent' or 'pagename'), Comparator.eq, tournamentData.pageName),
159159
ConditionNode(ColumnName('placement'), Comparator.neq, '')
160160
}
161161

0 commit comments

Comments
 (0)