Skip to content

Commit 0acb50b

Browse files
refactor: clean up tournaments summary table implementation (#6970)
* use Logic.readBool * use String.upperCaseFirst * use Array.parseCommaSeparatedString * use noneOf * use anyOf * use le/ge * adjust indents * avoid using 'type' as a name * remove unused import
1 parent f040516 commit 0acb50b

1 file changed

Lines changed: 44 additions & 76 deletions

File tree

lua/wikis/commons/TournamentsSummaryTable.lua

Lines changed: 44 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ local Class = Lua.import('Module:Class')
2020
local DateExt = Lua.import('Module:Date/Ext')
2121
local Logic = Lua.import('Module:Logic')
2222
local String = Lua.import('Module:StringUtils')
23-
local Table = Lua.import('Module:Table')
2423
local Template = Lua.import('Module:Template')
2524
local Variables = Lua.import('Module:Variables')
2625

@@ -30,6 +29,7 @@ local ConditionNode = Condition.Node
3029
local Comparator = Condition.Comparator
3130
local BooleanOperator = Condition.BooleanOperator
3231
local ColumnName = Condition.ColumnName
32+
local ConditionUtil = Condition.Util
3333

3434
local SECONDS_PER_DAY = 86400
3535

@@ -85,23 +85,23 @@ function TournamentsSummaryTable.run(args)
8585

8686
TournamentsSummaryTable._parseArgsToSettings(args)
8787

88-
local type
89-
if args.upcoming == 'true' then
90-
type = TournamentsSummaryTable.upcomingType
91-
elseif args.ongoing == 'true' then
92-
type = TournamentsSummaryTable.ongoingType
93-
elseif args.recent == 'true' then
94-
type = TournamentsSummaryTable.recentType
88+
local tableType
89+
if Logic.readBool(args.upcoming) then
90+
tableType = TournamentsSummaryTable.upcomingType
91+
elseif Logic.readBool(args.ongoing) then
92+
tableType = TournamentsSummaryTable.ongoingType
93+
elseif Logic.readBool(args.recent) then
94+
tableType = TournamentsSummaryTable.recentType
9595
else
9696
error('No type parameter (upcoming, ongoing, recent) specified')
9797
end
9898

99-
local title = mw.language.getContentLanguage():ucfirst(args.title or TYPE_TO_TITLE[type])
99+
local title = String.upperCaseFirst(args.title or TYPE_TO_TITLE[tableType])
100100
local limit = args.limit and tonumber(args.limit) or TournamentsSummaryTable.defaultLimit
101-
local sort = args.sort or (type == TournamentsSummaryTable.recentType and 'end' or 'start')
102-
local order = args.order or (type == TournamentsSummaryTable.recentType and 'desc' or 'asc')
101+
local sort = args.sort or (tableType == TournamentsSummaryTable.recentType and 'end' or 'start')
102+
local order = args.order or (tableType == TournamentsSummaryTable.recentType and 'desc' or 'asc')
103103

104-
local data = TournamentsSummaryTable._getTournaments(type, sort, order, limit)
104+
local data = TournamentsSummaryTable._getTournaments(tableType, sort, order, limit)
105105

106106
if Logic.readBool(args.reverseDisplay) then
107107
data = Array.reverse(data)
@@ -110,7 +110,7 @@ function TournamentsSummaryTable.run(args)
110110
local wrapper = mw.html.create():wikitext('*' .. title)
111111

112112
for _, tournamentData in ipairs(data) do
113-
wrapper:wikitext(TournamentsSummaryTable.row(tournamentData, type))
113+
wrapper:wikitext(TournamentsSummaryTable.row(tournamentData, tableType))
114114
end
115115

116116
return wrapper
@@ -128,13 +128,13 @@ function TournamentsSummaryTable._parseArgsToSettings(args)
128128
end
129129

130130
TournamentsSummaryTable.tiers = args.tiers
131-
and Array.map(mw.text.split(args.tiers, ','), parseTier)
131+
and Array.map(Array.parseCommaSeparatedString(args.tiers), parseTier)
132132
or TournamentsSummaryTable.tiers
133133

134134
TournamentsSummaryTable.disableLIS = Logic.readBool(args.disableLIS) or TournamentsSummaryTable.disableLIS
135135

136136
TournamentsSummaryTable.tierTypeExcluded = args.tierTypeExcluded
137-
and Array.map(mw.text.split(args.tierTypeExcluded, ','), parseTier)
137+
and Array.map(Array.parseCommaSeparatedString(args.tierTypeExcluded), parseTier)
138138
or TournamentsSummaryTable.tierTypeExcluded
139139
end
140140

@@ -158,55 +158,32 @@ function TournamentsSummaryTable._getTournaments(conditionType, sort, order, lim
158158
return {}
159159
end
160160

161-
---@param type conditionTypes
161+
---@param tableType conditionTypes
162162
---@return string
163-
function TournamentsSummaryTable._buildConditions(type)
163+
function TournamentsSummaryTable._buildConditions(tableType)
164164
local conditions = ConditionTree(BooleanOperator.all)
165165
:add(TournamentsSummaryTable._tierConditions())
166166
:add(TournamentsSummaryTable._tierTypeConditions())
167167
:add(TournamentsSummaryTable._statusConditions())
168-
:add(TournamentsSummaryTable.dateConditions(type))
169-
:add(TournamentsSummaryTable.additionalConditions(type))
168+
:add(TournamentsSummaryTable.dateConditions(tableType))
169+
:add(TournamentsSummaryTable.additionalConditions(tableType))
170170

171171
return conditions:toString()
172172
end
173173

174-
---@return ConditionTree
174+
---@return ConditionTree?
175175
function TournamentsSummaryTable._tierConditions()
176-
local conditions = ConditionTree(BooleanOperator.any)
177-
for _, tier in pairs(TournamentsSummaryTable.tiers) do
178-
conditions:add({ConditionNode(ColumnName('liquipediatier'), Comparator.eq, tier)})
179-
end
180-
181-
return conditions
176+
return ConditionUtil.anyOf(ColumnName('liquipediatier'), TournamentsSummaryTable.tiers)
182177
end
183178

184-
---@return ConditionTree|{}
179+
---@return ConditionTree?
185180
function TournamentsSummaryTable._tierTypeConditions()
186-
if Table.isEmpty(TournamentsSummaryTable.tierTypeExcluded) then
187-
return {}
188-
end
189-
190-
local conditions = ConditionTree(BooleanOperator.all)
191-
for _, tierType in pairs(TournamentsSummaryTable.tierTypeExcluded) do
192-
conditions:add({ConditionNode(ColumnName('liquipediatiertype'), Comparator.neq, tierType)})
193-
end
194-
195-
return conditions
181+
return ConditionUtil.noneOf(ColumnName('liquipediatiertype'), TournamentsSummaryTable.tierTypeExcluded)
196182
end
197183

198-
---@return ConditionTree|{}
184+
---@return ConditionTree?
199185
function TournamentsSummaryTable._statusConditions()
200-
if Table.isEmpty(TournamentsSummaryTable.statusExcluded) then
201-
return {}
202-
end
203-
204-
local conditions = ConditionTree(BooleanOperator.all)
205-
for _, status in pairs(TournamentsSummaryTable.statusExcluded) do
206-
conditions:add({ConditionNode(ColumnName('status'), Comparator.neq, status)})
207-
end
208-
209-
return conditions
186+
return ConditionUtil.noneOf(ColumnName('status'), TournamentsSummaryTable.statusExcluded)
210187
end
211188

212189
---@param type conditionTypes
@@ -221,33 +198,24 @@ function TournamentsSummaryTable.dateConditions(type)
221198
- TournamentsSummaryTable.completedOffset * SECONDS_PER_DAY)
222199

223200
if type == TournamentsSummaryTable.upcomingType then
224-
conditions
225-
:add({
226-
ConditionNode(ColumnName('startdate'), Comparator.lt, upcomingThreshold),
227-
ConditionNode(ColumnName('startdate'), Comparator.gt, _today),
228-
})
201+
conditions:add{
202+
ConditionNode(ColumnName('startdate'), Comparator.lt, upcomingThreshold),
203+
ConditionNode(ColumnName('startdate'), Comparator.gt, _today),
204+
}
229205
elseif type == TournamentsSummaryTable.ongoingType then
230-
conditions
231-
:add({
232-
ConditionTree(BooleanOperator.any):add({
233-
ConditionNode(ColumnName('startdate'), Comparator.lt, _today),
234-
ConditionNode(ColumnName('startdate'), Comparator.eq, _today),
235-
}),
236-
ConditionTree(BooleanOperator.any):add({
237-
ConditionNode(ColumnName('enddate'), Comparator.gt, _today),
238-
ConditionNode(ColumnName('enddate'), Comparator.eq, _today),
239-
}),
240-
ConditionTree(BooleanOperator.any):add({
241-
ConditionNode(ColumnName('status'), Comparator.neq, 'finished'),
242-
ConditionNode(ColumnName('enddate'), Comparator.gt, _today),
243-
}),
244-
})
206+
conditions:add{
207+
ConditionNode(ColumnName('startdate'), Comparator.le, _today),
208+
ConditionNode(ColumnName('enddate'), Comparator.ge, _today),
209+
ConditionTree(BooleanOperator.any):add{
210+
ConditionNode(ColumnName('status'), Comparator.neq, 'finished'),
211+
ConditionNode(ColumnName('enddate'), Comparator.gt, _today),
212+
},
213+
}
245214
elseif type == TournamentsSummaryTable.recentType then
246-
conditions
247-
:add({
248-
ConditionNode(ColumnName('enddate'), Comparator.gt, completedThreshold),
249-
ConditionNode(ColumnName('enddate'), Comparator.lt, _today),
250-
})
215+
conditions:add{
216+
ConditionNode(ColumnName('enddate'), Comparator.gt, completedThreshold),
217+
ConditionNode(ColumnName('enddate'), Comparator.lt, _today),
218+
}
251219
end
252220

253221
return conditions
@@ -258,10 +226,10 @@ function TournamentsSummaryTable.additionalConditions(type)
258226
end
259227

260228
---@param eventInformation table
261-
---@param type conditionTypes
229+
---@param tableType conditionTypes
262230
---@return string
263-
function TournamentsSummaryTable.row(eventInformation, type)
264-
if type == TournamentsSummaryTable.upcomingType then
231+
function TournamentsSummaryTable.row(eventInformation, tableType)
232+
if tableType == TournamentsSummaryTable.upcomingType then
265233
Variables.varDefine('upcoming_' .. eventInformation.pagename, 1)
266234
end
267235

0 commit comments

Comments
 (0)