2023-06-15 03:49:03 +00:00
|
|
|
use crate::{
|
2023-08-18 02:54:39 +00:00
|
|
|
types::{AppState, Config, DatabasePath, GameState, Player, Rank},
|
2023-07-24 23:43:22 +00:00
|
|
|
ui::{home, playing_field, HomeView, PlayingFieldView},
|
2023-06-15 03:49:03 +00:00
|
|
|
};
|
2023-04-21 02:50:48 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-03-24 13:59:04 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2023-04-21 02:50:48 +00:00
|
|
|
use typeshare::typeshare;
|
2023-03-20 12:16:20 +00:00
|
|
|
|
2023-06-15 03:49:03 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
2023-04-21 02:50:48 +00:00
|
|
|
#[typeshare]
|
|
|
|
#[serde(tag = "type", content = "content")]
|
2023-05-11 13:39:31 +00:00
|
|
|
pub enum CoreRequest {
|
2023-06-15 03:49:03 +00:00
|
|
|
CreateGame(CreateGameRequest),
|
2023-07-24 23:43:22 +00:00
|
|
|
Home,
|
2023-03-20 12:16:20 +00:00
|
|
|
PlayingField,
|
2023-06-15 03:49:03 +00:00
|
|
|
PlayStone(PlayStoneRequest),
|
2023-05-26 04:16:40 +00:00
|
|
|
StartGame,
|
2023-03-20 12:16:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-21 02:50:48 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
#[typeshare]
|
2023-04-07 01:52:39 +00:00
|
|
|
pub struct PlayStoneRequest {
|
2023-05-11 13:39:31 +00:00
|
|
|
pub column: u8,
|
|
|
|
pub row: u8,
|
2023-04-07 01:52:39 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 03:49:03 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
#[typeshare]
|
|
|
|
pub struct CreateGameRequest {
|
2023-06-15 04:09:50 +00:00
|
|
|
pub black_player: PlayerInfoRequest,
|
|
|
|
pub white_player: PlayerInfoRequest,
|
2023-06-15 03:49:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
#[typeshare]
|
|
|
|
pub enum PlayerInfoRequest {
|
|
|
|
Hotseat(HotseatPlayerRequest),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
#[typeshare]
|
|
|
|
pub struct HotseatPlayerRequest {
|
2023-06-16 02:20:59 +00:00
|
|
|
pub name: String,
|
|
|
|
pub rank: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<HotseatPlayerRequest> for Player {
|
|
|
|
fn from(p: HotseatPlayerRequest) -> Self {
|
|
|
|
Self {
|
|
|
|
name: p.name,
|
|
|
|
rank: p.rank.and_then(|r| Rank::try_from(r.as_ref()).ok()),
|
|
|
|
}
|
|
|
|
}
|
2023-06-15 03:49:03 +00:00
|
|
|
}
|
|
|
|
|
2023-04-21 02:50:48 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
#[typeshare]
|
|
|
|
#[serde(tag = "type", content = "content")]
|
2023-05-11 13:39:31 +00:00
|
|
|
pub enum CoreResponse {
|
2023-07-24 23:43:22 +00:00
|
|
|
HomeView(HomeView),
|
2023-03-20 12:16:20 +00:00
|
|
|
PlayingFieldView(PlayingFieldView),
|
|
|
|
}
|
|
|
|
|
2023-04-21 13:25:21 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2023-03-20 12:16:20 +00:00
|
|
|
pub struct CoreApp {
|
2023-07-20 03:57:19 +00:00
|
|
|
config: Config,
|
2023-03-20 12:16:20 +00:00
|
|
|
state: Arc<RwLock<AppState>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CoreApp {
|
2023-07-20 03:57:19 +00:00
|
|
|
pub fn new(config_path: std::path::PathBuf) -> Self {
|
|
|
|
let config = Config::from_path(config_path).expect("configuration to open");
|
2023-07-26 02:49:43 +00:00
|
|
|
|
2023-08-18 02:54:39 +00:00
|
|
|
let db_path: DatabasePath = config.get().unwrap();
|
2023-07-26 02:49:43 +00:00
|
|
|
let state = Arc::new(RwLock::new(AppState::new(db_path)));
|
2023-03-20 12:16:20 +00:00
|
|
|
|
2023-07-20 03:57:19 +00:00
|
|
|
Self { config, state }
|
2023-03-20 12:16:20 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 13:39:31 +00:00
|
|
|
pub async fn dispatch(&self, request: CoreRequest) -> CoreResponse {
|
2023-03-20 12:16:20 +00:00
|
|
|
match request {
|
2023-05-26 04:16:40 +00:00
|
|
|
/*
|
|
|
|
CoreRequest::LaunchScreen => {
|
|
|
|
let app_state = self.state.read().unwrap();
|
|
|
|
|
|
|
|
At launch, I want to either show a list of games in progress, the current game, or the game creation screen.
|
|
|
|
- if a live game is in progress, immmediately go to that game. Such a game will be classified at game creation, so it should be persisted to the state.
|
|
|
|
- if no live games are in progress, but there are slow games in progress, show a list of the slow games and let the player choose which one to jump into.
|
|
|
|
- if no games are in progress, show only the game creation screen
|
|
|
|
- game creation menu should be present both when there are only slow games and when there are no games
|
|
|
|
- the UI returned here will always be available in other places, such as when the user is viewing a game and wants to return to this page
|
|
|
|
|
|
|
|
For the initial version, I want only to show the game creation screen. Then I will backtrack record application state so that the only decisions can be made.
|
|
|
|
}
|
|
|
|
*/
|
2023-06-16 02:20:59 +00:00
|
|
|
CoreRequest::CreateGame(create_request) => {
|
|
|
|
let mut app_state = self.state.write().unwrap();
|
|
|
|
let white_player = {
|
|
|
|
match create_request.white_player {
|
|
|
|
PlayerInfoRequest::Hotseat(request) => Player::from(request),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let black_player = {
|
|
|
|
match create_request.black_player {
|
|
|
|
PlayerInfoRequest::Hotseat(request) => Player::from(request),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
app_state.game = Some(GameState {
|
|
|
|
white_player,
|
|
|
|
black_player,
|
|
|
|
..GameState::new()
|
|
|
|
});
|
|
|
|
let game_state = app_state.game.as_ref().unwrap();
|
|
|
|
CoreResponse::PlayingFieldView(playing_field(game_state))
|
2023-06-15 03:49:03 +00:00
|
|
|
}
|
2023-07-26 03:27:36 +00:00
|
|
|
CoreRequest::Home => {
|
|
|
|
CoreResponse::HomeView(home(self.state.read().unwrap().database.all_games()))
|
|
|
|
}
|
2023-05-11 13:39:31 +00:00
|
|
|
CoreRequest::PlayingField => {
|
2023-04-07 01:52:39 +00:00
|
|
|
let app_state = self.state.read().unwrap();
|
|
|
|
let game = app_state.game.as_ref().unwrap();
|
2023-05-11 13:39:31 +00:00
|
|
|
CoreResponse::PlayingFieldView(playing_field(game))
|
2023-04-07 01:52:39 +00:00
|
|
|
}
|
2023-06-15 03:49:03 +00:00
|
|
|
CoreRequest::PlayStone(request) => {
|
2023-04-07 01:52:39 +00:00
|
|
|
let mut app_state = self.state.write().unwrap();
|
|
|
|
app_state.place_stone(request);
|
|
|
|
|
|
|
|
let game = app_state.game.as_ref().unwrap();
|
2023-05-11 13:39:31 +00:00
|
|
|
CoreResponse::PlayingFieldView(playing_field(game))
|
2023-04-07 01:52:39 +00:00
|
|
|
}
|
2023-05-26 04:16:40 +00:00
|
|
|
CoreRequest::StartGame => {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2023-03-20 12:16:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(&self) {}
|
|
|
|
}
|