diff --git a/src/wasm.rs b/src/wasm.rs index 1614613..985bcb6 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -9,6 +9,19 @@ use crate::game::Game; #[wasm_bindgen] pub struct GameWasm(Game); +#[derive(Serialize, Deserialize, Tsify)] +#[tsify(from_wasm_abi)] +pub enum ResponseType { + OK, + ERR, +} + +#[derive(Serialize, Deserialize, Tsify)] +#[tsify(from_wasm_abi)] +pub struct MyResult { + response_type: ResponseType, + value: E, +} #[derive(Deserialize, Tsify, Copy, Clone)] #[tsify(from_wasm_abi)] @@ -70,11 +83,55 @@ impl GameWasm { } } - board_instance.receive_play(played_letters)?; + match board_instance.receive_play(played_letters) { + Err(e) => { + return Ok(serde_wasm_bindgen::to_value( + &MyResult { + response_type: ResponseType::ERR, + value: e + }).unwrap()); + }, + Ok(_) => {} + } - let result = board_instance.calculate_scores(self.0.get_dictionary())?; + #[derive(Debug, Serialize, Deserialize, Tsify)] + #[tsify(from_wasm_abi)] + struct WordResult { + word: String, + score: u32, + } - Ok(serde_wasm_bindgen::to_value(&&result.1).unwrap()) + let result = board_instance.calculate_scores(self.0.get_dictionary()); + let result = match result { + Ok(x) => { + let words: Vec = x.0.iter() + .map(|(word, score)| { + WordResult { + word: word.to_string(), + score: *score + } + + }) + .collect(); + //let total_score = x.1; + + serde_wasm_bindgen::to_value( + &MyResult { + response_type: ResponseType::OK, + value: words + }).unwrap() + + }, + Err(e) => { + serde_wasm_bindgen::to_value( + &MyResult { + response_type: ResponseType::ERR, + value: e + }).unwrap() + } + }; + + Ok(result) } } \ No newline at end of file diff --git a/ui/src/elements.tsx b/ui/src/elements.tsx index 1a8ad90..60ab153 100644 --- a/ui/src/elements.tsx +++ b/ui/src/elements.tsx @@ -1,5 +1,5 @@ import * as React from "react"; -import {GameWasm, Letter as LetterData, PlayedTile, Tray} from "word_grid"; +import {GameWasm, Letter as LetterData, MyResult, PlayedTile, Tray, WordResult} from "word_grid"; import {createRoot} from "react-dom/client"; import {Children, useMemo, useReducer, useState} from "react"; @@ -141,8 +141,23 @@ export function Game(props: {wasm: GameWasm}) { return null; }); - const result = props.wasm.receive_play(playedTiles, false); + const result: MyResult | string> = props.wasm.receive_play(playedTiles, false); console.log({result}); + + if(result.response_type === "ERR") { + alert(result.value); + } else { + + let total_points = 0; + for (let word_result of (result.value as Array)) { + total_points += word_result.score; + } + + alert(`You would score ${total_points} points.`); + } + + + }}>Check Submission ;