Compare commits
No commits in common. "353f04f2c46ccb24e7f1f2b78cd7e343bcfc4ef3" and "f899fdb6910ff87583b62cd4879e0b114bdf0f99" have entirely different histories.
353f04f2c4
...
f899fdb691
|
@ -2708,7 +2708,6 @@ dependencies = [
|
||||||
"serde 1.0.193",
|
"serde 1.0.193",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sgf",
|
"sgf",
|
||||||
"slab_tree",
|
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"uuid 0.8.2",
|
"uuid 0.8.2",
|
||||||
]
|
]
|
||||||
|
|
|
@ -14,7 +14,6 @@ sgf = { path = "../../sgf" }
|
||||||
grid = { version = "0.9" }
|
grid = { version = "0.9" }
|
||||||
serde_json = { version = "1" }
|
serde_json = { version = "1" }
|
||||||
serde = { version = "1", features = [ "derive" ] }
|
serde = { version = "1", features = [ "derive" ] }
|
||||||
slab_tree = { version = "0.3" }
|
|
||||||
thiserror = { version = "1" }
|
thiserror = { version = "1" }
|
||||||
uuid = { version = "0.8", features = ["v4", "serde"] }
|
uuid = { version = "0.8", features = ["v4", "serde"] }
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@ impl From<HotseatPlayerRequest> for Player {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub enum CoreResponse {
|
pub enum CoreResponse {
|
||||||
Library(library::LibraryResponse),
|
Library(library::LibraryResponse),
|
||||||
Settings(settings::SettingsResponse),
|
Settings(settings::SettingsResponse),
|
||||||
|
|
|
@ -42,8 +42,7 @@ impl Database {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
match parse_sgf(&buffer) {
|
match parse_sgf(&buffer) {
|
||||||
Ok(sgfs) => {
|
Ok(sgfs) => {
|
||||||
let mut sgfs =
|
let mut sgfs = sgfs.into_iter().flatten().collect::<Vec<sgf::GameRecord>>();
|
||||||
sgfs.into_iter().flatten().collect::<Vec<sgf::GameRecord>>();
|
|
||||||
games.append(&mut sgfs);
|
games.append(&mut sgfs);
|
||||||
}
|
}
|
||||||
Err(err) => println!("Error parsing {:?}: {:?}", entry.path(), err),
|
Err(err) => println!("Error parsing {:?}: {:?}", entry.path(), err),
|
||||||
|
|
|
@ -29,6 +29,5 @@ pub mod library;
|
||||||
pub mod settings;
|
pub mod settings;
|
||||||
|
|
||||||
mod types;
|
mod types;
|
||||||
pub use types::{
|
pub use types::{BoardError, Color, Config, ConfigOption, LibraryPath, Player, Rank, Size, Tree};
|
||||||
BoardError, Color, Config, ConfigOption, DepthTree, LibraryPath, Player, Rank, Size,
|
|
||||||
};
|
|
||||||
|
|
|
@ -14,18 +14,18 @@ General Public License for more details.
|
||||||
You should have received a copy of the GNU General Public License along with On the Grid. If not, see <https://www.gnu.org/licenses/>.
|
You should have received a copy of the GNU General Public License along with On the Grid. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::Core;
|
use crate::{Core};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sgf::GameRecord;
|
use sgf::GameRecord;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub enum LibraryRequest {
|
pub enum LibraryRequest {
|
||||||
ListGames,
|
ListGames
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub enum LibraryResponse {
|
pub enum LibraryResponse {
|
||||||
Games(Vec<GameRecord>),
|
Games(Vec<GameRecord>)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_list_games(model: &Core) -> LibraryResponse {
|
async fn handle_list_games(model: &Core) -> LibraryResponse {
|
||||||
|
@ -39,8 +39,10 @@ async fn handle_list_games(model: &Core) -> LibraryResponse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub async fn handle(model: &Core, request: LibraryRequest) -> LibraryResponse {
|
pub async fn handle(model: &Core, request: LibraryRequest) -> LibraryResponse {
|
||||||
match request {
|
match request {
|
||||||
LibraryRequest::ListGames => handle_list_games(model).await,
|
LibraryRequest::ListGames => handle_list_games(model).await,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,10 @@ use crate::goban::{Coordinate, Goban};
|
||||||
use config::define_config;
|
use config::define_config;
|
||||||
use config_derive::ConfigOption;
|
use config_derive::ConfigOption;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sgf::GameTree;
|
use sgf::GameNode;
|
||||||
use std::{
|
use std::{cell::RefCell, collections::VecDeque, fmt, path::PathBuf, time::Duration};
|
||||||
cell::RefCell, collections::{HashMap, VecDeque}, fmt, ops::Deref, path::PathBuf, time::Duration
|
|
||||||
};
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
define_config! {
|
define_config! {
|
||||||
LibraryPath(LibraryPath),
|
LibraryPath(LibraryPath),
|
||||||
|
@ -230,7 +229,6 @@ impl GameState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
// To properly generate a tree, I need to know how deep to go. Then I can backtrace. Each node
|
// To properly generate a tree, I need to know how deep to go. Then I can backtrace. Each node
|
||||||
// needs to have a depth. Given a tree, the depth of the node is just the distance from the root.
|
// needs to have a depth. Given a tree, the depth of the node is just the distance from the root.
|
||||||
// This seems obvious, but I had to write it to discover how important that fact was.
|
// This seems obvious, but I had to write it to discover how important that fact was.
|
||||||
|
@ -240,43 +238,19 @@ impl GameState {
|
||||||
pub struct Tree<T> {
|
pub struct Tree<T> {
|
||||||
nodes: Vec<Node<T>>,
|
nodes: Vec<Node<T>>,
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
pub struct DepthTree(slab_tree::Tree<SizeNode>);
|
|
||||||
|
|
||||||
impl Deref for DepthTree {
|
|
||||||
type Target = slab_tree::Tree<SizeNode>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct SizeNode {
|
pub struct Node<T> {
|
||||||
node_id: slab_tree::NodeId,
|
pub id: usize,
|
||||||
parent: Option<slab_tree::NodeId>,
|
node: T,
|
||||||
|
parent: Option<usize>,
|
||||||
depth: usize,
|
depth: usize,
|
||||||
width: usize,
|
width: RefCell<Option<usize>>,
|
||||||
|
children: Vec<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SizeNode {
|
impl<T> Tree<T> {
|
||||||
pub fn position(&self) -> (usize, usize) {
|
fn new(root: T) -> Self {
|
||||||
(self.depth, self.width)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DepthTree {
|
|
||||||
// My previous work to convert from a node tree to this tree-with-width dependend on the node tree
|
|
||||||
// being a recursive data structure. Now I need to find a way to convert a slab tree to this width
|
|
||||||
// tree.
|
|
||||||
//
|
|
||||||
// It all feels like a lot of custom weirdness. I shouldn't need a bunch of custom data structures,
|
|
||||||
// so I want to eliminate the "Tree" above and keep using the slab tree. I think I should be able
|
|
||||||
// to build these Node objects without needing a custom data structure.
|
|
||||||
fn new() -> Self {
|
|
||||||
Self(slab_tree::Tree::new())
|
|
||||||
/*
|
|
||||||
Tree {
|
Tree {
|
||||||
nodes: vec![Node {
|
nodes: vec![Node {
|
||||||
id: 0,
|
id: 0,
|
||||||
|
@ -287,10 +261,8 @@ impl DepthTree {
|
||||||
children: vec![],
|
children: vec![],
|
||||||
}],
|
}],
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
pub fn node(&self, idx: usize) -> &T {
|
pub fn node(&self, idx: usize) -> &T {
|
||||||
&self.nodes[idx].node
|
&self.nodes[idx].node
|
||||||
}
|
}
|
||||||
|
@ -314,21 +286,12 @@ impl DepthTree {
|
||||||
parent.children.push(next_idx);
|
parent.children.push(next_idx);
|
||||||
next_idx
|
next_idx
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
pub fn max_depth(&self) -> usize {
|
pub fn max_depth(&self) -> usize {
|
||||||
self.0
|
self.nodes.iter().fold(
|
||||||
.root()
|
0,
|
||||||
.unwrap()
|
|max, node| if node.depth > max { node.depth } else { max },
|
||||||
.traverse_pre_order()
|
)
|
||||||
.fold(0, |max, node| {
|
|
||||||
println!("node depth: {}", node.data().depth);
|
|
||||||
if node.data().depth > max {
|
|
||||||
node.data().depth
|
|
||||||
} else {
|
|
||||||
max
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since I know the width of a node, now I want to figure out its placement in the larger
|
// Since I know the width of a node, now I want to figure out its placement in the larger
|
||||||
|
@ -346,9 +309,7 @@ impl DepthTree {
|
||||||
// amounts to the position of the parent node.
|
// amounts to the position of the parent node.
|
||||||
//
|
//
|
||||||
// When drawing nodes, I don't know how to persist the level of indent.
|
// When drawing nodes, I don't know how to persist the level of indent.
|
||||||
|
pub fn position(&self, idx: usize) -> (usize, usize) {
|
||||||
// unimplemented!()
|
|
||||||
/*
|
|
||||||
let node = &self.nodes[idx];
|
let node = &self.nodes[idx];
|
||||||
match node.parent {
|
match node.parent {
|
||||||
Some(parent_idx) => {
|
Some(parent_idx) => {
|
||||||
|
@ -365,9 +326,8 @@ impl DepthTree {
|
||||||
// Root nodes won't have a parent, so just put them in the first column
|
// Root nodes won't have a parent, so just put them in the first column
|
||||||
None => (0, 0),
|
None => (0, 0),
|
||||||
}
|
}
|
||||||
*/
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
// Given a node, do a postorder traversal to figure out the width of the node based on all of
|
// Given a node, do a postorder traversal to figure out the width of the node based on all of
|
||||||
// its children. This is equivalent to the widest of all of its children at all depths.
|
// its children. This is equivalent to the widest of all of its children at all depths.
|
||||||
//
|
//
|
||||||
|
@ -393,102 +353,14 @@ impl DepthTree {
|
||||||
|
|
||||||
width
|
width
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
pub fn bfs_iter(&self) -> BFSIter<'_, SizeNode> {
|
pub fn bfs_iter(&self) -> BFSIter<T> {
|
||||||
let mut queue = VecDeque::new();
|
let mut queue = VecDeque::new();
|
||||||
queue.push_back(self.0.root().unwrap());
|
queue.push_back(&self.nodes[0]);
|
||||||
BFSIter { tree: self, queue }
|
BFSIter { tree: self, queue }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a GameTree> for DepthTree {
|
|
||||||
fn from(tree: &'a GameTree) -> Self {
|
|
||||||
// Like in the conversion from SGF to GameTree, I need to traverse the entire tree one node
|
|
||||||
// at a time, keeping track of node ids as we go. I'm going to go with a depth-first
|
|
||||||
// traversal. When generating each node, I think I want to generate all of the details of
|
|
||||||
// the node as we go.
|
|
||||||
let source_root_node = tree.root();
|
|
||||||
match source_root_node {
|
|
||||||
Some(source_root_node) => {
|
|
||||||
// Do the real work
|
|
||||||
// The id_map indexes from the source tree to the destination tree. Reverse
|
|
||||||
// indexing is accomplished by looking at the node_id in a node in the destination
|
|
||||||
// tree.
|
|
||||||
let mut id_map: HashMap<slab_tree::NodeId, slab_tree::NodeId> = HashMap::new();
|
|
||||||
let mut tree = slab_tree::Tree::new();
|
|
||||||
|
|
||||||
let mut iter = source_root_node.traverse_pre_order();
|
|
||||||
let _ = iter.next().unwrap(); // we already know that the first element to be
|
|
||||||
// returned is the root node, and that the root node
|
|
||||||
// already exists. Otherwise we wouldn't even be in
|
|
||||||
// this branch.
|
|
||||||
|
|
||||||
let dest_root_id = tree.set_root(SizeNode {
|
|
||||||
node_id: source_root_node.node_id(),
|
|
||||||
parent: None,
|
|
||||||
depth: 0,
|
|
||||||
width: 0,
|
|
||||||
});
|
|
||||||
|
|
||||||
id_map.insert(source_root_node.node_id(), dest_root_id);
|
|
||||||
|
|
||||||
for source_node in iter {
|
|
||||||
let dest_parent_id = id_map
|
|
||||||
.get(&source_node.parent().unwrap().node_id())
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut dest_parent = tree.get_mut(*dest_parent_id).unwrap();
|
|
||||||
|
|
||||||
let new_depth_node = SizeNode {
|
|
||||||
node_id: source_node.node_id(),
|
|
||||||
parent: Some(*dest_parent_id),
|
|
||||||
depth: 1 + dest_parent.data().depth,
|
|
||||||
width: dest_parent.data().width,
|
|
||||||
};
|
|
||||||
|
|
||||||
let new_node_id = dest_parent.append(new_depth_node).node_id();
|
|
||||||
|
|
||||||
match tree
|
|
||||||
.get(new_node_id)
|
|
||||||
.unwrap()
|
|
||||||
.prev_sibling()
|
|
||||||
.map(|node| node.data().width)
|
|
||||||
{
|
|
||||||
None => {}
|
|
||||||
Some(previous_width) => {
|
|
||||||
let mut new_node = tree.get_mut(new_node_id).unwrap();
|
|
||||||
new_node.data().width = previous_width + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
let new_node = tree.get_mut(*dest_parent_id).unwrap().append(new_depth_node);
|
|
||||||
let previous_node = new_node.prev_sibling();
|
|
||||||
|
|
||||||
match previous_node {
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
match dest_noderef.prev_sibling() {
|
|
||||||
None => {}
|
|
||||||
Some(mut node) => { dest_noderef.data().width = node.data().width + 1 }
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
id_map.insert(source_node.node_id(), new_node_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
Self(tree)
|
|
||||||
}
|
|
||||||
None => Self::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
impl<'a> From<&'a GameNode> for Tree<Uuid> {
|
impl<'a> From<&'a GameNode> for Tree<Uuid> {
|
||||||
fn from(root: &'a GameNode) -> Self {
|
fn from(root: &'a GameNode) -> Self {
|
||||||
fn add_subtree(tree: &mut Tree<Uuid>, parent_idx: usize, node: &GameNode) {
|
fn add_subtree(tree: &mut Tree<Uuid>, parent_idx: usize, node: &GameNode) {
|
||||||
|
@ -518,32 +390,32 @@ impl<'a> From<&'a GameNode> for Tree<Uuid> {
|
||||||
tree
|
tree
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
pub struct BFSIter<'a, T> {
|
pub struct BFSIter<'a, T> {
|
||||||
tree: &'a DepthTree,
|
tree: &'a Tree<T>,
|
||||||
queue: VecDeque<slab_tree::NodeRef<'a, T>>,
|
queue: VecDeque<&'a Node<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> Iterator for BFSIter<'a, T> {
|
impl<'a, T> Iterator for BFSIter<'a, T> {
|
||||||
type Item = &'a T;
|
type Item = &'a Node<T>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let retval = self.queue.pop_front();
|
let retval = self.queue.pop_front();
|
||||||
if let Some(ref retval) = retval {
|
if let Some(retval) = retval {
|
||||||
retval
|
retval
|
||||||
.children()
|
.children
|
||||||
.for_each(|noderef| self.queue.push_back(noderef));
|
.iter()
|
||||||
|
.for_each(|idx| self.queue.push_back(&self.tree.nodes[*idx]));
|
||||||
}
|
}
|
||||||
retval.map(|retval| retval.data())
|
retval
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
// use sgf::{GameRecord, GameTree, GameType, Move, MoveNode};
|
use cool_asserts::assert_matches;
|
||||||
use sgf::{GameNode, GameTree, Move, MoveNode};
|
use sgf::{Move, MoveNode};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn current_player_changes_after_move() {
|
fn current_player_changes_after_move() {
|
||||||
|
@ -602,138 +474,8 @@ mod test {
|
||||||
// B G H
|
// B G H
|
||||||
// C I
|
// C I
|
||||||
// D E F
|
// D E F
|
||||||
fn branching_tree() -> GameTree {
|
|
||||||
let mut game_tree = GameTree::default();
|
|
||||||
let node_a = game_tree.set_root(GameNode::MoveNode(MoveNode::new(
|
|
||||||
sgf::Color::Black,
|
|
||||||
Move::Move("dp".to_owned()),
|
|
||||||
)));
|
|
||||||
|
|
||||||
let node_b = game_tree
|
|
||||||
.get_mut(node_a)
|
|
||||||
.unwrap()
|
|
||||||
.append(GameNode::MoveNode(MoveNode::new(
|
|
||||||
sgf::Color::Black,
|
|
||||||
Move::Move("dp".to_owned()),
|
|
||||||
)))
|
|
||||||
.node_id();
|
|
||||||
|
|
||||||
let node_c = game_tree
|
|
||||||
.get_mut(node_b)
|
|
||||||
.unwrap()
|
|
||||||
.append(GameNode::MoveNode(MoveNode::new(
|
|
||||||
sgf::Color::Black,
|
|
||||||
Move::Move("dp".to_owned()),
|
|
||||||
)))
|
|
||||||
.node_id();
|
|
||||||
|
|
||||||
let node_d = game_tree
|
|
||||||
.get_mut(node_c)
|
|
||||||
.unwrap()
|
|
||||||
.append(GameNode::MoveNode(MoveNode::new(
|
|
||||||
sgf::Color::Black,
|
|
||||||
Move::Move("dp".to_owned()),
|
|
||||||
)))
|
|
||||||
.node_id();
|
|
||||||
|
|
||||||
let node_e = game_tree
|
|
||||||
.get_mut(node_c)
|
|
||||||
.unwrap()
|
|
||||||
.append(GameNode::MoveNode(MoveNode::new(
|
|
||||||
sgf::Color::Black,
|
|
||||||
Move::Move("dp".to_owned()),
|
|
||||||
)))
|
|
||||||
.node_id();
|
|
||||||
|
|
||||||
let node_f = game_tree
|
|
||||||
.get_mut(node_c)
|
|
||||||
.unwrap()
|
|
||||||
.append(GameNode::MoveNode(MoveNode::new(
|
|
||||||
sgf::Color::Black,
|
|
||||||
Move::Move("dp".to_owned()),
|
|
||||||
)))
|
|
||||||
.node_id();
|
|
||||||
|
|
||||||
let node_g = game_tree
|
|
||||||
.get_mut(node_a)
|
|
||||||
.unwrap()
|
|
||||||
.append(GameNode::MoveNode(MoveNode::new(
|
|
||||||
sgf::Color::Black,
|
|
||||||
Move::Move("dp".to_owned()),
|
|
||||||
)))
|
|
||||||
.node_id();
|
|
||||||
|
|
||||||
let node_h = game_tree
|
|
||||||
.get_mut(node_a)
|
|
||||||
.unwrap()
|
|
||||||
.append(GameNode::MoveNode(MoveNode::new(
|
|
||||||
sgf::Color::Black,
|
|
||||||
Move::Move("dp".to_owned()),
|
|
||||||
)))
|
|
||||||
.node_id();
|
|
||||||
|
|
||||||
let _ = game_tree
|
|
||||||
.get_mut(node_h)
|
|
||||||
.unwrap()
|
|
||||||
.append(GameNode::MoveNode(MoveNode::new(
|
|
||||||
sgf::Color::Black,
|
|
||||||
Move::Move("dp".to_owned()),
|
|
||||||
)))
|
|
||||||
.node_id();
|
|
||||||
|
|
||||||
game_tree
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_can_calculate_depth_from_game_tree() {
|
fn it_can_calculate_depth_from_game_tree() {
|
||||||
let game_tree = branching_tree();
|
|
||||||
let tree = DepthTree::from(&game_tree);
|
|
||||||
assert_eq!(
|
|
||||||
game_tree.root().unwrap().traverse_pre_order().count(),
|
|
||||||
tree.0.root().unwrap().traverse_pre_order().count()
|
|
||||||
);
|
|
||||||
assert_eq!(tree.max_depth(), 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_calculates_horizontal_position_of_nodes() {
|
|
||||||
let game_tree = branching_tree();
|
|
||||||
let tree = DepthTree::from(&game_tree);
|
|
||||||
|
|
||||||
let node_a = tree.root().unwrap();
|
|
||||||
assert_eq!(node_a.data().position(), (0, 0));
|
|
||||||
|
|
||||||
let node_b = node_a.first_child().unwrap();
|
|
||||||
assert_eq!(node_b.data().position(), (1, 0));
|
|
||||||
let node_g = node_b.next_sibling().unwrap();
|
|
||||||
assert_eq!(node_g.data().position(), (1, 1));
|
|
||||||
let node_h = node_g.next_sibling().unwrap();
|
|
||||||
assert_eq!(node_h.data().position(), (1, 2));
|
|
||||||
|
|
||||||
let node_c = node_b.first_child().unwrap();
|
|
||||||
assert_eq!(node_c.data().position(), (2, 0));
|
|
||||||
|
|
||||||
let node_d = node_c.first_child().unwrap();
|
|
||||||
assert_eq!(node_d.data().position(), (3, 0));
|
|
||||||
|
|
||||||
let node_i = node_h.first_child().unwrap();
|
|
||||||
assert_eq!(node_i.data().position(), (2, 2));
|
|
||||||
|
|
||||||
/*
|
|
||||||
assert_eq!(tree.position(test_tree.node_c), (2, 0));
|
|
||||||
assert_eq!(tree.position(test_tree.node_b), (1, 0));
|
|
||||||
assert_eq!(tree.position(test_tree.node_a), (0, 0));
|
|
||||||
assert_eq!(tree.position(test_tree.node_d), (3, 1));
|
|
||||||
assert_eq!(tree.position(test_tree.node_e), (3, 2));
|
|
||||||
assert_eq!(tree.position(test_tree.node_f), (1, 3));
|
|
||||||
assert_eq!(tree.position(test_tree.node_g), (1, 4));
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
#[ignore]
|
|
||||||
#[test]
|
|
||||||
fn breadth_first_iter() {
|
|
||||||
/*
|
|
||||||
let mut node_a = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
let mut node_a = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
let mut node_b = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
let mut node_b = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
let mut node_c = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
let mut node_c = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
@ -744,23 +486,77 @@ mod test {
|
||||||
let mut node_h = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
let mut node_h = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
let node_i = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
let node_i = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
|
||||||
let game = GameRecord::new(
|
node_c.children.push(GameNode::MoveNode(node_d));
|
||||||
GameType::Go,
|
node_c.children.push(GameNode::MoveNode(node_e));
|
||||||
Size {
|
node_c.children.push(GameNode::MoveNode(node_f));
|
||||||
width: 19,
|
|
||||||
height: 19,
|
node_b.children.push(GameNode::MoveNode(node_c));
|
||||||
},
|
|
||||||
Player {
|
node_h.children.push(GameNode::MoveNode(node_i));
|
||||||
name: Some("Black".to_owned()),
|
|
||||||
rank: None,
|
node_a.children.push(GameNode::MoveNode(node_b));
|
||||||
team: None,
|
node_a.children.push(GameNode::MoveNode(node_g));
|
||||||
},
|
node_a.children.push(GameNode::MoveNode(node_h));
|
||||||
Player {
|
|
||||||
name: Some("White".to_owned()),
|
let game_tree = GameNode::MoveNode(node_a);
|
||||||
rank: None,
|
|
||||||
team: None,
|
let tree = Tree::from(&game_tree);
|
||||||
},
|
|
||||||
);
|
assert_eq!(tree.max_depth(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
// A
|
||||||
|
// B G H
|
||||||
|
// C I
|
||||||
|
// D E F
|
||||||
|
#[test]
|
||||||
|
fn it_calculates_horizontal_position_of_nodes() {
|
||||||
|
let mut node_a = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let mut node_b = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let mut node_c = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let node_d = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let node_e = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let node_f = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let node_g = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let mut node_h = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let node_i = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
|
||||||
|
node_c.children.push(GameNode::MoveNode(node_d));
|
||||||
|
node_c.children.push(GameNode::MoveNode(node_e));
|
||||||
|
node_c.children.push(GameNode::MoveNode(node_f));
|
||||||
|
|
||||||
|
node_b.children.push(GameNode::MoveNode(node_c));
|
||||||
|
|
||||||
|
node_h.children.push(GameNode::MoveNode(node_i));
|
||||||
|
|
||||||
|
node_a.children.push(GameNode::MoveNode(node_b));
|
||||||
|
node_a.children.push(GameNode::MoveNode(node_g));
|
||||||
|
node_a.children.push(GameNode::MoveNode(node_h));
|
||||||
|
|
||||||
|
let game_tree = GameNode::MoveNode(node_a);
|
||||||
|
|
||||||
|
let tree = Tree::from(&game_tree);
|
||||||
|
|
||||||
|
assert_eq!(tree.position(2), (2, 0));
|
||||||
|
assert_eq!(tree.position(1), (1, 0));
|
||||||
|
assert_eq!(tree.position(0), (0, 0));
|
||||||
|
assert_eq!(tree.position(4), (3, 1));
|
||||||
|
assert_eq!(tree.position(5), (3, 2));
|
||||||
|
assert_eq!(tree.position(6), (1, 3));
|
||||||
|
assert_eq!(tree.position(7), (1, 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn breadth_first_iter() {
|
||||||
|
let mut node_a = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let mut node_b = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let mut node_c = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let node_d = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let node_e = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let node_f = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let node_g = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let mut node_h = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
let node_i = MoveNode::new(sgf::Color::Black, Move::Move("dp".to_owned()));
|
||||||
|
|
||||||
node_c.children.push(GameNode::MoveNode(node_d.clone()));
|
node_c.children.push(GameNode::MoveNode(node_d.clone()));
|
||||||
node_c.children.push(GameNode::MoveNode(node_e.clone()));
|
node_c.children.push(GameNode::MoveNode(node_e.clone()));
|
||||||
|
@ -789,6 +585,5 @@ mod test {
|
||||||
assert_matches!(iter.next(), Some(Node { node: uuid, .. }) => assert_eq!(*uuid, node_d.id));
|
assert_matches!(iter.next(), Some(Node { node: uuid, .. }) => assert_eq!(*uuid, node_d.id));
|
||||||
assert_matches!(iter.next(), Some(Node { node: uuid, .. }) => assert_eq!(*uuid, node_e.id));
|
assert_matches!(iter.next(), Some(Node { node: uuid, .. }) => assert_eq!(*uuid, node_e.id));
|
||||||
assert_matches!(iter.next(), Some(Node { node: uuid, .. }) => assert_eq!(*uuid, node_f.id));
|
assert_matches!(iter.next(), Some(Node { node: uuid, .. }) => assert_eq!(*uuid, node_f.id));
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with On
|
||||||
use cairo::Context;
|
use cairo::Context;
|
||||||
use glib::Object;
|
use glib::Object;
|
||||||
use gtk::{prelude::*, subclass::prelude::*};
|
use gtk::{prelude::*, subclass::prelude::*};
|
||||||
use otg_core::DepthTree;
|
use otg_core::Tree;
|
||||||
use sgf::GameRecord;
|
use sgf::GameRecord;
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
@ -28,7 +28,7 @@ const HEIGHT: i32 = 800;
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct ReviewTreePrivate {
|
pub struct ReviewTreePrivate {
|
||||||
record: Rc<RefCell<Option<GameRecord>>>,
|
record: Rc<RefCell<Option<GameRecord>>>,
|
||||||
tree: Rc<RefCell<Option<DepthTree>>>,
|
tree: Rc<RefCell<Option<Tree<Uuid>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[glib::object_subclass]
|
#[glib::object_subclass]
|
||||||
|
@ -50,9 +50,7 @@ impl ReviewTree {
|
||||||
pub fn new(record: GameRecord) -> Self {
|
pub fn new(record: GameRecord) -> Self {
|
||||||
let s: Self = Object::new();
|
let s: Self = Object::new();
|
||||||
|
|
||||||
// TODO: there can be more than one tree, especially in instructional files. Either unify
|
*s.imp().tree.borrow_mut() = Some(Tree::from(&record.children[0]));
|
||||||
// them into a single tree in the GameTree, or draw all of them here.
|
|
||||||
*s.imp().tree.borrow_mut() = Some(DepthTree::from(&record.trees[0]));
|
|
||||||
*s.imp().record.borrow_mut() = Some(record);
|
*s.imp().record.borrow_mut() = Some(record);
|
||||||
|
|
||||||
s.set_width_request(WIDTH);
|
s.set_width_request(WIDTH);
|
||||||
|
@ -69,7 +67,7 @@ impl ReviewTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn redraw(&self, ctx: &Context, _width: i32, _height: i32) {
|
pub fn redraw(&self, ctx: &Context, _width: i32, _height: i32) {
|
||||||
let tree: &Option<DepthTree> = &self.imp().tree.borrow();
|
let tree: &Option<Tree<Uuid>> = &self.imp().tree.borrow();
|
||||||
match tree {
|
match tree {
|
||||||
Some(ref tree) => {
|
Some(ref tree) => {
|
||||||
for node in tree.bfs_iter() {
|
for node in tree.bfs_iter() {
|
||||||
|
@ -78,7 +76,7 @@ impl ReviewTree {
|
||||||
// the parent? do I need to just make it more intrinsically a part of the position
|
// the parent? do I need to just make it more intrinsically a part of the position
|
||||||
// code?
|
// code?
|
||||||
ctx.set_source_rgb(0.7, 0.7, 0.7);
|
ctx.set_source_rgb(0.7, 0.7, 0.7);
|
||||||
let (row, column) = node.position();
|
let (row, column) = tree.position(node.id);
|
||||||
let y = (row as f64) * 20. + 10.;
|
let y = (row as f64) * 20. + 10.;
|
||||||
let x = (column as f64) * 20. + 10.;
|
let x = (column as f64) * 20. + 10.;
|
||||||
ctx.arc(x, y, 5., 0., 2. * std::f64::consts::PI);
|
ctx.arc(x, y, 5., 0., 2. * std::f64::consts::PI);
|
||||||
|
|
|
@ -55,10 +55,9 @@ impl GameReview {
|
||||||
// It's actually really bad to be just throwing away errors. Panics make everyone unhappy.
|
// It's actually really bad to be just throwing away errors. Panics make everyone unhappy.
|
||||||
// This is not a fatal error, so I'll replace this `unwrap` call with something that
|
// This is not a fatal error, so I'll replace this `unwrap` call with something that
|
||||||
// renders the board and notifies the user of a problem that cannot be resolved.
|
// renders the board and notifies the user of a problem that cannot be resolved.
|
||||||
let board_repr = match record.mainline() {
|
let board_repr = otg_core::Goban::default()
|
||||||
Some(iter) => otg_core::Goban::default().apply_moves(iter).unwrap(),
|
.apply_moves(record.mainline())
|
||||||
None => otg_core::Goban::default(),
|
.unwrap();
|
||||||
};
|
|
||||||
let board = Goban::new(board_repr, resources);
|
let board = Goban::new(board_repr, resources);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -7,7 +7,7 @@ use slab_tree::{NodeId, NodeMut, NodeRef, Tree};
|
||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet, VecDeque},
|
collections::{HashMap, HashSet, VecDeque},
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
ops::{Deref, DerefMut},
|
ops::Deref,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
@ -136,7 +136,8 @@ impl GameRecord {
|
||||||
/// was actually played out, and by convention consists of the first node in each list of
|
/// was actually played out, and by convention consists of the first node in each list of
|
||||||
/// children.
|
/// children.
|
||||||
pub fn mainline(&self) -> Option<impl Iterator<Item = &'_ GameNode>> {
|
pub fn mainline(&self) -> Option<impl Iterator<Item = &'_ GameNode>> {
|
||||||
if !self.trees.is_empty() {
|
println!("number of trees: {}", self.trees.len());
|
||||||
|
if !self.trees.is_empty(){
|
||||||
Some(MainlineIter {
|
Some(MainlineIter {
|
||||||
next: self.trees[0].root(),
|
next: self.trees[0].root(),
|
||||||
tree: &self.trees[0],
|
tree: &self.trees[0],
|
||||||
|
@ -308,12 +309,6 @@ impl<'a> Iterator for TreeIter<'a> {
|
||||||
|
|
||||||
pub struct GameTree(Tree<GameNode>);
|
pub struct GameTree(Tree<GameNode>);
|
||||||
|
|
||||||
impl Default for GameTree {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self(Tree::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Clone for GameTree {
|
impl Clone for GameTree {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
match self.0.root() {
|
match self.0.root() {
|
||||||
|
@ -367,12 +362,6 @@ impl Deref for GameTree {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DerefMut for GameTree {
|
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for GameTree {
|
impl PartialEq for GameTree {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
// Get pre-order iterators over both trees, zip them, and ensure that the data contents are
|
// Get pre-order iterators over both trees, zip them, and ensure that the data contents are
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
mod date;
|
mod date;
|
||||||
|
|
||||||
mod game;
|
mod game;
|
||||||
pub use game::{GameNode, GameRecord, GameTree, MoveNode, Player};
|
pub use game::{GameNode, GameRecord, MoveNode, Player};
|
||||||
|
|
||||||
mod parser;
|
mod parser;
|
||||||
pub use parser::{parse_collection, Move};
|
pub use parser::{parse_collection, Move};
|
||||||
|
|
Loading…
Reference in New Issue