monorepo/sgf/src/go.rs

419 lines
11 KiB
Rust
Raw Normal View History

2023-07-21 03:22:00 +00:00
// https://red-bean.com/sgf/
// https://red-bean.com/sgf/user_guide/index.html
// https://red-bean.com/sgf/sgf4.html
// todo: support collections in a file
// Properties to support. Remove each one as it gets support.
// B
// KO
// MN
// W
// AB
// AE
// AW
// PL
// C
// DM
// GB
// GW
// HO
// N
// UC
// V
// BM
// DO
// IT
// TE
// AR
// CR
// DD
// LB
// LN
// MA
// SL
// SQ
// TR
// AP
// CA
// FF
// GM
// ST
// SZ
// AN
// BR
// BT
// CP
// DT
// EV
// GN
// GC
// ON
// OT
// PB
// PC
// PW
// RE
// RO
// RU
// SO
// TM
// US
// WR
// WT
// BL
// OB
// OW
// WL
// FG
// PM
// VW
2023-07-22 03:48:05 +00:00
use crate::{
date::{self, parse_date_field, Date},
2023-07-22 03:48:05 +00:00
tree::{parse_collection, ParseSizeError, Size},
2023-07-26 23:33:50 +00:00
Error,
2023-07-22 03:48:05 +00:00
};
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
2023-07-26 23:33:50 +00:00
pub struct Game {}
impl<'a> From<nom::Err<nom::error::VerboseError<&'a str>>> for Error<'a> {
fn from(err: nom::Err<nom::error::VerboseError<&'a str>>) -> Self {
match err {
nom::Err::Incomplete(_) => Error::Incomplete,
nom::Err::Error(e) => Error::InvalidSgf(e.clone()),
nom::Err::Failure(e) => Error::InvalidSgf(e.clone()),
}
}
}
impl<'a> From<ParseSizeError> for Error<'a> {
fn from(_: ParseSizeError) -> Self {
Self::InvalidBoardSize
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[typeshare]
2023-07-21 03:22:00 +00:00
pub enum Rank {
Kyu(u8),
Dan(u8),
Pro(u8),
}
impl TryFrom<&str> for Rank {
type Error = String;
fn try_from(r: &str) -> Result<Rank, Self::Error> {
let parts = r.split(" ").map(|s| s.to_owned()).collect::<Vec<String>>();
let cnt = parts[0].parse::<u8>().map_err(|err| format!("{:?}", err))?;
match parts[1].to_ascii_lowercase().as_str() {
"kyu" => Ok(Rank::Kyu(cnt)),
"dan" => Ok(Rank::Dan(cnt)),
"pro" => Ok(Rank::Pro(cnt)),
_ => Err("unparsable".to_owned()),
}
}
}
impl ToString for Rank {
fn to_string(&self) -> String {
unimplemented!()
}
}
2023-07-21 03:22:00 +00:00
#[derive(Clone, Debug)]
pub struct GameTree {
pub file_format: i8,
2023-07-21 03:22:00 +00:00
pub app_name: Option<String>,
pub game_type: GameType,
pub board_size: Size,
2023-07-21 03:22:00 +00:00
pub info: GameInfo,
// pub text: String,
}
2023-07-21 03:22:00 +00:00
#[derive(Clone, Debug, Default)]
pub struct GameInfo {
pub annotator: Option<String>,
pub copyright: Option<String>,
pub event: Option<String>,
// Games can be played across multiple days, even multiple years. The format specifies
// shortcuts.
2023-07-22 03:48:05 +00:00
pub date: Vec<Date>,
pub location: Option<String>,
// special rules for the round-number and type
pub round: Option<String>,
pub ruleset: Option<String>,
pub source: Option<String>,
pub time_limits: Option<std::time::Duration>,
pub game_keeper: Option<String>,
pub komi: Option<f32>,
pub game_name: Option<String>,
pub game_comments: Option<String>,
pub black_player: Option<String>,
2023-07-21 03:22:00 +00:00
pub black_rank: Option<Rank>,
pub black_team: Option<String>,
pub white_player: Option<String>,
2023-07-21 03:22:00 +00:00
pub white_rank: Option<Rank>,
pub white_team: Option<String>,
pub opening: Option<String>,
pub overtime: Option<String>,
pub result: Option<GameResult>,
}
2023-07-21 03:22:00 +00:00
#[derive(Clone, Debug, PartialEq)]
pub enum GameResult {
Annulled,
Draw,
Black(Win),
White(Win),
}
2023-07-21 03:22:00 +00:00
impl TryFrom<&str> for GameResult {
type Error = String;
fn try_from(s: &str) -> Result<GameResult, Self::Error> {
if s == "0" {
Ok(GameResult::Draw)
} else if s == "Void" {
Ok(GameResult::Annulled)
} else {
let parts = s.split("+").collect::<Vec<&str>>();
let res = match parts[0].to_ascii_lowercase().as_str() {
"b" => GameResult::Black,
"w" => GameResult::White,
2023-07-21 03:22:00 +00:00
_ => panic!("unknown result format"),
};
match parts[1].to_ascii_lowercase().as_str() {
"r" | "resign" => Ok(res(Win::Resignation)),
"t" | "time" => Ok(res(Win::Time)),
"f" | "forfeit" => Ok(res(Win::Forfeit)),
2023-07-21 03:22:00 +00:00
_ => {
let score = parts[1].parse::<f32>().unwrap();
Ok(res(Win::Score(score)))
}
}
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Win {
2023-07-21 03:22:00 +00:00
Score(f32),
Resignation,
Forfeit,
Time,
}
2023-07-21 03:22:00 +00:00
#[derive(Clone, Debug, PartialEq)]
pub enum GameType {
Go,
Unsupported,
}
2023-07-22 03:48:05 +00:00
/*
enum PropType {
Move,
Setup,
Root,
GameInfo,
}
enum PropValue {
Empty,
Number,
Real,
Double,
Color,
SimpleText,
Text,
Point,
Move,
Stone,
}
2023-07-22 03:48:05 +00:00
*/
pub fn parse_sgf<'a>(input: &'a str) -> Result<Vec<GameTree>, Error<'a>> {
2023-07-22 03:48:05 +00:00
let (_, trees) = parse_collection::<nom::error::VerboseError<&'a str>>(input)?;
let games = trees
.into_iter()
.map(|tree| {
let file_format = match tree.sequence[0].find_prop("FF") {
Some(prop) => prop.values[0].parse::<i8>().unwrap(),
None => 4,
};
2023-07-21 03:22:00 +00:00
let app_name = tree.sequence[0]
.find_prop("AP")
.map(|prop| prop.values[0].clone());
let board_size = match tree.sequence[0].find_prop("SZ") {
Some(prop) => Size::try_from(prop.values[0].as_str())?,
None => Size {
width: 19,
height: 19,
},
};
2023-07-21 03:22:00 +00:00
let mut info = GameInfo::default();
info.black_player = tree.sequence[0]
.find_prop("PB")
.map(|prop| prop.values.join(", "));
info.black_rank = tree.sequence[0]
.find_prop("BR")
.and_then(|prop| Rank::try_from(prop.values[0].as_str()).ok());
info.white_player = tree.sequence[0]
.find_prop("PW")
.map(|prop| prop.values.join(", "));
info.white_rank = tree.sequence[0]
.find_prop("WR")
.and_then(|prop| Rank::try_from(prop.values[0].as_str()).ok());
info.result = tree.sequence[0]
.find_prop("RE")
.and_then(|prop| GameResult::try_from(prop.values[0].as_str()).ok());
info.time_limits = tree.sequence[0]
.find_prop("TM")
.and_then(|prop| prop.values[0].parse::<u64>().ok())
.and_then(|seconds| Some(std::time::Duration::from_secs(seconds)));
info.date = tree.sequence[0]
.find_prop("DT")
.and_then(|prop| {
let v = prop
.values
.iter()
.map(|val| parse_date_field(val))
.fold(Ok(vec![]), |acc, v| match (acc, v) {
(Ok(mut acc), Ok(mut values)) => {
acc.append(&mut values);
Ok(acc)
}
(Ok(_), Err(err)) => Err(err),
(Err(err), _) => Err(err),
})
.ok()?;
Some(v)
})
.unwrap_or(vec![]);
info.event = tree.sequence[0]
.find_prop("EV")
.map(|prop| prop.values.join(", "));
info.round = tree.sequence[0]
.find_prop("RO")
.map(|prop| prop.values.join(", "));
info.source = tree.sequence[0]
.find_prop("SO")
.map(|prop| prop.values.join(", "));
info.game_keeper = tree.sequence[0]
.find_prop("US")
.map(|prop| prop.values.join(", "));
Ok(GameTree {
file_format,
2023-07-21 03:22:00 +00:00
app_name,
game_type: GameType::Go,
board_size,
2023-07-21 03:22:00 +00:00
info,
})
})
.collect::<Result<Vec<GameTree>, Error>>()?;
Ok(games)
}
#[cfg(test)]
mod tests {
use super::*;
2023-07-22 03:48:05 +00:00
use crate::{date::Date, tree::Size};
use std::fs::File;
use std::io::Read;
const EXAMPLE: &'static str = "(;FF[4]C[root](;C[a];C[b](;C[c])
(;C[d];C[e]))
(;C[f](;C[g];C[h];C[i])
(;C[j])))";
fn with_text(text: &str, f: impl FnOnce(Vec<GameTree>)) {
let games = parse_sgf(text).unwrap();
f(games);
}
fn with_file(path: &std::path::Path, f: impl FnOnce(Vec<GameTree>)) {
let mut file = File::open(path).unwrap();
let mut text = String::new();
let _ = file.read_to_string(&mut text);
with_text(&text, f);
}
#[test]
fn it_parses_game_root() {
with_text(EXAMPLE, |trees| {
assert_eq!(trees.len(), 1);
let tree = &trees[0];
assert_eq!(tree.file_format, 4);
2023-07-21 03:22:00 +00:00
assert_eq!(tree.app_name, None);
assert_eq!(tree.game_type, GameType::Go);
assert_eq!(
tree.board_size,
Size {
width: 19,
height: 19
}
);
// assert_eq!(tree.text, EXAMPLE.to_owned());
});
with_file(std::path::Path::new("test_data/print1.sgf"), |trees| {
assert_eq!(trees.len(), 1);
let tree = &trees[0];
assert_eq!(tree.file_format, 4);
2023-07-21 03:22:00 +00:00
assert_eq!(tree.app_name, None);
assert_eq!(tree.game_type, GameType::Go);
assert_eq!(
tree.board_size,
Size {
width: 19,
height: 19
}
);
});
}
2023-07-21 03:22:00 +00:00
#[test]
fn it_parses_game_info() {
with_file(std::path::Path::new("test_data/print1.sgf"), |trees| {
assert_eq!(trees.len(), 1);
let tree = &trees[0];
assert_eq!(tree.info.black_player, Some("Takemiya Masaki".to_owned()));
assert_eq!(tree.info.black_rank, Some(Rank::Dan(9)));
assert_eq!(tree.info.white_player, Some("Cho Chikun".to_owned()));
assert_eq!(tree.info.white_rank, Some(Rank::Dan(9)));
assert_eq!(tree.info.result, Some(GameResult::White(Win::Resignation)));
assert_eq!(
tree.info.time_limits,
Some(std::time::Duration::from_secs(28800))
);
assert_eq!(
tree.info.date,
2023-07-21 03:22:00 +00:00
vec![
2023-07-22 03:48:05 +00:00
Date::Date(chrono::NaiveDate::from_ymd_opt(1996, 10, 18).unwrap()),
Date::Date(chrono::NaiveDate::from_ymd_opt(1996, 10, 19).unwrap()),
2023-07-21 03:22:00 +00:00
]
);
assert_eq!(tree.info.event, Some("21st Meijin".to_owned()));
assert_eq!(tree.info.round, Some("2 (final)".to_owned()));
2023-07-21 03:22:00 +00:00
assert_eq!(tree.info.source, Some("Go World #78".to_owned()));
assert_eq!(tree.info.game_keeper, Some("Arno Hollosi".to_owned()));
});
}
}