diff --git a/html/index.html b/html/index.html index 3d99899..a140d15 100644 --- a/html/index.html +++ b/html/index.html @@ -15,7 +15,6 @@ { const msg = { type: "CreateGame", - name: "dfkjslödf" }; ws.send(JSON.stringify(msg)); } @@ -26,20 +25,31 @@ if(msg.type == "GameCreated") { current_game = msg.id; - const action = { - board: "White", - home_side: "White", - start_x: 0, - start_y: 0, - end_x: 0, - end_y: 0, - aggressive: true + const action_p = { + board_colour: "White", + home_colour: "Black", + x1: 0, + y1: 0, + x2: 1, + y2: 1, + aggr: false + }; + + const action_a = { + board_colour: "White", + home_colour: "Black", + x1: 0, + y1: 1, + x2: 2, + y2: 3, + aggr: true }; const packet = { type: "Action", id: current_game, - action: action + move_p: action_p, + move_a: action_a }; ws.send(JSON.stringify(packet)); diff --git a/src/api/mod.rs b/src/api/mod.rs index 177ef5b..2f91757 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,5 +1,6 @@ //pub mod api_controller; //pub mod api_gamestate; +pub mod web_sockets; #[cfg(test)] pub mod tests; diff --git a/src/api/web_sockets.rs b/src/api/web_sockets.rs new file mode 100644 index 0000000..589e106 --- /dev/null +++ b/src/api/web_sockets.rs @@ -0,0 +1,108 @@ +use axum::{ + extract::{ + ws::{Message, WebSocket, WebSocketUpgrade}, + State, + }, + response::*, +}; +use serde::{Deserialize, Serialize}; + +use crate::rules::{ + game_board::Color, game_hodler::GameHodler, game_instance::Game, game_tile::Tile, +}; + +#[derive(Debug, Clone, Deserialize, Serialize)] +#[serde(tag = "type")] +enum GamePacket { + Action { + id: String, + move_p: Action, + move_a: Action, + }, + CreateGame, + GameCreated { + id: String, + }, +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +struct Action { + board_colour: Color, + home_colour: Color, + x1: i8, + y1: i8, + x2: i8, + y2: i8, + aggr: bool, +} + +pub async fn handler(ws: WebSocketUpgrade, State(state): State) -> Response { + ws.on_upgrade(|socket| handle_socket(socket, state)) +} + +pub async fn handle_socket(mut socket: WebSocket, game_hodler: GameHodler) { + while let Some(msg) = socket.recv().await { + let msg = if let Ok(msg) = msg { + msg + } else { + return; + }; + + if let Message::Text(text) = msg { + println!("{}", text); + let packet = match serde_json::from_str::(&text) { + Ok(packet) => packet, + Err(e) => { + eprintln!("{e}"); + return; + } + }; + + match packet { + GamePacket::CreateGame => { + let id = Game::generate_url(); + println!("\nCreated game: {}\n", id); + game_hodler + .games + .lock() + .unwrap() + .insert(id.clone(), Game::new_game()); + + let packet = GamePacket::GameCreated { id }; + if socket + .send(Message::Text(serde_json::to_string(&packet).unwrap())) + .await + .is_err() + { + return; + } + } + GamePacket::Action { id, move_p, move_a } => { + let mut games = game_hodler.games.lock().unwrap(); + let Some(game) = games.get_mut(&id) else { + return; + }; + + let board_p = game + .get_board(move_p.home_colour, move_p.board_colour) + .unwrap(); + let moved_p: bool = + Tile::passive_move(board_p, (move_p.x1, move_p.y1), (move_p.x2, move_p.y2)); + println!("moved_p: {moved_p}"); + + let board_a = game + .get_board(move_a.home_colour, move_a.board_colour) + .unwrap(); + let moved_a: bool = + Tile::passive_move(board_a, (move_a.x1, move_a.y1), (move_a.x2, move_a.y2)); + println!("moved_a: {moved_a}"); + + if !moved_p || !moved_a { + panic!() + } + } + GamePacket::GameCreated { .. } => (), + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 5b05ca4..a9091c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,37 +1,7 @@ // use rustbu::api::api_controller::*; // use rustbu::api::api_gamestate::*; -use axum::{ - extract::{ - ws::{Message, WebSocket, WebSocketUpgrade}, - State, - }, - response::{Html, IntoResponse, Response}, - routing::get, - Router, -}; -use rustbu::rules::{ - game_board::Color, game_hodler::GameHodler, game_instance::Game, game_tile::Tile, -}; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Clone, Deserialize, Serialize)] -#[serde(tag = "type")] -enum GamePacket { - Action { id: String, action: Action }, - CreateGame { name: String }, - GameCreated { id: String }, -} - -#[derive(Debug, Clone, Deserialize, Serialize)] -struct Action { - board: Color, - home_side: Color, - start_x: i8, - start_y: i8, - end_x: i8, - end_y: i8, - aggressive: bool, -} +use axum::{response::*, routing::get, Router}; +use rustbu::{api::web_sockets::*, rules::game_hodler::GameHodler}; #[tokio::main] async fn main() { @@ -51,64 +21,3 @@ async fn main() { async fn root() -> impl IntoResponse { Html(include_str!("../html/index.html")) } - -async fn handler(ws: WebSocketUpgrade, State(state): State) -> Response { - ws.on_upgrade(|socket| handle_socket(socket, state)) -} - -async fn handle_socket(mut socket: WebSocket, game_hodler: GameHodler) { - while let Some(msg) = socket.recv().await { - let msg = if let Ok(msg) = msg { - msg - } else { - return; - }; - - if let Message::Text(text) = msg { - println!("{}", text); - let packet = match serde_json::from_str::(&text) { - Ok(packet) => packet, - Err(e) => { - eprintln!("{e}"); - return; - } - }; - - match packet { - GamePacket::CreateGame { name: _name } => { - let id = Game::generate_url(); - game_hodler - .games - .lock() - .unwrap() - .insert(id.clone(), Game::new_game()); - - let packet = GamePacket::GameCreated { id }; - if socket - .send(Message::Text(serde_json::to_string(&packet).unwrap())) - .await - .is_err() - { - return; - } - } - GamePacket::Action { id, action } => { - let mut games = game_hodler.games.lock().unwrap(); - let Some(game) = games.get_mut(&id) else { - return; - }; - - let board = game.get_board(action.home_side, action.board).unwrap(); - - let moved = Tile::passive_move( - board, - (action.start_x, action.start_y), - (action.end_x, action.end_y), - ); - println!("moved: {moved}"); - } - GamePacket::GameCreated { .. } => (), - } - } - } -}