Update the build environment and some architectural elements of the Kifu app #210

Merged
savanni merged 13 commits from kifu/flake into main 2024-02-28 04:42:58 +00:00
3 changed files with 30 additions and 24 deletions
Showing only changes of commit 4674a7d1d6 - Show all commits

View File

@ -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<HotseatPlayerRequest> 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<RwLock<Config>>,
// state: Arc<RwLock<AppState>>,
database: Arc<RwLock<Option<Database>>>,
subscribers: Arc<RwLock<Vec<Sender<CoreNotification>>>>,
}
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<CoreNotification> {
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 {

View File

@ -29,7 +29,6 @@ impl StoneElement {
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "content")]
pub enum IntersectionElement {
Unplayable,
Empty(CoreRequest),

View File

@ -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<CoreNotification>,
) -> 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;
}