Skip to content

Commit

Permalink
WIP initialise turn machine outside of round machine
Browse files Browse the repository at this point in the history
Rather than invoke the turn machine directly in the round machine we now
use the round model class as an intermediary to initiate the round
machine. Next one the round is in the 'finished' state we will need to
pass the received answers back from the turn to the round machine.
  • Loading branch information
rich committed May 23, 2024
1 parent a4d3573 commit 5a041bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
27 changes: 14 additions & 13 deletions server/machines/round.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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: {
Expand Down
21 changes: 20 additions & 1 deletion server/models/round.ts
Original file line number Diff line number Diff line change
@@ -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<typeof roundMachine>;
turnMachine?: Actor<typeof turnMachine>;
server: SocketServer;

constructor(server: SocketServer) {
Expand All @@ -28,17 +30,34 @@ class Round {
this.server.onQuestionSet(
this.machine.getSnapshot().context.selectedQuestion as Question,
);

this.initialiseTurnMachine()
break;
}
default:
break;
}
});
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})
}
}

Expand Down

0 comments on commit 5a041bf

Please sign in to comment.