diff --git a/Makefile b/Makefile index 28ac177..1269985 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,7 @@ kifu-pwa: kifu-pwa/dev: pushd kifu/ffi/wasm && make && popd + pushd kifu/core-types && make && popd pushd kifu/kifu-pwa && make dev kifu-pwa/test-server: diff --git a/kifu/core-types/Makefile b/kifu/core-types/Makefile new file mode 100644 index 0000000..d3b1f09 --- /dev/null +++ b/kifu/core-types/Makefile @@ -0,0 +1,6 @@ + +SOURCES = $(shell find ../kifu-core -name "*.rs") +dist/core.d.ts: $(SOURCES) + mkdir -p dist + typeshare ../kifu-core --lang=typescript --output-file=dist/core.d.ts + diff --git a/kifu/core-types/package.json b/kifu/core-types/package.json new file mode 100644 index 0000000..4691e70 --- /dev/null +++ b/kifu/core-types/package.json @@ -0,0 +1,12 @@ +{ + "name": "core-types", + "version": "0.0.1", + "description": "", + "types": "dist/core.d.ts", + "scripts": { + "build": "make", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Savanni D'Gerinel ", + "license": "GPL-3.0-or-later" +} diff --git a/kifu/ffi/wasm/Cargo.lock b/kifu/ffi/wasm/Cargo.lock index ee15236..e2f131e 100644 --- a/kifu/ffi/wasm/Cargo.lock +++ b/kifu/ffi/wasm/Cargo.lock @@ -172,6 +172,8 @@ name = "kifu-wasm" version = "0.1.0" dependencies = [ "kifu-core", + "serde", + "serde-wasm-bindgen", "wasm-bindgen", "wasm-bindgen-futures", ] @@ -270,6 +272,17 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-wasm-bindgen" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3b143e2833c57ab9ad3ea280d21fd34e285a42837aeb0ee301f4f41890fa00e" +dependencies = [ + "js-sys", + "serde", + "wasm-bindgen", +] + [[package]] name = "serde_derive" version = "1.0.160" diff --git a/kifu/ffi/wasm/Cargo.toml b/kifu/ffi/wasm/Cargo.toml index d2407ce..03475b2 100644 --- a/kifu/ffi/wasm/Cargo.toml +++ b/kifu/ffi/wasm/Cargo.toml @@ -9,9 +9,11 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] -kifu-core = { path = "../../kifu-core" } -wasm-bindgen = "0.2" -wasm-bindgen-futures = "*" +kifu-core = { path = "../../kifu-core" } +serde = { version = "1.0", features = ["derive"] } +serde-wasm-bindgen = "0.5.0" +wasm-bindgen = "0.2" +wasm-bindgen-futures = "*" [package.metadata.wasm-pack.profile.release] wasm-opt = false diff --git a/kifu/ffi/wasm/src/lib.rs b/kifu/ffi/wasm/src/lib.rs index 3b14a16..6a457ca 100644 --- a/kifu/ffi/wasm/src/lib.rs +++ b/kifu/ffi/wasm/src/lib.rs @@ -1,4 +1,6 @@ use wasm_bindgen::prelude::*; +// use serde::{Serialize, Deserialize}; +use kifu_core::{CoreRequest, CoreResponse}; #[wasm_bindgen] extern "C" { @@ -8,8 +10,11 @@ extern "C" { /* #[wasm_bindgen] +#[derive(Serialize, Deserialize)] pub struct CoreRequest(kifu_core::Request); +*/ +/* #[wasm_bindgen] impl CoreRequest { #[wasm_bindgen(constructor)] @@ -22,22 +27,26 @@ impl CoreRequest { /* Somehow uncommenting this code actually causes the module to not load. Maybe a name conflict? * Don't know. */ +/* #[wasm_bindgen] +#[derive(Serialize, Deserialize)] pub struct CoreResponse(kifu_core::Response); +*/ #[wasm_bindgen] #[derive(Debug)] -pub struct CoreApp; +pub struct CoreApp(kifu_core::CoreApp); #[wasm_bindgen] impl CoreApp { #[wasm_bindgen(constructor)] pub fn new() -> Self { - Self + Self(kifu_core::CoreApp::new()) } #[wasm_bindgen] - pub async fn dispatch(&self, param: &JsValue) { - log(&format!("disptach! {:?}", param)); + pub async fn dispatch(&self, param: &JsValue) -> JsValue { + let request: CoreRequest = serde_wasm_bindgen::from_value(param.clone()).unwrap(); + serde_wasm_bindgen::to_value(&self.0.dispatch(request).await).unwrap() } } diff --git a/kifu/kifu-core/src/api.rs b/kifu/kifu-core/src/api.rs index d34e05d..8635268 100644 --- a/kifu/kifu-core/src/api.rs +++ b/kifu/kifu-core/src/api.rs @@ -7,7 +7,7 @@ use typeshare::typeshare; #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[typeshare] #[serde(tag = "type", content = "content")] -pub enum Request { +pub enum CoreRequest { PlayingField, PlayStoneRequest(PlayStoneRequest), } @@ -15,14 +15,14 @@ pub enum Request { #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[typeshare] pub struct PlayStoneRequest { - pub column: usize, - pub row: usize, + pub column: u8, + pub row: u8, } #[derive(Clone, Debug, Serialize, Deserialize)] #[typeshare] #[serde(tag = "type", content = "content")] -pub enum Response { +pub enum CoreResponse { PlayingFieldView(PlayingFieldView), } @@ -38,19 +38,19 @@ impl CoreApp { Self { state } } - pub async fn dispatch(&self, request: Request) -> Response { + pub async fn dispatch(&self, request: CoreRequest) -> CoreResponse { match request { - Request::PlayingField => { + CoreRequest::PlayingField => { let app_state = self.state.read().unwrap(); let game = app_state.game.as_ref().unwrap(); - Response::PlayingFieldView(playing_field(game)) + CoreResponse::PlayingFieldView(playing_field(game)) } - Request::PlayStoneRequest(request) => { + CoreRequest::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)) + CoreResponse::PlayingFieldView(playing_field(game)) } } } diff --git a/kifu/kifu-core/src/board.rs b/kifu/kifu-core/src/board.rs index 55a3fc5..8a7b790 100644 --- a/kifu/kifu-core/src/board.rs +++ b/kifu/kifu-core/src/board.rs @@ -73,8 +73,8 @@ impl Board { #[derive(Clone, Copy, Debug, PartialEq, Hash, Eq)] pub struct Coordinate { - pub column: usize, - pub row: usize, + pub column: u8, + pub row: u8, } impl Board { diff --git a/kifu/kifu-core/src/lib.rs b/kifu/kifu-core/src/lib.rs index 8b598bb..bcdd2a0 100644 --- a/kifu/kifu-core/src/lib.rs +++ b/kifu/kifu-core/src/lib.rs @@ -1,5 +1,5 @@ mod api; -pub use api::{CoreApp, Request, Response}; +pub use api::{CoreApp, CoreRequest, CoreResponse}; mod types; pub use types::{BoardError, Color, Size}; diff --git a/kifu/kifu-core/src/types.rs b/kifu/kifu-core/src/types.rs index 129c9ba..3a1b3f3 100644 --- a/kifu/kifu-core/src/types.rs +++ b/kifu/kifu-core/src/types.rs @@ -27,8 +27,8 @@ pub enum Color { #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[typeshare] pub struct Size { - pub width: usize, - pub height: usize, + pub width: u8, + pub height: u8, } impl Default for Size { diff --git a/kifu/kifu-core/src/ui/types.rs b/kifu/kifu-core/src/ui/types.rs index 7f226cc..c12c416 100644 --- a/kifu/kifu-core/src/ui/types.rs +++ b/kifu/kifu-core/src/ui/types.rs @@ -1,6 +1,6 @@ use crate::types::{Color, Size}; use crate::{ - api::{PlayStoneRequest, Request}, + api::{CoreRequest, PlayStoneRequest}, Board, Coordinate, }; use serde::{Deserialize, Serialize}; @@ -36,7 +36,7 @@ impl StoneElement { #[serde(tag = "type", content = "content")] pub enum IntersectionElement { Unplayable, - Empty(Request), + Empty(CoreRequest), Filled(StoneElement), } @@ -53,10 +53,9 @@ impl BoardElement { .map(|row| { (0..size.width) .map(|column| { - IntersectionElement::Empty(Request::PlayStoneRequest(PlayStoneRequest { - column, - row, - })) + IntersectionElement::Empty(CoreRequest::PlayStoneRequest( + PlayStoneRequest { column, row }, + )) }) .collect::>() }) @@ -86,7 +85,7 @@ impl From<&Board> for BoardElement { liberties: None, color, }), - None => IntersectionElement::Empty(Request::PlayStoneRequest( + None => IntersectionElement::Empty(CoreRequest::PlayStoneRequest( PlayStoneRequest { column, row }, )), }) diff --git a/kifu/kifu-pwa/Makefile b/kifu/kifu-pwa/Makefile index 65d915d..251b688 100644 --- a/kifu/kifu-pwa/Makefile +++ b/kifu/kifu-pwa/Makefile @@ -4,3 +4,4 @@ dev: test-server: npx http-server ./dist + diff --git a/kifu/kifu-pwa/core.d.ts b/kifu/kifu-pwa/core.d.ts deleted file mode 100644 index c1236cb..0000000 --- a/kifu/kifu-pwa/core.d.ts +++ /dev/null @@ -1,69 +0,0 @@ -/* - Generated by typeshare 1.5.0 -*/ - -export interface PlayStoneRequest { - column: number; - row: number; -} - -export interface Size { - width: number; - height: number; -} - -export type IntersectionElement = - | { type: "Unplayable", content?: undefined } - | { type: "Empty", content: Request } - | { type: "Filled", content: StoneElement }; - -export interface BoardElement { - size: Size; - spaces: IntersectionElement[]; -} - -export enum Color { - Black = "Black", - White = "White", -} - -export interface PlayerCardElement { - color: Color; - name: string; - rank: string; - clock: string; -} - -export interface ChatElement { - messages: string[]; -} - -export interface TextFieldElement { -} - -export interface PlayingFieldView { - board: BoardElement; - player_card_black: PlayerCardElement; - player_card_white: PlayerCardElement; - chat: ChatElement; - message: TextFieldElement; - current_player: Color; -} - -export interface Jitter { - x: number; - y: number; -} - -export interface StoneElement { - color: Color; - jitter: Jitter; -} - -export type Request = - | { type: "PlayingField", content?: undefined } - | { type: "PlayStoneRequest", content: PlayStoneRequest }; - -export type Response = - | { type: "PlayingFieldView", content: PlayingFieldView }; - diff --git a/kifu/kifu-pwa/src/converter.ts b/kifu/kifu-pwa/src/converter.ts deleted file mode 100644 index 3332d4a..0000000 --- a/kifu/kifu-pwa/src/converter.ts +++ /dev/null @@ -1,52 +0,0 @@ -import init, { CoreApp } from "kifu-wasm"; - -const inputField = document.getElementById("input-temp"); -const fromUnitField = document.getElementById("input-unit"); -const toUnitField = document.getElementById("output-unit"); -const outputField = document.getElementById("output-temp"); -const form = document.getElementById("converter"); - -function convertTemp(value, fromUnit, toUnit) { - if (fromUnit === "c") { - if (toUnit === "f") { - return (value * 9) / 5 + 32; - } else if (toUnit === "k") { - return value + 273.15; - } - return value; - } - if (fromUnit === "f") { - if (toUnit === "c") { - return ((value - 32) * 5) / 9; - } else if (toUnit === "k") { - return ((value + 459.67) * 5) / 9; - } - return value; - } - if (fromUnit === "k") { - if (toUnit === "c") { - return value - 273.15; - } else if (toUnit === "f") { - return (value * 9) / 5 - 459.67; - } - return value; - } - throw new Error("Invalid unit"); -} - -form.addEventListener("input", () => { - const inputTemp = parseFloat(inputField.value); - const fromUnit = fromUnitField.value; - const toUnit = toUnitField.value; - - const outputTemp = convertTemp(inputTemp, fromUnit, toUnit); - outputField.value = - Math.round(outputTemp * 100) / 100 + " " + toUnit.toUpperCase(); -}); - -init().then(async () => { - let app = new CoreApp(); - console.log("app: ", app, CoreApp); - await app.dispatch({ type: "PlayingField" }); - console.log("kifu_wasm successfully initted"); -}); diff --git a/kifu/kifu-pwa/src/coreApi.ts b/kifu/kifu-pwa/src/coreApi.ts new file mode 100644 index 0000000..e5e4feb --- /dev/null +++ b/kifu/kifu-pwa/src/coreApi.ts @@ -0,0 +1,24 @@ +import init, { CoreApp } from "kifu-wasm"; +import { CoreResponse, CoreRequest, PlayingFieldView } from "core-types"; + +export class CoreApi { + core: CoreApp; + + constructor() { + let app = new CoreApp(); + this.core = app; + } + + async dispatch(request: CoreRequest): Promise { + return await this.core.dispatch(request); + } + + async playingField(): Promise { + return (await this.dispatch({ type: "PlayingField" })).content; + } +} + +export const initCore = async (): Promise => { + await init(); + return new CoreApi(); +}; diff --git a/kifu/kifu-pwa/src/main.ts b/kifu/kifu-pwa/src/main.ts index afd966a..3332ebb 100644 --- a/kifu/kifu-pwa/src/main.ts +++ b/kifu/kifu-pwa/src/main.ts @@ -1,10 +1,8 @@ -import init, { CoreApp } from "kifu-wasm"; +import { CoreApi, initCore } from "./coreApi"; const main = async () => { - await init(); - let coreApi = new CoreApp(); - - let response = await coreApi.dispatch({ type: "PlayingField" }); + let coreApi = await initCore(); + let response = await coreApi.playingField(); console.log("playing field response: ", response); }; diff --git a/kifu/kifu-pwa/webpack.config.js b/kifu/kifu-pwa/webpack.config.js index 8fb82a3..63f2c9c 100644 --- a/kifu/kifu-pwa/webpack.config.js +++ b/kifu/kifu-pwa/webpack.config.js @@ -6,9 +6,9 @@ module.exports = { "kifu-bundle": "./src/main.ts", sw: "./src/sw.js", }, - loader: { + module: { rules: [ - { test: /.ts$/, use: "ts-loader", exclude: /node_modules/ }, + { test: /\.ts$/, use: "ts-loader", exclude: /node_modules/ }, { test: /\.wasm$/, type: "asset/inline" } ], }, @@ -19,5 +19,8 @@ module.exports = { { from: "src/converter.css" } ] }) - ] + ], + resolve: { + extensions: ['.ts'], + } } diff --git a/package-lock.json b/package-lock.json index 0afbd30..9aac826 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,10 +7,16 @@ "name": "tools", "workspaces": [ "kifu/ffi/wasm/pkg", + "kifu/core-types", "kifu/kifu-pwa" ] }, + "kifu/core-types": { + "version": "0.0.1", + "license": "GPL-3.0-or-later" + }, "kifu/ffi/wasm/pkg": { + "name": "kifu-wasm", "version": "0.1.0" }, "kifu/kifu-pwa": { @@ -616,6 +622,10 @@ "webpack": "^5.1.0" } }, + "node_modules/core-types": { + "resolved": "kifu/core-types", + "link": true + }, "node_modules/cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", diff --git a/package.json b/package.json index 95dab61..9ce3a4d 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "tools", "workspaces": [ "kifu/ffi/wasm/pkg", + "kifu/core-types", "kifu/kifu-pwa" ] }