Skip to content

Commit

Permalink
WIP again
Browse files Browse the repository at this point in the history
  • Loading branch information
yndajas committed May 30, 2024
1 parent 5a041bf commit dcf7332
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 77 deletions.
111 changes: 67 additions & 44 deletions server/machines/round.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import { assign, createMachine } from "xstate";
import type { Question } from "../@types/entities";
import type { Socket } from "socket.io";
import { assign, setup } from "xstate";
import type { Answer, Question } from "../@types/entities";
import questions from "../data/questions.json";
const context = {
questions: questions as Question[],
selectedQuestion: {} as Question | undefined,
scores: {} as Record<Socket["id"], number>,
};

type Context = typeof context;

type Events = {
type: "turnEnd";
answers: Answer[];
};

const dynamicParamFuncs = {
setQuestion: ({ context }: { context: Context }) => {
return { questions: context.questions };
},
// TODO: updateScores
};
// players are awarded one point for each person who guesses wrong plus any points from the bonus round
// show what answers every gave
// there could be no correct answers - bonus points are then reset
Expand All @@ -19,53 +32,63 @@ type Context = typeof context;
// if all answers are correct ++bonus points -> check win conditions -> next question
// if there are some correct and some incorrect answers add the number of incorrect answers (+ any bonus points - then reset bonus points) to the scores of the players with correct answers -> check win conditions -> next question

const roundMachine = createMachine(
{
context: context,
id: "round",
initial: "turn",
types: {
context: {} as Context,
events: {} as Event,
},
states: {
turn: {
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: {
type: "final",
const roundMachine = setup({
types: {} as {
context: Context;
events: Events;
},

actions: {
setQuestion: assign({
selectedQuestion: (
_,
params: ReturnType<typeof dynamicParamFuncs.setQuestion>,
) => {
const questionIndex = Math.floor(
Math.random() * (params.questions.length - 1),
);
// can we splice the selected question out of the questions array with params.questions.splice[questionIndex, 1]?
return params.questions[questionIndex];
},
}),
updateScores: assign({
// TODO
}),
},
guards: {
noClearWinner: {
// TODO - need to work out how to transition into a new turn either here or in the model
},
},
{
actions: {
setQuestion: assign({
selectedQuestion: (args) => {
const questionIndex = Math.floor(
Math.random() * (args.context.questions.length - 1),
);
// we can splice the selected question out of the questions array with args.context.questions.splice[questionIndex, 1]
return args.context.questions[questionIndex];
}).createMachine({
context: context,
id: "round",
initial: "turn",
types: {
context: {} as Context,
events: {} as Event,
},
states: {
turn: {
entry: [{ type: "setQuestion", params: dynamicParamFuncs.setQuestion }], // keep track of which questions have been asked in round and/or entire game/lobby]
on: {
turnEnd: {
target: "finished",
actions: {
type: "updateScores",
params: dynamicParamFuncs.updateScores,
},
guard: {
type: "noClearWinner",
params: dynamicParamFuncs.noClearWinner,
},
},
}),
processTurn: () => console.log("processing turn"),
},
},
finished: {
type: "final",
},
},
);
});

export { context, roundMachine };
24 changes: 0 additions & 24 deletions server/machines/round.typegen.ts

This file was deleted.

1 change: 0 additions & 1 deletion server/machines/turn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const turnMachine = createMachine(
types: {
context: {} as Context,
events: {} as Event,
typegen: {} as import("./round.typegen").Typegen0,
},
states: {
turnStart: {
Expand Down
15 changes: 7 additions & 8 deletions server/models/round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Round {
this.machine = createActor(roundMachine, {
...context,
inspect: (inspectionEvent: InspectionEvent) => {
machineLogger(inspectionEvent, 'round');
machineLogger(inspectionEvent, "round");
},
});
this.machine.subscribe((state) => {
Expand All @@ -31,15 +31,14 @@ class Round {
this.machine.getSnapshot().context.selectedQuestion as Question,
);

this.initialiseTurnMachine()
this.initialiseTurnMachine();
break;
}
default:
break;
}
});
this.machine.start();

}

initialiseTurnMachine() {
Expand All @@ -49,15 +48,15 @@ class Round {
},
});
this.turnMachine.subscribe((state) => {
// if state.value === "finished"
// pass answer back to round and kill this instance of the turn machine
})
if (state.value === "finished") {
this.machine.send({ type: "turnEnd", answers: state.context.answers });
}
});
this.turnMachine.start();
}

addAnswer(answer: Answer) {

this.turnMachine?.send({ type: "playerSubmitsAnswer", answer})
this.turnMachine?.send({ type: "playerSubmitsAnswer", answer });
}
}

Expand Down

0 comments on commit dcf7332

Please sign in to comment.