2023-03-20 12:16:20 +00:00
|
|
|
use crate::types::AppState;
|
2023-03-24 13:59:04 +00:00
|
|
|
use crate::ui::{playing_field, PlayingFieldView};
|
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-04-21 02:50:48 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
#[typeshare]
|
|
|
|
#[serde(tag = "type", content = "content")]
|
2023-05-11 13:39:31 +00:00
|
|
|
pub enum CoreRequest {
|
2023-03-20 12:16:20 +00:00
|
|
|
PlayingField,
|
2023-04-07 01:52:39 +00:00
|
|
|
PlayStoneRequest(PlayStoneRequest),
|
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-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-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 {
|
|
|
|
state: Arc<RwLock<AppState>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CoreApp {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let state = Arc::new(RwLock::new(AppState::new()));
|
|
|
|
|
|
|
|
Self { state }
|
|
|
|
}
|
|
|
|
|
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-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-05-11 13:39:31 +00:00
|
|
|
CoreRequest::PlayStoneRequest(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-03-20 12:16:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(&self) {}
|
|
|
|
}
|