fix: clippy

This commit is contained in:
2026-09-05 15:50:52 +02:00
parent 1a21405927
commit f580adf743
17 changed files with 56 additions and 85 deletions

View File

@@ -63,6 +63,7 @@ impl TryFrom<&str> for CliCommands {
} }
} }
#[derive(Default)]
struct ClientContext { struct ClientContext {
pub identity: Option<NetworkIdentity>, pub identity: Option<NetworkIdentity>,
pub is_my_turn: bool, pub is_my_turn: bool,
@@ -71,17 +72,6 @@ struct ClientContext {
pub available_moves: Option<Vec<MovePath>>, pub available_moves: Option<Vec<MovePath>>,
} }
impl Default for ClientContext {
fn default() -> Self {
Self {
identity: None,
is_my_turn: false,
available_captures: None,
available_moves: None,
}
}
}
async fn run_bot() { async fn run_bot() {
let bot = Bot::new(BotDificulty::Easy).await; let bot = Bot::new(BotDificulty::Easy).await;
@@ -106,11 +96,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
loop { loop {
let mut buffer = String::new(); let mut buffer = String::new();
if stdin.read_line(&mut buffer).is_ok() { if stdin.read_line(&mut buffer).is_ok()
if stdin_sender.blocking_send(buffer).is_err() { && stdin_sender.blocking_send(buffer).is_err() {
break; break;
} }
}
} }
}); });

View File

@@ -218,7 +218,7 @@ impl Board {
from, to from, to
); );
if !self.is_player_owner_of_the_pawn(player, &pawn) { if !self.is_player_owner_of_the_pawn(player, pawn) {
debug!("Player wasn't owner of the pawn..."); debug!("Player wasn't owner of the pawn...");
return false; return false;
} }
@@ -279,9 +279,8 @@ impl Board {
let target_option: Option<Position> = current_pos.checked_add(direction); let target_option: Option<Position> = current_pos.checked_add(direction);
if let Some(target) = target_option if let Some(target) = target_option
&& self.can_capture(current_pos, target, player, captured_enemies, &pawn) && self.can_capture(current_pos, target, player, captured_enemies, pawn)
{ && let Some(path_after_capture) = target.checked_add(direction) {
if let Some(path_after_capture) = target.checked_add(direction) {
debug!( debug!(
"Found possible capture: {} -> {}", "Found possible capture: {} -> {}",
current_pos, path_after_capture current_pos, path_after_capture
@@ -303,10 +302,9 @@ impl Board {
available_directions, available_directions,
player, player,
all_paths, all_paths,
&pawn, pawn,
); );
} }
}
} }
if !found_any_capture && !current_path.is_empty() && !captured_enemies.is_empty() { if !found_any_capture && !current_path.is_empty() && !captured_enemies.is_empty() {
@@ -415,7 +413,7 @@ impl Board {
return false; return false;
} }
if !self.is_player_owner_of_the_pawn(&player, self[from].pawn.as_ref().unwrap()) { if !self.is_player_owner_of_the_pawn(player, self[from].pawn.as_ref().unwrap()) {
warn!("Player {} tried moving a pawn which he doesn own!", player); warn!("Player {} tried moving a pawn which he doesn own!", player);
debug!("Tried moving from {} to {}", from, to); debug!("Tried moving from {} to {}", from, to);
return false; return false;
@@ -462,7 +460,7 @@ impl Board {
return false; return false;
} }
if !self.is_player_owner_of_the_pawn(player, &pawn) { if !self.is_player_owner_of_the_pawn(player, pawn) {
warn!("Player {} tried moving a pawn which he doesn own!", player); warn!("Player {} tried moving a pawn which he doesn own!", player);
return false; return false;
} }
@@ -491,9 +489,9 @@ impl Board {
impl Index<usize> for Board { impl Index<usize> for Board {
type Output = [Field]; type Output = [Field];
fn index<'a>(&'a self, i: usize) -> &'a [Field] { fn index(&self, i: usize) -> &[Field] {
let start = i * &self.size; let start = i * self.size;
let end: usize = start + &self.size; let end: usize = start + self.size;
&self.board[start..end] &self.board[start..end]
} }
} }
@@ -507,9 +505,9 @@ impl Index<Position> for Board {
} }
impl IndexMut<usize> for Board { impl IndexMut<usize> for Board {
fn index_mut<'a>(&'a mut self, i: usize) -> &'a mut [Field] { fn index_mut(&mut self, i: usize) -> &mut [Field] {
let start = i * &self.size; let start = i * self.size;
let end: usize = start + &self.size; let end: usize = start + self.size;
&mut self.board[start..end] &mut self.board[start..end]
} }
} }

View File

