Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-mauran committed Dec 15, 2024
1 parent 775981a commit 10c3569
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 50 deletions.
20 changes: 0 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ impl App {
};
if self.hosting.unwrap() {
self.current_popup = Some(Popups::WaitingForOpponentToJoin);
self.host_ip = Some(format!("{}:2308", self.get_host_ip().to_string()));
self.host_ip = Some(format!("{}:2308", self.get_host_ip()));
}

let addr = self.host_ip.as_ref().unwrap().to_string();
let addr_with_port = format!("{}", addr);
let addr_with_port = addr.to_string();

// ping the server to see if it's up

Expand Down Expand Up @@ -127,9 +127,8 @@ impl App {
pub fn get_host_ip(&self) -> IpAddr {
let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
socket.connect("8.8.8.8:80").unwrap(); // Use an external IP to identify the default route
let default_ip = socket.local_addr().unwrap().ip();

default_ip
socket.local_addr().unwrap().ip()
}

/// Handles the tick event of the terminal.
Expand Down
4 changes: 2 additions & 2 deletions src/game_logic/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Clone for Game {
bot: self.bot.clone(),
opponent: opponent_clone,
player_turn: self.player_turn,
game_state: self.game_state.clone(),
game_state: self.game_state,
}
}
}
Expand Down Expand Up @@ -421,7 +421,7 @@ impl Game {
let from = &Coord::new(from_y, from_x);
let to = &Coord::new(to_y, to_x);

self.execute_move(&from, &to);
self.execute_move(from, to);

if promotion_piece.is_some() {
self.game_board.board[to_y as usize][to_x as usize] =
Expand Down
2 changes: 1 addition & 1 deletion src/game_logic/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl UI {
}

/// Method to render the board
pub fn board_render<'a>(&mut self, area: Rect, frame: &mut Frame<'a>, game: &Game) {
pub fn board_render(&mut self, area: Rect, frame: &mut Frame<'_>, game: &Game) {
let width = area.width / 8;
let height = area.height / 8;
let border_height = area.height / 2 - (4 * height);
Expand Down
29 changes: 16 additions & 13 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
}
}
// Counter handlers
// Counter handlers
KeyCode::Right | KeyCode::Char('l') => {
if app.current_page == Pages::Multiplayer && app.hosting.is_none() {
app.menu_cursor_right(2);
} else if app.current_page == Pages::Multiplayer && app.selected_color.is_none() {
app.menu_cursor_right(2);
} else if app.current_page == Pages::Bot && app.selected_color.is_none() {
if (app.current_page == Pages::Multiplayer
&& (app.hosting.is_none() || app.selected_color.is_none()))
|| (app.current_page == Pages::Bot && app.selected_color.is_none())
{
app.menu_cursor_right(2);
} else if app.game.game_state == GameState::Promotion {
app.game.ui.cursor_right_promotion();
Expand All @@ -84,12 +84,12 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.game.ui.cursor_right(authorized_positions);
}
}

KeyCode::Left | KeyCode::Char('h') => {
if app.current_page == Pages::Multiplayer && app.hosting.is_none() {
app.menu_cursor_left(2);
} else if app.current_page == Pages::Multiplayer && app.selected_color.is_none() {
app.menu_cursor_left(2);
} else if app.current_page == Pages::Bot && app.selected_color.is_none() {
if (app.current_page == Pages::Multiplayer
&& (app.hosting.is_none() || app.selected_color.is_none()))
|| (app.current_page == Pages::Bot && app.selected_color.is_none())
{
app.menu_cursor_left(2);
} else if app.game.game_state == GameState::Promotion {
app.game.ui.cursor_left_promotion();
Expand Down Expand Up @@ -149,7 +149,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
if app.hosting.is_none() {
app.hosting_selection();
} else if app.selected_color.is_none() {
if app.hosting.is_some() && app.hosting.unwrap() == true {
if app.hosting.is_some() && app.hosting.unwrap() {
app.color_selection();
}
} else {
Expand Down Expand Up @@ -224,7 +224,11 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.game.bot = None;
}
if app.game.opponent.is_some() {
app.game.opponent.as_mut().unwrap().send_end_game_to_server();
app.game
.opponent
.as_mut()
.unwrap()
.send_end_game_to_server();
app.game.opponent = None;
app.hosting = None;
app.host_ip = None;
Expand Down Expand Up @@ -269,7 +273,6 @@ pub fn handle_mouse_events(mouse_event: MouseEvent, app: &mut App) -> AppResult<
app.game.promote_piece();
if app.game.opponent.is_some() {
app.game.handle_multiplayer_promotion();

}
}
if mouse_event.column < app.game.ui.top_x || mouse_event.row < app.game.ui.top_y {
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ struct Args {
}

fn main() -> AppResult<()> {

// Used to enable mouse capture
ratatui::crossterm::execute!(
std::io::stdout(),
Expand Down Expand Up @@ -76,7 +75,8 @@ fn main() -> AppResult<()> {
ratatui::crossterm::execute!(
std::io::stdout(),
ratatui::crossterm::event::DisableMouseCapture
).unwrap();
)
.unwrap();
default_panic(info);
}));

Expand Down Expand Up @@ -114,7 +114,7 @@ fn main() -> AppResult<()> {
.map_or(false, |opponent| !opponent.game_started)
{
let opponent = app.game.opponent.as_mut().unwrap();
wait_for_game_start(&opponent.stream.as_ref().unwrap());
wait_for_game_start(opponent.stream.as_ref().unwrap());
opponent.game_started = true;
app.current_popup = None;
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/game_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl GameServer {

loop {
// Check for shutdown signal
if let Ok(_) = shutdown_rx.try_recv() {
if shutdown_rx.try_recv().is_ok() {
break;
}

Expand Down
12 changes: 6 additions & 6 deletions src/ui/main_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
};

/// Renders the user interface widgets.
pub fn render<'a>(app: &mut App, frame: &mut Frame<'a>) {
pub fn render(app: &mut App, frame: &mut Frame<'_>) {
let main_area = frame.area();

// Solo game
Expand All @@ -37,11 +37,11 @@ pub fn render<'a>(app: &mut App, frame: &mut Frame<'a>) {
else if app.current_page == Pages::Multiplayer {
if app.hosting.is_none() {
app.current_popup = Some(Popups::MultiplayerSelection);
} else if app.selected_color.is_none() && app.hosting.unwrap() == true {
} else if app.selected_color.is_none() && app.hosting.unwrap() {
app.current_popup = Some(Popups::ColorSelection);
} else if app.game.opponent.is_none() {
if app.host_ip.is_none() {
if app.hosting.is_some() && app.hosting.unwrap() == true {
if app.hosting.is_some() && app.hosting.unwrap() {
app.setup_game_server(app.selected_color.unwrap());
app.host_ip = Some("127.0.0.1".to_string());
} else {
Expand All @@ -50,7 +50,7 @@ pub fn render<'a>(app: &mut App, frame: &mut Frame<'a>) {
} else {
app.create_opponent();
}
} else if app.game.opponent.as_mut().unwrap().game_started == true {
} else if app.game.opponent.as_mut().unwrap().game_started {
render_game_ui(frame, app, main_area);
}
}
Expand Down Expand Up @@ -193,7 +193,7 @@ pub fn render_menu_ui(frame: &mut Frame, app: &App, main_area: Rect) {
}

// Method to render the game board and handle game popups
pub fn render_game_ui<'a>(frame: &mut Frame<'a>, app: &mut App, main_area: Rect) {
pub fn render_game_ui(frame: &mut Frame<'_>, app: &mut App, main_area: Rect) {
let main_layout_horizontal = Layout::default()
.direction(Direction::Vertical)
.constraints(
Expand Down Expand Up @@ -284,6 +284,6 @@ pub fn render_game_ui<'a>(frame: &mut Frame<'a>, app: &mut App, main_area: Rect)
}

if app.game.game_state == GameState::Draw {
render_end_popup(frame, &format!("That's a draw"), app.game.opponent.is_some());
render_end_popup(frame, "That's a draw", app.game.opponent.is_some());
}
}

0 comments on commit 10c3569

Please sign in to comment.