Compare commits
2 Commits
9db26562e4
...
c07e793b55
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | c07e793b55 | |
Savanni D'Gerinel | f19c3a6d79 |
310
sgf/src/game.rs
310
sgf/src/game.rs
|
@ -1,17 +1,10 @@
|
|||
use crate::{
|
||||
parser::{self, Annotation, Evaluation, UnknownProperty},
|
||||
Color,
|
||||
parser::{self, Annotation, Evaluation, GameType, SetupInstr, Size, UnknownProperty},
|
||||
Color, Date, GameResult,
|
||||
};
|
||||
use std::{collections::HashSet, time::Duration};
|
||||
use uuid::Uuid;
|
||||
|
||||
/*
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum PropertyError {
|
||||
ConflictingProperty,
|
||||
}
|
||||
*/
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum MoveNodeError {
|
||||
IncompatibleProperty,
|
||||
|
@ -24,11 +17,106 @@ pub enum SetupNodeError {
|
|||
ConflictingPosition,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum GameError {
|
||||
InvalidGame,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum GameNodeError {
|
||||
UnsupportedGameNode,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Default)]
|
||||
pub struct Player {
|
||||
pub name: Option<String>,
|
||||
pub rank: Option<String>,
|
||||
pub team: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Game {
|
||||
game_type: GameType,
|
||||
|
||||
// TODO: board size is not necessary in all games. Hive has no defined board size.
|
||||
board_size: Size,
|
||||
black_player: Player,
|
||||
white_player: Player,
|
||||
|
||||
app: Option<String>,
|
||||
annotator: Option<String>,
|
||||
copyright: Option<String>,
|
||||
dates: Vec<Date>,
|
||||
event: Option<String>,
|
||||
game_name: Option<String>,
|
||||
extra_info: Option<String>,
|
||||
opening_info: Option<String>,
|
||||
location: Option<String>,
|
||||
result: GameResult,
|
||||
round: Option<String>,
|
||||
rules: Option<String>,
|
||||
source: Option<String>,
|
||||
time_limit: Option<Duration>,
|
||||
overtime: Option<String>,
|
||||
data_entry: Option<String>,
|
||||
|
||||
children: Vec<GameNode>,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
fn new(
|
||||
game_type: GameType,
|
||||
board_size: Size,
|
||||
black_player: Player,
|
||||
white_player: Player,
|
||||
) -> Self {
|
||||
Self {
|
||||
game_type,
|
||||
board_size,
|
||||
black_player,
|
||||
white_player,
|
||||
|
||||
app: None,
|
||||
annotator: None,
|
||||
copyright: None,
|
||||
dates: vec![],
|
||||
event: None,
|
||||
game_name: None,
|
||||
extra_info: None,
|
||||
opening_info: None,
|
||||
location: None,
|
||||
result: GameResult::Unknown,
|
||||
round: None,
|
||||
rules: None,
|
||||
source: None,
|
||||
time_limit: None,
|
||||
overtime: None,
|
||||
data_entry: None,
|
||||
|
||||
children: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Node for Game {
|
||||
fn children<'a>(&'a self) -> Vec<&'a GameNode> {
|
||||
self.children.iter().collect::<Vec<&'a GameNode>>()
|
||||
}
|
||||
|
||||
fn add_child<'a>(&'a mut self, node: GameNode) -> &'a mut GameNode {
|
||||
self.children.push(node);
|
||||
self.children.last_mut().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&parser::Tree> for Game {
|
||||
type Error = GameError;
|
||||
|
||||
fn try_from(tree: &parser::Tree) -> Result<Self, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum GameNode {
|
||||
MoveNode(MoveNode),
|
||||
|
@ -96,28 +184,6 @@ impl TryFrom<&parser::Node> for GameNode {
|
|||
}
|
||||
}
|
||||
|
||||
// Root node
|
||||
pub struct GameTree {
|
||||
children: Vec<GameNode>,
|
||||
}
|
||||
|
||||
impl GameTree {
|
||||
fn new() -> Self {
|
||||
Self { children: vec![] }
|
||||
}
|
||||
}
|
||||
|
||||
impl Node for GameTree {
|
||||
fn children<'a>(&'a self) -> Vec<&'a GameNode> {
|
||||
self.children.iter().collect::<Vec<&'a GameNode>>()
|
||||
}
|
||||
|
||||
fn add_child<'a>(&'a mut self, node: GameNode) -> &'a mut GameNode {
|
||||
self.children.push(node);
|
||||
self.children.last_mut().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MoveNode {
|
||||
id: Uuid,
|
||||
|
@ -222,19 +288,6 @@ impl TryFrom<&parser::Node> for MoveNode {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
fn parse_position(s: &str) -> Result<Position, ConversionError> {
|
||||
if s.len() == 2 {
|
||||
Ok(Position {
|
||||
row: s[0],
|
||||
column: s[1],
|
||||
})
|
||||
} else {
|
||||
Err(ConversionError::InvalidPositionSyntax)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct SetupNode {
|
||||
id: Uuid,
|
||||
|
@ -245,6 +298,18 @@ pub struct SetupNode {
|
|||
|
||||
impl SetupNode {
|
||||
pub fn new(positions: Vec<parser::SetupInstr>) -> Result<Self, SetupNodeError> {
|
||||
let mut board = HashSet::new();
|
||||
for position in positions.iter() {
|
||||
let point = match position {
|
||||
SetupInstr::Piece((_, point)) => point,
|
||||
SetupInstr::Clear(point) => point,
|
||||
};
|
||||
if board.contains(point) {
|
||||
return Err(SetupNodeError::ConflictingPosition);
|
||||
}
|
||||
board.insert(point);
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
id: Uuid::new_v4(),
|
||||
positions,
|
||||
|
@ -297,20 +362,36 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn it_can_create_an_empty_game_tree() {
|
||||
let tree = GameTree::new();
|
||||
let tree = Game::new(
|
||||
GameType::Go,
|
||||
Size {
|
||||
width: 19,
|
||||
height: 19,
|
||||
},
|
||||
Player::default(),
|
||||
Player::default(),
|
||||
);
|
||||
assert_eq!(tree.nodes().len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_can_add_moves_to_a_game() {
|
||||
let mut tree = GameTree::new();
|
||||
let mut game = Game::new(
|
||||
GameType::Go,
|
||||
Size {
|
||||
width: 19,
|
||||
height: 19,
|
||||
},
|
||||
Player::default(),
|
||||
Player::default(),
|
||||
);
|
||||
|
||||
let first_move = MoveNode::new(Color::Black, "dd".to_owned());
|
||||
let first_ = tree.add_child(GameNode::MoveNode(first_move.clone()));
|
||||
let first_ = game.add_child(GameNode::MoveNode(first_move.clone()));
|
||||
let second_move = MoveNode::new(Color::White, "qq".to_owned());
|
||||
first_.add_child(GameNode::MoveNode(second_move.clone()));
|
||||
|
||||
let nodes = tree.nodes();
|
||||
let nodes = game.nodes();
|
||||
assert_eq!(nodes.len(), 2);
|
||||
assert_eq!(nodes[0].id(), first_move.id);
|
||||
assert_eq!(nodes[1].id(), second_move.id);
|
||||
|
@ -444,3 +525,136 @@ mod path_test {
|
|||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod file_test {
|
||||
use crate::Win;
|
||||
|
||||
use super::*;
|
||||
use parser::parse_collection;
|
||||
use std::{fs::File, io::Read};
|
||||
|
||||
fn with_text(text: &str, f: impl FnOnce(Vec<Game>)) {
|
||||
let (_, games) = parse_collection::<nom::error::VerboseError<&str>>(text).unwrap();
|
||||
let games = games
|
||||
.into_iter()
|
||||
.map(|game| Game::try_from(&game).expect("game to parse"))
|
||||
.collect::<Vec<Game>>();
|
||||
f(games);
|
||||
}
|
||||
|
||||
fn with_file(path: &std::path::Path, f: impl FnOnce(Vec<Game>)) {
|
||||
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_can_load_basic_game_records() {
|
||||
with_file(
|
||||
std::path::Path::new("test_data/2020 USGO DDK, Round 1.sgf"),
|
||||
|games| {
|
||||
assert_eq!(games.len(), 1);
|
||||
let game = &games[0];
|
||||
|
||||
assert_eq!(game.game_type, GameType::Go);
|
||||
assert_eq!(
|
||||
game.board_size,
|
||||
Size {
|
||||
width: 19,
|
||||
height: 19
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
game.black_player,
|
||||
Player {
|
||||
name: Some("savanni".to_owned()),
|
||||
rank: Some("23k".to_owned()),
|
||||
team: None
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
game.white_player,
|
||||
Player {
|
||||
name: Some("Geckoz".to_owned()),
|
||||
rank: None,
|
||||
team: None
|
||||
}
|
||||
);
|
||||
assert_eq!(game.app, Some("CGoban:3".to_owned()));
|
||||
|
||||
assert_eq!(game.annotator, None);
|
||||
assert_eq!(game.copyright, None);
|
||||
assert_eq!(
|
||||
game.dates,
|
||||
vec![Date::Date(
|
||||
chrono::NaiveDate::from_ymd_opt(2020, 8, 5).unwrap()
|
||||
)]
|
||||
);
|
||||
assert_eq!(game.event, None);
|
||||
assert_eq!(game.game_name, None);
|
||||
assert_eq!(game.extra_info, None);
|
||||
assert_eq!(game.opening_info, None);
|
||||
assert_eq!(
|
||||
game.location,
|
||||
Some("The KGS Go Server at http://www.gokgs.com/".to_owned())
|
||||
);
|
||||
assert_eq!(game.result, GameResult::White(Win::Score(17.5)));
|
||||
assert_eq!(game.round, None);
|
||||
assert_eq!(game.rules, Some("AGA".to_owned()));
|
||||
assert_eq!(game.source, None);
|
||||
assert_eq!(game.time_limit, Some(Duration::from_secs(1800)));
|
||||
assert_eq!(game.overtime, Some("5x30 byo-yomi".to_owned()));
|
||||
assert_eq!(game.data_entry, None);
|
||||
|
||||
/*
|
||||
Property {
|
||||
ident: "KM".to_owned(),
|
||||
values: vec!["7.50".to_owned()],
|
||||
},
|
||||
];
|
||||
|
||||
for i in 0..16 {
|
||||
assert_eq!(node.properties[i], expected_properties[i]);
|
||||
}
|
||||
|
||||
let node = node.next().unwrap();
|
||||
let expected_properties = vec![
|
||||
Property {
|
||||
ident: "B".to_owned(),
|
||||
values: vec!["pp".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "BL".to_owned(),
|
||||
values: vec!["1795.449".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["Geckoz [?]: Good game\nsavanni [23k?]: There we go! This UI is... tough.\nsavanni [23k?]: Have fun! Talk to you at the end.\nGeckoz [?]: Yeah, OGS is much better; I'm a UX professional\n".to_owned()],
|
||||
}
|
||||
];
|
||||
|
||||
for i in 0..3 {
|
||||
assert_eq!(node.properties[i], expected_properties[i]);
|
||||
}
|
||||
|
||||
let node = node.next().unwrap();
|
||||
let expected_properties = vec![
|
||||
Property {
|
||||
ident: "W".to_owned(),
|
||||
values: vec!["dp".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "WL".to_owned(),
|
||||
values: vec!["1765.099".to_owned()],
|
||||
},
|
||||
];
|
||||
for i in 0..2 {
|
||||
assert_eq!(node.properties[i], expected_properties[i]);
|
||||
}
|
||||
*/
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
120
sgf/src/go.rs
120
sgf/src/go.rs
|
@ -233,8 +233,6 @@ pub struct GameInfo {
|
|||
pub result: Option<GameResult>,
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
enum PropType {
|
||||
Move,
|
||||
|
@ -347,121 +345,5 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn it_presents_the_mainline_of_game_without_branches() {
|
||||
with_file(
|
||||
std::path::Path::new("test_data/2020 USGO DDK, Round 1.sgf"),
|
||||
|trees| {
|
||||
assert_eq!(trees.len(), 1);
|
||||
let tree = &trees[0];
|
||||
|
||||
let node = &tree.root;
|
||||
assert_eq!(node.properties.len(), 16);
|
||||
let expected_properties = vec![
|
||||
Property {
|
||||
ident: "GM".to_owned(),
|
||||
values: vec!["1".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "FF".to_owned(),
|
||||
values: vec!["4".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "CA".to_owned(),
|
||||
values: vec!["UTF-8".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "AP".to_owned(),
|
||||
values: vec!["CGoban:3".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "ST".to_owned(),
|
||||
values: vec!["2".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "RU".to_owned(),
|
||||
values: vec!["AGA".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "SZ".to_owned(),
|
||||
values: vec!["19".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "KM".to_owned(),
|
||||
values: vec!["7.50".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "TM".to_owned(),
|
||||
values: vec!["1800".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "OT".to_owned(),
|
||||
values: vec!["5x30 byo-yomi".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "PW".to_owned(),
|
||||
values: vec!["Geckoz".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "PB".to_owned(),
|
||||
values: vec!["savanni".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "BR".to_owned(),
|
||||
values: vec!["23k".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "DT".to_owned(),
|
||||
values: vec!["2020-08-05".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "PC".to_owned(),
|
||||
values: vec!["The KGS Go Server at http://www.gokgs.com/".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "RE".to_owned(),
|
||||
values: vec!["W+17.50".to_owned()],
|
||||
},
|
||||
];
|
||||
|
||||
for i in 0..16 {
|
||||
assert_eq!(node.properties[i], expected_properties[i]);
|
||||
}
|
||||
|
||||
let node = node.next().unwrap();
|
||||
let expected_properties = vec![
|
||||
Property {
|
||||
ident: "B".to_owned(),
|
||||
values: vec!["pp".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "BL".to_owned(),
|
||||
values: vec!["1795.449".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "C".to_owned(),
|
||||
values: vec!["Geckoz [?]: Good game\nsavanni [23k?]: There we go! This UI is... tough.\nsavanni [23k?]: Have fun! Talk to you at the end.\nGeckoz [?]: Yeah, OGS is much better; I'm a UX professional\n".to_owned()],
|
||||
}
|
||||
];
|
||||
|
||||
for i in 0..3 {
|
||||
assert_eq!(node.properties[i], expected_properties[i]);
|
||||
}
|
||||
|
||||
let node = node.next().unwrap();
|
||||
let expected_properties = vec![
|
||||
Property {
|
||||
ident: "W".to_owned(),
|
||||
values: vec!["dp".to_owned()],
|
||||
},
|
||||
Property {
|
||||
ident: "WL".to_owned(),
|
||||
values: vec!["1765.099".to_owned()],
|
||||
},
|
||||
];
|
||||
for i in 0..2 {
|
||||
assert_eq!(node.properties[i], expected_properties[i]);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
fn it_presents_the_mainline_of_game_without_branches() {}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{Color, Error, GameResult};
|
||||
use crate::{Color, Date, Error, GameResult};
|
||||
use nom::{
|
||||
branch::alt,
|
||||
bytes::complete::{escaped_transform, tag, take_until1},
|
||||
|
@ -422,7 +422,7 @@ pub enum Property {
|
|||
Copyright(String),
|
||||
|
||||
// DT
|
||||
EventDates(Vec<chrono::NaiveDate>),
|
||||
EventDates(Vec<Date>),
|
||||
|
||||
// EV
|
||||
EventName(String),
|
||||
|
|
|
@ -70,7 +70,7 @@ pub enum GameResult {
|
|||
Black(Win),
|
||||
White(Win),
|
||||
Void,
|
||||
Unknown(String),
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for GameResult {
|
||||
|
@ -85,7 +85,7 @@ impl TryFrom<&str> for GameResult {
|
|||
let res = match parts[0].to_ascii_lowercase().as_str() {
|
||||
"b" => GameResult::Black,
|
||||
"w" => GameResult::White,
|
||||
_ => return Ok(GameResult::Unknown(parts[0].to_owned())),
|
||||
_ => return Ok(GameResult::Unknown),
|
||||
};
|
||||
match parts[1].to_ascii_lowercase().as_str() {
|
||||
"r" | "resign" => Ok(res(Win::Resignation)),
|
||||
|
|
Loading…
Reference in New Issue