@@ -24,9 +24,9 @@ impl From<Board> for BoardView {
impl Index<usize> for BoardView { impl Index<usize> for BoardView {
type Output = [Field]; type Output = [Field];
fn index<'a>(&'a self, i: usize) -> &'a [Field] { fn index(&self, i: usize) -> &[Field] {
let start = i * &self.size; let start = i * self.size;
let end: usize = start + &self.size; let end: usize = start + self.size;
&self.board[start..end] &self.board[start..end]
} }
} }

View File

@@ -105,7 +105,7 @@ impl GameMaster {
fn is_player_lost(&self, player: &Player) -> bool { fn is_player_lost(&self, player: &Player) -> bool {
let player_pawns = self.board.get_player_pawns_positions(player); let player_pawns = self.board.get_player_pawns_positions(player);
player_pawns.len() == 0 player_pawns.is_empty()
|| player_pawns.iter().all(|pos| { || player_pawns.iter().all(|pos| {
self.board.get_available_moves(*pos, player).is_none() self.board.get_available_moves(*pos, player).is_none()
&& self.board.get_available_captures(*pos, player).is_none() && self.board.get_available_captures(*pos, player).is_none()

View File

@@ -129,7 +129,7 @@ impl Client {
async fn send_message(&mut self, msg: Result<Message<ClientMessage>, postcard::Error>) { async fn send_message(&mut self, msg: Result<Message<ClientMessage>, postcard::Error>) {
debug!("Sending message to the server!"); debug!("Sending message to the server!");
let _ = match msg { match msg {
Err(e) => { Err(e) => {
error!("There was an error while creating the message: {}", e); error!("There was an error while creating the message: {}", e);
return; return;

View File

@@ -110,8 +110,7 @@ where
if let Err(e) = tcp.write_all(&msg).await { if let Err(e) = tcp.write_all(&msg).await {
error!("Error while sending content to: {:?}\n{}", self.peer, e); error!("Error while sending content to: {:?}\n{}", self.peer, e);
return; }
};
} }
pub async fn start_listening(&mut self) { pub async fn start_listening(&mut self) {
@@ -202,20 +201,20 @@ where
{ {
error!("Erro while sending message to main thread: {}", e); error!("Erro while sending message to main thread: {}", e);
} }
return Ok(()); Ok(())
} }
Err(e) => { Err(e) => {
error!( error!(
"Error while preparing message in read_content for sender: {}", "Error while preparing message in read_content for sender: {}",
e e
); );
return Ok(()); Ok(())
} }
} }
} }
Err(e) => { Err(e) => {
error!("Error while getting the message from bits: {}", e); error!("Error while getting the message from bits: {}", e);
return Ok(()); Ok(())
} }
} }
} }

View File

@@ -156,7 +156,7 @@ impl Server {
let reason = "There is currently game going and it doesn't allow spectators".to_string(); let reason = "There is currently game going and it doesn't allow spectators".to_string();
info!("Rejecting {}, because {}", addr, reason); info!("Rejecting {}, because {}", addr, reason);
let msg = Message::new(ServerMessage::DeclineHandshake { reason: reason }); let msg = Message::new(ServerMessage::DeclineHandshake { reason });
if let Ok(msg) = msg { if let Ok(msg) = msg {
let _ = sender.send(msg).await; // we can't use send_message because we let _ = sender.send(msg).await; // we can't use send_message because we
@@ -500,7 +500,6 @@ impl Server {
} }
Err(e) => { Err(e) => {
error!("Error while broadcasting the message: {}", e); error!("Error while broadcasting the message: {}", e);
return;
} }
} }
} }

View File

@@ -66,12 +66,12 @@ impl Bot {
.choose(&mut rand::rng()) .choose(&mut rand::rng())
.expect("Could not get random move"); .expect("Could not get random move");
client.send_move(rand_pawn.from, rand_move.clone()).await; client.send_move(rand_pawn.from, *rand_move).await;
} }
crate::network::message::ServerMessage::BroadcastBoardState { board: _ } => { crate::network::message::ServerMessage::BroadcastBoardState { board: _ } => {
info!("Got server state!"); info!("Got server state!");
match dificulty.clone() { match dificulty {
BotDificulty::Easy => {} // we do nothing with it because easy bot BotDificulty::Easy => {} // we do nothing with it because easy bot
// just does random stuff. In future // just does random stuff. In future
// board_view can be used to calculate the // board_view can be used to calculate the

View File

@@ -5,7 +5,7 @@ use macroquad::{
math::vec2, math::vec2,
shapes::draw_rectangle, shapes::draw_rectangle,
ui::{hash, root_ui, widgets::Group}, ui::{hash, root_ui, widgets::Group},
window::{next_frame, screen_height, screen_width}, window::{screen_height, screen_width},
}; };
use crate::{ use crate::{
@@ -72,7 +72,7 @@ fn start_single(state: &mut GuiState, context: &GameContext) {
let result = rx.recv(); let result = rx.recv();
if let Ok(result) = result { if let Ok(_result) = result {
connect_bot(context.difficulty); connect_bot(context.difficulty);
} }

View File

@@ -185,9 +185,9 @@ impl Board {
self.highlight_move_paths(from); self.highlight_move_paths(from);
if let Some(field) = &self.get_clicked_field() { if let Some(field) = &self.get_clicked_field()
if let Some(available_moves) = &self.available_moves { && let Some(available_moves) = &self.available_moves
if available_moves.iter().any(|available_move| { && available_moves.iter().any(|available_move| {
available_move.from == from available_move.from == from
&& available_move.available_steps.contains(&field.pos) && available_move.available_steps.contains(&field.pos)
}) { }) {
@@ -197,8 +197,6 @@ impl Board {
}); });
self.state = BoardState::View; self.state = BoardState::View;
} }
}
}
} }
BoardState::ChooseCapture(from) => { BoardState::ChooseCapture(from) => {
@@ -230,8 +228,8 @@ impl Board {
.copied() .copied()
.collect(); .collect();
if let Some(clicked_field) = &self.get_clicked_field().map(|field| field.pos) { if let Some(clicked_field) = &self.get_clicked_field().map(|field| field.pos)
if possible_moves.iter().any(|possible_move| { && possible_moves.iter().any(|possible_move| {
if let Some(valid_move) = possible_move.get(self.current_deepness) { if let Some(valid_move) = possible_move.get(self.current_deepness) {
valid_move == clicked_field valid_move == clicked_field
} else { } else {
@@ -241,7 +239,6 @@ impl Board {
self.current_capture_path.push(*clicked_field); self.current_capture_path.push(*clicked_field);
self.current_deepness += 1; self.current_deepness += 1;
} }
}
if current_highlight.is_empty() { if current_highlight.is_empty() {
debug!("Capture path complete!"); debug!("Capture path complete!");
@@ -278,7 +275,6 @@ impl Board {
&& available_moves.iter().any(|path| path.from == from) && available_moves.iter().any(|path| path.from == from)
{ {
self.state = BoardState::ChooseMove(from); self.state = BoardState::ChooseMove(from);
return;
} }
} }
@@ -324,8 +320,8 @@ impl Board {
for field in &self.board_view { for field in &self.board_view {
let pos: Position = field.position; let pos: Position = field.position;
let is_row_even: bool = pos.row % 2 == 0; let is_row_even: bool = pos.row.is_multiple_of(2);
let is_column_even: bool = pos.column % 2 == 0; let is_column_even: bool = pos.column.is_multiple_of(2);
let is_field_black: bool = is_row_even ^ is_column_even; let is_field_black: bool = is_row_even ^ is_column_even;
let abs_x = self.rect.x + pos.column as f32 * self.field_size; let abs_x = self.rect.x + pos.column as f32 * self.field_size;
@@ -453,9 +449,7 @@ impl Board {
for available_move in available_moves { for available_move in available_moves {
self.fields[available_move.from].color = MOVE_HIGHLITGHT_COLOR; self.fields[available_move.from].color = MOVE_HIGHLITGHT_COLOR;
} }
}
return;
};
} }
fn highlight_move_paths(&mut self, from: Position) { fn highlight_move_paths(&mut self, from: Position) {

View File

@@ -1,4 +1,3 @@
use log::debug;
use macroquad::{ use macroquad::{
color::{BLACK, WHITE}, color::{BLACK, WHITE},
math::{Rect, vec2}, math::{Rect, vec2},
@@ -6,7 +5,7 @@ use macroquad::{
text::measure_text, text::measure_text,
ui::{ ui::{
Skin, Ui, hash, root_ui, Skin, Ui, hash, root_ui,
widgets::{Group, InputText, Label}, widgets::{Group, InputText},
}, },
window::{screen_height, screen_width}, window::{screen_height, screen_width},
}; };
@@ -86,7 +85,7 @@ impl Chat {
.ui(&mut root_ui(), |ui| { .ui(&mut root_ui(), |ui| {
for message in &self.messages { for message in &self.messages {
if prev_sender != message.sender { if prev_sender != message.sender {
if prev_sender != "" { if !prev_sender.is_empty() {
// make space if it's not the first message // make space if it's not the first message
ui.label(None, ""); ui.label(None, "");
} }
@@ -108,7 +107,7 @@ fn wrap_message(ui: &mut Ui, msg: &String, font_size: u16, chat_width: f32) {
let mut buffer = String::new(); let mut buffer = String::new();
if measure_text(msg, None, font_size, 1.0).width < chat_width { if measure_text(msg, None, font_size, 1.0).width < chat_width {
ui.label(None, &msg); ui.label(None, msg);
return; return;
} }

View File

@@ -12,10 +12,7 @@ use macroquad::{
use tokio::sync::mpsc::{Receiver, error::TryRecvError::Disconnected}; use tokio::sync::mpsc::{Receiver, error::TryRecvError::Disconnected};
use crate::{ use crate::{
logic::board::{ logic::board::pawn::{CapturePath, MovePath},
board_view::BoardView,
pawn::{CapturePath, MovePath},
},
network::{ network::{
client::Client, message::ServerMessage, network_identity::NetworkIdentity, client::Client, message::ServerMessage, network_identity::NetworkIdentity,
server::ServerStage, server::ServerStage,
@@ -66,7 +63,7 @@ pub enum GuiCommands {
} }
impl GameClient { impl GameClient {
pub fn new(mut client: Client, context: &GameContext) -> Option<Self> { pub fn new(mut client: Client, _context: &GameContext) -> Option<Self> {
let Some(update_receiver) = client.get_update_receiver() else { let Some(update_receiver) = client.get_update_receiver() else {
return None; return None;
}; };
@@ -226,15 +223,11 @@ impl GameClient {
} }
for key in get_keys_pressed() { for key in get_keys_pressed() {
match key { if key == KeyCode::Enter {
KeyCode::Enter => { self.chat.send_message(self.cmd_sender.clone()); // not sure about cloning this
self.chat.send_message(self.cmd_sender.clone()); // not sure about cloning this // every message sent. Maybe
// every message sent. Maybe // Chat should have it's own
// Chat should have it's own // clone?
// clone?
}
_ => {}
} }
} }
@@ -288,7 +281,7 @@ impl GameClient {
self.chat.push_message(sender, content); self.chat.push_message(sender, content);
} }
ServerMessage::GameEnd { result } => { ServerMessage::GameEnd { result: _ } => {
self.game_state = ServerStage::End; self.game_state = ServerStage::End;
} }

View File

@@ -1,9 +1,9 @@
use macroquad::{ use macroquad::{
color::{BLACK, GRAY, WHITE}, color::GRAY,
math::vec2, math::vec2,
shapes::draw_rectangle, shapes::draw_rectangle,
ui::{Skin, hash, root_ui, widgets::Group}, ui::{hash, root_ui, widgets::Group},
window::{next_frame, screen_height, screen_width}, window::{screen_height, screen_width},
}; };
use crate::ui::{macroquad::menu_builder::MenuBuilder, state::GuiState}; use crate::ui::{macroquad::menu_builder::MenuBuilder, state::GuiState};

View File

@@ -3,7 +3,7 @@ use macroquad::{
math::vec2, math::vec2,
shapes::draw_rectangle, shapes::draw_rectangle,
ui::{hash, root_ui, widgets::Group}, ui::{hash, root_ui, widgets::Group},
window::{next_frame, screen_height, screen_width}, window::{screen_height, screen_width},
}; };
use crate::ui::{ use crate::ui::{

View File

@@ -5,7 +5,7 @@ use macroquad::{
math::vec2, math::vec2,
shapes::draw_rectangle, shapes::draw_rectangle,
ui::{hash, root_ui, widgets::Group}, ui::{hash, root_ui, widgets::Group},
window::{next_frame, screen_height, screen_width}, window::{screen_height, screen_width},
}; };
use crate::ui::{ use crate::ui::{
@@ -40,7 +40,7 @@ pub async fn draw_server_selection(state: &mut GuiState, context: &mut GameConte
let result = rx.recv(); let result = rx.recv();
if let Ok(result) = result { if let Ok(_result) = result {
*state = GuiState::Connecting(connect_to_server()); *state = GuiState::Connecting(connect_to_server());
} }
} }

View File

@@ -52,7 +52,7 @@ pub async fn draw_settings(
menu_builder.label(ui, "TODO"); menu_builder.label(ui, "TODO");
if let Some(error_msg) = &context.latest_error_message { if let Some(error_msg) = &context.latest_error_message {
menu_builder.label(ui, &error_msg); menu_builder.label(ui, error_msg);
} }
// Exit // Exit

View File

@@ -73,7 +73,7 @@ pub fn run_server(sender: Sender<()>) {
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
let mut server = rt.block_on(Server::new()); let mut server = rt.block_on(Server::new());
let _ = rt.block_on(server.start()); rt.block_on(server.start());
let _ = sender.send(()); let _ = sender.send(());