Skip to content
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
40 changes: 40 additions & 0 deletions conformance/knowledge/selection.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{"session":"knowledge-selection","description":"draft-1 knowledge, puzzle selection: a random pass names its seed and repeats from it, quality names its thresholds, facets answer with counts by band, and a seed without random order is refused (spec/knowledge.md, Order, seed and quality)","requires":["knowledge.puzzleSelection"]}
{"send":{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocol":"1.0","client":{"name":"cnp-conformance","version":"1.0"}}}}
{"expect":{"id":1,"result":{"capabilities":{"knowledge":{"contract":"draft-1","read":true,"puzzleSelection":true}}}}}
{"comment":"A random pass with no seed: the node picks one and says it. Asking again with that seed starts the same pass."}
{"send":{"jsonrpc":"2.0","id":2,"method":"knowledge/puzzles","params":{"theme":"noSuchThemeAnywhere","order":"random","limit":5}}}
{"expect":{"id":2,"result":{"puzzles":[],"sources":[],"seed":"$bind:seed"}}}
{"send":{"jsonrpc":"2.0","id":3,"method":"knowledge/puzzles","params":{"theme":"noSuchThemeAnywhere","order":"random","seed":"$ref:seed","limit":5}}}
{"expect":{"id":3,"result":{"puzzles":[],"sources":[],"seed":"$ref:seed"}}}
{"comment":"A seed the client chose is the seed the answer names, at both ends of its range."}
{"send":{"jsonrpc":"2.0","id":4,"method":"knowledge/puzzles","params":{"order":"random","seed":0,"limit":1}}}
{"expect":{"id":4,"result":{"puzzles":"$array","sources":"$array","seed":0}}}
{"send":{"jsonrpc":"2.0","id":5,"method":"knowledge/puzzles","params":{"order":"random","seed":4294967295,"limit":1}}}
{"expect":{"id":5,"result":{"puzzles":"$array","sources":"$array","seed":4294967295}}}
{"comment":"Quality names the thresholds it used; a plain request names none."}
{"send":{"jsonrpc":"2.0","id":6,"method":"knowledge/puzzles","params":{"theme":"noSuchThemeAnywhere","quality":"reliable"}}}
{"expect":{"id":6,"result":{"puzzles":[],"sources":[],"quality":{"minPlays":"$integer","minPopularity":"$integer","maxRatingDeviation":"$integer"},"seed":"$absent"}}}
{"send":{"jsonrpc":"2.0","id":7,"method":"knowledge/puzzles","params":{"theme":"noSuchThemeAnywhere"}}}
{"expect":{"id":7,"result":{"puzzles":[],"sources":[],"quality":"$absent","seed":"$absent"}}}
{"comment":"Facets: the node's bands, the total in each, and a count in each for every theme listed. The params may be left out. A theme nobody has is not listed."}
{"send":{"jsonrpc":"2.0","id":8,"method":"knowledge/puzzleFacets"}}
{"expect":{"id":8,"result":{"bands":"$array","total":"$array","themes":"$array","sources":"$array","quality":"$absent"}}}
{"send":{"jsonrpc":"2.0","id":9,"method":"knowledge/puzzleFacets","params":{"quality":"reliable"}}}
{"expect":{"id":9,"result":{"bands":"$array","total":"$array","themes":"$array","sources":"$array","quality":{"minPlays":"$integer","minPopularity":"$integer","maxRatingDeviation":"$integer"}}}}
{"comment":"Refused: a seed with the default order or with easiest, an order or a quality that isn't one, a seed out of range, and facets for a quality that isn't one."}
{"send":{"jsonrpc":"2.0","id":10,"method":"knowledge/puzzles","params":{"seed":3}}}
{"expect":{"id":10,"error":{"code":-32602}}}
{"send":{"jsonrpc":"2.0","id":11,"method":"knowledge/puzzles","params":{"order":"easiest","seed":3}}}
{"expect":{"id":11,"error":{"code":-32602}}}
{"send":{"jsonrpc":"2.0","id":12,"method":"knowledge/puzzles","params":{"order":"shuffled"}}}
{"expect":{"id":12,"error":{"code":-32602}}}
{"send":{"jsonrpc":"2.0","id":13,"method":"knowledge/puzzles","params":{"quality":"best"}}}
{"expect":{"id":13,"error":{"code":-32602}}}
{"send":{"jsonrpc":"2.0","id":14,"method":"knowledge/puzzles","params":{"order":"random","seed":4294967296}}}
{"expect":{"id":14,"error":{"code":-32602}}}
{"send":{"jsonrpc":"2.0","id":15,"method":"knowledge/puzzleFacets","params":{"quality":"best"}}}
{"expect":{"id":15,"error":{"code":-32602}}}
{"send":{"jsonrpc":"2.0","id":16,"method":"shutdown"}}
{"expect":{"id":16,"result":null}}
{"send":{"jsonrpc":"2.0","method":"exit"}}
{"expectExit":0}
149 changes: 144 additions & 5 deletions crates/cnp/src/knowledge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
//!
//! The import half: [`StartImport`] gives the node a file and a [`Manifest`] saying where it is
//! from, answers at once, and the import runs on; [`ListImports`] reports where each has got
//! to, and [`CancelImport`] stops one. The read half: [`FindPuzzles`] by theme and rating,
//! [`FindAnalyses`] of a position, and [`FindExercises`] by theme, each from complete imports
//! only and naming its [`Source`]s.
//! to, and [`CancelImport`] stops one. The read half: [`FindPuzzles`] by theme and rating (easiest
//! first, or from a random point, and only the reliable ones if asked), [`FindPuzzleFacets`]
//! for how many there are of each theme and rating band, [`FindAnalyses`] of a position, and
//! [`FindExercises`] by theme, each from complete imports only and naming its [`Source`]s.
//!
//! **The node doesn't download.** The client fetches the file, where it can show the licence,
//! the size and the progress. The node verifies the file against the manifest's checksum before
Expand Down Expand Up @@ -70,6 +71,10 @@ pub struct Capabilities {
/// `knowledge/puzzles`, `knowledge/analyses` and `knowledge/exercises` are available.
#[serde(default)]
pub read: bool,
/// `knowledge/puzzles` takes `order`, `seed` and `quality`, and `knowledge/puzzleFacets`
/// is available. A client sends none of them to a node that doesn't offer it.
#[serde(default)]
pub puzzle_selection: bool,
/// The kinds of file the node can import ([`KIND_LICHESS_PUZZLES`] and the like).
#[serde(default)]
pub kinds: Vec<String>,
Expand Down Expand Up @@ -384,6 +389,41 @@ fn valid_page(
Ok(())
}

/// The order `knowledge/puzzles` answers in.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Order {
/// By rating, lowest first, then by identity: the same puzzles in the same order every
/// time. The default.
#[default]
Easiest,
/// From a point the `seed` picks, in the node's own storage order, wrapping round to the
/// start, so every matching puzzle comes once. Not a shuffle: see the spec.
Random,
}

/// A named filter on how far a puzzle's rating can be trusted.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Quality {
/// Puzzles the source's own counts support: played enough, liked, and with a settled
/// rating. The node chooses the thresholds and says them in [`QualityThresholds`].
Reliable,
}

/// The thresholds a node used for a [`Quality`] filter, so a client can show them and a
/// puzzle's absence from a result can be explained.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QualityThresholds {
/// At least this many plays.
pub min_plays: i64,
/// At least this popularity.
pub min_popularity: i64,
/// At most this rating deviation.
pub max_rating_deviation: i64,
}

