monorepo/kifu/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")]
pub enum Request {
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 {
pub column: usize,
pub row: usize,
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")]
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))
}
}
}
pub async fn run(&self) {}
}