Compare commits
4 Commits
Engine/mak
...
UI/RotateB
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
933bb46f17 | ||
| 5deebb0621 | |||
| 60911f93f6 | |||
| 7820555fa8 |
@@ -7,12 +7,6 @@ use tokio_tungstenite::{connect_async, tungstenite::Message};
|
||||
use url::Url;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Step {
|
||||
from: String,
|
||||
to: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
enum ClientMessage {
|
||||
@@ -24,17 +18,6 @@ enum ClientMessage {
|
||||
RequestLegalMoves { fen: String },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct ServerMessage {
|
||||
#[serde(rename = "type")]
|
||||
message_type: String,
|
||||
player_id: Option<String>,
|
||||
match_id: Option<String>,
|
||||
opponent: Option<String>,
|
||||
color: Option<String>,
|
||||
reason: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum ServerMessage2 {
|
||||
GameEnd {
|
||||
@@ -48,6 +31,9 @@ pub enum ServerMessage2 {
|
||||
color: String,
|
||||
opponent_name: String,
|
||||
},
|
||||
Ok {
|
||||
response: Result<(), String>,
|
||||
},
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
||||
@@ -6,7 +6,6 @@ use engine::{get_available_moves, is_game_over};
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use log::{error, info, warn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::char::from_u32_unchecked;
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::sync::Arc;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
mod connection;
|
||||
mod matchmaking;
|
||||
use env_logger::{Env, Logger};
|
||||
use log::{error, info, warn};
|
||||
use std::env;
|
||||
use env_logger::Env;
|
||||
use log::{error, info};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
#[tokio::main]
|
||||
|
||||
@@ -26,12 +26,8 @@ impl MatchmakingSystem {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn clean_up(&self, match_id: Uuid) {
|
||||
self.matches.lock().await.remove(&match_id);
|
||||
}
|
||||
|
||||
async fn try_create_match(&self) {
|
||||
info!("Checking for new matches!");
|
||||
//info!("Checking for new matches!");
|
||||
let mut queue = self.waiting_queue.lock().await;
|
||||
|
||||
while queue.len() >= 2 {
|
||||
@@ -72,6 +68,7 @@ impl MatchmakingSystem {
|
||||
}
|
||||
if let Some(player) = conn_map.get_mut(&black_player) {
|
||||
player.current_match = Some(match_id);
|
||||
//TODO: at the end of a match delete this from player
|
||||
} else {
|
||||
error!("Could not store match id for black player");
|
||||
}
|
||||
|
||||
@@ -6,5 +6,17 @@ edition = "2024"
|
||||
[dependencies]
|
||||
eframe = "0.33.0"
|
||||
egui = "0.33.0"
|
||||
tokio-tungstenite = "0.28.0"
|
||||
winit = "0.30.12"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
tokio-tungstenite = "0.21"
|
||||
tungstenite = "0.21"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
futures-util = "0.3.31"
|
||||
url = "2.5.7"
|
||||
uuid = {version = "1.18.1", features = ["v4", "serde"] }
|
||||
engine = {path = "../engine/"}
|
||||
log = {version = "0.4.28"}
|
||||
env_logger = "0.11.8"
|
||||
local-ip-address = "0.6.5"
|
||||
anyhow = "1.0.100"
|
||||
|
||||
96
ui/src/connection.rs
Normal file
96
ui/src/connection.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
use engine::{chessmove::ChessMove, gameend::GameEnd};
|
||||
use futures_util::StreamExt;
|
||||
use local_ip_address::local_ip;
|
||||
use log::{error, info, warn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
error::Error,
|
||||
net::{IpAddr, Ipv4Addr},
|
||||
};
|
||||
use tokio_tungstenite::connect_async;
|
||||
use url::Url;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum ServerMessage2 {
|
||||
GameEnd {
|
||||
winner: GameEnd,
|
||||
},
|
||||
UIUpdate {
|
||||
fen: String,
|
||||
},
|
||||
MatchFound {
|
||||
match_id: Uuid,
|
||||
color: String,
|
||||
opponent_name: String,
|
||||
},
|
||||
Ok {
|
||||
response: Result<(), String>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
enum ClientMessage {
|
||||
Join { username: String },
|
||||
FindMatch,
|
||||
Move { step: ChessMove, fen: String },
|
||||
Resign,
|
||||
Chat { text: String },
|
||||
RequestLegalMoves { fen: String },
|
||||
}
|
||||
|
||||
fn get_ip_address() -> IpAddr {
|
||||
let ip = local_ip().unwrap_or(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)));
|
||||
|
||||
ip
|
||||
}
|
||||
|
||||
pub async fn handle_connection(server_port: &str) -> anyhow::Result<()> {
|
||||
let address = get_ip_address();
|
||||
|
||||
//start main loop
|
||||
let server_address = String::from("ws://") + &address.to_string() + ":" + server_port;
|
||||
warn!(
|
||||
"Machine IpAddress is bound for listener. Ip: {}",
|
||||
server_address
|
||||
);
|
||||
|
||||
let url = Url::parse(&server_address)?;
|
||||
|
||||
let (ws_stream, _) = connect_async(url).await?;
|
||||
let (mut write, mut read) = ws_stream.split();
|
||||
|
||||
let read_handle = while let Some(message) = read.next().await {
|
||||
info!("connection");
|
||||
match message {
|
||||
Ok(msg) => {
|
||||
if msg.is_text() {
|
||||
let text = msg.to_text().unwrap();
|
||||
info!("text: {}", text);
|
||||
|
||||
if let Ok(parsed) = serde_json::from_str::<ServerMessage2>(text) {
|
||||
match parsed {
|
||||
ServerMessage2::GameEnd { winner } => {}
|
||||
ServerMessage2::UIUpdate { fen } => {}
|
||||
ServerMessage2::MatchFound {
|
||||
match_id,
|
||||
color,
|
||||
opponent_name,
|
||||
} => {}
|
||||
ServerMessage2::Ok { response } => {}
|
||||
_ => {
|
||||
error!("Received unkown servermessage2");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Error receiving message: {}", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
220
ui/src/main.rs
220
ui/src/main.rs
@@ -1,31 +1,42 @@
|
||||
use eframe::egui;
|
||||
use env_logger::Env;
|
||||
use log::{error, info, warn};
|
||||
|
||||
fn main() -> eframe::Result<()> {
|
||||
let options = eframe::NativeOptions{
|
||||
use crate::connection::handle_connection;
|
||||
mod connection;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<(), eframe::Error> {
|
||||
//set up for logging
|
||||
let env = Env::default().filter_or("MY_LOG_LEVEL", "INFO");
|
||||
env_logger::init_from_env(env);
|
||||
warn!("Initialized logger");
|
||||
|
||||
let options = eframe::NativeOptions {
|
||||
viewport: egui::ViewportBuilder::default()
|
||||
.with_fullscreen(true)
|
||||
.with_fullscreen(false)
|
||||
.with_min_inner_size(egui::vec2(800.0, 600.0)) // Minimum width, height
|
||||
.with_inner_size(egui::vec2(7680.0, 4320.0)), // Initial size
|
||||
..Default::default()
|
||||
};
|
||||
eframe::run_native(
|
||||
"Knightly",
|
||||
options,
|
||||
Box::new(|cc| {
|
||||
let mut fonts = egui::FontDefinitions::default();
|
||||
fonts.font_data.insert(
|
||||
"symbols".to_owned(),
|
||||
egui::FontData::from_static(include_bytes!("../fonts/DejaVuSans.ttf")).into(),
|
||||
);
|
||||
fonts
|
||||
.families
|
||||
.entry(egui::FontFamily::Proportional)
|
||||
.or_default()
|
||||
.insert(0, "symbols".to_owned());
|
||||
cc.egui_ctx.set_fonts(fonts);
|
||||
Ok(Box::new(ChessApp::default()))
|
||||
}),
|
||||
)
|
||||
"Knightly",
|
||||
options,
|
||||
Box::new(|cc| {
|
||||
let mut fonts = egui::FontDefinitions::default();
|
||||
fonts.font_data.insert(
|
||||
"symbols".to_owned(),
|
||||
egui::FontData::from_static(include_bytes!("../fonts/DejaVuSans.ttf")).into(),
|
||||
);
|
||||
fonts
|
||||
.families
|
||||
.entry(egui::FontFamily::Proportional)
|
||||
.or_default()
|
||||
.insert(0, "symbols".to_owned());
|
||||
cc.egui_ctx.set_fonts(fonts);
|
||||
Ok(Box::new(ChessApp::default()))
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
@@ -42,12 +53,12 @@ enum Piece {
|
||||
impl Piece {
|
||||
fn symbol(&self) -> &'static str {
|
||||
match self {
|
||||
Piece::King('w') => "♔",
|
||||
Piece::Queen('w') => "♕",
|
||||
Piece::Rook('w') => "♖",
|
||||
Piece::Bishop('w') => "♗",
|
||||
Piece::Knight('w') => "♘",
|
||||
Piece::Pawn('w') => "♙",
|
||||
Piece::King('w') => "♚",
|
||||
Piece::Queen('w') => "♛",
|
||||
Piece::Rook('w') => "♜",
|
||||
Piece::Bishop('w') => "♝",
|
||||
Piece::Knight('w') => "♞",
|
||||
Piece::Pawn('w') => "♟︎",
|
||||
Piece::King('b') => "♚",
|
||||
Piece::Queen('b') => "♛",
|
||||
Piece::Rook('b') => "♜",
|
||||
@@ -109,7 +120,7 @@ impl Default for ChessApp {
|
||||
selected: None,
|
||||
turn: Turn::White,
|
||||
pending_settings: PendingSettings::default(),
|
||||
server_port: "8080".to_string(), // Default port
|
||||
server_port: "9001".to_string(), // Default port
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -171,9 +182,10 @@ impl ChessApp {
|
||||
self.server_port = self.pending_settings.server_port.clone();
|
||||
|
||||
if let Some(resolution) = self.resolutions.get(self.selected_resolution) {
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(
|
||||
egui::Vec2::new(resolution.0 as f32, resolution.1 as f32)
|
||||
));
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(egui::Vec2::new(
|
||||
resolution.0 as f32,
|
||||
resolution.1 as f32,
|
||||
)));
|
||||
}
|
||||
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Fullscreen(self.fullscreen));
|
||||
@@ -196,12 +208,27 @@ impl eframe::App for ChessApp {
|
||||
ui.heading("♞ Knightly ♞");
|
||||
ui.add_space(30.0);
|
||||
|
||||
if ui.add_sized([300.0, 60.0], egui::Button::new("Play")).clicked() {
|
||||
if ui
|
||||
.add_sized([300.0, 60.0], egui::Button::new("Play"))
|
||||
.clicked()
|
||||
{
|
||||
let port = self.server_port.clone();
|
||||
info!("\nstarting connection\n");
|
||||
|
||||
//create a TCPlistener with tokio and bind machine ip for connection
|
||||
tokio::spawn(async move {
|
||||
info!("tokoi");
|
||||
handle_connection(&port).await
|
||||
});
|
||||
|
||||
self.state = AppState::InGame;
|
||||
}
|
||||
ui.add_space(8.0);
|
||||
|
||||
if ui.add_sized([300.0, 60.0], egui::Button::new("Settings")).clicked() {
|
||||
if ui
|
||||
.add_sized([300.0, 60.0], egui::Button::new("Settings"))
|
||||
.clicked()
|
||||
{
|
||||
self.enter_settings();
|
||||
}
|
||||
ui.add_space(8.0);
|
||||
@@ -225,7 +252,10 @@ impl eframe::App for ChessApp {
|
||||
// Fullscreen toggle
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Fullscreen:");
|
||||
if ui.checkbox(&mut self.pending_settings.fullscreen, "").changed() {
|
||||
if ui
|
||||
.checkbox(&mut self.pending_settings.fullscreen, "")
|
||||
.changed()
|
||||
{
|
||||
// If enabling fullscreen, we might want to disable resolution selection
|
||||
}
|
||||
});
|
||||
@@ -241,7 +271,8 @@ impl eframe::App for ChessApp {
|
||||
self.resolutions[self.pending_settings.selected_resolution].1
|
||||
))
|
||||
.show_ui(ui, |ui| {
|
||||
for (i, &(width, height)) in self.resolutions.iter().enumerate() {
|
||||
for (i, &(width, height)) in self.resolutions.iter().enumerate()
|
||||
{
|
||||
ui.selectable_value(
|
||||
&mut self.pending_settings.selected_resolution,
|
||||
i,
|
||||
@@ -255,20 +286,28 @@ impl eframe::App for ChessApp {
|
||||
// Server port input field
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Local Server Port:");
|
||||
ui.add(egui::TextEdit::singleline(&mut self.pending_settings.server_port)
|
||||
.desired_width(100.0)
|
||||
.hint_text("8080"));
|
||||
ui.add(
|
||||
egui::TextEdit::singleline(&mut self.pending_settings.server_port)
|
||||
.desired_width(100.0)
|
||||
.hint_text("9001"),
|
||||
);
|
||||
});
|
||||
ui.add_space(30.0);
|
||||
|
||||
// Apply and Cancel buttons
|
||||
ui.horizontal(|ui| {
|
||||
if ui.add_sized([140.0, 40.0], egui::Button::new("Apply")).clicked() {
|
||||
if ui
|
||||
.add_sized([140.0, 40.0], egui::Button::new("Apply"))
|
||||
.clicked()
|
||||
{
|
||||
self.apply_settings(ctx);
|
||||
self.state = AppState::MainMenu;
|
||||
}
|
||||
|
||||
if ui.add_sized([140.0, 40.0], egui::Button::new("Cancel")).clicked() {
|
||||
if ui
|
||||
.add_sized([140.0, 40.0], egui::Button::new("Cancel"))
|
||||
.clicked()
|
||||
{
|
||||
self.state = AppState::MainMenu;
|
||||
}
|
||||
});
|
||||
@@ -303,30 +342,32 @@ impl eframe::App for ChessApp {
|
||||
// Create a child UI at the board position
|
||||
let (response, painter) = ui.allocate_painter(
|
||||
egui::Vec2::new(board_size, board_size),
|
||||
egui::Sense::click()
|
||||
egui::Sense::click(),
|
||||
);
|
||||
|
||||
let board_rect = egui::Rect::from_center_size(
|
||||
full_avail.center(),
|
||||
egui::vec2(board_size, board_size)
|
||||
egui::vec2(board_size, board_size),
|
||||
);
|
||||
|
||||
// Draw the chess board
|
||||
let player = "black";
|
||||
if player =="white"{
|
||||
let tile_size = board_size / 8.0;
|
||||
for row in 0..8 {
|
||||
for col in 0..8 {
|
||||
let color = if (row + col) % 2 == 0 {
|
||||
egui::Color32::from_rgb(100, 97, 97)
|
||||
} else {
|
||||
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
|
||||
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);
|
||||
@@ -341,11 +382,19 @@ impl eframe::App for ChessApp {
|
||||
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')) {
|
||||
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
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -355,19 +404,92 @@ impl eframe::App for ChessApp {
|
||||
rect,
|
||||
0.0,
|
||||
egui::Stroke::new(3.0, egui::Color32::RED),
|
||||
egui::StrokeKind::Inside
|
||||
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();
|
||||
let click_pos =
|
||||
ui.ctx().input(|i| i.pointer.interact_pos()).unwrap();
|
||||
if rect.contains(click_pos) {
|
||||
self.handle_click(row, col);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if player=="black"{
|
||||
{
|
||||
let tile_size = board_size / 8.0;
|
||||
for row in 0..8 {
|
||||
for col in 0..8 {
|
||||
let color = if (row + col) % 2 == 0 {
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user