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-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)]
|
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-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
|
|
|
}
|
|
|
|
|
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-09-10 22:17:36 +00:00
|
|
|
pub fn receive_play(&mut self, 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-09-10 22:17:36 +00:00
|
|
|
let result = self.0.receive_play(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
|
|
|
|
2023-09-10 22:17:36 +00:00
|
|
|
pub fn exchange_tiles(&mut self, tray_tile_locations: JsValue) -> Result<JsValue, Error>{
|
2023-08-23 03:52:03 +00:00
|
|
|
|
|
|
|
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) {
|
2023-09-15 01:53:47 +00:00
|
|
|
Ok((_, turn_action)) => {
|
2023-08-23 03:52:03 +00:00
|
|
|
serde_wasm_bindgen::to_value(&MyResult {
|
|
|
|
response_type: ResponseType::OK,
|
2023-09-15 01:53:47 +00:00
|
|
|
value: turn_action
|
2023-08-23 03:52:03 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
serde_wasm_bindgen::to_value(&MyResult {
|
|
|
|
response_type: ResponseType::ERR,
|
|
|
|
value: e
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-25 03:29:08 +00:00
|
|
|
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()}
|
|
|
|
|
2023-09-13 05:13:36 +00:00
|
|
|
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-08-23 03:52:03 +00:00
|
|
|
|
2023-08-06 01:59:30 +00:00
|
|
|
}
|