Fix bug in Rust relating to PlayedTile

The bug broke a contract that was relied on by WASM
This commit is contained in:
Joel Therrien 2023-09-08 17:50:54 -07:00
parent e23c1d139b
commit ef655f99cd
2 changed files with 16 additions and 3 deletions

View file

@ -37,7 +37,7 @@ pub struct PlayerState {
#[tsify(from_wasm_abi)] #[tsify(from_wasm_abi)]
pub struct PlayedTile { pub struct PlayedTile {
pub index: usize, pub index: usize,
pub character: char, pub character: Option<char>, // we only set this if PlayedTile is a blank
} }
#[derive(Debug, Serialize, Deserialize, Tsify)] #[derive(Debug, Serialize, Deserialize, Tsify)]
@ -164,7 +164,15 @@ impl Game {
let coord = Coordinates::new_from_index(played_tile.index); let coord = Coordinates::new_from_index(played_tile.index);
if letter.is_blank { if letter.is_blank {
letter.text = played_tile.character; match played_tile.character {
None => {
panic!("You can't play a blank character without providing a letter value")
}
Some(x) => {
// TODO - check that x is a valid alphabet letter
letter.text = x;
}
}
} }
played_letters.push((letter, coord)); played_letters.push((letter, coord));

View file

@ -72,9 +72,14 @@ impl CompleteMove {
match m { match m {
Some(x) => { Some(x) => {
if letter.partial_match(&x.letter) { if letter.partial_match(&x.letter) {
let character = if x.letter.is_blank {
Some(x.letter.text)
} else {
None
};
played_tiles.push(Some(PlayedTile { played_tiles.push(Some(PlayedTile {
index: x.coordinates.map_to_index(), index: x.coordinates.map_to_index(),
character: x.letter.text, character,
})); }));
*m = None; *m = None;
found_match = true; found_match = true;