Skip to content

Commit

Permalink
We can now fetch games
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapsyloffer committed Jan 22, 2024
1 parent a9be07d commit c3bb009
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
39 changes: 39 additions & 0 deletions html/game.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="UTF-8" />
<title>Hello, world!</title>
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="description" content="" />
<link rel="icon" href="favicon.png">
<script>
var url = window.location.href;
var res = url.split("/");
var pos = res.indexOf('game');
var gameUrl = res[pos+1];
alert(gameUrl);
const ws = new WebSocket("ws://localhost:3000/ws");
let current_game = null;
ws.onopen = (event) => {
{
const fetchGameMsg = {
type: "FetchGame",
url: gameUrl
};
ws.send(JSON.stringify(fetchGameMsg));
}
};

ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
console.log(event.data);
}
</script>
</head>

<body>
<h1>Hello, world!</h1>
</body>
</html>
2 changes: 1 addition & 1 deletion src/api/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
[3,0][3,1][3,2][3,3] <--- Black start

Så vi tar cur_pos och flyttar till new_pos
*/
*/
26 changes: 22 additions & 4 deletions src/api/web_sockets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ enum GamePacket {
GameCreated {
id: String,
},
FetchGame{
url: String
}
}

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand All @@ -36,7 +39,7 @@ struct Action {
}

pub async fn handler(ws: WebSocketUpgrade, State(state): State<GameHodler>) -> Response {
ws.on_upgrade(|socket| handle_socket(socket, state))
return ws.on_upgrade(|socket| handle_socket(socket, state));
}

pub async fn handle_socket(mut socket: WebSocket, game_hodler: GameHodler) {
Expand Down Expand Up @@ -76,6 +79,21 @@ pub async fn handle_socket(mut socket: WebSocket, game_hodler: GameHodler) {
return;
}
}
GamePacket::FetchGame { url } =>
{
let mut games = game_hodler.games.lock().unwrap().clone();
let Some(game) = games.get_mut(&url) else {
return;
};
let state: String = serde_json::to_string(game).unwrap();
if socket
.send(Message::Text(state))
.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 {
Expand All @@ -87,14 +105,14 @@ pub async fn handle_socket(mut socket: WebSocket, game_hodler: GameHodler) {
.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}");
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}");
println!("moved_a: {moved_a}");

if moved_p && moved_a {
game.next_turn();
Expand All @@ -110,4 +128,4 @@ pub async fn handle_socket(mut socket: WebSocket, game_hodler: GameHodler) {
}
}
}
}
}
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async fn main() {
let app = Router::new()
.route("/", get(root))
.route("/ws", get(handler))
.route("/game/:id", get(handler)) //Join a game using URL
.route("/game/:id", get(fetch_game)) //Join a game using URL
.with_state(GameHodler::new());

let listener = tokio::net::TcpListener::bind("localhost:3000")
Expand All @@ -22,3 +22,9 @@ async fn main() {
async fn root() -> impl IntoResponse {
Html(include_str!("../html/index.html"))
}


async fn fetch_game() -> impl IntoResponse {
Html(include_str!("../html/game.html"))
}

0 comments on commit c3bb009

Please sign in to comment.