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};
|
|
|
|
use std::sync::{Arc, RwLock};
|
2023-03-20 12:16:20 +00:00
|
|
|
|
2023-04-07 01:52:39 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2023-03-20 12:16:20 +00:00
|
|
|
pub enum Request {
|
|
|
|
PlayingField,
|
2023-04-07 01:52:39 +00:00
|
|
|
PlayStoneRequest(PlayStoneRequest),
|
2023-03-20 12:16:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 01:52:39 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
pub struct PlayStoneRequest {
|
2023-04-28 05:21:23 +00:00
|
|
|
pub column: usize,
|
|
|
|
pub row: usize,
|
2023-04-07 01:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2023-03-20 12:16:20 +00:00
|
|
|
pub enum Response {
|
|
|
|
PlayingFieldView(PlayingFieldView),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct CoreApp {
|
|
|
|
state: Arc<RwLock<AppState>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CoreApp {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let state = Arc::new(RwLock::new(AppState::new()));
|
|
|
|
|
|
|
|
Self { state }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn dispatch(&self, request: Request) -> Response {
|
|
|
|
match request {
|
2023-04-07 01:52:39 +00:00
|
|
|
Request::PlayingField => {
|
|
|
|
let app_state = self.state.read().unwrap();
|
|
|
|
let game = app_state.game.as_ref().unwrap();
|
|
|
|
Response::PlayingFieldView(playing_field(game))
|
|
|
|
}
|
|
|
|
Request::PlayStoneRequest(request) => {
|
|
|
|
let mut app_state = self.state.write().unwrap();
|
|
|
|
app_state.place_stone(request);
|
|
|
|
|
|
|
|
let game = app_state.game.as_ref().unwrap();
|
|
|
|
Response::PlayingFieldView(playing_field(game))
|
|
|
|
}
|
2023-03-20 12:16:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(&self) {}
|
|
|
|
}
|