From d9572be9bc0f154f87708ac9929242f72f8448b8 Mon Sep 17 00:00:00 2001 From: Ashhhleyyy Date: Mon, 31 Oct 2022 13:30:34 +0000 Subject: [PATCH] fix: clippy warnings --- src/chess.rs | 7 +++---- src/chess/mv.rs | 8 +++----- src/chess/perft.rs | 6 ++---- src/game.rs | 8 +++----- src/main.rs | 8 ++++++-- src/routes.rs | 24 ++++++++++-------------- static/test.html | 30 ++++++++++++++++++++++++++++++ 7 files changed, 57 insertions(+), 34 deletions(-) create mode 100644 static/test.html diff --git a/src/chess.rs b/src/chess.rs index c20e811..4d47b1f 100644 --- a/src/chess.rs +++ b/src/chess.rs @@ -28,7 +28,7 @@ impl Coordinate { pub fn from_index(index: usize) -> Self { let rank = (index / 8) as isize; let file = (index % 8) as isize; - return Self { rank, file }; + Self { rank, file } } pub fn parse_algebraic(s: &str) -> Result { @@ -49,8 +49,7 @@ impl Coordinate { let rank = s .chars() - .skip(1) - .next() + .nth(1) .unwrap() .to_digit(9) .map(|v| v - 1) @@ -317,7 +316,7 @@ impl Board { let white_checked = black_moves.into_iter().any(|m| { self.board[m.to.to_index()] == Some(Side::White | PieceType::King) }); - return BySide { + BySide { black: black_checked, white: white_checked, } diff --git a/src/chess/mv.rs b/src/chess/mv.rs index 84f8300..96e72c0 100644 --- a/src/chess/mv.rs +++ b/src/chess/mv.rs @@ -204,7 +204,7 @@ fn generate_castling_moves(board: &Board, moves: &mut Vec) { if board.get(mid).is_some() { return None; } - let test_state = Move::new(king_from, mid).make(&board); + let test_state = Move::new(king_from, mid).make(board); if *test_state.calc_check_state().get(board.to_move) { return None; } @@ -294,15 +294,13 @@ fn generate_pawn_moves(board: &Board, moves: &mut Vec) { None }; - let mv = Move { + Move { from, to, other: None, promotions, set_en_passant, - }; - - mv + } } let (start_rank, direction) = match board.to_move { diff --git a/src/chess/perft.rs b/src/chess/perft.rs index b9ceade..9e35459 100644 --- a/src/chess/perft.rs +++ b/src/chess/perft.rs @@ -9,7 +9,7 @@ fn perft_board(board: &Board, depth: u64, start_depth: u64) -> u64 { let mut moves = vec![]; crate::chess::mv::generate_pseudolegal(board, &mut moves); - let count = moves.into_par_iter().map(|mv| { + moves.into_par_iter().map(|mv| { let mut count = 0; if let Some(promotions) = mv.promotions { for promotion in promotions { @@ -44,9 +44,7 @@ fn perft_board(board: &Board, depth: u64, start_depth: u64) -> u64 { } } count - }).sum(); - - count + }).sum() } pub fn run_perft(fen: &str, depth: u64, expected_positions: u64) -> bool { diff --git a/src/game.rs b/src/game.rs index 8f61d74..20b267e 100644 --- a/src/game.rs +++ b/src/game.rs @@ -3,18 +3,16 @@ use std::collections::HashMap; use uuid::Uuid; use xtra::{prelude::*, WeakAddress}; -use crate::{prelude::{Board, Move}, constants::START_FEN, chess::{Side, mv::generate_legal}, player::{Player, OutgoingPlayerEvent, IncomingPlayerEvent}}; +use crate::{prelude::Board, constants::START_FEN, chess::{Side, mv::generate_legal}, player::{Player, OutgoingPlayerEvent, IncomingPlayerEvent}}; -#[derive(Actor)] +#[derive(Actor, Default)] pub struct GameManager { games: HashMap>, } impl GameManager { pub fn new() -> Self { - Self { - games: HashMap::new(), - } + Default::default() } } diff --git a/src/main.rs b/src/main.rs index d5ae5b9..20ff246 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use axum::Extension; -use chs::{prelude::*, game::GameManager}; +use chs::game::GameManager; use tower_http::trace::TraceLayer; use tracing_subscriber::prelude::*; use xtra::Mailbox; @@ -22,7 +22,11 @@ async fn main() { .layer(Extension(game_manager)) .layer(TraceLayer::new_for_http()); - axum::Server::bind(&format!("0.0.0.0:3000").parse().unwrap()) + let port: u16 = std::env::var("PORT") + .map(|s| s.parse().unwrap()) + .unwrap_or(3000); + + axum::Server::bind(&format!("0.0.0.0:{port}").parse().unwrap()) .serve(app.into_make_service()) .await .unwrap(); diff --git a/src/routes.rs b/src/routes.rs index 273f833..d27e2bc 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,7 +1,6 @@ -use axum::{Router, extract::{WebSocketUpgrade, Path, ws::{Message, WebSocket}}, response::IntoResponse, routing::{get, get_service}, http::{StatusCode, Uri}, Extension}; +use axum::{Router, extract::{WebSocketUpgrade, Path, ws::{Message, WebSocket}}, response::IntoResponse, routing::get, Extension}; use futures::{StreamExt, SinkExt, stream::{SplitStream, SplitSink}, channel::mpsc::{self, UnboundedSender}}; use tokio::task::JoinHandle; -use tower_http::services::ServeDir; use uuid::Uuid; use xtra::{Mailbox, Address}; @@ -9,6 +8,7 @@ use crate::{player::{Player, IncomingPlayerEvent, OutgoingPlayerEvent, GameInfo} #[cfg(not(debug_assertions))] use crate::assets::StaticFile; +#[allow(clippy::let_and_return)] pub fn routes() -> Router { let router = Router::new() .route("/ws/:id", get(ws_handler)); @@ -50,15 +50,13 @@ async fn handle_socket(socket: WebSocket, id: Uuid, game_manager: Address {}, - _ = tx_task => {}, - } + tokio::select! { + _ = rx_task => {}, + _ = tx_task => {}, } } } @@ -85,7 +83,7 @@ fn socket_send(mut tx: SplitSink) -> (UnboundedSender, player: Address) -> JoinHandle<()> { - let task = tokio::spawn(async move { + tokio::spawn(async move { while let Some(msg) = rx.next().await { if let Ok(msg) = msg { match msg { @@ -115,7 +113,5 @@ fn socket_recv(mut rx: SplitStream, player: Address) -> JoinH return; } } - }); - - task + }) } diff --git a/static/test.html b/static/test.html new file mode 100644 index 0000000..25db329 --- /dev/null +++ b/static/test.html @@ -0,0 +1,30 @@ + + + + + + + Document + + + + +