Skip to content

Commit a6b1c5a

Browse files
authored
Improved FTA User (#923)
1 parent 4865b1e commit a6b1c5a

File tree

15 files changed

+970
-579
lines changed

15 files changed

+970
-579
lines changed

apps/backend/src/websocket/handlers/field.ts

+102-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export const handleUpdateMatchTeams = async (
205205

206206
console.log(`🖊️ Updating teams for match ${matchId} in division ${divisionId}`);
207207

208-
newTeams.forEach(async newTeam => {
208+
for (const newTeam of newTeams) {
209209
const participantIndex = match.participants.findIndex(
210210
p => p.tableId.toString() === newTeam.tableId
211211
);
@@ -217,13 +217,113 @@ export const handleUpdateMatchTeams = async (
217217
: null
218218
}
219219
);
220-
});
220+
}
221221

222222
callback({ ok: true });
223223
match = await db.getMatch({ _id: new ObjectId(matchId) });
224224
namespace.to('field').emit('matchUpdated', match);
225225
};
226226

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+
227327
export const handleUpdateMatchParticipant = async (
228328
namespace,
229329
divisionId: string,

apps/backend/src/websocket/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import {
3030
handleStartMatch,
3131
handleStartTestMatch,
3232
handleUpdateScoresheet,
33-
handleUpdateMatchTeams
33+
handleUpdateMatchTeams,
34+
handleSwitchMatchTeams,
35+
handleMergeMatches
3436
} from './handlers/field';
3537
import { handleUpdateAudienceDisplay, handleUpdatePresentation } from './handlers/audience-display';
3638

@@ -100,6 +102,10 @@ const websocket = (
100102

101103
socket.on('updateMatchTeams', (...args) => handleUpdateMatchTeams(namespace, ...args));
102104

105+
socket.on('switchMatchTeams', (...args) => handleSwitchMatchTeams(namespace, ...args));
106+
107+
socket.on('mergeMatches', (...args) => handleMergeMatches(namespace, ...args));
108+
103109
socket.on('updateMatchParticipant', (...args) =>
104110
handleUpdateMatchParticipant(namespace, ...args)
105111
);

0 commit comments

Comments
 (0)