-
Notifications
You must be signed in to change notification settings - Fork 0
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
Attempt at seperating round + turn machines #106
Closed
+172
−82
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0bb5287
Attempt at seperating round + turn machines
db9017f
WIP fix some types/names
yndajas f95dcf5
WIP turn machine iterations
yndajas 05dd674
Add logging function
9048300
Add annotation for logging machine name
fb0d954
WIP initialise turn machine outside of round machine
18d9d96
WIP again
yndajas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,94 @@ | ||
import { assign, createMachine } from "xstate"; | ||
import type { Answer } 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"; | ||
|
||
type Question = { | ||
answer: string[]; | ||
number: number; | ||
question: string; | ||
}; | ||
|
||
const context = { | ||
answers: [] as Answer[], | ||
questions: questions as Question[], | ||
selectedQuestion: {} as Question | undefined, | ||
scores: {} as Record<Socket["id"], number>, | ||
}; | ||
|
||
type Context = typeof context; | ||
|
||
type Event = { | ||
type: "playerSubmitsAnswer"; | ||
answer: Answer; | ||
type Events = { | ||
type: "turnEnd"; | ||
answers: Answer[]; | ||
}; | ||
|
||
const roundMachine = createMachine( | ||
{ | ||
context, | ||
id: "round", | ||
initial: "roundStart", | ||
types: { | ||
context: {} as Context, | ||
events: {} as Event, | ||
typegen: {} as import("./round.typegen").Typegen0, | ||
}, | ||
states: { | ||
roundStart: { | ||
entry: ["setQuestion"], | ||
on: { | ||
playerSubmitsAnswer: { actions: "addAnswer", target: "countdown" }, | ||
}, | ||
}, | ||
countdown: { | ||
on: { | ||
playerSubmitsAnswer: { actions: "addAnswer" }, | ||
}, | ||
after: { | ||
[15000]: { target: "finished" }, | ||
}, | ||
}, | ||
finished: { | ||
type: "final", | ||
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: { | ||
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: { | ||
addAnswer: assign({ | ||
answers: (args) => [...args.context.answers, args.event.answer], | ||
}), | ||
setQuestion: assign({ | ||
selectedQuestion: (args) => { | ||
const questionIndex = Math.floor( | ||
Math.random() * (args.context.questions.length - 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, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}, | ||
finished: { | ||
type: "final", | ||
}, | ||
}, | ||
); | ||
}); | ||
|
||
export { context, roundMachine }; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { assign, createMachine } from "xstate"; | ||
import type { Answer, Question } from "../@types/entities"; | ||
|
||
const context = { | ||
answers: [] as Answer[], | ||
selectedQuestion: {} as Question | undefined, | ||
}; | ||
|
||
type Context = typeof context; | ||
|
||
type Event = { | ||
type: "playerSubmitsAnswer"; | ||
answer: Answer; | ||
}; | ||
|
||
const turnMachine = createMachine( | ||
{ | ||
context: context, | ||
id: "turn", | ||
initial: "turnStart", | ||
types: { | ||
context: {} as Context, | ||
events: {} as Event, | ||
}, | ||
states: { | ||
turnStart: { | ||
on: { | ||
playerSubmitsAnswer: { actions: "addAnswer", target: "countdown" }, | ||
}, | ||
}, | ||
countdown: { | ||
on: { | ||
playerSubmitsAnswer: { actions: "addAnswer" }, | ||
}, | ||
after: { | ||
[15000]: { target: "finished" }, | ||
}, | ||
}, | ||
finished: { | ||
type: "final", | ||
// pass answers back to round machine | ||
}, | ||
}, | ||
}, | ||
{ | ||
actions: { | ||
addAnswer: assign({ | ||
answers: (args) => [...args.context.answers, args.event.answer], | ||
}), | ||
}, | ||
}, | ||
); | ||
|
||
export { context, turnMachine }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { InspectionEvent } from "xstate"; | ||
|
||
export const machineLogger = ( | ||
inspectionEvent: InspectionEvent, | ||
machine: "turn" | "round", | ||
) => { | ||
if (inspectionEvent.type === "@xstate.event") { | ||
console.info(inspectionEvent.event, `machine: ${machine}`); | ||
} | ||
if (inspectionEvent.type === "@xstate.snapshot") { | ||
console.info(inspectionEvent.snapshot, `machine: ${machine}`); | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Liz suggested that we calculate the points for the turn in the turn, then pass that up to the round which can update the ongoing score tracking with the new points. Not sure if this will help us unpick some of the complexity we need to work through with
updateScores
ornoClearWinner
, but it feels like a good idea?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, when you say it like that it makes sense