Tournament SSE tweaks

This commit is contained in:
Kalle
2024-09-02 17:07:18 +03:00
parent 751862e6e1
commit 708c266de4
2 changed files with 41 additions and 11 deletions

View File

@@ -5,5 +5,7 @@ const globalForEmitter = global as unknown as {
};
export const emitter = globalForEmitter.emitter ?? new EventEmitter();
// the default of 10 is not relevant for us because we use it for server-sent events
emitter.setMaxListeners(0);
globalForEmitter.emitter = emitter;

View File

@@ -121,6 +121,8 @@ export const action: ActionFunction = async ({ params, request }) => {
})
: null;
let emitMatchUpdate = false;
let emitBracketUpdate = false;
switch (data._action) {
case "REPORT_SCORE": {
// they are trying to report score that was already reported
@@ -215,6 +217,9 @@ export const action: ActionFunction = async ({ params, request }) => {
}
})();
emitMatchUpdate = true;
emitBracketUpdate = true;
break;
}
case "SET_ACTIVE_ROSTER": {
@@ -243,6 +248,8 @@ export const action: ActionFunction = async ({ params, request }) => {
activeRosterUserIds: data.roster,
});
emitMatchUpdate = true;
break;
}
case "UNDO_REPORT_SCORE": {
@@ -310,6 +317,9 @@ export const action: ActionFunction = async ({ params, request }) => {
}
})();
emitMatchUpdate = true;
emitBracketUpdate = true;
break;
}
case "UPDATE_REPORTED_SCORE": {
@@ -373,6 +383,9 @@ export const action: ActionFunction = async ({ params, request }) => {
}
})();
emitMatchUpdate = true;
emitBracketUpdate = true;
break;
}
case "BAN_PICK": {
@@ -433,6 +446,8 @@ export const action: ActionFunction = async ({ params, request }) => {
type: match.roundMaps.pickBan === "BAN_2" ? "BAN" : "PICK",
});
emitMatchUpdate = true;
break;
}
case "REOPEN_MATCH": {
@@ -481,6 +496,9 @@ export const action: ActionFunction = async ({ params, request }) => {
});
})();
emitMatchUpdate = true;
emitBracketUpdate = true;
break;
}
case "SET_AS_CASTED": {
@@ -492,6 +510,8 @@ export const action: ActionFunction = async ({ params, request }) => {
twitchAccount: data.twitchAccount,
});
emitBracketUpdate = true;
break;
}
case "LOCK": {
@@ -507,6 +527,8 @@ export const action: ActionFunction = async ({ params, request }) => {
tournamentId: tournament.ctx.id,
});
emitMatchUpdate = true;
break;
}
case "UNLOCK": {
@@ -517,6 +539,8 @@ export const action: ActionFunction = async ({ params, request }) => {
tournamentId: tournament.ctx.id,
});
emitMatchUpdate = true;
break;
}
default: {
@@ -524,17 +548,21 @@ export const action: ActionFunction = async ({ params, request }) => {
}
}
emitter.emit(matchSubscriptionKey(match.id), {
eventId: nanoid(),
userId: user.id,
});
emitter.emit(bracketSubscriptionKey(tournament.ctx.id), {
matchId: match.id,
scores,
isOver:
scores[0] === Math.ceil(match.bestOf / 2) ||
scores[1] === Math.ceil(match.bestOf / 2),
});
if (emitMatchUpdate) {
emitter.emit(matchSubscriptionKey(match.id), {
eventId: nanoid(),
userId: user.id,
});
}
if (emitBracketUpdate) {
emitter.emit(bracketSubscriptionKey(tournament.ctx.id), {
matchId: match.id,
scores,
isOver:
scores[0] === Math.ceil(match.bestOf / 2) ||
scores[1] === Math.ceil(match.bestOf / 2),
});
}
clearTournamentDataCache(tournamentId);