@@ -205,7 +205,7 @@ export const handleUpdateMatchTeams = async (
205
205
206
206
console . log ( `🖊️ Updating teams for match ${ matchId } in division ${ divisionId } ` ) ;
207
207
208
- newTeams . forEach ( async newTeam => {
208
+ for ( const newTeam of newTeams ) {
209
209
const participantIndex = match . participants . findIndex (
210
210
p => p . tableId . toString ( ) === newTeam . tableId
211
211
) ;
@@ -217,13 +217,113 @@ export const handleUpdateMatchTeams = async (
217
217
: null
218
218
}
219
219
) ;
220
- } ) ;
220
+ }
221
221
222
222
callback ( { ok : true } ) ;
223
223
match = await db . getMatch ( { _id : new ObjectId ( matchId ) } ) ;
224
224
namespace . to ( 'field' ) . emit ( 'matchUpdated' , match ) ;
225
225
} ;
226
226
227
+ export const handleSwitchMatchTeams = async (
228
+ namespace ,
229
+ divisionId : string ,
230
+ fromMatchId : string ,
231
+ toMatchId : string ,
232
+ participantIndex : number ,
233
+ callback
234
+ ) => {
235
+ let fromMatch = await db . getMatch ( { _id : new ObjectId ( fromMatchId ) } ) ;
236
+ let toMatch = await db . getMatch ( { _id : new ObjectId ( toMatchId ) } ) ;
237
+
238
+ if ( ! fromMatch || ! toMatch ) {
239
+ callback ( { ok : false , error : `Could not find match(es) ${ fromMatchId } /${ toMatchId } !` } ) ;
240
+ return ;
241
+ }
242
+
243
+ if ( fromMatch . status !== 'not-started' || toMatch . status !== 'not-started' ) {
244
+ callback ( { ok : false , error : `Match(es) ${ fromMatchId } /${ toMatchId } are not editable!` } ) ;
245
+ return ;
246
+ }
247
+
248
+ console . log (
249
+ `🖊️ Switching teams with index ${ participantIndex } from match ${ fromMatchId } to match ${ toMatchId } in division ${ divisionId } `
250
+ ) ;
251
+
252
+ await db . updateMatch (
253
+ { _id : new ObjectId ( fromMatchId ) } ,
254
+ { [ `participants.${ participantIndex } .teamId` ] : toMatch . participants [ participantIndex ] . teamId }
255
+ ) ;
256
+
257
+ await db . updateMatch (
258
+ { _id : new ObjectId ( toMatchId ) } ,
259
+ { [ `participants.${ participantIndex } .teamId` ] : fromMatch . participants [ participantIndex ] . teamId }
260
+ ) ;
261
+
262
+ callback ( { ok : true } ) ;
263
+ fromMatch = await db . getMatch ( { _id : new ObjectId ( fromMatchId ) } ) ;
264
+ toMatch = await db . getMatch ( { _id : new ObjectId ( toMatchId ) } ) ;
265
+ namespace . to ( 'field' ) . emit ( 'matchUpdated' , fromMatch ) ;
266
+ namespace . to ( 'field' ) . emit ( 'matchUpdated' , toMatch ) ;
267
+ } ;
268
+
269
+ export const handleMergeMatches = async (
270
+ namespace ,
271
+ divisionId : string ,
272
+ fromMatchId : string ,
273
+ toMatchId : string ,
274
+ callback
275
+ ) => {
276
+ let fromMatch = await db . getMatch ( { _id : new ObjectId ( fromMatchId ) } ) ;
277
+ let toMatch = await db . getMatch ( { _id : new ObjectId ( toMatchId ) } ) ;
278
+
279
+ if ( ! fromMatch || ! toMatch ) {
280
+ callback ( { ok : false , error : `Could not find match(es) ${ fromMatchId } /${ toMatchId } !` } ) ;
281
+ return ;
282
+ }
283
+
284
+ if ( fromMatch . status !== 'not-started' || toMatch . status !== 'not-started' ) {
285
+ callback ( { ok : false , error : `Match(es) ${ fromMatchId } /${ toMatchId } are not editable!` } ) ;
286
+ return ;
287
+ }
288
+
289
+ console . log ( `🔄 Merging match ${ fromMatchId } into match ${ toMatchId } in division ${ divisionId } ` ) ;
290
+
291
+ const fromMatchNewParticipants = fromMatch . participants . map ( participant => ( {
292
+ ...participant ,
293
+ teamId : null
294
+ } ) ) ;
295
+
296
+ const teamsToMerge = fromMatch . participants . filter (
297
+ participant => participant . teamId && participant . team . registered
298
+ ) ;
299
+
300
+ const toMatchNewParticipants = toMatch . participants . map ( participant => {
301
+ if ( ! participant . teamId || ! participant . team . registered ) {
302
+ // Destructure out some properties, should be unused
303
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
304
+ const { team, teamId, ...rest } = participant ;
305
+ return {
306
+ ...rest ,
307
+ teamId : teamsToMerge . shift ( ) ?. teamId ?? null
308
+ } ;
309
+ }
310
+ return participant ;
311
+ } ) ;
312
+
313
+ await db . updateMatch (
314
+ { _id : new ObjectId ( fromMatchId ) } ,
315
+ { participants : fromMatchNewParticipants , status : 'completed' }
316
+ ) ;
317
+
318
+ await db . updateMatch ( { _id : new ObjectId ( toMatchId ) } , { participants : toMatchNewParticipants } ) ;
319
+
320
+ callback ( { ok : true } ) ;
321
+ fromMatch = await db . getMatch ( { _id : new ObjectId ( fromMatchId ) } ) ;
322
+ toMatch = await db . getMatch ( { _id : new ObjectId ( toMatchId ) } ) ;
323
+ namespace . to ( 'field' ) . emit ( 'matchUpdated' , fromMatch ) ;
324
+ namespace . to ( 'field' ) . emit ( 'matchUpdated' , toMatch ) ;
325
+ } ;
326
+
227
327
export const handleUpdateMatchParticipant = async (
228
328
namespace ,
229
329
divisionId : string ,
0 commit comments