diff --git a/server/machines/round.ts b/server/machines/round.ts index 5bd2d257..04fbe430 100644 --- a/server/machines/round.ts +++ b/server/machines/round.ts @@ -1,8 +1,6 @@ import { assign, createMachine } from "xstate"; import type { Question } from "../@types/entities"; import questions from "../data/questions.json"; -import { turnMachine } from "./turn"; - const context = { questions: questions as Question[], selectedQuestion: {} as Question | undefined, @@ -32,18 +30,21 @@ const roundMachine = createMachine( }, states: { turn: { - entry: ["setQuestion"], // keep track of which questions have been asked in round and/or entire game/lobby - invoke: { - id: "turn", - src: turnMachine, - input: ({ context }) => ({ - selectedQuestion: context.selectedQuestion, - }), - onDone: { - target: "turn", - actions: "processTurn", // need to receive answers from turn machine - }, + entry: ["setQuestion"], // keep track of which questions have been asked in round and/or entire game/lobby] + on: { + turnEnd: { target: "finished", actions: ["processTurn"] }, }, + // invoke: { + // id: "turn", + // src: turnMachine, + // input: ({ context }) => ({ + // selectedQuestion: context.selectedQuestion, + // }), + // onDone: { + // target: "finished", + // actions: "processTurn", // need to receive answers from turn machine + // }, + // }, // guard: if win conditions, finished }, finished: { diff --git a/server/models/round.ts b/server/models/round.ts index 9439afca..d1547995 100644 --- a/server/models/round.ts +++ b/server/models/round.ts @@ -1,11 +1,13 @@ import { type Actor, type InspectionEvent, createActor } from "xstate"; import type { Answer, Question } from "../@types/entities"; import { context, roundMachine } from "../machines/round"; +import { turnMachine } from "../machines/turn"; import type { SocketServer } from "../socketServer"; import { machineLogger } from "../utils/machineLogger"; class Round { machine: Actor; + turnMachine?: Actor; server: SocketServer; constructor(server: SocketServer) { @@ -28,6 +30,8 @@ class Round { this.server.onQuestionSet( this.machine.getSnapshot().context.selectedQuestion as Question, ); + + this.initialiseTurnMachine() break; } default: @@ -35,10 +39,25 @@ class Round { } }); this.machine.start(); + + } + + initialiseTurnMachine() { + this.turnMachine = createActor(turnMachine, { + inspect: (inspectionEvent: InspectionEvent) => { + machineLogger(inspectionEvent, "turn"); + }, + }); + this.turnMachine.subscribe((state) => { + // if state.value === "finished" + // pass answer back to round and kill this instance of the turn machine + }) + this.turnMachine.start(); } addAnswer(answer: Answer) { - this.machine.send({ type: "playerSubmitsAnswer", answer }); + + this.turnMachine?.send({ type: "playerSubmitsAnswer", answer}) } }