|
212 | 212 | ); |
213 | 213 |
|
214 | 214 | unsubscribers.push( |
215 | | - gameRound.subscribe(async (value) => { |
| 215 | + gameRound.subscribe((value) => { |
216 | 216 | if (!value) { |
217 | 217 | return; |
218 | 218 | } |
|
230 | 230 | let lastSeenRoundStatus: GameRound["status"] | null = null; |
231 | 231 | unsubscribers.push( |
232 | 232 | gameRoundStatus.subscribe(async (value) => { |
233 | | - if (Boolean(value) !== Boolean($gameRound)) { |
| 233 | + const previousValue = lastSeenRoundStatus; |
| 234 | + lastSeenRoundStatus = value; |
| 235 | + const gameRoundValue = svelteStore.get(gameRound); |
| 236 | + if (Boolean(value) !== Boolean(gameRoundValue)) { |
234 | 237 | throw new Error("gameRoundStatus and $gameRound out of sync"); |
235 | 238 | } |
236 | 239 |
|
237 | 240 | /** |
238 | 241 | * Handle when the round ends in any way. PATCH the round in the backend (if applicable.) |
239 | 242 | * The `ongoing` / new round creation case (POST) is not handled here, see loadRound. |
240 | 243 | */ |
241 | | - if (value !== "ongoing" && lastSeenRoundStatus === "ongoing") { |
| 244 | + if (value !== "ongoing" && previousValue === "ongoing") { |
242 | 245 | const roundAttributeUpdates: Partial<Round["attributes"]> = {}; |
243 | | - if (!$gameRound) { |
| 246 | + if (!gameRoundValue) { |
244 | 247 | throw new Error("round is falsy"); |
245 | 248 | } |
246 | 249 |
|
247 | 250 | if (value === "completed") { |
248 | | - if ($totalScore === null) { |
| 251 | + const totalScoreValue = svelteStore.get(totalScore); |
| 252 | + if (totalScoreValue === null) { |
249 | 253 | throw new Error("totalScore is undefined"); |
250 | 254 | } |
251 | 255 |
|
252 | 256 | roundAttributeUpdates.score = computeTotalScore( |
253 | | - $totalScore, |
254 | | - $gameRound, |
| 257 | + totalScoreValue, |
| 258 | + gameRoundValue, |
255 | 259 | ); |
256 | 260 | roundAttributeUpdates.status = "completed"; |
257 | 261 |
|
258 | 262 | // New device best score? |
259 | | - if (roundAttributeUpdates.score > ($deviceBestScore ?? 0)) { |
| 263 | + if (roundAttributeUpdates.score > (svelteStore.get(deviceBestScore) ?? 0)) { |
260 | 264 | deviceBestScore.set(roundAttributeUpdates.score); |
261 | 265 | gameRound.update((value) => { |
262 | 266 | if (!value) { |
|
276 | 280 | roundAttributeUpdates.status = "abandoned"; |
277 | 281 | } |
278 | 282 |
|
279 | | - if ($isOrganizationUrl) { |
280 | | - if ($gameRound.id === "local-only") { |
| 283 | + if (svelteStore.get(isOrganizationUrl)) { |
| 284 | + if (gameRoundValue.id === "local-only") { |
281 | 285 | throw new Error("Round ID is 'local-only'"); |
282 | 286 | } |
283 | 287 | await api.patchResource<Round>({ |
284 | 288 | attributes: roundAttributeUpdates, |
285 | | - id: $gameRound.id, |
| 289 | + id: gameRoundValue.id, |
286 | 290 | type: "round", |
287 | 291 | }); |
288 | 292 | } |
289 | 293 | } |
290 | | -
|
291 | | - lastSeenRoundStatus = value; |
292 | 294 | }), |
293 | 295 | ); |
294 | 296 |
|
|
0 commit comments