Set up SGF reading and start on the game database #47

Merged
savanni merged 20 commits from kifu/sgf into main 2023-07-26 13:54:28 +00:00
4 changed files with 47 additions and 16 deletions
Showing only changes of commit 4b30bf288a - Show all commits

View File

@ -133,7 +133,7 @@ pub struct GameInfo {
pub event: Option<String>, pub event: Option<String>,
// Games can be played across multiple days, even multiple years. The format specifies // Games can be played across multiple days, even multiple years. The format specifies
// shortcuts. // shortcuts.
pub date_time: Vec<chrono::NaiveDate>, pub date: Vec<chrono::NaiveDate>,
pub location: Option<String>, pub location: Option<String>,
// special rules for the round-number and type // special rules for the round-number and type
pub round: Option<String>, pub round: Option<String>,
@ -141,7 +141,7 @@ pub struct GameInfo {
pub source: Option<String>, pub source: Option<String>,
pub time_limits: Option<std::time::Duration>, pub time_limits: Option<std::time::Duration>,
pub game_keeper: Option<String>, pub game_keeper: Option<String>,
pub komi: Option<String>, pub komi: Option<f32>,
pub game_name: Option<String>, pub game_name: Option<String>,
pub game_comments: Option<String>, pub game_comments: Option<String>,
@ -177,15 +177,15 @@ impl TryFrom<&str> for GameResult {
Ok(GameResult::Annulled) Ok(GameResult::Annulled)
} else { } else {
let parts = s.split("+").collect::<Vec<&str>>(); let parts = s.split("+").collect::<Vec<&str>>();
let res = match parts[0] { let res = match parts[0].to_ascii_lowercase().as_str() {
"B" => GameResult::Black, "b" => GameResult::Black,
"W" => GameResult::White, "w" => GameResult::White,
_ => panic!("unknown result format"), _ => panic!("unknown result format"),
}; };
match parts[1] { match parts[1].to_ascii_lowercase().as_str() {
"R" | "Resign" => Ok(res(Win::Resignation)), "r" | "resign" => Ok(res(Win::Resignation)),
"T" | "Time" => Ok(res(Win::Time)), "t" | "time" => Ok(res(Win::Time)),
"F" | "Forfeit" => Ok(res(Win::Forfeit)), "f" | "forfeit" => Ok(res(Win::Forfeit)),
_ => { _ => {
let score = parts[1].parse::<f32>().unwrap(); let score = parts[1].parse::<f32>().unwrap();
Ok(res(Win::Score(score))) Ok(res(Win::Score(score)))
@ -360,7 +360,7 @@ mod tests {
Some(std::time::Duration::from_secs(28800)) Some(std::time::Duration::from_secs(28800))
); );
assert_eq!( assert_eq!(
tree.info.date_time, tree.info.date,
vec![ vec![
chrono::NaiveDate::from_ymd_opt(1996, 10, 18).unwrap(), chrono::NaiveDate::from_ymd_opt(1996, 10, 18).unwrap(),
chrono::NaiveDate::from_ymd_opt(1996, 10, 19).unwrap(), chrono::NaiveDate::from_ymd_opt(1996, 10, 19).unwrap(),

17
kifu/core/Cargo.lock generated
View File

@ -60,6 +60,15 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "cool_asserts"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee9f254e53f61e2688d3677fa2cbe4e9b950afd56f48819c98817417cf6b28ec"
dependencies = [
"indent_write",
]
[[package]] [[package]]
name = "core-foundation-sys" name = "core-foundation-sys"
version = "0.8.4" version = "0.8.4"
@ -152,6 +161,12 @@ dependencies = [
"cxx-build", "cxx-build",
] ]
[[package]]
name = "indent_write"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0cfe9645a18782869361d9c8732246be7b410ad4e919d3609ebabdac00ba12c3"
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.6" version = "1.0.6"
@ -171,6 +186,8 @@ dependencies = [
name = "kifu-core" name = "kifu-core"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono",
"cool_asserts",
"go-sgf", "go-sgf",
"grid", "grid",
"serde", "serde",

View File

@ -6,9 +6,13 @@ 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]
go-sgf = { path = "../../go-sgf" } chrono = { version = "0.4" }
grid = { version = "0.9" } go-sgf = { path = "../../go-sgf" }
serde_json = { version = "1" } grid = { version = "0.9" }
serde = { version = "1", features = [ "derive" ] } serde_json = { version = "1" }
thiserror = { version = "1" } serde = { version = "1", features = [ "derive" ] }
typeshare = { version = "1" } thiserror = { version = "1" }
typeshare = { version = "1" }
[dev-dependencies]
cool_asserts = { version = "2" }

View File

@ -57,6 +57,7 @@ impl Database {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use cool_asserts::assert_matches;
#[test] #[test]
fn it_reads_empty_database() { fn it_reads_empty_database() {
@ -73,5 +74,14 @@ mod test {
for game in db.all_games() { for game in db.all_games() {
assert_eq!(game.game_type, GameType::Go); assert_eq!(game.game_type, GameType::Go);
} }
assert_matches!(db.all_games().find(|g| g.info.black_player == Some("Steve".to_owned())),
Some(game) => {
assert_eq!(game.info.black_player, Some("Steve".to_owned()));
assert_eq!(game.info.white_player, Some("Savanni".to_owned()));
assert_eq!(game.info.date, vec![chrono::NaiveDate::from_ymd_opt(2023, 4, 19).unwrap()]);
assert_eq!(game.info.komi, Some(6.5));
}
);
} }
} }