Fix bug where UI wasn't detecting 7-letter word bonus

This commit is contained in:
Joel Therrien 2023-08-24 20:36:41 -07:00
parent 733feecfe3
commit cdfd8b5ee9
2 changed files with 26 additions and 9 deletions

View file

@ -47,6 +47,13 @@ pub struct WordResult {
score: u32,
}
#[derive(Debug, Serialize, Deserialize, Tsify)]
#[tsify(from_wasm_abi)]
pub struct ScoreResult {
words: Vec<WordResult>,
total: u32,
}
pub struct PlayerStates(pub Vec<PlayerState>);
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<Option<PlayedTile>>, commit_move: bool) -> Result<Vec<WordResult>, String> {
pub fn receive_play(&mut self, player: &str, tray_tile_locations: Vec<Option<PlayedTile>>, commit_move: bool) -> Result<ScoreResult, String> {
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<WordResult> = 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<bool>) -> Result<Tray, String> {

View file

@ -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<Array<WordResult> | string> = props.wasm.receive_play("Player", playedTiles, confirmedScorePoints > -1);
const result: MyResult<ScoreResult | string> = 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<WordResult>)) {
total_points += word_result.score;
}
const score_result = result.value as ScoreResult;
const total_points = score_result.total;
let msg: string;
if (confirmedScorePoints > -1) {