added optional parameters that the ui will receive from server

This commit is contained in:
2025-11-26 17:53:12 +01:00
parent 933bb46f17
commit 2112109470

View File

@@ -1,6 +1,7 @@
use eframe::egui; use eframe::egui;
use env_logger::Env; use env_logger::Env;
use log::{error, info, warn}; use log::{error, info, warn};
use uuid::Uuid;
use crate::connection::handle_connection; use crate::connection::handle_connection;
mod connection; mod connection;
@@ -83,6 +84,7 @@ enum AppState {
Settings, Settings,
} }
#[derive(Clone)]
struct ChessApp { struct ChessApp {
fullscreen: bool, fullscreen: bool,
resolutions: Vec<(u32, u32)>, resolutions: Vec<(u32, u32)>,
@@ -93,6 +95,9 @@ struct ChessApp {
turn: Turn, turn: Turn,
pending_settings: PendingSettings, pending_settings: PendingSettings,
server_port: String, server_port: String,
player_color: Option<String>,
match_id: Option<Uuid>,
opponent_name: Option<String>,
} }
#[derive(Default)] #[derive(Default)]
@@ -121,6 +126,9 @@ impl Default for ChessApp {
turn: Turn::White, turn: Turn::White,
pending_settings: PendingSettings::default(), pending_settings: PendingSettings::default(),
server_port: "9001".to_string(), // Default port server_port: "9001".to_string(), // Default port
player_color: None,
match_id: None,
opponent_name: None,
} }
} }
} }
@@ -218,7 +226,7 @@ impl eframe::App for ChessApp {
//create a TCPlistener with tokio and bind machine ip for connection //create a TCPlistener with tokio and bind machine ip for connection
tokio::spawn(async move { tokio::spawn(async move {
info!("tokoi"); info!("tokoi");
handle_connection(&port).await handle_connection(&port, self).await
}); });
self.state = AppState::InGame; self.state = AppState::InGame;
@@ -351,145 +359,145 @@ impl eframe::App for ChessApp {
); );
// Draw the chess board // Draw the chess board
let player = "black"; if self.player_color == Some("white".to_string()) {
if player =="white"{ let tile_size = board_size / 8.0;
let tile_size = board_size / 8.0; for row in 0..8 {
for row in 0..8 { for col in 0..8 {
for col in 0..8 { let color = if (row + col) % 2 == 0 {
let color = if (row + col) % 2 == 0 { egui::Color32::from_rgb(217, 217, 217)
egui::Color32::from_rgb(217, 217, 217) } else {
} else { egui::Color32::from_rgb(100, 97, 97)
egui::Color32::from_rgb(100, 97, 97) };
};
let rect = egui::Rect::from_min_size( let rect = egui::Rect::from_min_size(
egui::Pos2::new( egui::Pos2::new(
board_rect.min.x + col as f32 * tile_size, board_rect.min.x + col as f32 * tile_size,
board_rect.min.y + row as f32 * tile_size, board_rect.min.y + row as f32 * tile_size,
), ),
egui::Vec2::new(tile_size, tile_size), egui::Vec2::new(tile_size, tile_size),
);
painter.rect_filled(rect, 0.0, color);
// Draw piece
let piece = self.board[row][col];
if piece != Piece::Empty {
let symbol = piece.symbol();
let font_id = egui::FontId::proportional(tile_size * 0.75);
painter.text(
rect.center(),
egui::Align2::CENTER_CENTER,
symbol,
font_id,
if matches!(
piece,
Piece::King('w')
| Piece::Queen('w')
| Piece::Rook('w')
| Piece::Bishop('w')
| Piece::Knight('w')
| Piece::Pawn('w')
) {
egui::Color32::WHITE
} else {
egui::Color32::BLACK
},
); );
}
// Draw selection highlight painter.rect_filled(rect, 0.0, color);
if self.selected == Some((row, col)) {
painter.rect_stroke(
rect,
0.0,
egui::Stroke::new(3.0, egui::Color32::RED),
egui::StrokeKind::Inside,
);
}
// Handle clicks // Draw piece
if ui.ctx().input(|i| i.pointer.primary_clicked()) { let piece = self.board[row][col];
let click_pos = if piece != Piece::Empty {
ui.ctx().input(|i| i.pointer.interact_pos()).unwrap(); let symbol = piece.symbol();
if rect.contains(click_pos) { let font_id = egui::FontId::proportional(tile_size * 0.75);
self.handle_click(row, col); painter.text(
rect.center(),
egui::Align2::CENTER_CENTER,
symbol,
font_id,
if matches!(
piece,
Piece::King('w')
| Piece::Queen('w')
| Piece::Rook('w')
| Piece::Bishop('w')
| Piece::Knight('w')
| Piece::Pawn('w')
) {
egui::Color32::WHITE
} else {
egui::Color32::BLACK
},
);
}
// Draw selection highlight
if self.selected == Some((row, col)) {
painter.rect_stroke(
rect,
0.0,
egui::Stroke::new(3.0, egui::Color32::RED),
egui::StrokeKind::Inside,
);
}
// Handle clicks
if ui.ctx().input(|i| i.pointer.primary_clicked()) {
let click_pos =
ui.ctx().input(|i| i.pointer.interact_pos()).unwrap();
if rect.contains(click_pos) {
self.handle_click(row, col);
}
} }
} }
} }
} }
} if self.player_color == Some("black".to_string()) {
if player=="black"{ {
{ let tile_size = board_size / 8.0;
let tile_size = board_size / 8.0; for row in 0..8 {
for row in 0..8 { for col in 0..8 {
for col in 0..8 { let color = if (row + col) % 2 == 0 {
let color = if (row + col) % 2 == 0 { egui::Color32::from_rgb(217, 217, 217)
egui::Color32::from_rgb(217, 217, 217)
} else {
egui::Color32::from_rgb(100, 97, 97)
};
let rect = egui::Rect::from_min_size(
egui::Pos2::new(
board_rect.min.x + col as f32 * tile_size,
board_rect.min.y + row as f32 * tile_size,
),
egui::Vec2::new(tile_size, tile_size),
);
painter.rect_filled(rect, 0.0, color);
// Draw piece
let piece = self.board[row][col];
if piece != Piece::Empty {
let symbol = piece.symbol();
let font_id = egui::FontId::proportional(tile_size * 0.75);
painter.text(
rect.center(),
egui::Align2::CENTER_CENTER,
symbol,
font_id,
if matches!(
piece,
Piece::King('w')
| Piece::Queen('w')
| Piece::Rook('w')
| Piece::Bishop('w')
| Piece::Knight('w')
| Piece::Pawn('w')
) {
egui::Color32::BLACK
} else { } else {
egui::Color32::WHITE egui::Color32::from_rgb(100, 97, 97)
}, };
);
}
// Draw selection highlight let rect = egui::Rect::from_min_size(
if self.selected == Some((row, col)) { egui::Pos2::new(
painter.rect_stroke( board_rect.min.x + col as f32 * tile_size,
rect, board_rect.min.y + row as f32 * tile_size,
0.0, ),
egui::Stroke::new(3.0, egui::Color32::RED), egui::Vec2::new(tile_size, tile_size),
egui::StrokeKind::Inside, );
);
}
// Handle clicks painter.rect_filled(rect, 0.0, color);
if ui.ctx().input(|i| i.pointer.primary_clicked()) {
let click_pos = // Draw piece
ui.ctx().input(|i| i.pointer.interact_pos()).unwrap(); let piece = self.board[row][col];
if rect.contains(click_pos) { if piece != Piece::Empty {
self.handle_click(row, col); let symbol = piece.symbol();
let font_id =
egui::FontId::proportional(tile_size * 0.75);
painter.text(
rect.center(),
egui::Align2::CENTER_CENTER,
symbol,
font_id,
if matches!(
piece,
Piece::King('w')
| Piece::Queen('w')
| Piece::Rook('w')
| Piece::Bishop('w')
| Piece::Knight('w')
| Piece::Pawn('w')
) {
egui::Color32::BLACK
} else {
egui::Color32::WHITE
},
);
}
// Draw selection highlight
if self.selected == Some((row, col)) {
painter.rect_stroke(
rect,
0.0,
egui::Stroke::new(3.0, egui::Color32::RED),
egui::StrokeKind::Inside,
);
}
// Handle clicks
if ui.ctx().input(|i| i.pointer.primary_clicked()) {
let click_pos = ui
.ctx()
.input(|i| i.pointer.interact_pos())
.unwrap();
if rect.contains(click_pos) {
self.handle_click(row, col);
}
}
} }
} }
} }
} }
}
}
}); });
}); });
} }
@@ -548,6 +556,6 @@ mod tests {
#[test] #[test]
fn test_server_port_default() { fn test_server_port_default() {
let app = ChessApp::default(); let app = ChessApp::default();
assert_eq!(app.server_port, "8080"); assert_eq!(app.server_port, "9001");
} }
} }