monorepo/kifu/core/src/api.rs

60 lines
1.6 KiB
Rust
Raw Normal View History

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-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 {
PlayingField,
2023-04-07 01:52:39 +00:00
PlayStoneRequest(PlayStoneRequest),
}
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 {
PlayingFieldView(PlayingFieldView),
}
2023-04-21 13:25:21 +00:00
#[derive(Clone, Debug)]
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 {
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
}
}
}
pub async fn run(&self) {}
}