/// `knowledge/puzzles`' params: all optional, and the params may be left out.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(from = "Option<PuzzlesFields>", rename_all = "camelCase")]
Expand All @@ -403,6 +443,17 @@ pub struct PuzzlesParams {
/// How many, 1 to [`MAX_LIMIT`]; [`DEFAULT_LIMIT`] if left out.
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
/// The order: [`Order::Easiest`] if left out. Needs [`Capabilities::puzzle_selection`]
/// for anything else.
#[serde(skip_serializing_if = "Option::is_none")]
pub order: Option<Order>,
/// Where a [`Order::Random`] pass starts, so a session can be repeated: the node picks one
/// and answers with it if this is left out. Only with [`Order::Random`].
#[serde(skip_serializing_if = "Option::is_none")]
pub seed: Option<u32>,
/// Only puzzles that pass this filter. Needs [`Capabilities::puzzle_selection`].
#[serde(skip_serializing_if = "Option::is_none")]
pub quality: Option<Quality>,
}

/// [`PuzzlesParams`] as they are read, from an object or from nothing.
Expand All @@ -419,6 +470,12 @@ struct PuzzlesFields {
after: Option<String>,
#[serde(default)]
limit: Option<u32>,
#[serde(default)]
order: Option<Order>,
#[serde(default)]
seed: Option<u32>,
#[serde(default)]
quality: Option<Quality>,
}

