From 78061aacb3f61817b2deff6eb8d12d96a08bd090 Mon Sep 17 00:00:00 2001 From: sgfost Date: Thu, 12 Dec 2024 13:42:27 -0700 Subject: [PATCH] fix: don't run participant status check until a game is initialized a stupid hardcoded timeout hack was the only measure in place for this previously --- client/src/api/study/request.ts | 5 ++++- client/src/views/ProlificStudy.vue | 10 ++++++---- server/src/rooms/sologame/commands.ts | 7 +++++++ server/src/services/study.ts | 9 +++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/client/src/api/study/request.ts b/client/src/api/study/request.ts index de6756305..0d1cc4b6d 100644 --- a/client/src/api/study/request.ts +++ b/client/src/api/study/request.ts @@ -7,7 +7,10 @@ export class StudyAPI { constructor(public store: TStore, public ajax: AjaxRequest) {} async getProlificParticipantStatus(): Promise { - return this.ajax.get(url("/study/prolific/status"), ({ data }) => { + return this.ajax.get(url("/study/prolific/status"), ({ data, status }) => { + if (status !== 200) { + return null; + } return data; }); } diff --git a/client/src/views/ProlificStudy.vue b/client/src/views/ProlificStudy.vue index d90dd6b53..09933048e 100644 --- a/client/src/views/ProlificStudy.vue +++ b/client/src/views/ProlificStudy.vue @@ -120,10 +120,9 @@ export default class ProlificStudy extends Vue { this.api.room = await this.$client.create(SOLO_ROOM_NAME, { type }); applySoloGameServerResponses(this.api.room, this); this.started = true; - setTimeout(() => { - // wait a bit for the room to be initialized + this.api.room.onMessage("ready", () => { this.fetchProlificParticipantStatus(); - }, 3 * 1000); + }); } catch (err) { console.log("Error creating game room"); console.error(err); @@ -132,7 +131,10 @@ export default class ProlificStudy extends Vue { async fetchProlificParticipantStatus() { this.statusLoading = true; - this.participantStatus = await this.studyApi.getProlificParticipantStatus(); + const status = await this.studyApi.getProlificParticipantStatus(); + if (status) { + this.participantStatus = status; + } this.statusLoading = false; } diff --git a/server/src/rooms/sologame/commands.ts b/server/src/rooms/sologame/commands.ts index c2e76fd52..892142d19 100644 --- a/server/src/rooms/sologame/commands.ts +++ b/server/src/rooms/sologame/commands.ts @@ -23,6 +23,7 @@ export class InitGameCmd extends Cmd<{ user: User }> { new SetGameParamsCmd(), new PersistGameCmd(), new SetFirstRoundCmd(), + new BroadcastReadyCmd(), ]; } } @@ -120,6 +121,12 @@ export class SetFirstRoundCmd extends CmdWithoutPayload { } } +export class BroadcastReadyCmd extends CmdWithoutPayload { + execute() { + this.room.broadcast("ready", { kind: "ready" }); + } +} + export class SendHiddenParamsCmd extends CmdWithoutPayload { execute() { const data: any = {}; diff --git a/server/src/services/study.ts b/server/src/services/study.ts index 7faf50b20..bb4bafc88 100644 --- a/server/src/services/study.ts +++ b/server/src/services/study.ts @@ -54,6 +54,15 @@ export class StudyService extends BaseService { ]; let currentStep = 1; + // fail nicely if one of the games hasn't been initialized yet + if (soloPlayers.some(p => !p.game)) { + throw new ServerError({ + code: 202, + message: "Game not yet initialized", + displayMessage: "Game not yet initialized", + }); + } + const hasPlayedBaseline = !!soloPlayers.find(p => p.game.type === "prolificBaseline"); const hasPlayedVariable = !!soloPlayers.find(p => p.game.type === "prolificVariable"); if (hasPlayedVariable) {