WordGrid/src/wasm.rs

179 lines
4.9 KiB
Rust
Raw Normal View History

use serde::{Deserialize, Serialize};
2023-08-06 01:59:30 +00:00
use serde_wasm_bindgen::Error;
use tsify::Tsify;
2023-08-06 01:59:30 +00:00
use wasm_bindgen::JsValue;
use wasm_bindgen::prelude::wasm_bindgen;
use crate::board::{CellType, Letter};
use crate::game::{Game, PlayedTile};
2023-09-10 22:17:36 +00:00
use crate::player_interaction::ai::Difficulty;
2023-08-06 01:59:30 +00:00
#[wasm_bindgen]
pub struct GameWasm(Game);
2023-08-14 00:02:13 +00:00
#[derive(Serialize, Deserialize, Tsify)]
#[tsify(from_wasm_abi)]
pub enum ResponseType {
OK,
ERR,
}
#[derive(Serialize, Deserialize, Tsify)]
#[tsify(from_wasm_abi)]
pub struct MyResult<E: Serialize> {
2023-08-14 00:02:13 +00:00
response_type: ResponseType,
value: E,
}
2023-08-06 01:59:30 +00:00
#[wasm_bindgen]
impl GameWasm {
#[wasm_bindgen(constructor)]
2023-09-10 22:17:36 +00:00
pub fn new(seed: u64, dictionary_text: &str, ai_difficulty: JsValue) -> GameWasm {
let difficulty: Difficulty = serde_wasm_bindgen::from_value(ai_difficulty).unwrap();
GameWasm(Game::new(seed, dictionary_text, vec!["Player".to_string()], vec![difficulty]))
2023-08-06 01:59:30 +00:00
}
pub fn get_tray(&self, name: &str) -> Result<JsValue, Error> {
let tray = self.0.player_states.get_tray(name);
2023-08-06 01:59:30 +00:00
serde_wasm_bindgen::to_value(&tray)
2023-08-06 01:59:30 +00:00
}
2023-08-10 03:00:14 +00:00
pub fn get_board_cell_types(&self) -> Result<JsValue, Error> {
let board = self.0.get_board();
let cell_types: Vec<CellType> = board.cells.iter().map(|cell| -> CellType {
cell.cell_type.clone()
}).collect();
serde_wasm_bindgen::to_value(&cell_types)
}
2023-08-18 01:05:20 +00:00
pub fn get_board_letters(&self) -> Result<JsValue, Error> {
let board = self.0.get_board();
let letters: Vec<Option<Letter>> = board.cells.iter().map(|cell| -> Option<Letter> {
cell.value.clone()
}).collect();
serde_wasm_bindgen::to_value(&letters)
}
2023-09-10 22:17:36 +00:00
pub fn receive_play(&mut self, tray_tile_locations: JsValue, commit_move: bool) -> Result<JsValue, Error> {
let tray_tile_locations: Vec<Option<PlayedTile>> = serde_wasm_bindgen::from_value(tray_tile_locations)?;
2023-09-10 22:17:36 +00:00
let result = self.0.receive_play(tray_tile_locations, commit_move);
match result {
2023-08-14 00:02:13 +00:00
Ok(x) => {
serde_wasm_bindgen::to_value(&MyResult {
2023-08-14 00:02:13 +00:00
response_type: ResponseType::OK,
value: x
})
2023-08-14 00:02:13 +00:00
},
Err(e) => {
serde_wasm_bindgen::to_value(&MyResult {
2023-08-14 00:02:13 +00:00
response_type: ResponseType::ERR,
value: e
})
2023-08-14 00:02:13 +00:00
}
}
}
pub fn get_scores(&self) -> Result<JsValue, JsValue> {
#[derive(Serialize, Deserialize, Tsify)]
#[tsify(from_wasm_abi)]
pub struct PlayerAndScore {
name: String,
score: u32,
}
let scores: Vec<PlayerAndScore> = self.0.player_states.0.iter()
.map(|player_state| {
PlayerAndScore {
name: player_state.player.get_name().to_string(),
score: player_state.score,
}
})
.collect();
Ok(serde_wasm_bindgen::to_value(&scores)?)
}
2023-09-10 22:17:36 +00:00
pub fn exchange_tiles(&mut self, tray_tile_locations: JsValue) -> Result<JsValue, Error>{
let tray_tile_locations: Vec<bool> = serde_wasm_bindgen::from_value(tray_tile_locations)?;
2023-09-10 22:17:36 +00:00
match self.0.exchange_tiles(tray_tile_locations) {
Ok((_, turn_action)) => {
serde_wasm_bindgen::to_value(&MyResult {
response_type: ResponseType::OK,
value: turn_action
})
},
Err(e) => {
serde_wasm_bindgen::to_value(&MyResult {
response_type: ResponseType::ERR,
value: e
})
}
}
}
pub fn add_word(&mut self, word: String) {
self.0.add_word(word);
}
2023-09-10 22:17:36 +00:00
pub fn skip_turn(&mut self) {self.0.increment_turn()}
pub fn advance_turn(&mut self) -> Result<JsValue, Error> {
let result = self.0.advance_turn();
match result {
Ok(x) => {
Ok(serde_wasm_bindgen::to_value(&x)?)
},
Err(e) => {
Ok(serde_wasm_bindgen::to_value(&e)?)
}
}
}
2023-09-15 02:14:19 +00:00
pub fn get_current_player(&self) -> String {
self.0.current_player_name()
}
pub fn get_remaining_tiles(&self) -> usize {
self.0.get_remaining_tiles()
}
pub fn get_player_tile_count(&self, player: &str) -> Result<JsValue, Error> {
match self.0.get_player_tile_count(player) {
Ok(count) => {
Ok(serde_wasm_bindgen::to_value(&MyResult{
response_type: ResponseType::OK,
value: count,
})?)
},
Err(msg) => {
Ok(serde_wasm_bindgen::to_value(&MyResult{
response_type: ResponseType::OK,
value: msg,
})?)
}
}
}
2023-08-06 01:59:30 +00:00
}