/
update.ts
279 lines (235 loc) · 10.4 KB
/
update.ts
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { Id, IdSeeding, Match, MatchGame, Round, Seeding, SeedOrdering, Status } from 'brackets-model';
import { ordering } from './ordering';
import { BaseUpdater } from './base/updater';
import { ChildCountLevel, DeepPartial } from './types';
import * as helpers from './helpers';
export class Update extends BaseUpdater {
/**
* Updates partial information of a match. Its id must be given.
*
* This will update related matches accordingly.
*
* @param match Values to change in a match.
*/
public async match<M extends Match = Match>(match: DeepPartial<M>): Promise<void> {
if (match.id === undefined)
throw Error('No match id given.');
const stored = await this.storage.select('match', match.id);
if (!stored) throw Error('Match not found.');
await this.updateMatch(stored, match);
}
/**
* Updates partial information of a match game. Its id must be given.
*
* This will update the parent match accordingly.
*
* @param game Values to change in a match game.
*/
public async matchGame<G extends MatchGame = MatchGame>(game: DeepPartial<G>): Promise<void> {
const stored = await this.findMatchGame(game);
await this.updateMatchGame(stored, game);
}
/**
* Updates the seed ordering of every ordered round in a stage.
*
* @param stageId ID of the stage.
* @param seedOrdering A list of ordering methods.
*/
public async ordering(stageId: Id, seedOrdering: SeedOrdering[]): Promise<void> {
const stage = await this.storage.select('stage', stageId);
if (!stage) throw Error('Stage not found.');
helpers.ensureNotRoundRobin(stage);
const roundsToOrder = await this.getOrderedRounds(stage);
if (seedOrdering.length !== roundsToOrder.length)
throw Error('The count of seed orderings is incorrect.');
for (let i = 0; i < roundsToOrder.length; i++)
await this.updateRoundOrdering(roundsToOrder[i], seedOrdering[i]);
}
/**
* Updates the seed ordering of a round.
*
* @param roundId ID of the round.
* @param method Seed ordering method.
*/
public async roundOrdering(roundId: Id, method: SeedOrdering): Promise<void> {
const round = await this.storage.select('round', roundId);
if (!round) throw Error('This round does not exist.');
const stage = await this.storage.select('stage', round.stage_id);
if (!stage) throw Error('Stage not found.');
helpers.ensureNotRoundRobin(stage);
await this.updateRoundOrdering(round, method);
}
/**
* Updates child count of all matches of a given level.
*
* @param level The level at which to act.
* @param id ID of the chosen level.
* @param childCount The target child count.
*/
public async matchChildCount(level: ChildCountLevel, id: Id, childCount: number): Promise<void> {
switch (level) {
case 'stage':
await this.updateStageMatchChildCount(id, childCount);
break;
case 'group':
await this.updateGroupMatchChildCount(id, childCount);
break;
case 'round':
await this.updateRoundMatchChildCount(id, childCount);
break;
case 'match':
const match = await this.storage.select('match', id);
if (!match) throw Error('Match not found.');
await this.adjustMatchChildGames(match, childCount);
break;
default:
throw Error('Unknown child count level.');
}
}
/**
* Updates the seeding of a stage.
*
* @param stageId ID of the stage.
* @param seeding The new seeding.
* @param keepSameSize Whether to keep the same size as before for the stage. **Default:** false.
*/
public async seeding(stageId: Id, seeding: Seeding, keepSameSize = false): Promise<void> {
await this.updateSeeding(stageId, { seeding }, keepSameSize);
}
/**
* Updates the seeding of a stage (with a list of IDs).
*
* @param stageId ID of the stage.
* @param seedingIds The new seeding, containing only IDs.
* @param keepSameSize Whether to keep the same size as before for the stage. **Default:** false.
*/
public async seedingIds(stageId: Id, seedingIds: IdSeeding, keepSameSize = false): Promise<void> {
await this.updateSeeding(stageId, { seedingIds }, keepSameSize);
}
/**
* Confirms the seeding of a stage.
*
* This will convert TBDs to BYEs and propagate them.
*
* @param stageId ID of the stage.
*/
public async confirmSeeding(stageId: Id): Promise<void> {
await this.confirmCurrentSeeding(stageId);
}
/**
* Update the seed ordering of a round.
*
* @param round The round of which to update the ordering.
* @param method The new ordering method.
*/
private async updateRoundOrdering(round: Round, method: SeedOrdering): Promise<void> {
const matches = await this.storage.select('match', { round_id: round.id });
if (!matches) throw Error('This round has no match.');
if (matches.some(match => match.status > Status.Ready))
throw Error('At least one match has started or is completed.');
const stage = await this.storage.select('stage', round.stage_id);
if (!stage) throw Error('Stage not found.');
if (stage.settings.size === undefined) throw Error('Undefined stage size.');
const group = await this.storage.select('group', round.group_id);
if (!group) throw Error('Group not found.');
const inLoserBracket = helpers.isLoserBracket(stage.type, group.number);
const roundCountLB = helpers.getLowerBracketRoundCount(stage.settings.size);
const seeds = helpers.getSeeds(inLoserBracket, round.number, roundCountLB, matches.length);
const positions = ordering[method](seeds);
await this.applyRoundOrdering(round.number, matches, positions);
}
/**
* Updates child count of all matches of a stage.
*
* @param stageId ID of the stage.
* @param childCount The target child count.
*/
private async updateStageMatchChildCount(stageId: Id, childCount: number): Promise<void> {
if (!await this.storage.update('match', { stage_id: stageId }, { child_count: childCount }))
throw Error('Could not update the match.');
const matches = await this.storage.select('match', { stage_id: stageId });
if (!matches) throw Error('This stage has no match.');
for (const match of matches)
await this.adjustMatchChildGames(match, childCount);
}
/**
* Updates child count of all matches of a group.
*
* @param groupId ID of the group.
* @param childCount The target child count.
*/
private async updateGroupMatchChildCount(groupId: Id, childCount: number): Promise<void> {
if (!await this.storage.update('match', { group_id: groupId }, { child_count: childCount }))
throw Error('Could not update the match.');
const matches = await this.storage.select('match', { group_id: groupId });
if (!matches) throw Error('This group has no match.');
for (const match of matches)
await this.adjustMatchChildGames(match, childCount);
}
/**
* Updates child count of all matches of a round.
*
* @param roundId ID of the round.
* @param childCount The target child count.
*/
private async updateRoundMatchChildCount(roundId: Id, childCount: number): Promise<void> {
if (!await this.storage.update('match', { round_id: roundId }, { child_count: childCount }))
throw Error('Could not update the match.');
const matches = await this.storage.select('match', { round_id: roundId });
if (!matches) throw Error('This round has no match.');
for (const match of matches)
await this.adjustMatchChildGames(match, childCount);
}
/**
* Updates the ordering of participants in a round's matches.
*
* @param roundNumber The number of the round.
* @param matches The matches of the round.
* @param positions The new positions.
*/
private async applyRoundOrdering(roundNumber: number, matches: Match[], positions: number[]): Promise<void> {
for (const match of matches) {
const updated = { ...match };
updated.opponent1 = helpers.findPosition(matches, positions.shift()!);
// The only rounds where we have a second ordered participant are first rounds of brackets (upper and lower).
if (roundNumber === 1)
updated.opponent2 = helpers.findPosition(matches, positions.shift()!);
if (!await this.storage.update('match', updated.id, updated))
throw Error('Could not update the match.');
}
}
/**
* Adds or deletes match games of a match based on a target child count.
*
* @param match The match of which child games need to be adjusted.
* @param targetChildCount The target child count.
*/
private async adjustMatchChildGames(match: Match, targetChildCount: number): Promise<void> {
const games = await this.storage.select('match_game', { parent_id: match.id });
let childCount = games ? games.length : 0;
while (childCount < targetChildCount) {
const id = await this.storage.insert('match_game', {
number: childCount + 1,
stage_id: match.stage_id,
parent_id: match.id,
status: match.status,
opponent1: { id: null },
opponent2: { id: null },
});
if (id === -1)
throw Error('Could not adjust the match games when inserting.');
childCount++;
}
while (childCount > targetChildCount) {
const deleted = await this.storage.delete('match_game', {
parent_id: match.id,
number: childCount,
});
if (!deleted)
throw Error('Could not adjust the match games when deleting.');
childCount--;
}
if (!await this.storage.update('match', match.id, { ...match, child_count: targetChildCount }))
throw Error('Could not update the match.');
}
}