use std::collections::HashMap;
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
-use web_sys::{window, Document, Request, RequestInit, Window};
+use web_sys::{window, Document, Window};
use crate::constants::BACKEND_URL;
Ok(element_class.contains(class))
}
-pub fn create_json_request(target_url: &str, json_body: &str) -> Result<Request, String> {
- let mut req_init: RequestInit = RequestInit::new();
- req_init.body(Some(&JsValue::from_str(json_body)));
- req_init.method("POST");
- // TODO omit the NoCors when hosted on website
- req_init.mode(web_sys::RequestMode::NoCors);
- // req_init.headers(
- // &JsValue::from_str("{'Content-Type': 'application/json'}"),
- // &JsValue::from_serde("{'Content-Type': 'application/json'}")
- // .map_err(|e| format!("{}", e))?,
- // &JsValue::from_serde("'headers': { 'Content-Type': 'application/json' }")
- // .map_err(|e| format!("{}", e))?,
- // );
-
- let request: Request =
- Request::new_with_str_and_init(target_url, &req_init).map_err(|e| format!("{:?}", e))?;
- request
- .headers()
- .set("Content-Type", "application/json")
- .map_err(|e| format!("{:?}", e))?;
- request
- .headers()
- .set("Accept", "application/json")
- .map_err(|e| format!("{:?}", e))?;
-
- Ok(request)
-}
-
pub async fn send_to_backend(entries: HashMap<String, String>) -> Result<String, String> {
let mut send_json_string = String::from("{");
for (key, value) in entries {
use std::fmt::Display;
use std::rc::Rc;
+#[allow(dead_code)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GameState {
MainMenu,
}
impl GameState {
+ #[allow(dead_code)]
pub fn is_networked_multiplayer(&self) -> bool {
matches!(
*self,
)
}
+ #[allow(dead_code)]
pub fn set_networked_paired(&mut self) {
if let GameState::NetworkedMultiplayer {
ref mut paired,
}
}
+ #[allow(dead_code)]
pub fn get_networked_current_side(&self) -> Option<Turn> {
if let GameState::NetworkedMultiplayer {
paired: _,
}
}
+ #[allow(dead_code)]
pub fn set_networked_current_side(&mut self, side: Option<Turn>) {
if let GameState::NetworkedMultiplayer {
paired: _,
}
}
+ #[allow(dead_code)]
pub fn get_current_turn(&self) -> Turn {
if let GameState::SinglePlayer(turn, _) = *self {
turn
}
}
+ #[allow(dead_code)]
pub fn set_networked_current_turn(&mut self, turn: Turn) {
if let GameState::NetworkedMultiplayer {
paired: _,
}
}
+ #[allow(dead_code)]
pub fn get_phrase(&self) -> Option<String> {
if let GameState::NetworkedMultiplayer {
paired: _,
}
}
+ #[allow(dead_code)]
pub fn get_singleplayer_current_side(&self) -> Option<Turn> {
if let GameState::SinglePlayer(turn, _) = *self {
Some(turn)
}
}
+#[allow(dead_code)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum BoardState {
Empty,
}
impl BoardState {
+ #[allow(dead_code)]
pub fn is_empty(&self) -> bool {
*self == BoardState::Empty
}
+ #[allow(dead_code)]
pub fn is_win(self) -> bool {
match self {
BoardState::Empty | BoardState::Cyan | BoardState::Magenta => false,
}
}
+ #[allow(dead_code)]
pub fn into_win(self) -> Self {
match self {
BoardState::Empty => BoardState::Empty,
}
}
- pub fn from_win(&self) -> Self {
- match *self {
+ #[allow(dead_code, clippy::wrong_self_convention)]
+ pub fn from_win(self) -> Self {
+ match self {
BoardState::Empty => BoardState::Empty,
BoardState::Cyan | BoardState::CyanWin => BoardState::Cyan,
BoardState::Magenta | BoardState::MagentaWin => BoardState::Magenta,
}
}
+#[allow(dead_code)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Turn {
CyanPlayer,
}
impl Turn {
+ #[allow(dead_code)]
pub fn get_color(&self) -> &str {
match *self {
Turn::CyanPlayer => "cyan",
pub type BoardType = [Rc<Cell<BoardState>>; 56];
+#[allow(dead_code)]
pub fn new_empty_board() -> BoardType {
[
Rc::new(Cell::new(BoardState::default())),
]
}
+#[allow(dead_code)]
pub fn board_deep_clone(board: &BoardType) -> BoardType {
let cloned_board = new_empty_board();
for i in 0..board.len() {
pub type PlacedType = [Rc<Cell<bool>>; 56];
+#[allow(dead_code)]
pub fn new_placed() -> PlacedType {
[
Rc::new(Cell::new(false)),
]
}
+#[allow(dead_code)]
#[derive(Clone, Debug, PartialEq)]
pub struct SharedState {
pub board: BoardType,
// This enum moved from yew_components module so that this module would have no
// dependencies on the yew_components module
+#[allow(dead_code)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MainMenuMessage {
SinglePlayer(Turn, AIDifficulty),
NetworkedMultiplayer(Option<String>),
}
+#[allow(dead_code)]
pub fn new_string_board() -> String {
let mut board = String::with_capacity(56);
for _i in 0..56 {
board
}
+#[allow(dead_code)]
pub fn board_from_string(board_string: String) -> BoardType {
let board = new_empty_board();
/// Returns the board as a String, and None if game has not ended, Empty if game
/// ended in a draw, or a player if that player has won
+#[allow(dead_code)]
pub fn string_from_board(board: &BoardType, placed: usize) -> (String, Option<BoardState>) {
let mut board_string = String::with_capacity(56);
}
}
+#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct PairingRequestResponse {
pub r#type: String,
pub color: Option<String>,
}
+#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct PairingStatusResponse {
pub r#type: String,
pub color: Option<String>,
}
+#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct GameStateResponse {
pub r#type: String,
pub updated_time: Option<String>,
}
+#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct PlaceTokenResponse {
pub r#type: String,
pub board: String,
}
+#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct SendEmoteRequestResponse {
pub r#type: String,
pub status: String,
}
+#[allow(dead_code)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum NetworkedGameState {
CyanTurn,
UnknownID,
}
+#[allow(dead_code)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum PlacedEnum {
Accepted,
Other(NetworkedGameState),
}
+#[allow(dead_code)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum EmoteEnum {
Smile,