Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP remaining iterations from #106 #180

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions server/machines/round.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
import type { Socket } from "socket.io";
import { assign, setup } from "xstate";
import type { Question } from "../@types/entities";
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
// all players could be correct and no score would be awarded but 1 point is added to the next round
// first to 10

// count how many players have correct answers
// if 0 correct answers set bonus points to 0 -> check win conditions -> next question
// 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 = setup({
types: {} as {
context: Context;
events: Events;
},

actions: {
Expand All @@ -29,17 +47,42 @@ const roundMachine = setup({
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
}),
Comment on lines +54 to +56
Copy link
Member Author

@yndajas yndajas Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See this suggestion: #106 (comment)

},
guards: {
noClearWinner: {
// TODO - need to work out how to transition into a new turn either here or in the model
},
},
}).createMachine({
context,
id: "round",
initial: "turn",
states: {
turn: {
entry: [{ type: "setQuestion", params: dynamicParamFuncs.setQuestion }],
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,
},
},
},
},
finished: {
type: "final",
},
},
});
Expand Down
7 changes: 6 additions & 1 deletion server/models/round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { machineLogger } from "../utils/loggingUtils";
class Round {
machine: Actor<typeof roundMachine>;
server: SocketServer;
turnMachine: Actor<typeof turnMachine> | undefined;
turnMachine?: Actor<typeof turnMachine>;

constructor(server: SocketServer) {
this.server = server;
Expand Down Expand Up @@ -43,6 +43,11 @@ class Round {
.selectedQuestion as Question,
},
});
this.turnMachine.subscribe((state) => {
if (state.value === "finished") {
this.machine.send({ type: "turnEnd", answers: state.context.answers });
}
});
this.turnMachine.start();
}

Expand Down
Loading