impl From<Option<PuzzlesFields>> for PuzzlesParams {
Expand All @@ -430,6 +487,9 @@ impl From<Option<PuzzlesFields>> for PuzzlesParams {
max_rating: f.max_rating,
after: f.after,
limit: f.limit,
order: f.order,
seed: f.seed,
quality: f.quality,
}
}
}
Expand All @@ -438,6 +498,9 @@ impl PuzzlesParams {
/// Checks the params' shapes, or says what is wrong.
pub fn validate(&self) -> Result<(), String> {
valid_page(self.limit, &self.after, &self.theme)?;
if self.seed.is_some() && self.order != Some(Order::Random) {
return Err("a seed is for order random".into());
}
if let (Some(min), Some(max)) = (self.min_rating, self.max_rating)
&& min > max
{
Expand Down Expand Up @@ -483,23 +546,99 @@ pub struct Puzzle {
/// `knowledge/puzzles`' result.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PuzzlesResult {
/// The puzzles, easiest first.
/// The puzzles, in the order asked for.
pub puzzles: Vec<Puzzle>,
/// The sources of these puzzles.
pub sources: Vec<Source>,
/// Where the next page starts, if more may follow.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub next: Option<String>,
/// For [`Order::Random`]: the seed the pass started from, the one asked for or the one the
/// node picked.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub seed: Option<u32>,
/// When a [`Quality`] filter was asked for: the thresholds it used.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub quality: Option<QualityThresholds>,
}

/// Puzzles from complete imports, easiest first, by theme and rating.
/// Puzzles from complete imports, by theme and rating, easiest first or from a random point.
pub enum FindPuzzles {}
impl RequestMethod for FindPuzzles {
const NAME: &'static str = "knowledge/puzzles";
type Params = PuzzlesParams;
type Result = PuzzlesResult;
}

/// `knowledge/puzzleFacets`' params: may be left out.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(from = "Option<FacetsFields>", rename_all = "camelCase")]
pub struct FacetsParams {
/// Count only puzzles that pass this filter.
#[serde(skip_serializing_if = "Option::is_none")]
pub quality: Option<Quality>,
}

/// [`FacetsParams`] as they are read, from an object or from nothing.
#[derive(Default, Deserialize)]
#[serde(rename_all = "camelCase")]
struct FacetsFields {
#[serde(default)]
quality: Option<Quality>,
}

impl From<Option<FacetsFields>> for FacetsParams {
fn from(fields: Option<FacetsFields>) -> FacetsParams {
FacetsParams {
quality: fields.unwrap_or_default().quality,
}
}
}

/// A rating band the node counts puzzles in.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Band {
/// The lowest rating in the band.
pub min_rating: u32,
/// The highest rating in the band; absent for the last, which has none.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_rating: Option<u32>,
}

/// How many puzzles a theme has in each [`Band`].
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ThemeCounts {
/// The theme, as the source writes it.
pub theme: String,
/// One count for each band of [`FacetsResult::bands`], in its order.
pub counts: Vec<u64>,
}

/// `knowledge/puzzleFacets`' result.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct FacetsResult {
/// The rating bands the counts are in, lowest first. A client uses these, never its own.
pub bands: Vec<Band>,
/// All puzzles in each band, whatever their themes.
pub total: Vec<u64>,
/// Each theme that has at least one puzzle, with its counts.
pub themes: Vec<ThemeCounts>,
/// The sources of the puzzles counted.
pub sources: Vec<Source>,
/// When a [`Quality`] filter was asked for: the thresholds it used.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub quality: Option<QualityThresholds>,
}

/// How many puzzles each theme has in each rating band, from complete imports.
pub enum FindPuzzleFacets {}
impl RequestMethod for FindPuzzleFacets {
const NAME: &'static str = "knowledge/puzzleFacets";
type Params = FacetsParams;
type Result = FacetsResult;
}

/// `knowledge/analyses`' params.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AnalysesParams {
Expand Down
Loading
Loading