Chess server uwu

This commit is contained in:
Aode (lion) 2021-12-30 23:21:43 -06:00
parent 1cd980989a
commit 0175f2d933
3 changed files with 124 additions and 22 deletions

17
Cargo.lock generated
View file

@ -19,6 +19,22 @@ dependencies = [
"tokio-util", "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]] [[package]]
name = "actix-http" name = "actix-http"
version = "3.0.0-beta.17" version = "3.0.0-beta.17"
@ -258,6 +274,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "chess" name = "chess"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix-cors",
"actix-web", "actix-web",
"serde", "serde",
"serde_repr", "serde_repr",

View file

@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
actix-cors = "0.6.0-beta.8"
actix-web = { version = "4.0.0-beta.18", default-features = false } actix-web = { version = "4.0.0-beta.18", default-features = false }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_repr = "0.1" serde_repr = "0.1"

View file

@ -1,3 +1,4 @@
use actix_cors::Cors;
use actix_web::web::{Data, Json}; use actix_web::web::{Data, Json};
use actix_web::{App, HttpResponse, HttpServer}; use actix_web::{App, HttpResponse, HttpServer};
use std::collections::HashMap; use std::collections::HashMap;
@ -66,7 +67,9 @@ struct Piece {
color: Color, color: Color,
} }
#[derive(serde::Deserialize, serde::Serialize)] #[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize,
)]
struct Coordinates { struct Coordinates {
file: File, file: File,
rank: Rank, rank: Rank,
@ -87,14 +90,18 @@ struct Start {
player_color: Color, 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")] #[serde(rename_all = "camelCase")]
enum Color { enum Color {
Black, Black,
White, 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")] #[serde(rename_all = "camelCase")]
enum PieceKind { enum PieceKind {
Pawn, Pawn,
@ -107,6 +114,7 @@ enum PieceKind {
#[derive( #[derive(
Clone, Clone,
Debug,
PartialEq, PartialEq,
Eq, Eq,
PartialOrd, PartialOrd,
@ -128,6 +136,44 @@ enum Rank {
} }
impl 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 { fn to_number(&self) -> u8 {
match self { match self {
Self::One => 0, 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")] #[serde(rename_all = "camelCase")]
enum File { enum File {
A, A,
@ -170,6 +218,44 @@ enum File {
} }
impl 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 { fn to_number(&self) -> u8 {
match self { match self {
Self::A => 0, Self::A => 0,
@ -198,14 +284,9 @@ impl File {
} }
} }
#[derive(Clone, Default)]
struct FileState {
inner: HashMap<Rank, Piece>,
}
#[derive(Clone, Default)] #[derive(Clone, Default)]
struct BoardState { struct BoardState {
inner: HashMap<File, FileState>, inner: HashMap<Coordinates, Piece>,
} }
impl BoardState { impl BoardState {
@ -213,9 +294,8 @@ impl BoardState {
let mut this = Self::default(); let mut this = Self::default();
for (file, rank, kind, color) in INITIAL_PIECES { for (file, rank, kind, color) in INITIAL_PIECES {
let rank_entry = this.inner.entry(file).or_default(); this.inner
.insert(Coordinates { file, rank }, Piece { kind, color });
rank_entry.inner.insert(rank, Piece { kind, color });
} }
this this
@ -224,15 +304,13 @@ impl BoardState {
fn to_serializable(&self) -> Vec<(File, Rank, PieceKind, Color)> { fn to_serializable(&self) -> Vec<(File, Rank, PieceKind, Color)> {
self.inner self.inner
.iter() .iter()
.flat_map(|(file, ranks)| { .map(|(coordinates, piece)| {
ranks.inner.iter().map(|(rank, piece)| { (
( coordinates.file.clone(),
file.clone(), coordinates.rank.clone(),
rank.clone(), piece.kind.clone(),
piece.kind.clone(), piece.color.clone(),
piece.color.clone(), )
)
})
}) })
.collect() .collect()
} }
@ -350,6 +428,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
App::new() App::new()
.app_data(Data::new(GameState::default())) .app_data(Data::new(GameState::default()))
.wrap(TracingLogger::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("/start", actix_web::web::post().to(start))
.route("/move", actix_web::web::post().to(make_move)) .route("/move", actix_web::web::post().to(make_move))
}) })