2023-08-13 20:08:37 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-08-06 01:59:30 +00:00
|
|
|
use serde_wasm_bindgen::Error;
|
2023-08-13 20:08:37 +00:00
|
|
|
use tsify::Tsify;
|
2023-08-06 01:59:30 +00:00
|
|
|
use wasm_bindgen::JsValue;
|
|
|
|
use wasm_bindgen::prelude::wasm_bindgen;
|
2023-08-19 01:14:27 +00:00
|
|
|
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)]
|
2023-08-19 01:14:27 +00:00
|
|
|
pub struct MyResult<E: Serialize> {
|
2023-08-14 00:02:13 +00:00
|
|
|
response_type: ResponseType,
|
|
|
|
value: E,
|
|
|
|
}
|
2023-08-13 20:08:37 +00:00
|
|
|
|
2023-08-19 01:14:27 +00:00
|
|
|
|
2023-08-13 20:08:37 +00:00
|
|
|
|
2023-08-06 01:59:30 +00:00
|
|
|
#[wasm_bindgen]
|
|
|
|
impl GameWasm {
|
|
|
|
|
|
|
|
#[wasm_bindgen(constructor)]
|
2023-08-13 20:08:37 +00:00
|
|
|
pub fn new(seed: u64, dictionary_text: &str) -> GameWasm {
|
2023-08-16 02:35:23 +00:00
|
|
|
GameWasm(Game::new(seed, dictionary_text, vec!["Player".to_string(), "AI".to_string()]))
|
2023-08-06 01:59:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 02:35:23 +00:00
|
|
|
pub fn get_tray(&self, name: &str) -> Result<JsValue, Error> {
|
2023-08-23 03:52:03 +00:00
|
|
|
let tray = self.0.player_states.get_tray(name);
|
2023-08-06 01:59:30 +00:00
|
|
|
|
2023-08-16 02:35:23 +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-13 20:08:37 +00:00
|
|
|
|
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-08-19 01:14:27 +00:00
|
|
|
pub fn receive_play(&mut self, player: &str, tray_tile_locations: JsValue, commit_move: bool) -> Result<JsValue, Error> {
|
2023-08-13 20:08:37 +00:00
|
|
|
let tray_tile_locations: Vec<Option<PlayedTile>> = serde_wasm_bindgen::from_value(tray_tile_locations)?;
|
|
|
|
|
2023-08-19 01:14:27 +00:00
|
|
|
let result = self.0.receive_play(player, tray_tile_locations, commit_move);
|
2023-08-13 20:08:37 +00:00
|
|
|
|
2023-08-19 01:14:27 +00:00
|
|
|
match result {
|
2023-08-14 00:02:13 +00:00
|
|
|
Ok(x) => {
|
2023-08-19 01:14:27 +00:00
|
|
|
serde_wasm_bindgen::to_value(&MyResult {
|
2023-08-14 00:02:13 +00:00
|
|
|
response_type: ResponseType::OK,
|
2023-08-19 01:14:27 +00:00
|
|
|
value: x
|
|
|
|
})
|
2023-08-14 00:02:13 +00:00
|
|
|
},
|
|
|
|
Err(e) => {
|
2023-08-19 01:14:27 +00:00
|
|
|
serde_wasm_bindgen::to_value(&MyResult {
|
2023-08-14 00:02:13 +00:00
|
|
|
response_type: ResponseType::ERR,
|
|
|
|
value: e
|
2023-08-19 01:14:27 +00:00
|
|
|
})
|
2023-08-14 00:02:13 +00:00
|
|
|
}
|
2023-08-19 01:14:27 +00:00
|
|
|
}
|
2023-08-13 20:08:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-08-16 02:35:23 +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,
|
|
|
|
}
|
|
|
|
|
2023-08-23 03:52:03 +00:00
|
|
|
let scores: Vec<PlayerAndScore> = self.0.player_states.0.iter()
|
2023-08-16 02:35:23 +00:00
|
|
|
.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-08-23 03:52:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-06 01:59:30 +00:00
|
|
|
}
|