2 releases
Uses old Rust 2015
0.1.1 | Nov 1, 2018 |
---|---|
0.1.0 | Nov 1, 2018 |
#10 in #futures-based
16KB
163 lines
chess_clock
A futures-based chess clock which provides timeout-like functionality for other futures.
Please see the documentation (to be linked) for more information, including example usage.
lib.rs
:
A futures-based chess clock which provides timeout-like functionality for other futures.
In addition to the typical configurable base time and time-per-turn, ChessClock
supports [1,
usize::max] players, which can be useful in time-controlled, turn-based games that support more
than two players, such as Hanabi.
Per FIDE rules, time-per-turn is added on the very first move.
Example usage:
extern crate futures;
extern crate tokio;
extern crate chess_clock;
use chess_clock::{ChessClock, BaseTime, TimePerTurn};
use std::thread::sleep;
use std::time::{Duration, Instant};
use futures::{prelude::*, future::{self, ok, err, Either, FutureResult}};
use tokio;
use tokio::timer::Delay;
let mut clock = ChessClock::new(
2,
BaseTime(Duration::new(2, 0)),
TimePerTurn(Duration::new(2, 0)),
);
let when = Instant::now() + Duration::from_secs(2);
let task = Delay::new(when)
.map_err(|_| ());
let clocked_task = clock.bind(task)
.then(|res| {
match res {
Ok(Some(_)) => {
println!("Task succeeded");
ok(())
},
Ok(None) => {
println!("Task timed out");
ok(())
},
_ => {
err(())
}
}
}
);
tokio::run(clocked_task);
Output: Task succeeded
, since the example task has duration less than the first player's base
time + time-per-turn
Implementation details
ChessClock
is ARCed and (rw-mutexed)std::sync::RwLock so that it works
nicely with the borrow checker, but by nature of its design, which is sequential passing of
turns, it shouldn't be used simultaneously by two threads.
It tracks the time of each player, a single global time-per-turn, the active player index, and the time the last turn was passed, so it can calculate the time to subtract from the next active player's clock.
Dependencies
~3.5MB
~47K SLoC