Skip to content

Commit

Permalink
Moves get made
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapsyloffer committed Jan 21, 2024
1 parent 1823dcc commit e8ee38a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 103 deletions.
30 changes: 20 additions & 10 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
{
const msg = {
type: "CreateGame",
name: "dfkjslödf"
};
ws.send(JSON.stringify(msg));
}
Expand All @@ -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));
Expand Down
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//pub mod api_controller;
//pub mod api_gamestate;
pub mod web_sockets;

#[cfg(test)]
pub mod tests;
108 changes: 108 additions & 0 deletions src/api/web_sockets.rs
Original file line number Diff line number Diff line change
@@ -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<GameHodler>) -> 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::<GamePacket>(&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 { .. } => (),
}
}
}
}
95 changes: 2 additions & 93 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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<GameHodler>) -> 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::<GamePacket>(&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 { .. } => (),
}
}
}
}

0 comments on commit e8ee38a

Please sign in to comment.