Chess server uwu
This commit is contained in:
parent
1cd980989a
commit
0175f2d933
17
Cargo.lock
generated
17
Cargo.lock
generated
|
@ -19,6 +19,22 @@ dependencies = [
|
|||
"tokio-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "actix-cors"
|
||||
version = "0.6.0-beta.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4f1bd0e31c745df129f0e94efd374d21f2a455bcc386c15d78ed9a9e7d4dd50"
|
||||
dependencies = [
|
||||
"actix-service",
|
||||
"actix-utils",
|
||||
"actix-web",
|
||||
"derive_more",
|
||||
"futures-util",
|
||||
"log",
|
||||
"once_cell",
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "actix-http"
|
||||
version = "3.0.0-beta.17"
|
||||
|
@ -258,6 +274,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||
name = "chess"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"actix-cors",
|
||||
"actix-web",
|
||||
"serde",
|
||||
"serde_repr",
|
||||
|
|
|
@ -6,6 +6,7 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
actix-cors = "0.6.0-beta.8"
|
||||
actix-web = { version = "4.0.0-beta.18", default-features = false }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_repr = "0.1"
|
||||
|
|
128
src/main.rs
128
src/main.rs
|
@ -1,3 +1,4 @@
|
|||
use actix_cors::Cors;
|
||||
use actix_web::web::{Data, Json};
|
||||
use actix_web::{App, HttpResponse, HttpServer};
|
||||
use std::collections::HashMap;
|
||||
|
@ -66,7 +67,9 @@ struct Piece {
|
|||
color: Color,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
#[derive(
|
||||
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize,
|
||||
)]
|
||||
struct Coordinates {
|
||||
file: File,
|
||||
rank: Rank,
|
||||
|
@ -87,14 +90,18 @@ struct Start {
|
|||
player_color: Color,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize)]
|
||||
#[derive(
|
||||
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize,
|
||||
)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
enum Color {
|
||||
Black,
|
||||
White,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize)]
|
||||
#[derive(
|
||||
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize,
|
||||
)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
enum PieceKind {
|
||||
Pawn,
|
||||
|
@ -107,6 +114,7 @@ enum PieceKind {
|
|||
|
||||
#[derive(
|
||||
Clone,
|
||||
Debug,
|
||||
PartialEq,
|
||||
Eq,
|
||||
PartialOrd,
|
||||
|
@ -128,6 +136,44 @@ enum Rank {
|
|||
}
|
||||
|
||||
impl Rank {
|
||||
fn next(&self) -> Option<Self> {
|
||||
match self {
|
||||
Self::One => Some(Self::Two),
|
||||
Self::Two => Some(Self::Three),
|
||||
Self::Three => Some(Self::Four),
|
||||
Self::Four => Some(Self::Five),
|
||||
Self::Five => Some(Self::Six),
|
||||
Self::Six => Some(Self::Seven),
|
||||
Self::Seven => Some(Self::Eight),
|
||||
Self::Eight => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn prev(&self) -> Option<Self> {
|
||||
match self {
|
||||
Self::One => None,
|
||||
Self::Two => Some(Self::One),
|
||||
Self::Three => Some(Self::Two),
|
||||
Self::Four => Some(Self::Three),
|
||||
Self::Five => Some(Self::Four),
|
||||
Self::Six => Some(Self::Five),
|
||||
Self::Seven => Some(Self::Six),
|
||||
Self::Eight => Some(Self::Seven),
|
||||
}
|
||||
}
|
||||
|
||||
fn diff(&self, other: &Self) -> Option<u8> {
|
||||
if self > other {
|
||||
Some(self.to_number() - other.to_number())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn absolute_diff(&self, other: &Self) -> u8 {
|
||||
self.diff(other).or_else(|| other.diff(self)).unwrap_or(0)
|
||||
}
|
||||
|
||||
fn to_number(&self) -> u8 {
|
||||
match self {
|
||||
Self::One => 0,
|
||||
|
@ -156,7 +202,9 @@ impl Rank {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize)]
|
||||
#[derive(
|
||||
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize,
|
||||
)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
enum File {
|
||||
A,
|
||||
|
@ -170,6 +218,44 @@ enum File {
|
|||
}
|
||||
|
||||
impl File {
|
||||
fn next(&self) -> Option<Self> {
|
||||
match self {
|
||||
Self::A => Some(Self::B),
|
||||
Self::B => Some(Self::C),
|
||||
Self::C => Some(Self::D),
|
||||
Self::D => Some(Self::E),
|
||||
Self::E => Some(Self::F),
|
||||
Self::F => Some(Self::G),
|
||||
Self::G => Some(Self::H),
|
||||
Self::H => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn prev(&self) -> Option<Self> {
|
||||
match self {
|
||||
Self::A => None,
|
||||
Self::B => Some(Self::A),
|
||||
Self::C => Some(Self::B),
|
||||
Self::D => Some(Self::C),
|
||||
Self::E => Some(Self::D),
|
||||
Self::F => Some(Self::E),
|
||||
Self::G => Some(Self::F),
|
||||
Self::H => Some(Self::G),
|
||||
}
|
||||
}
|
||||
|
||||
fn diff(&self, other: &Self) -> Option<u8> {
|
||||
if self > other {
|
||||
Some(self.to_number() - other.to_number())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn absolute_diff(&self, other: &Self) -> u8 {
|
||||
self.diff(other).or_else(|| other.diff(self)).unwrap_or(0)
|
||||
}
|
||||
|
||||
fn to_number(&self) -> u8 {
|
||||
match self {
|
||||
Self::A => 0,
|
||||
|
@ -198,14 +284,9 @@ impl File {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct FileState {
|
||||
inner: HashMap<Rank, Piece>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct BoardState {
|
||||
inner: HashMap<File, FileState>,
|
||||
inner: HashMap<Coordinates, Piece>,
|
||||
}
|
||||
|
||||
impl BoardState {
|
||||
|
@ -213,9 +294,8 @@ impl BoardState {
|
|||
let mut this = Self::default();
|
||||
|
||||
for (file, rank, kind, color) in INITIAL_PIECES {
|
||||
let rank_entry = this.inner.entry(file).or_default();
|
||||
|
||||
rank_entry.inner.insert(rank, Piece { kind, color });
|
||||
this.inner
|
||||
.insert(Coordinates { file, rank }, Piece { kind, color });
|
||||
}
|
||||
|
||||
this
|
||||
|
@ -224,15 +304,13 @@ impl BoardState {
|
|||
fn to_serializable(&self) -> Vec<(File, Rank, PieceKind, Color)> {
|
||||
self.inner
|
||||
.iter()
|
||||
.flat_map(|(file, ranks)| {
|
||||
ranks.inner.iter().map(|(rank, piece)| {
|
||||
(
|
||||
file.clone(),
|
||||
rank.clone(),
|
||||
piece.kind.clone(),
|
||||
piece.color.clone(),
|
||||
)
|
||||
})
|
||||
.map(|(coordinates, piece)| {
|
||||
(
|
||||
coordinates.file.clone(),
|
||||
coordinates.rank.clone(),
|
||||
piece.kind.clone(),
|
||||
piece.color.clone(),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
@ -350,6 +428,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
App::new()
|
||||
.app_data(Data::new(GameState::default()))
|
||||
.wrap(TracingLogger::default())
|
||||
.wrap(
|
||||
Cors::default()
|
||||
.allow_any_origin()
|
||||
.allow_any_header()
|
||||
.allow_any_method(),
|
||||
)
|
||||
.route("/start", actix_web::web::post().to(start))
|
||||
.route("/move", actix_web::web::post().to(make_move))
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue