Compare commits
51 Commits
UI/Setting
...
v132
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c1b9a96d9 | ||
|
|
e99b2defa3 | ||
|
|
db333a693f | ||
|
|
fd0d26486b | ||
|
|
e1f4ae717e | ||
|
|
7b9c1edbab | ||
|
|
05294a7736 | ||
|
|
d8da808580 | ||
|
|
c420d8b3dd | ||
|
|
57af3aaae3 | ||
|
|
887b9abed8 | ||
|
|
a862d31c7d | ||
| 48f66aac75 | |||
|
|
431c65e075 | ||
|
|
7e64a7ca16 | ||
|
|
6e0efc76f3 | ||
|
|
1a9d7ad460 | ||
| 6dfe92f211 | |||
|
|
9209f1c4e0 | ||
| 4bb485e833 | |||
|
|
3ebfc61ac7 | ||
|
|
d8d8b58ed7 | ||
|
|
a8970053f9 | ||
|
|
7cd77ab904 | ||
|
|
fa0a93172d | ||
|
|
bb74a486d8 | ||
|
|
10c176a0f5 | ||
|
|
d1932762f9 | ||
|
|
9181b6c4ca | ||
|
|
b703d8b9a1 | ||
|
|
acaa7d078a | ||
|
|
30448b5f3d | ||
|
|
ff38c5c475 | ||
|
|
42daeb9971 | ||
|
|
fc7ece9e94 | ||
|
|
c8b6bc35d5 | ||
|
|
5f2a4e1721 | ||
| c77e534ed8 | |||
|
|
5c42b6e52a | ||
|
|
f7364e7939 | ||
| 1c92918692 | |||
|
|
b4a1f0820f | ||
|
|
6d9d5c49ae | ||
|
|
1ca44b472b | ||
|
|
79aabeac8c | ||
|
|
5eb85dab89 | ||
|
|
291b7e71a2 | ||
|
|
2aaa2aca70 | ||
| cca852d466 | |||
| a098c8051a | |||
| e599722e45 |
3
.github/workflows/upload_data.yml
vendored
3
.github/workflows/upload_data.yml
vendored
@@ -20,7 +20,7 @@ jobs:
|
|||||||
echo "$GOOGLE_SERVICE_ACCOUNT_JSON" > service_account.json
|
echo "$GOOGLE_SERVICE_ACCOUNT_JSON" > service_account.json
|
||||||
|
|
||||||
python <<'PYCODE'
|
python <<'PYCODE'
|
||||||
import gspread, json, subprocess
|
import gspread, json, subprocess, time
|
||||||
|
|
||||||
creds = json.load(open("service_account.json"))
|
creds = json.load(open("service_account.json"))
|
||||||
gc = gspread.service_account_from_dict(creds)
|
gc = gspread.service_account_from_dict(creds)
|
||||||
@@ -38,6 +38,7 @@ jobs:
|
|||||||
|
|
||||||
for i, row in enumerate(rows_to_append):
|
for i, row in enumerate(rows_to_append):
|
||||||
worksheet.insert_row(row, start_row + i)
|
worksheet.insert_row(row, start_row + i)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
with open(f"{v}/test_data.log", "r") as f:
|
with open(f"{v}/test_data.log", "r") as f:
|
||||||
|
|||||||
@@ -22,6 +22,68 @@ A vágyálom rendszer alapját egy központi szerver képezné, amely kezeli a f
|
|||||||
A platform célja a megbízható és folyamatos működés, akár nagyobb terhelés mellett is. A rendszer fejlesztése során kiemelt szempont lenne a biztonság (adatvédelem, csalás elleni védelem), a stabil hálózati kommunikáció, valamint a bővíthetőség – például ranglisták, versenyek vagy mobilalkalmazás későbbi integrálásának lehetősége.
|
A platform célja a megbízható és folyamatos működés, akár nagyobb terhelés mellett is. A rendszer fejlesztése során kiemelt szempont lenne a biztonság (adatvédelem, csalás elleni védelem), a stabil hálózati kommunikáció, valamint a bővíthetőség – például ranglisták, versenyek vagy mobilalkalmazás későbbi integrálásának lehetősége.
|
||||||
|
|
||||||
Összességében a vágyálom rendszer egy minden szempontból teljes értékű, közösségorientált sakkalkalmazás lenne, amely a mostani, helyi hálózaton működő változatból fejlődne tovább egy interneten keresztül bárhonnan elérhető platformmá.
|
Összességében a vágyálom rendszer egy minden szempontból teljes értékű, közösségorientált sakkalkalmazás lenne, amely a mostani, helyi hálózaton működő változatból fejlődne tovább egy interneten keresztül bárhonnan elérhető platformmá.
|
||||||
|
|
||||||
|
## 3. Igényelt funkciók
|
||||||
|
|
||||||
|
### Alapok
|
||||||
|
- Két játékos közti sakkjátszma lebonyolítása.
|
||||||
|
- Teljes szabályrendszer megvalósítása (érvényes lépések, sakk/sakkmatt/patt felismerése).
|
||||||
|
- Új játék indítása.
|
||||||
|
- Játékosok nevének megadása a játszma elején.
|
||||||
|
- Felhasználóbarát grafikus felület (UI) – látható tábla, figurák, órák, státuszjelzések.
|
||||||
|
- Játékoslépések vizuális kiemelése (pl. kijelölt mező, lehetséges lépések megjelenítése).
|
||||||
|
- A játék állapotának kijelzése (folyamatban, sakk, matt, döntetlen).
|
||||||
|
|
||||||
|
### LAN és hálózati funkciók
|
||||||
|
- „Szerver indítása” funkció – a játékos hostként indíthat egy helyi szervert.
|
||||||
|
- „Csatlakozás” funkció – másik játékos IP-cím alapján tud csatlakozni.
|
||||||
|
- Helyi hálózaton keresztüli valós idejű kommunikáció.
|
||||||
|
- LAN játék automatikus felfedezése (broadcast keresés).
|
||||||
|
- Játék mentése és visszatöltése hálózati módban.
|
||||||
|
|
||||||
|
### Online vágyálom funkciók
|
||||||
|
- Felhasználói regisztráció és bejelentkezés.
|
||||||
|
- Jelszóval védett fiókok, email- vagy OAuth-alapú hitelesítés (Google, GitHub stb.).
|
||||||
|
- Profiloldal megtekintése (név, avatar, statisztikák, értékszám).
|
||||||
|
- Automatikus matchmaking rendszer.
|
||||||
|
- Kézi játékindítás – meghívó küldése barátnak.
|
||||||
|
- Játszmák mentése és visszajátszása.
|
||||||
|
- Játszmaelemzés – lépések listázása, hibák kiemelése.
|
||||||
|
- Webes felület vagy mobilalkalmazás támogatása.
|
||||||
|
- Játék előzményeinek és statisztikáinak megtekintése (győzelmek, vereségek, döntetlenek).
|
||||||
|
- Automatikus szervermentés és adatbázis szinkronizáció.
|
||||||
|
|
||||||
|
### Felhasználói felület
|
||||||
|
- Letisztult, reszponzív, platformfüggetlen felület (asztali és webes verzió).
|
||||||
|
- Sötét/világos téma lehetősége.
|
||||||
|
- Egyéni figurakészlet vagy tábla kinézet választása.
|
||||||
|
- Animált figuramozgások.
|
||||||
|
- Egérrel és billentyűzettel is vezérelhető játék.
|
||||||
|
- Hangjelzések a lépésekhez és az idő lejártához.
|
||||||
|
- Lépéselőzmények (move list) megjelenítése oldalt.
|
||||||
|
- Tábla forgatásának lehetősége (pl. a fehér vagy fekete nézőpontból).
|
||||||
|
- Állapotjelzők (sakk, matt, döntetlen, várakozás az ellenfélre).
|
||||||
|
- Teljes képernyős mód.
|
||||||
|
|
||||||
|
### Technikai / fejlesztői funkciók
|
||||||
|
- Kliens–szerver architektúra.
|
||||||
|
- REST API vagy WebSocket alapú kommunikáció.
|
||||||
|
- Adatbázis a felhasználói adatok és meccsek tárolására (pl. SQLite, PostgreSQL, MongoDB).
|
||||||
|
- Logolási és hibakezelési rendszer.
|
||||||
|
- Automatikus mentés és adatvisszaállítás.
|
||||||
|
- Verziókezelés (Git).
|
||||||
|
- Tesztelhető, moduláris kódszerkezet (külön modulok: logika, UI, hálózat, adat).
|
||||||
|
- Cross-platform működés (Windows, Linux, esetleg web).
|
||||||
|
|
||||||
|
### További funkciók
|
||||||
|
- Egyszemélyes mód (ember vs. gép, AI-bot).
|
||||||
|
- Több nehézségi szintű AI.
|
||||||
|
- Oktató mód (javasolt lépések, hibák magyarázata).
|
||||||
|
- Hivatalos FEN/PGN formátum export/import.
|
||||||
|
- Beépített sakkfeladványok, kihívások.
|
||||||
|
- Érintéses vezérlés mobilon.
|
||||||
|
- Többnyelvű felület (pl. magyar, angol).
|
||||||
|
|
||||||
## 4. Rendszer követelmények
|
## 4. Rendszer követelmények
|
||||||
|
|
||||||
A rendszer célja egy kétjátékos sakkalkalmazás megvalósítása, amely alapvetően hálózati kapcsolat (LAN vagy internet) segítségével biztosítja a valós idejű játékot. A rendszer kliens–szerver architektúrán alapul, ahol az egyes komponensek jól elkülönülten, meghatározott feladatokat látnak el.
|
A rendszer célja egy kétjátékos sakkalkalmazás megvalósítása, amely alapvetően hálózati kapcsolat (LAN vagy internet) segítségével biztosítja a valós idejű játékot. A rendszer kliens–szerver architektúrán alapul, ahol az egyes komponensek jól elkülönülten, meghatározott feladatokat látnak el.
|
||||||
|
|||||||
BIN
Knightly prezentáció.pptx
Normal file
BIN
Knightly prezentáció.pptx
Normal file
Binary file not shown.
@@ -4,4 +4,6 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
once_cell = "1.19"
|
once_cell = "1.19"
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
serde_json = "1"
|
||||||
|
|||||||
@@ -3,5 +3,6 @@ mod utils;
|
|||||||
mod legality;
|
mod legality;
|
||||||
mod checkinfo;
|
mod checkinfo;
|
||||||
mod attacks;
|
mod attacks;
|
||||||
|
mod bitmove;
|
||||||
|
|
||||||
pub mod board;
|
pub mod board;
|
||||||
77
engine/src/bitboard/bitmove.rs
Normal file
77
engine/src/bitboard/bitmove.rs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
use super::utils::*;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
|
pub struct BitMove {
|
||||||
|
|
||||||
|
move_type: BitMoveType,
|
||||||
|
from_square: u8,
|
||||||
|
to_square: u8,
|
||||||
|
promotion_piece: Option<u8>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BitMove {
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn quiet(from: u8, to: u8, promotion_piece: Option<u8>) -> Self {
|
||||||
|
return Self {
|
||||||
|
move_type: BitMoveType::Quiet,
|
||||||
|
from_square: from,
|
||||||
|
to_square: to,
|
||||||
|
promotion_piece: promotion_piece
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn capture(from: u8, to: u8, promotion_piece: Option<u8>) -> Self {
|
||||||
|
return Self {
|
||||||
|
move_type: BitMoveType::Capture,
|
||||||
|
from_square: from,
|
||||||
|
to_square: to,
|
||||||
|
promotion_piece: promotion_piece
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn castle(from: u8, to: u8) -> Self {
|
||||||
|
return Self {
|
||||||
|
move_type: BitMoveType::Castle,
|
||||||
|
from_square: from,
|
||||||
|
to_square: to,
|
||||||
|
promotion_piece: None
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn move_type(&self) -> BitMoveType {
|
||||||
|
return self.move_type;
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn from_square(&self) -> u8 {
|
||||||
|
return self.from_square;
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn to_square(&self) -> u8 {
|
||||||
|
return self.to_square;
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn promotion_piece(&self) -> Option<u8> {
|
||||||
|
return self.promotion_piece;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn uci_notation(&self) -> String {
|
||||||
|
let mut notation = notation_from_square_number(self.from_square());
|
||||||
|
notation.push_str(¬ation_from_square_number(self.to_square()));
|
||||||
|
|
||||||
|
if let Some(promotion_piece) = self.promotion_piece {
|
||||||
|
notation.push(get_character_by_piece_id(promotion_piece).to_ascii_lowercase());
|
||||||
|
}
|
||||||
|
|
||||||
|
return notation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
|
pub enum BitMoveType {
|
||||||
|
Quiet,
|
||||||
|
Capture,
|
||||||
|
Castle,
|
||||||
|
EnPassant
|
||||||
|
}
|
||||||
@@ -64,7 +64,7 @@ impl Board {
|
|||||||
|
|
||||||
for (i, c) in coming_up.chars().enumerate() {
|
for (i, c) in coming_up.chars().enumerate() {
|
||||||
if pieces.contains(&c) {
|
if pieces.contains(&c) {
|
||||||
// board.place_piece(row*8 + col, c);
|
board.place_piece(row*8 + col, c);
|
||||||
col += 1;
|
col += 1;
|
||||||
}
|
}
|
||||||
else if ('1'..='8').contains(&c) {
|
else if ('1'..='8').contains(&c) {
|
||||||
@@ -179,4 +179,21 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn place_piece(&mut self, sq: i32, piece: char) {
|
||||||
|
match piece {
|
||||||
|
'p' => {self.bitboards[6] |= 1 << sq}
|
||||||
|
'n' => {self.bitboards[7] |= 1 << sq}
|
||||||
|
'b' => {self.bitboards[8] |= 1 << sq}
|
||||||
|
'r' => {self.bitboards[9] |= 1 << sq}
|
||||||
|
'q' => {self.bitboards[10] |= 1 << sq}
|
||||||
|
'k' => {self.bitboards[11] |= 1 << sq}
|
||||||
|
'P' => {self.bitboards[0] |= 1 << sq}
|
||||||
|
'N' => {self.bitboards[1] |= 1 << sq}
|
||||||
|
'B' => {self.bitboards[2] |= 1 << sq}
|
||||||
|
'R' => {self.bitboards[3] |= 1 << sq}
|
||||||
|
'Q' => {self.bitboards[4] |= 1 << sq}
|
||||||
|
'K' => {self.bitboards[5] |= 1 << sq}
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,56 @@
|
|||||||
use super::board::Board;
|
use super::board::Board;
|
||||||
use super::attackmaps::RAY_TABLE;
|
use super::attackmaps::RAY_TABLE;
|
||||||
|
use super::checkinfo::CheckInfo;
|
||||||
|
use super::attacks::get_raycast_from_square_in_direction;
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
|
|
||||||
|
pub fn check_test(&self) -> CheckInfo {
|
||||||
|
let mut check_info: CheckInfo = CheckInfo::new();
|
||||||
|
let offset: usize = 6 * self.side_to_move as usize;
|
||||||
|
let king: u64 = self.bitboards[5 + offset];
|
||||||
|
let king_sq = king.trailing_zeros() as usize;
|
||||||
|
let occupancy = self.occupancy[2];
|
||||||
|
|
||||||
|
// queen-rook checks (+)
|
||||||
|
let attacker_mask = self.bitboards[10 - offset] | self.bitboards[9 - offset];
|
||||||
|
|
||||||
|
for dir in [0, 2, 4, 6] {
|
||||||
|
let threat_mask: u64 = get_raycast_from_square_in_direction(occupancy, king_sq, dir);
|
||||||
|
if threat_mask & attacker_mask != 0 {
|
||||||
|
check_info.add_checker(threat_mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// queen-bishop checks (x)
|
||||||
|
let attacker_mask = self.bitboards[10 - offset] | self.bitboards[8 - offset];
|
||||||
|
|
||||||
|
for dir in [1, 3, 5, 7] {
|
||||||
|
let threat_mask = get_raycast_from_square_in_direction(occupancy, king_sq, dir);
|
||||||
|
if threat_mask & attacker_mask != 0 {
|
||||||
|
check_info.add_checker(threat_mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// knight checks (L)
|
||||||
|
let attacker_mask = self.bitboards[7 - offset];
|
||||||
|
let threat_mask = self.get_pseudo_knight_moves(king_sq as u32);
|
||||||
|
let checker = threat_mask & attacker_mask;
|
||||||
|
if checker != 0 {
|
||||||
|
check_info.add_checker(checker);
|
||||||
|
}
|
||||||
|
|
||||||
|
// pawn checks (v)
|
||||||
|
let attacker_mask = self.bitboards[6 - offset];
|
||||||
|
let threat_mask = self.get_pseudo_pawn_captures(king_sq as u32);
|
||||||
|
let checker = threat_mask & attacker_mask;
|
||||||
|
if checker != 0 {
|
||||||
|
check_info.add_checker(checker);
|
||||||
|
}
|
||||||
|
|
||||||
|
return check_info;
|
||||||
|
}
|
||||||
|
|
||||||
pub(in super) fn calc_pinned_squares(&mut self) {
|
pub(in super) fn calc_pinned_squares(&mut self) {
|
||||||
self.pinned_squares = [4; 64];
|
self.pinned_squares = [4; 64];
|
||||||
self.pin_mask = 0u64;
|
self.pin_mask = 0u64;
|
||||||
@@ -45,4 +93,35 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// <----- TESTS ----->
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn check_test_test() {
|
||||||
|
|
||||||
|
let fens = [
|
||||||
|
"rnb1k1nr/pppppppp/4q3/8/1b6/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", // no check
|
||||||
|
"rnb1k1nr/pppppppp/4q3/8/1b1P4/8/PPP1PPPP/RNBQKBNR w KQkq d3 0 1", // single check
|
||||||
|
"rnb1k1nr/ppp1p2p/3pq1p1/8/1b1P1P2/8/PPP2PPP/RNBQKBNR w KQkq - 0 1" // double check
|
||||||
|
];
|
||||||
|
let expected_results = [
|
||||||
|
CheckInfo { check_count: 0, move_mask: 0xFFFF_FFFF_FFFF_FFFF },
|
||||||
|
CheckInfo { check_count: 1, move_mask: 0x0000_0000_0204_0800 },
|
||||||
|
CheckInfo { check_count: 2, move_mask: 0x0000_0000_0000_0000 }
|
||||||
|
];
|
||||||
|
|
||||||
|
for test_nr in 0..3 {
|
||||||
|
let fen = fens[test_nr];
|
||||||
|
let board = Board::build(fen);
|
||||||
|
let check_test_actual = board.check_test();
|
||||||
|
assert_eq!(check_test_actual.check_count, expected_results[test_nr].check_count);
|
||||||
|
assert_eq!(check_test_actual.move_mask, expected_results[test_nr].move_mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -48,6 +48,11 @@ pub fn try_get_square_number_from_notation(notation: &str) -> Result<u8, ()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const PIECE_CHARACTERS: [char; 12] = ['P', 'N', 'B', 'R', 'Q', 'K', 'p', 'n', 'b', 'r', 'q', 'k'];
|
||||||
|
pub fn get_character_by_piece_id(id: u8) -> char {
|
||||||
|
return PIECE_CHARACTERS[id as usize];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// <----- TESTS ----->
|
// <----- TESTS ----->
|
||||||
|
|
||||||
|
|||||||
31
engine/src/boardsquare.rs
Normal file
31
engine/src/boardsquare.rs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct BoardSquare {
|
||||||
|
pub x: usize,
|
||||||
|
pub y: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BoardSquare {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
return Self { x: 0, y: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_coord(x: usize, y: usize) -> Self {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
{
|
||||||
|
if x > 7 {
|
||||||
|
println!(
|
||||||
|
"Warning: x coordinate of square is bigger than 7, it might not be on the board!"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if y > 7 {
|
||||||
|
println!(
|
||||||
|
"Warning: y coordinate of square is bigger than 7, it might not be on the board!"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Self { x: x, y: y };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
71
engine/src/chessmove.rs
Normal file
71
engine/src/chessmove.rs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
use crate::piecetype;
|
||||||
|
|
||||||
|
use super::boardsquare::BoardSquare;
|
||||||
|
use super::movetype::MoveType;
|
||||||
|
use super::piecetype::PieceType;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct ChessMove {
|
||||||
|
pub move_type: MoveType,
|
||||||
|
pub piece_type: PieceType,
|
||||||
|
pub from_square: BoardSquare,
|
||||||
|
pub to_square: BoardSquare,
|
||||||
|
pub rook_from: BoardSquare,
|
||||||
|
pub rook_to: BoardSquare,
|
||||||
|
pub promotion_piece: Option<PieceType>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChessMove {
|
||||||
|
pub fn quiet(
|
||||||
|
piece_type: PieceType,
|
||||||
|
from_square: BoardSquare,
|
||||||
|
to_square: BoardSquare,
|
||||||
|
promotion_piece: Option<PieceType>,
|
||||||
|
) -> Self {
|
||||||
|
return Self {
|
||||||
|
move_type: MoveType::Quiet,
|
||||||
|
piece_type: piece_type,
|
||||||
|
from_square: from_square,
|
||||||
|
to_square: to_square,
|
||||||
|
rook_from: BoardSquare::new(),
|
||||||
|
rook_to: BoardSquare::new(),
|
||||||
|
promotion_piece: promotion_piece,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn capture(
|
||||||
|
piece_type: PieceType,
|
||||||
|
from_square: BoardSquare,
|
||||||
|
to_square: BoardSquare,
|
||||||
|
promotion_piece: Option<PieceType>,
|
||||||
|
) -> Self {
|
||||||
|
return Self {
|
||||||
|
move_type: MoveType::Capture,
|
||||||
|
piece_type: piece_type,
|
||||||
|
from_square: from_square,
|
||||||
|
to_square: to_square,
|
||||||
|
rook_from: BoardSquare::new(),
|
||||||
|
rook_to: BoardSquare::new(),
|
||||||
|
promotion_piece: promotion_piece,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn castle(
|
||||||
|
piece_type: PieceType,
|
||||||
|
from_square: BoardSquare,
|
||||||
|
to_square: BoardSquare,
|
||||||
|
rook_from: BoardSquare,
|
||||||
|
rook_to: BoardSquare,
|
||||||
|
) -> Self {
|
||||||
|
return Self {
|
||||||
|
move_type: MoveType::Quiet,
|
||||||
|
piece_type: piece_type,
|
||||||
|
from_square: from_square,
|
||||||
|
to_square: to_square,
|
||||||
|
rook_from: rook_from,
|
||||||
|
rook_to: rook_to,
|
||||||
|
promotion_piece: None,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
6
engine/src/gameend.rs
Normal file
6
engine/src/gameend.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
pub enum GameEnd {
|
||||||
|
WhiteWon(String),
|
||||||
|
BlackWon(String),
|
||||||
|
Draw(String)
|
||||||
|
}
|
||||||
24
engine/src/lib.rs
Normal file
24
engine/src/lib.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
mod bitboard;
|
||||||
|
pub mod chessmove;
|
||||||
|
pub mod piecetype;
|
||||||
|
pub mod boardsquare;
|
||||||
|
pub mod movetype;
|
||||||
|
pub mod gameend;
|
||||||
|
|
||||||
|
use chessmove::ChessMove;
|
||||||
|
use gameend::GameEnd;
|
||||||
|
|
||||||
|
pub fn get_available_moves(fen: &str) -> Vec<ChessMove> {
|
||||||
|
println!("get_available_moves answered");
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_game_over(fen: &str) -> Option<GameEnd> {
|
||||||
|
println!("is_game_over answered");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_board_after_move(fen: &str, chess_move: &ChessMove) -> String {
|
||||||
|
println!("get_board_after_move answered");
|
||||||
|
return String::from("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
||||||
|
}
|
||||||
11
engine/src/movetype.rs
Normal file
11
engine/src/movetype.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub enum MoveType {
|
||||||
|
Quiet,
|
||||||
|
Capture,
|
||||||
|
Castle,
|
||||||
|
EnPassant,
|
||||||
|
}
|
||||||
|
|
||||||
18
engine/src/piecetype.rs
Normal file
18
engine/src/piecetype.rs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub enum PieceType {
|
||||||
|
WhitePawn,
|
||||||
|
WhiteKnight,
|
||||||
|
WhiteBishop,
|
||||||
|
WhiteRook,
|
||||||
|
WhiteQueen,
|
||||||
|
WhiteKing,
|
||||||
|
BlackPawn,
|
||||||
|
BlackKnight,
|
||||||
|
BlackBishop,
|
||||||
|
BlackRook,
|
||||||
|
BlackQueen,
|
||||||
|
BlackKing,
|
||||||
|
}
|
||||||
|
|
||||||
@@ -14,3 +14,9 @@ url = "2.5.7"
|
|||||||
uuid = {version = "1.18.1", features = ["v4", "serde"] }
|
uuid = {version = "1.18.1", features = ["v4", "serde"] }
|
||||||
anyhow = "1.0.100"
|
anyhow = "1.0.100"
|
||||||
rand = "0.9.2"
|
rand = "0.9.2"
|
||||||
|
engine = {path = "../engine/"}
|
||||||
|
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "client"
|
||||||
|
path = "src/bin/client.rs"
|
||||||
|
|||||||
195
server/src/bin/client.rs
Normal file
195
server/src/bin/client.rs
Normal file
@@ -0,0 +1,195 @@
|
|||||||
|
use futures_util::{SinkExt, StreamExt};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::io::{self, Write};
|
||||||
|
use tokio_tungstenite::{connect_async, tungstenite::Message};
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
enum ClientMessage {
|
||||||
|
Join { username: String },
|
||||||
|
FindMatch,
|
||||||
|
Move { from: String, to: String },
|
||||||
|
Resign,
|
||||||
|
Chat { text: 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>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("Knightly Chess Client");
|
||||||
|
println!("========================");
|
||||||
|
|
||||||
|
// Get server address from user
|
||||||
|
print!("Enter server address [ws://127.0.0.1:9001]: ");
|
||||||
|
io::stdout().flush()?;
|
||||||
|
let mut server_addr = String::new();
|
||||||
|
io::stdin().read_line(&mut server_addr)?;
|
||||||
|
let server_addr = server_addr.trim();
|
||||||
|
let server_addr = if server_addr.is_empty() {
|
||||||
|
"ws://127.0.0.1:9001".to_string()
|
||||||
|
} else {
|
||||||
|
server_addr.to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Connect to server
|
||||||
|
println!("Connecting to {}...", server_addr);
|
||||||
|
let url = Url::parse(&server_addr)?;
|
||||||
|
let (ws_stream, _) = connect_async(url).await?;
|
||||||
|
println!("Connected to server!");
|
||||||
|
|
||||||
|
let (mut write, mut read) = ws_stream.split();
|
||||||
|
|
||||||
|
// Spawn a task to handle incoming messages
|
||||||
|
let read_handle = tokio::spawn(async move {
|
||||||
|
while let Some(message) = read.next().await {
|
||||||
|
match message {
|
||||||
|
Ok(msg) => {
|
||||||
|
if msg.is_text() {
|
||||||
|
let text = msg.to_text().unwrap();
|
||||||
|
println!("\nServer: {}", text);
|
||||||
|
|
||||||
|
// Try to parse as structured message
|
||||||
|
if let Ok(parsed) = serde_json::from_str::<ServerMessage>(text) {
|
||||||
|
match parsed.message_type.as_str() {
|
||||||
|
"welcome" => {
|
||||||
|
if let Some(player_id) = parsed.player_id {
|
||||||
|
println!("Welcome! Your player ID: {}", player_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
println!("cucc: {:?}", parsed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error receiving message: {}", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Main loop for sending messages
|
||||||
|
println!("\nAvailable commands:");
|
||||||
|
println!(" join <username> - Join the server");
|
||||||
|
println!(" findmatch - Find a match");
|
||||||
|
println!(" move <from> <to> - Make a move (e.g., move e2 e4)");
|
||||||
|
println!(" chat <message> - Send chat message");
|
||||||
|
println!(" resign - Resign from current game");
|
||||||
|
println!(" quit - Exit client");
|
||||||
|
println!();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
print!("➡️ Enter command: ");
|
||||||
|
io::stdout().flush()?;
|
||||||
|
|
||||||
|
let mut input = String::new();
|
||||||
|
io::stdin().read_line(&mut input)?;
|
||||||
|
let input = input.trim();
|
||||||
|
|
||||||
|
if input.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let parts: Vec<&str> = input.split_whitespace().collect();
|
||||||
|
let command = parts[0].to_lowercase();
|
||||||
|
|
||||||
|
match command.as_str() {
|
||||||
|
"quit" | "exit" => {
|
||||||
|
println!("👋 Goodbye!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
"join" => {
|
||||||
|
if parts.len() >= 2 {
|
||||||
|
let username = parts[1..].join(" ");
|
||||||
|
let message = ClientMessage::Join { username };
|
||||||
|
send_message(&mut write, &message).await?;
|
||||||
|
} else {
|
||||||
|
println!("Usage: join <username>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"findmatch" | "find" => {
|
||||||
|
let message = ClientMessage::FindMatch;
|
||||||
|
send_message(&mut write, &message).await?;
|
||||||
|
println!("🔍 Searching for a match...");
|
||||||
|
}
|
||||||
|
"move" => {
|
||||||
|
if parts.len() >= 3 {
|
||||||
|
let from = parts[1].to_string();
|
||||||
|
let to = parts[2].to_string();
|
||||||
|
let message = ClientMessage::Move { from, to };
|
||||||
|
send_message(&mut write, &message).await?;
|
||||||
|
println!("♟️ Sent move: {} -> {}", parts[1], parts[2]);
|
||||||
|
} else {
|
||||||
|
println!("Usage: move <from> <to> (e.g., move e2 e4)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"chat" => {
|
||||||
|
if parts.len() >= 2 {
|
||||||
|
let text = parts[1..].join(" ");
|
||||||
|
let message = ClientMessage::Chat { text };
|
||||||
|
send_message(&mut write, &message).await?;
|
||||||
|
} else {
|
||||||
|
println!("Usage: chat <message>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"resign" => {
|
||||||
|
let message = ClientMessage::Resign;
|
||||||
|
send_message(&mut write, &message).await?;
|
||||||
|
println!("Resigned from current game");
|
||||||
|
}
|
||||||
|
"help" => {
|
||||||
|
print_help();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
println!(
|
||||||
|
"Unknown command: {}. Type 'help' for available commands.",
|
||||||
|
command
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
read_handle.abort();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn send_message(
|
||||||
|
write: &mut futures_util::stream::SplitSink<
|
||||||
|
tokio_tungstenite::WebSocketStream<
|
||||||
|
tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>,
|
||||||
|
>,
|
||||||
|
Message,
|
||||||
|
>,
|
||||||
|
message: &ClientMessage,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let json = serde_json::to_string(message)?;
|
||||||
|
write.send(Message::Text(json)).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_help() {
|
||||||
|
println!("\n📖 Available Commands:");
|
||||||
|
println!(" join <username> - Register with a username");
|
||||||
|
println!(" findmatch - Enter matchmaking queue");
|
||||||
|
println!(" move <from> <to> - Make a chess move");
|
||||||
|
println!(" chat <message> - Send chat to opponent");
|
||||||
|
println!(" resign - Resign from current game");
|
||||||
|
println!(" help - Show this help");
|
||||||
|
println!(" quit - Exit the client");
|
||||||
|
println!();
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use crate::connection::ClientEvent::*;
|
||||||
|
use engine::get_available_moves;
|
||||||
use futures_util::{SinkExt, StreamExt};
|
use futures_util::{SinkExt, StreamExt};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::{HashMap, VecDeque};
|
||||||
@@ -26,6 +28,28 @@ pub fn new_waiting_queue() -> WaitingQueue {
|
|||||||
Arc::new(Mutex::new(VecDeque::new()))
|
Arc::new(Mutex::new(VecDeque::new()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Step {
|
||||||
|
pub from: String,
|
||||||
|
pub to: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
enum ClientEvent {
|
||||||
|
Join { username: String },
|
||||||
|
FindMatch,
|
||||||
|
Move { from: String, to: String },
|
||||||
|
Resign,
|
||||||
|
Chat { text: String },
|
||||||
|
RequestLegalMoves { fen: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct EventResponse {
|
||||||
|
pub response: Result<(), String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PlayerConnection {
|
pub struct PlayerConnection {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
@@ -43,12 +67,6 @@ pub struct GameMatch {
|
|||||||
pub move_history: Vec<String>,
|
pub move_history: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct Step {
|
|
||||||
pub from: String,
|
|
||||||
pub to: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Message sending utilities
|
// Message sending utilities
|
||||||
pub async fn send_message_to_player(
|
pub async fn send_message_to_player(
|
||||||
connections: &ConnectionMap,
|
connections: &ConnectionMap,
|
||||||
@@ -102,7 +120,6 @@ pub async fn handle_connection(
|
|||||||
connections: ConnectionMap,
|
connections: ConnectionMap,
|
||||||
matches: MatchMap,
|
matches: MatchMap,
|
||||||
waiting_queue: WaitingQueue,
|
waiting_queue: WaitingQueue,
|
||||||
event_system: crate::events::EventSystem,
|
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
use tokio_tungstenite::accept_async;
|
use tokio_tungstenite::accept_async;
|
||||||
|
|
||||||
@@ -141,8 +158,52 @@ pub async fn handle_connection(
|
|||||||
let text = message.to_text()?;
|
let text = message.to_text()?;
|
||||||
println!("Received from {}: {}", player_id, text);
|
println!("Received from {}: {}", player_id, text);
|
||||||
|
|
||||||
// TODO: Parse and handle message with event system
|
let client_data: ClientEvent = serde_json::from_str(text)
|
||||||
// This will be implemented when we integrate the event system
|
.expect("Failed to convert data into json at handle_connection");
|
||||||
|
|
||||||
|
println!("client: {:?}", client_data);
|
||||||
|
|
||||||
|
match client_data {
|
||||||
|
Join { username } => {
|
||||||
|
{
|
||||||
|
let mut conn_map = connections.lock().await;
|
||||||
|
let player = conn_map.get_mut(&player_id).unwrap();
|
||||||
|
player.username = Some(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
//respone to client
|
||||||
|
let response: EventResponse = EventResponse {
|
||||||
|
response: core::result::Result::Ok(()),
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("response: {:?}", response);
|
||||||
|
|
||||||
|
let _ = send_message_to_player(
|
||||||
|
&connections,
|
||||||
|
player_id,
|
||||||
|
&serde_json::to_string(&response).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
FindMatch => {
|
||||||
|
let mut wait_queue = waiting_queue.lock().await;
|
||||||
|
wait_queue.push_back(player_id.clone());
|
||||||
|
println!("Appended {} to the waiting queue", player_id);
|
||||||
|
println!("queue {:?}", wait_queue);
|
||||||
|
}
|
||||||
|
Move { from, to } => {}
|
||||||
|
RequestLegalMoves { fen } => {
|
||||||
|
let moves = get_available_moves(&fen);
|
||||||
|
let _ = send_message_to_player(
|
||||||
|
&connections,
|
||||||
|
player_id,
|
||||||
|
&serde_json::to_string(&moves).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
println!("Sent moves to player: {}", player_id);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,126 +0,0 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::sync::Arc;
|
|
||||||
use tokio::sync::Mutex;
|
|
||||||
use tokio::sync::mpsc;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct Step {
|
|
||||||
pub from: String,
|
|
||||||
pub to: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
#[serde(tag = "type")]
|
|
||||||
pub enum ClientEvent {
|
|
||||||
Join { username: String },
|
|
||||||
FindMatch,
|
|
||||||
Move { from: String, to: String },
|
|
||||||
Resign,
|
|
||||||
Chat { text: String },
|
|
||||||
RequestLegalMoves { fen: String },
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum ServerEvent {
|
|
||||||
PlayerJoined(Uuid, String),
|
|
||||||
PlayerLeft(Uuid),
|
|
||||||
PlayerJoinedQueue(Uuid),
|
|
||||||
PlayerJoinedMatch(Uuid, Uuid), // player_id, match_id
|
|
||||||
PlayerMove(Uuid, Step),
|
|
||||||
PlayerResigned(Uuid),
|
|
||||||
MatchCreated(Uuid, Uuid, Uuid), // match_id, white_id, black_id
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct EventSystem {
|
|
||||||
sender: mpsc::UnboundedSender<(Uuid, ClientEvent)>,
|
|
||||||
receiver: Arc<Mutex<mpsc::UnboundedReceiver<(Uuid, ClientEvent)>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Clone for EventSystem {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
sender: self.sender.clone(),
|
|
||||||
receiver: Arc::clone(&self.receiver),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EventSystem {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
let (sender, receiver) = mpsc::unbounded_channel();
|
|
||||||
Self {
|
|
||||||
sender,
|
|
||||||
receiver: Arc::new(Mutex::new(receiver)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn send_event(
|
|
||||||
&self,
|
|
||||||
player_id: Uuid,
|
|
||||||
event: ClientEvent,
|
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
self.sender.send((player_id, event))?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn next_event(&self) -> Option<(Uuid, ClientEvent)> {
|
|
||||||
let mut receiver = self.receiver.lock().await;
|
|
||||||
receiver.recv().await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for EventSystem {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_event_system_send_and_receive() {
|
|
||||||
let event_system = EventSystem::new();
|
|
||||||
let player_id = Uuid::new_v4();
|
|
||||||
|
|
||||||
let join_event = ClientEvent::Join {
|
|
||||||
username: "test_user".to_string(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let send_result = event_system.send_event(player_id, join_event).await;
|
|
||||||
assert!(send_result.is_ok(), "Should send event successfully");
|
|
||||||
|
|
||||||
let received = event_system.next_event().await;
|
|
||||||
assert!(received.is_some(), "Should receive sent event");
|
|
||||||
|
|
||||||
let (received_id, received_event) = received.unwrap();
|
|
||||||
assert_eq!(received_id, player_id, "Should receive correct player ID");
|
|
||||||
|
|
||||||
match received_event {
|
|
||||||
ClientEvent::Join { username } => {
|
|
||||||
assert_eq!(username, "test_user", "Should receive correct username");
|
|
||||||
}
|
|
||||||
_ => panic!("Should receive Join event"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_event_system_clone() {
|
|
||||||
let event_system1 = EventSystem::new();
|
|
||||||
let event_system2 = event_system1.clone();
|
|
||||||
|
|
||||||
let player_id = Uuid::new_v4();
|
|
||||||
let event = ClientEvent::FindMatch;
|
|
||||||
|
|
||||||
event_system1.send_event(player_id, event).await.unwrap();
|
|
||||||
|
|
||||||
let received = event_system2.next_event().await;
|
|
||||||
assert!(
|
|
||||||
received.is_some(),
|
|
||||||
"Cloned event system should receive events"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
mod connection;
|
mod connection;
|
||||||
mod events;
|
|
||||||
mod matchmaking;
|
mod matchmaking;
|
||||||
|
mod messages;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@@ -14,15 +14,11 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
let matches = connection::new_match_map();
|
let matches = connection::new_match_map();
|
||||||
let waiting_queue = connection::new_waiting_queue();
|
let waiting_queue = connection::new_waiting_queue();
|
||||||
|
|
||||||
// Event system for communication between components
|
|
||||||
let event_system = events::EventSystem::new();
|
|
||||||
|
|
||||||
// Start matchmaking background task
|
// Start matchmaking background task
|
||||||
let matchmaker = matchmaking::MatchmakingSystem::new(
|
let matchmaker = matchmaking::MatchmakingSystem::new(
|
||||||
connections.clone(),
|
connections.clone(),
|
||||||
matches.clone(),
|
matches.clone(),
|
||||||
waiting_queue.clone(),
|
waiting_queue.clone(),
|
||||||
event_system.clone(),
|
|
||||||
);
|
);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
matchmaker.run().await;
|
matchmaker.run().await;
|
||||||
@@ -33,17 +29,10 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
let connections = connections.clone();
|
let connections = connections.clone();
|
||||||
let matches = matches.clone();
|
let matches = matches.clone();
|
||||||
let waiting_queue = waiting_queue.clone();
|
let waiting_queue = waiting_queue.clone();
|
||||||
let event_system = event_system.clone();
|
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Err(e) = connection::handle_connection(
|
if let Err(e) =
|
||||||
stream,
|
connection::handle_connection(stream, connections, matches, waiting_queue).await
|
||||||
connections,
|
|
||||||
matches,
|
|
||||||
waiting_queue,
|
|
||||||
event_system,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
{
|
||||||
eprintln!("Connection error: {}", e);
|
eprintln!("Connection error: {}", e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use crate::connection::{ConnectionMap, GameMatch, MatchMap, WaitingQueue};
|
use crate::connection::{ConnectionMap, GameMatch, MatchMap, WaitingQueue};
|
||||||
use crate::events::EventSystem;
|
|
||||||
use rand::random;
|
use rand::random;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@@ -7,21 +6,14 @@ pub struct MatchmakingSystem {
|
|||||||
connections: ConnectionMap,
|
connections: ConnectionMap,
|
||||||
matches: MatchMap,
|
matches: MatchMap,
|
||||||
waiting_queue: WaitingQueue,
|
waiting_queue: WaitingQueue,
|
||||||
event_system: EventSystem,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MatchmakingSystem {
|
impl MatchmakingSystem {
|
||||||
pub fn new(
|
pub fn new(connections: ConnectionMap, matches: MatchMap, waiting_queue: WaitingQueue) -> Self {
|
||||||
connections: ConnectionMap,
|
|
||||||
matches: MatchMap,
|
|
||||||
waiting_queue: WaitingQueue,
|
|
||||||
event_system: EventSystem,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
connections,
|
connections,
|
||||||
matches,
|
matches,
|
||||||
waiting_queue,
|
waiting_queue,
|
||||||
event_system,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +106,6 @@ impl MatchmakingSystem {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::events::EventSystem;
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::connection::new_connection_map;
|
use crate::connection::new_connection_map;
|
||||||
@@ -126,14 +117,9 @@ mod tests {
|
|||||||
let connections = new_connection_map();
|
let connections = new_connection_map();
|
||||||
let matches = new_match_map();
|
let matches = new_match_map();
|
||||||
let waiting_queue = new_waiting_queue();
|
let waiting_queue = new_waiting_queue();
|
||||||
let event_system = EventSystem::new();
|
|
||||||
|
|
||||||
let matchmaking = MatchmakingSystem::new(
|
let matchmaking =
|
||||||
connections.clone(),
|
MatchmakingSystem::new(connections.clone(), matches.clone(), waiting_queue.clone());
|
||||||
matches.clone(),
|
|
||||||
waiting_queue.clone(),
|
|
||||||
event_system.clone(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let player1 = Uuid::new_v4();
|
let player1 = Uuid::new_v4();
|
||||||
let player2 = Uuid::new_v4();
|
let player2 = Uuid::new_v4();
|
||||||
@@ -172,14 +158,9 @@ mod tests {
|
|||||||
let connections = new_connection_map();
|
let connections = new_connection_map();
|
||||||
let matches = new_match_map();
|
let matches = new_match_map();
|
||||||
let waiting_queue = new_waiting_queue();
|
let waiting_queue = new_waiting_queue();
|
||||||
let event_system = EventSystem::new();
|
|
||||||
|
|
||||||
let matchmaking = MatchmakingSystem::new(
|
let matchmaking =
|
||||||
connections.clone(),
|
MatchmakingSystem::new(connections.clone(), matches.clone(), waiting_queue.clone());
|
||||||
matches.clone(),
|
|
||||||
waiting_queue.clone(),
|
|
||||||
event_system.clone(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let player1 = Uuid::new_v4();
|
let player1 = Uuid::new_v4();
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use eframe::egui;
|
|||||||
fn main() -> eframe::Result<()> {
|
fn main() -> eframe::Result<()> {
|
||||||
let options = eframe::NativeOptions{
|
let options = eframe::NativeOptions{
|
||||||
viewport: egui::ViewportBuilder::default()
|
viewport: egui::ViewportBuilder::default()
|
||||||
.with_fullscreen(false)
|
.with_fullscreen(true)
|
||||||
.with_min_inner_size(egui::vec2(800.0, 600.0)) // Minimum width, height
|
.with_min_inner_size(egui::vec2(800.0, 600.0)) // Minimum width, height
|
||||||
.with_inner_size(egui::vec2(7680.0, 4320.0)), // Initial size
|
.with_inner_size(egui::vec2(7680.0, 4320.0)), // Initial size
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@@ -101,6 +101,7 @@ impl Default for ChessApp {
|
|||||||
(1920, 1080),
|
(1920, 1080),
|
||||||
(2560, 1440),
|
(2560, 1440),
|
||||||
(3840, 2160),
|
(3840, 2160),
|
||||||
|
(7680, 4320),
|
||||||
],
|
],
|
||||||
selected_resolution: 2, // Default to 1920x1080
|
selected_resolution: 2, // Default to 1920x1080
|
||||||
state: AppState::MainMenu,
|
state: AppState::MainMenu,
|
||||||
@@ -427,4 +428,4 @@ mod tests {
|
|||||||
let app = ChessApp::default();
|
let app = ChessApp::default();
|
||||||
assert_eq!(app.server_port, "8080");
|
assert_eq!(app.server_port, "8080");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user