Add adding words to dictionary mid-game

This commit is contained in:
Joel Therrien 2023-08-24 20:29:08 -07:00
parent 4f8f14364c
commit 733feecfe3
3 changed files with 37 additions and 2 deletions

View file

@ -229,4 +229,10 @@ impl Game {
} }
pub fn add_word(&mut self, word: String) {
let word = word.to_uppercase();
self.dictionary.insert(word, -1.0);
}
} }

View file

@ -126,5 +126,9 @@ impl GameWasm {
} }
pub fn add_word(&mut self, word: String) {
self.0.add_word(word);
}
} }

View file

@ -124,6 +124,11 @@ export function Game(props: {wasm: GameWasm, settings: Settings}) {
} }
function addWordFn(word: string) {
props.wasm.add_word(word);
logDispatch(<div><em>{word} was added to dictionary.</em></div>);
}
return <> return <>
<TileExchangeModal <TileExchangeModal
@ -168,7 +173,15 @@ export function Game(props: {wasm: GameWasm, settings: Settings}) {
console.log({result}); console.log({result});
if(result.response_type === "ERR") { if(result.response_type === "ERR") {
logDispatch(<div><em>{(result.value as string)}</em></div>); const message = result.value as string;
if (message.endsWith("is not a valid word")) {
// extract out word
const word = message.split(" ")[0];
logDispatch(<div><em>{message}</em><AddWordButton word={word} addWordFn={addWordFn} /></div>);
} else {
logDispatch(<div><em>{message}</em></div>);
}
} else { } else {
let total_points = 0; let total_points = 0;
@ -202,5 +215,17 @@ export function Game(props: {wasm: GameWasm, settings: Settings}) {
}}>Return Tiles</button> }}>Return Tiles</button>
</>; </>;
}
function AddWordButton(props: {word: string, addWordFn: (x: string) => void}) {
const [isClicked, setIsClicked] = useState<boolean>(false);
return <button
disabled={isClicked}
onClick={() => {
setIsClicked(true);
props.addWordFn(props.word);
}}>
Add to dictionary
</button>
} }