Skip to content

Commit

Permalink
PositionId -> PositionIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Jan 1, 2024
1 parent e592ff4 commit acfe0a0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ impl From<Centis> for Duration {
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Deserialize)]
pub struct PositionId(pub usize);
pub struct PositionIndex(pub usize);

#[serde_as]
#[derive(Debug, Deserialize)]
Expand All @@ -306,7 +306,7 @@ pub struct AcquireResponseBody {
#[serde_as(as = "StringWithSeparator::<SpaceSeparator, Uci>")]
pub moves: Vec<Uci>,
#[serde(rename = "skipPositions", default)]
pub skip_positions: Vec<PositionId>,
pub skip_positions: Vec<PositionIndex>,
}

impl AcquireResponseBody {
Expand Down
6 changes: 3 additions & 3 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tokio::sync::oneshot;
use url::Url;

use crate::{
api::{AnalysisPart, BatchId, PositionId, Score, Work},
api::{AnalysisPart, BatchId, PositionIndex, Score, Work},
assets::EngineFlavor,
};

Expand All @@ -28,7 +28,7 @@ impl Chunk {
#[derive(Debug, Clone)]
pub struct Position {
pub work: Work,
pub position_id: Option<PositionId>,
pub position_index: Option<PositionIndex>,
pub url: Option<Url>,
pub skip: bool,

Expand All @@ -39,7 +39,7 @@ pub struct Position {
#[derive(Debug, Clone)]
pub struct PositionResponse {
pub work: Work,
pub position_id: Option<PositionId>,
pub position_index: Option<PositionIndex>,
pub url: Option<Url>,

pub scores: Matrix<Score>,
Expand Down
14 changes: 7 additions & 7 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use shakmaty::variant::Variant;
use url::Url;

use crate::{
api::{BatchId, PositionId},
api::{BatchId, PositionIndex},
configure::Verbose,
ipc::{Chunk, Position, PositionResponse},
util::NevermindExt as _,
Expand Down Expand Up @@ -108,20 +108,20 @@ impl Logger {
pub struct ProgressAt {
pub batch_id: BatchId,
pub batch_url: Option<Url>,
pub position_id: Option<PositionId>,
pub position_index: Option<PositionIndex>,
}

impl fmt::Display for ProgressAt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref batch_url) = self.batch_url {
let mut url = batch_url.clone();
if let Some(PositionId(positon_id)) = self.position_id {
if let Some(PositionIndex(positon_id)) = self.position_index {
url.set_fragment(Some(&positon_id.to_string()));
}
fmt::Display::fmt(&url, f)
} else {
write!(f, "{}", self.batch_id)?;
if let Some(PositionId(positon_id)) = self.position_id {
if let Some(PositionIndex(positon_id)) = self.position_index {
write!(f, "#{positon_id}")?;
}
Ok(())
Expand All @@ -134,7 +134,7 @@ impl From<&Chunk> for ProgressAt {
ProgressAt {
batch_id: chunk.work.id(),
batch_url: chunk.positions.last().and_then(|pos| pos.url.clone()),
position_id: chunk.positions.last().and_then(|pos| pos.position_id),
position_index: chunk.positions.last().and_then(|pos| pos.position_index),
}
}
}
Expand All @@ -144,7 +144,7 @@ impl From<&Position> for ProgressAt {
ProgressAt {
batch_id: pos.work.id(),
batch_url: pos.url.clone(),
position_id: pos.position_id,
position_index: pos.position_index,
}
}
}
Expand All @@ -154,7 +154,7 @@ impl From<&PositionResponse> for ProgressAt {
ProgressAt {
batch_id: pos.work.id(),
batch_url: pos.url.clone(),
position_id: pos.position_id,
position_index: pos.position_index,
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use url::Url;

use crate::{
api::{
AcquireQuery, AcquireResponseBody, Acquired, AnalysisPart, ApiStub, BatchId, PositionId,
AcquireQuery, AcquireResponseBody, Acquired, AnalysisPart, ApiStub, BatchId, PositionIndex,
Work,
},
assets::{EngineFlavor, EvalFlavor},
Expand Down Expand Up @@ -163,12 +163,12 @@ impl QueueState {
let mut positions = Vec::new();
for chunk in batch.chunks {
for pos in &chunk.positions {
if let Some(position_id) = pos.position_id {
if positions.len() <= position_id.0 {
positions.resize(position_id.0 + 1, Some(Skip::Skip));
if let Some(position_index) = pos.position_index {
if positions.len() <= position_index.0 {
positions.resize(position_index.0 + 1, Some(Skip::Skip));
}
if !pos.skip {
positions[position_id.0] = None;
positions[position_index.0] = None;
}
}
}
Expand Down Expand Up @@ -199,14 +199,14 @@ impl QueueState {
let mut progress_at = None;
let mut batch_ids = Vec::new();
for res in responses {
let Some(position_id) = res.position_id else {
let Some(position_index) = res.position_index else {
continue;
};
let batch_id = res.work.id();
let Some(pending) = self.pending.get_mut(&batch_id) else {
continue;
};
let Some(pos) = pending.positions.get_mut(position_id.0) else {
let Some(pos) = pending.positions.get_mut(position_index.0) else {
continue;
};
progress_at = Some(ProgressAt::from(&res));
Expand Down Expand Up @@ -384,7 +384,7 @@ impl QueueActor {
let context = ProgressAt {
batch_id: body.work.id(),
batch_url: body.batch_url(self.api.endpoint()),
position_id: None,
position_index: None,
};

match IncomingBatch::from_acquired(self.api.endpoint(), body) {
Expand Down Expand Up @@ -575,7 +575,7 @@ impl IncomingBatch {
work: body.work,
url,
skip: false,
position_id: Some(PositionId(0)),
position_index: Some(PositionIndex(0)),
root_fen,
moves: body_moves,
}],
Expand All @@ -592,22 +592,22 @@ impl IncomingBatch {
url.set_fragment(Some("0"));
url
}),
skip: body.skip_positions.contains(&PositionId(0)),
position_id: Some(PositionId(0)),
skip: body.skip_positions.contains(&PositionIndex(0)),
position_index: Some(PositionIndex(0)),
root_fen: root_fen.clone(),
moves: moves.clone(),
});
for (i, m) in body_moves.into_iter().enumerate() {
let position_id = PositionId(i + 1);
let position_index = PositionIndex(i + 1);
moves.push(m);
positions.push(Position {
work: body.work.clone(),
url: url.clone().map(|mut url| {
url.set_fragment(Some(&position_id.0.to_string()));
url.set_fragment(Some(&position_index.0.to_string()));
url
}),
skip: body.skip_positions.contains(&position_id),
position_id: Some(position_id),
skip: body.skip_positions.contains(&position_index),
position_index: Some(position_index),
root_fen: root_fen.clone(),
moves: moves.clone(),
});
Expand All @@ -621,7 +621,7 @@ impl IncomingBatch {
let prev_and_current: Vec<_> = zip(
once(None).chain(positions.clone().into_iter().map(|pos| {
Some(Position {
position_id: None,
position_index: None,
..pos
})
})),
Expand Down Expand Up @@ -680,7 +680,7 @@ impl From<&IncomingBatch> for ProgressAt {
ProgressAt {
batch_id: batch.work.id(),
batch_url: batch.url.clone(),
position_id: None,
position_index: None,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/stockfish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl StockfishActor {

return Ok(PositionResponse {
work: position.work,
position_id: position.position_id,
position_index: position.position_index,
url: position.url,
best_move: parts.next().and_then(|m| m.parse().ok()),
scores,
Expand Down

0 comments on commit acfe0a0

Please sign in to comment.