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
13 changed files with 149 additions and 13 deletions
Showing only changes of commit c8d21d0e25 - Show all commits

85
go-sgf/Cargo.lock generated
View File

@ -51,6 +51,7 @@ dependencies = [
"iana-time-zone", "iana-time-zone",
"js-sys", "js-sys",
"num-traits", "num-traits",
"serde",
"time", "time",
"wasm-bindgen", "wasm-bindgen",
"winapi", "winapi",
@ -68,7 +69,9 @@ version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"nom", "nom",
"serde",
"thiserror", "thiserror",
"typeshare",
] ]
[[package]] [[package]]
@ -94,6 +97,12 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "itoa"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.64" version = "0.3.64"
@ -170,6 +179,54 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "ryu"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "serde"
version = "1.0.164"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.164"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.18",
]
[[package]]
name = "serde_json"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46266871c240a00b8f503b877622fe33430b3c7d963bdc0f2adc511e54a1eae3"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.18" version = "2.0.18"
@ -198,7 +255,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.18",
] ]
[[package]] [[package]]
@ -212,6 +269,28 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "typeshare"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f44d1a2f454cb35fbe05b218c410792697e76bd868f48d3a418f2cd1a7d527d6"
dependencies = [
"chrono",
"serde",
"serde_json",
"typeshare-annotation",
]
[[package]]
name = "typeshare-annotation"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc670d0e358428857cc3b4bf504c691e572fccaec9542ff09212d3f13d74b7a9"
dependencies = [
"quote",
"syn 1.0.109",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.9" version = "1.0.9"
@ -245,7 +324,7 @@ dependencies = [
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.18",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -267,7 +346,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.18",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]

View File

@ -6,6 +6,8 @@ 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]
chrono = { version = "0.4", features = [ "serde" ] }
nom = { version = "7" } nom = { version = "7" }
serde = { version = "1", features = [ "derive" ] }
thiserror = { version = "1"} thiserror = { version = "1"}
chrono = { version = "0.4" } typeshare = { version = "1" }

View File

