@@ -12,11 +12,12 @@ export type State = Readonly<
1212 phase : "in-game" ;
1313 goal : string ;
1414 guess : string ;
15+ wordsGuessed : number ;
1516 wordPack : readonly string [ ] ;
1617 }
1718 | {
1819 phase : "post-game" ;
19- goal : string ;
20+ wordsGuessed : number ;
2021 wordPack : readonly string [ ] ;
2122 }
2223> ;
@@ -28,10 +29,24 @@ export function getInitialState(): State {
2829export type Action =
2930 | { type : "load-data" ; wordPack : readonly string [ ] }
3031 | { type : "start-game" }
31- | { type : "update-guess" ; newGuess : string } ;
32+ | { type : "update-guess" ; newGuess : string }
33+ | { type : "end-game" } ;
3234
3335export function reducer ( state : State , action : Action ) : State {
3436 switch ( action . type ) {
37+ case "end-game" : {
38+ // No-op if not in a game.
39+ if ( state . phase !== "in-game" ) {
40+ return state ;
41+ }
42+
43+ return {
44+ phase : "post-game" ,
45+ wordsGuessed : state . wordsGuessed ,
46+ wordPack : state . wordPack ,
47+ } ;
48+ }
49+
3550 case "load-data" : {
3651 // No-op if not in pre-game phase.
3752 if ( state . phase !== "pre-game" ) {
@@ -57,6 +72,7 @@ export function reducer(state: State, action: Action): State {
5772 phase : "in-game" ,
5873 goal : getRandomElement ( wordPack ) ,
5974 guess : "" ,
75+ wordsGuessed : 0 ,
6076 wordPack,
6177 } ;
6278 }
@@ -69,9 +85,10 @@ export function reducer(state: State, action: Action): State {
6985
7086 if ( normalizeString ( action . newGuess ) === state . goal ) {
7187 return {
72- phase : "post-game" ,
73- goal : state . goal ,
74- wordPack : state . wordPack ,
88+ ...state ,
89+ wordsGuessed : state . wordsGuessed + 1 ,
90+ goal : getRandomElement ( state . wordPack ) ,
91+ guess : "" ,
7592 } ;
7693 }
7794
0 commit comments