WordGrid/src/wasm.rs

134 lines
3.6 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-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)]
pub fn new(seed: u64, dictionary_text: &str) -> GameWasm {
GameWasm(Game::new(seed, dictionary_text, vec!["Player".to_string(), "AI".to_string()]))
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)
}
pub fn receive_play(&mut self, player: &str, 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)?;
let result = self.0.receive_play(player, 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)?)
}
pub fn exchange_tiles(&mut self, player: &str, tray_tile_locations: JsValue) -> Result<JsValue, Error>{
let tray_tile_locations: Vec<bool> = serde_wasm_bindgen::from_value(tray_tile_locations)?;
match self.0.exchange_tiles(player, tray_tile_locations) {
Ok(tray) => {
serde_wasm_bindgen::to_value(&MyResult {
response_type: ResponseType::OK,
value: tray
})
},
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-08-06 01:59:30 +00:00
}