Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for the Analysis category #31

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ path = "src/lib.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = "1.40.0"
tokio-stream = { version = "0.1.16", default-features = false, features = ["io-util"] }
tokio-util = "0.7.12"
reqwest = { version = "0.12.7", default-features = false, features = ["stream", "rustls-tls"] }
time = "0.3.36"
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
serde_with = { version = "3.9.0", features = ["time_0_3"] }
futures-util = { version = "0.3.30", default-features = false }
tokio = "1.42.0"
tokio-stream = { version = "0.1.17", default-features = false, features = ["io-util"] }
tokio-util = "0.7.13"
reqwest = { version = "0.12.9", default-features = false, features = ["stream", "rustls-tls"] }
time = "0.3.37"
serde = { version = "1.0.216", features = ["derive"] }
serde_json = "1.0.133"
serde_with = { version = "3.11.0", features = ["time_0_3"] }
futures-util = { version = "0.3.31", default-features = false }
comma_serde_urlencoded = "0.8.1"

[dev-dependencies]
tokio = { version = "1.40.0", features = ["macros"] }
tokio = { version = "1.42.0", features = ["macros"] }

[features]
default = ["bot"]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ Below is a list of supported API endpoints as of the last release:
| Board | ✅ |
| Challenges | ✅ |
| Bulk pairings | ❌ |
| Arena tournaments | ❌ |
| Arena tournaments | ❌ |
| Swiss tournaments | ❌ |
| Simuls | |
| Simuls | |
| Studies | ❌ |
| Messaging | ✅ |
| Broadcasts | ❌ |
| Analysis | |
| Analysis | |
| External engine | ❌ |
| Opening explorer | ✅ |
| Tablebase | ✅ |
Expand Down
33 changes: 33 additions & 0 deletions src/api/analysis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::{
client::Licheszter,
error::Result,
models::{analysis::CloudAnalysis, game::VariantMode},
};

impl Licheszter {
pub async fn analysis_cloud(
&self,
fen: &str,
multi_pv: Option<u8>,
variant: Option<VariantMode>,
) -> Result<CloudAnalysis> {
let mut url = self.base_url();
url.set_path("api/cloud-eval");
let mut builder = self
.client()
.get(url)
.query(&[("fen", fen.replace(" ", "_"))]);

// Add the multiPv amount as a query parameter if it's present
if let Some(multi_pv) = multi_pv {
builder = builder.query(&[("multiPv", multi_pv)]);
}

// Add the variant as a query parameter if it's present
if let Some(variant) = variant {
builder = builder.query(&[("variant", variant)]);
}

self.into::<CloudAnalysis>(builder).await
}
}
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod account;
pub mod analysis;
pub mod challenges;
pub mod messaging;
pub mod misc;
Expand Down
3 changes: 3 additions & 0 deletions src/api/simuls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{client::Licheszter, error::Result, models::simul::Simuls};

impl Licheszter {
// Get recently created, started and finished simuls.
// Created and finished simuls are only visible if the host is strong enough.
// When authenticated, the pending simuls will contain your created, but unstarted simuls.
pub async fn simuls_current(&self) -> Result<Simuls> {
let mut url = self.base_url();
url.set_path("api/simul");
Expand Down
17 changes: 17 additions & 0 deletions src/models/analysis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "serde-strict", serde(deny_unknown_fields))]
pub struct CloudAnalysis {
pub fen: String,
pub knodes: u32,
pub depth: u8,
pub pvs: Vec<PositionVariation>,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "serde-strict", serde(deny_unknown_fields))]
pub struct PositionVariation {
pub moves: String,
pub cp: i8,
}
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod analysis;
pub mod board;
pub mod challenge;
pub mod chat;
Expand Down
55 changes: 55 additions & 0 deletions tests/analysis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::{error::Error, sync::LazyLock};

use licheszter::{client::Licheszter, models::game::VariantMode};

// Connect to test accounts
static LI: LazyLock<Licheszter> = LazyLock::new(|| {
Licheszter::builder()
.with_base_url("http://localhost:8080")
.unwrap()
.with_authentication("lip_li")
.build()
});

#[tokio::test]
async fn analysis_cloud() {
// Run some test cases
let result = LI
.analysis_cloud(
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
None,
None,
)
.await;
assert!(
result.is_ok(),
"Failed to get cloud analysis: {:?}",
result.unwrap_err().source().unwrap()
);

let result = LI
.analysis_cloud(
"rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1",
Some(5),
Some(VariantMode::Standard),
)
.await;
assert!(
result.is_ok(),
"Failed to get cloud analysis: {:?}",
result.unwrap_err().source().unwrap()
);

let result = LI
.analysis_cloud(
"rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3",
Some(3),
Some(VariantMode::Atomic),
)
.await;
assert!(
result.is_err(),
"Getting cloud analysis did not fail: {:?}",
result.unwrap()
);
}