diff --git a/kifu/core/src/api.rs b/kifu/core/src/api.rs index a665c6e..6ffecc4 100644 --- a/kifu/core/src/api.rs +++ b/kifu/core/src/api.rs @@ -3,14 +3,13 @@ use crate::{ types::{AppState, Config, ConfigOption, DatabasePath, GameState, Player, Rank}, ui::{configuration, home, playing_field, ConfigurationView, HomeView, PlayingFieldView}, }; +use async_std::channel::{Receiver, Sender}; use serde::{Deserialize, Serialize}; use std::{ path::PathBuf, sync::{Arc, RwLock}, }; - #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(tag = "type", content = "content")] pub enum CoreRequest { ChangeSetting(ChangeSettingRequest), CreateGame(CreateGameRequest), @@ -22,7 +21,6 @@ pub enum CoreRequest { } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(tag = "type", content = "content")] pub enum ChangeSettingRequest { LibraryPath(String), } @@ -60,7 +58,6 @@ impl From for Player { } #[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(tag = "type", content = "content")] pub enum CoreResponse { ConfigurationView(ConfigurationView), HomeView(HomeView), @@ -78,6 +75,7 @@ pub struct Core { // config: Arc>, // state: Arc>, database: Arc>>, + subscribers: Arc>>>, } impl Core { @@ -90,9 +88,19 @@ impl Core { // config: Arc::new(RwLock::new(config)), // state, database: Arc::new(RwLock::new(None)), + subscribers: Arc::new(RwLock::new(vec![])), } } + pub fn subscribe(&self) -> Receiver { + let mut subscribers = self.subscribers.write().unwrap(); + + let (sender, receiver) = async_std::channel::unbounded(); + subscribers.push(sender); + + receiver + } + /* pub async fn dispatch(&self, request: CoreRequest) -> CoreResponse { match request { diff --git a/kifu/core/src/ui/types.rs b/kifu/core/src/ui/types.rs index 4e60fa5..cdca848 100644 --- a/kifu/core/src/ui/types.rs +++ b/kifu/core/src/ui/types.rs @@ -29,7 +29,6 @@ impl StoneElement { } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -#[serde(tag = "type", content = "content")] pub enum IntersectionElement { Unplayable, Empty(CoreRequest), diff --git a/kifu/gtk/src/view_models/game_view_model.rs b/kifu/gtk/src/view_models/game_view_model.rs index bd1cde5..e8ac87a 100644 --- a/kifu/gtk/src/view_models/game_view_model.rs +++ b/kifu/gtk/src/view_models/game_view_model.rs @@ -22,13 +22,20 @@ use std::{ time::Duration, }; +pub struct GameState { + goban: Goban, + white_clock: Duration, + black_clock: Duration, + white_score: f32, + black_score: f32, + current: Color, +} + struct GameViewModelPrivate { white: Player, /* Maybe this should be PlayerState, instead, combining the player info, current clock, and current captures. */ black: Player, - current: Color, - goban: Goban, /* Or perhaps clocks, captures, and the board should be bound into GameState. */ - white_clock: Duration, - black_clock: Duration, + + state: GameState, } /// The Game View Model manages the current state of the game. It shows the two player cards, the board, the current capture count, the current player, and it maintains the UI for the clock (bearing in mind that the real clock is managed in the core). This view model should only be created once the details of the game, whether a game in progress or a new game (this view model won't know the difference) is known. @@ -44,32 +51,24 @@ impl GameViewModelPrivate { } impl GameViewModel { - pub fn new( - white: Player, - black: Player, - current: Color, - goban: Goban, - white_clock: Duration, - black_clock: Duration, - core: Core, - notifications: Receiver, - ) -> Self { + pub fn new(white: Player, black: Player, game: GameState, core: Core) -> Self { let data = Arc::new(RwLock::new(GameViewModelPrivate { white, black, - current, - goban, - white_clock, - black_clock, + state: game, })); let handler = spawn({ + let core = core.clone(); let data = data.clone(); async move { + let notifications = core.subscribe(); loop { match notifications.recv().await { Ok(msg) => data.write().unwrap().handle(msg), - Err(err) => unimplemented!("Should display an error message in the UI"), + Err(err) => { + unimplemented!("Should display an error message in the UI: {}", err) + } } yield_now().await; }