@ -1,6 +1,8 @@
use chrono::{Datelike, NaiveDate}; use chrono::{Datelike, NaiveDate};
use serde::{Deserialize, Serialize};
use std::num::ParseIntError; use std::num::ParseIntError;
use thiserror::Error; use thiserror::Error;
use typeshare::typeshare;
#[derive(Debug, Error, PartialEq)] #[derive(Debug, Error, PartialEq)]
pub enum Error { pub enum Error {
@ -14,7 +16,8 @@ pub enum Error {
Unsupported, Unsupported,
} }
#[derive(Clone, Debug, PartialEq, PartialOrd)] #[derive(Clone, Debug, PartialEq, PartialOrd, Deserialize, Serialize)]
#[typeshare]
pub enum Date { pub enum Date {
Year(i32), Year(i32),
YearMonth(i32, u32), YearMonth(i32, u32),

View File

@ -72,6 +72,8 @@ use crate::{
date::{self, parse_date_field, Date}, date::{self, parse_date_field, Date},
tree::{parse_collection, ParseSizeError, Size}, tree::{parse_collection, ParseSizeError, Size},
}; };
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
#[derive(Debug)] #[derive(Debug)]
pub enum Error<'a> { pub enum Error<'a> {
@ -97,7 +99,8 @@ impl<'a> From<ParseSizeError> for Error<'a> {
} }
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[typeshare]
pub enum Rank { pub enum Rank {
Kyu(u8), Kyu(u8),
Dan(u8), Dan(u8),

View File

@ -1,6 +1,10 @@
pub mod date; mod date;
pub mod go; pub use date::Date;
pub mod tree;
mod go;
pub use go::{parse_sgf, GameTree, GameType, Rank};
mod tree;
use thiserror::Error; use thiserror::Error;

3
kifu/core/Cargo.lock generated
View File

@ -45,6 +45,7 @@ dependencies = [
"js-sys", "js-sys",
"num-integer", "num-integer",
"num-traits", "num-traits",
"serde",
"time", "time",
"wasm-bindgen", "wasm-bindgen",
"winapi", "winapi",
@ -125,7 +126,9 @@ version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"nom", "nom",
"serde",
"thiserror", "thiserror",
"typeshare",
] ]
[[package]] [[package]]

View File

@ -74,6 +74,8 @@ impl CoreApp {
let config = Config::from_path(config_path).expect("configuration to open"); let config = Config::from_path(config_path).expect("configuration to open");
let state = Arc::new(RwLock::new(AppState::new())); let state = Arc::new(RwLock::new(AppState::new()));
println!("config: {:?}", config);
Self { config, state } Self { config, state }
} }

View File

@ -1,6 +1,6 @@
use std::{ffi::OsStr, io::Read, os::unix::ffi::OsStrExt, path::PathBuf}; use std::{ffi::OsStr, io::Read, os::unix::ffi::OsStrExt, path::PathBuf};
use go_sgf::go::{parse_sgf, GameTree, GameType}; use go_sgf::{parse_sgf, GameTree};
use thiserror::Error; use thiserror::Error;
#[derive(Error, Debug)] #[derive(Error, Debug)]
@ -58,6 +58,7 @@ impl Database {
mod test { mod test {
use super::*; use super::*;
use cool_asserts::assert_matches; use cool_asserts::assert_matches;
use go_sgf::{Date, GameType};
#[test] #[test]
fn it_reads_empty_database() { fn it_reads_empty_database() {
@ -79,7 +80,7 @@ mod test {
Some(game) => { Some(game) => {
assert_eq!(game.info.black_player, Some("Steve".to_owned())); assert_eq!(game.info.black_player, Some("Steve".to_owned()));
assert_eq!(game.info.white_player, Some("Savanni".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.date, vec![Date::Date(chrono::NaiveDate::from_ymd_opt(2023, 4, 19).unwrap())]);
assert_eq!(game.info.komi, Some(6.5)); assert_eq!(game.info.komi, Some(6.5));
} }
); );

View File

@ -0,0 +1,33 @@
use go_sgf::{Date, GameTree, Rank};
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[typeshare]
pub struct GamePreviewElement {
pub date: Vec<Date>,
pub black_player: String,
pub black_rank: Option<Rank>,
pub white_player: String,
pub white_rank: Option<Rank>,
}
impl GamePreviewElement {
pub fn new(game: GameTree) -> GamePreviewElement {
GamePreviewElement {
date: game.info.date.clone(),
black_player: game
.info
.black_player
.clone()
.unwrap_or("black_player".to_owned()),
black_rank: game.info.black_rank.clone(),
white_player: game
.info
.white_player
.clone()
.unwrap_or("white_player".to_owned()),
white_rank: game.info.white_rank.clone(),
}
}
}

View File

@ -1,2 +1,3 @@
pub mod action; pub mod action;
pub mod game_preview;
pub mod menu; pub mod menu;

View File

@ -1,4 +1,4 @@
use crate::ui::Action; use crate::ui::{Action, GamePreviewElement};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use typeshare::typeshare; use typeshare::typeshare;
@ -51,6 +51,7 @@ pub struct BotPlayerElement {}
pub struct HomeView { pub struct HomeView {
pub black_player: PlayerElement, pub black_player: PlayerElement,
pub white_player: PlayerElement, pub white_player: PlayerElement,
pub games: Vec<GamePreviewElement>,
pub start_game: Action<()>, pub start_game: Action<()>,
} }
@ -68,6 +69,7 @@ pub fn home() -> HomeView {
HomeView { HomeView {
black_player, black_player,
white_player, white_player,
games: vec![],
start_game: Action { start_game: Action {
id: "start-game-action".to_owned(), id: "start-game-action".to_owned(),
label: "New Game".to_owned(), label: "New Game".to_owned(),

View File

@ -1,5 +1,5 @@
mod elements; mod elements;
pub use elements::{action::Action, menu::Menu}; pub use elements::{action::Action, game_preview::GamePreviewElement, menu::Menu};
mod playing_field; mod playing_field;
pub use playing_field::{playing_field, PlayingFieldView}; pub use playing_field::{playing_field, PlayingFieldView};

3
kifu/gtk/Cargo.lock generated
View File

@ -121,6 +121,7 @@ dependencies = [
"js-sys", "js-sys",
"num-integer", "num-integer",
"num-traits", "num-traits",
"serde",
"time", "time",
"wasm-bindgen", "wasm-bindgen",
"winapi", "winapi",
@ -550,7 +551,9 @@ version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"nom", "nom",
"serde",
"thiserror", "thiserror",
"typeshare",
] ]
[[package]] [[package]]