diff --git a/src/game.rs b/src/game.rs index 827c5c5..45b1251 100644 --- a/src/game.rs +++ b/src/game.rs @@ -47,6 +47,13 @@ pub struct WordResult { score: u32, } +#[derive(Debug, Serialize, Deserialize, Tsify)] +#[tsify(from_wasm_abi)] +pub struct ScoreResult { + words: Vec, + total: u32, +} + pub struct PlayerStates(pub Vec); impl PlayerStates { pub fn get_player_state(&self, name: &str) -> Option<&PlayerState> { @@ -143,7 +150,7 @@ impl Game { &self.dictionary } - pub fn receive_play(&mut self, player: &str, tray_tile_locations: Vec>, commit_move: bool) -> Result, String> { + pub fn receive_play(&mut self, player: &str, tray_tile_locations: Vec>, commit_move: bool) -> Result { let mut board_instance = self.get_board().clone(); let mut tray = self.player_states.get_tray(player).unwrap().clone(); @@ -176,6 +183,7 @@ impl Game { let x = board_instance.calculate_scores(self.get_dictionary())?; + let total_score = x.1; let words: Vec = x.0.iter() .map(|(word, score)| { @@ -189,7 +197,7 @@ impl Game { if commit_move { let mut player_state = self.player_states.get_player_state_mut(player).unwrap(); - player_state.score += x.1; + player_state.score += total_score; player_state.tray = tray; board_instance.fix_tiles(); @@ -197,7 +205,10 @@ impl Game { self.fill_trays() } - Ok(words) + Ok(ScoreResult { + words, + total: total_score, + }) } pub fn exchange_tiles(&mut self, player: &str, tray_tile_locations: Vec) -> Result { diff --git a/ui/src/Game.tsx b/ui/src/Game.tsx index b3fce4c..17f56df 100644 --- a/ui/src/Game.tsx +++ b/ui/src/Game.tsx @@ -1,5 +1,13 @@ import * as React from "react"; -import {GameWasm, Letter as LetterData, MyResult, PlayedTile, PlayerAndScore, Tray, WordResult} from "word_grid"; +import { + GameWasm, + Letter as LetterData, + MyResult, + PlayedTile, + PlayerAndScore, + ScoreResult, + Tray +} from "word_grid"; import { LocationType, matchCoordinate, @@ -169,7 +177,7 @@ export function Game(props: {wasm: GameWasm, settings: Settings}) { return null; }); - const result: MyResult | string> = props.wasm.receive_play("Player", playedTiles, confirmedScorePoints > -1); + const result: MyResult = props.wasm.receive_play("Player", playedTiles, confirmedScorePoints > -1); console.log({result}); if(result.response_type === "ERR") { @@ -184,10 +192,8 @@ export function Game(props: {wasm: GameWasm, settings: Settings}) { } else { - let total_points = 0; - for (let word_result of (result.value as Array)) { - total_points += word_result.score; - } + const score_result = result.value as ScoreResult; + const total_points = score_result.total; let msg: string; if (confirmedScorePoints > -1) {