game api for wasm (#54)

This commit is contained in:
Gregor Zeitlinger 2024-09-27 08:58:25 +02:00 • committed by GitHub
commit 0834ec9f3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 81 additions and 18 deletions

View file

@ -6,15 +6,71 @@
### Run native client
In the `client` directory, run `cargo run`.
- `cd client`
- `cargo run`.
### Run local web client
`./wasm-bindgen-macroquad.sh local_client`
In the `client` directory, run `basic-http-server .` in `dist/` and open `http://localhost:4000` in a browser.
- `cd client`
- `./wasm-bindgen-macroquad.sh local_client`
In the `client` directory, run `basic-http-server .` in `dist/` and open `http://localhost:4000` in a browser.
### Server wrapper
- `cd server`
- `cargo install wasm-pack` (if you haven't already)
- `./build-wasm.sh`
# Notes
- For random, use https://github.com/not-fl3/quad-rand
- boardgamers-mono
- https://github.com/boardgamers/boardgamers-mono/blob/683f4d473586ffe359ad7e58f7bf08d95c96d821/.gitpod.yml#L12-L18
- https://stackoverflow.com/questions/40102686/how-to-install-package-with-local-path-by-yarn-it-couldnt-find-package
## Boardgamers Mono
- `docker run -d -p 27017:27017 mongo:4.4`
- `cd apps/api && pnpm seed && echo cron=1 > .env`
- `pnpm dev --filter @bgs/api --filter @bgs/game-server --filter @bgs/web --filter @bgs/admin`
- admin: http://localhost:3000 (admin@test.com/password)
- user: http://localhost:8612/ (user@test.com/password)
old
- https://github.com/boardgamers/boardgamers-mono/blob/683f4d473586ffe359ad7e58f7bf08d95c96d821/.gitpod.yml#L12-L18 (if
you have't already)
- This will create three users, with emails admin@test.com, user@test.com and user2@test.com, and "password"
password".
.tool-versions
```
nodejs 14.21.3
pnpm 6.14.1
```
### Bypass npm publish
```
Index: apps/game-server/app/services/engines.ts
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/apps/game-server/app/services/engines.ts b/apps/game-server/app/services/engines.ts
--- a/apps/game-server/app/services/engines.ts (revision 741eecd403ed7c5b38b3b98b1e26be8a502cafc0)
+++ b/apps/game-server/app/services/engines.ts (date 1726905060117)
@@ -7,9 +7,7 @@
const engines = {};
async function requirePath(name: string, version: number) {
- const entryPoint = (await GameInfo.findById({ game: name, version }, "engine.entryPoint", { lean: true })).engine
- .entryPoint;
- return `../../games/node_modules/${name}_${version}/${entryPoint}`;
+ return `/home/gregor/source/clash/server/pkg`;
}
export async function getEngine(name: string, version: number): Promise<Engine> {
```
## Docs
- [Game API](https://docs.boardgamers.space/guide/engine-api.html)

View file

@ -7,7 +7,7 @@ edition = "2021"
[lib]
name = "server"
crate-type = ["lib"]
crate-type = ["cdylib", "rlib"]
[dependencies]
hex2d = "1.1.0"

7
server/build-wasm.sh Executable file
View file

@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -e
wasm-pack build --target nodejs
echo "Done!"

View file

@ -34,7 +34,7 @@ pub async fn init(
from_game(game)
}
#[wasm_bindgen]
#[wasm_bindgen(js_name = move)]
pub fn execute_move(game: JsValue, move_data: JsValue, player: JsValue) -> JsValue {
let game = get_game(game);
let action = serde_wasm_bindgen::from_value(move_data).expect("move should be of type action");
@ -56,7 +56,7 @@ pub fn scores(game: JsValue) -> JsValue {
serde_wasm_bindgen::to_value(&scores).expect("scores should be serializable")
}
#[wasm_bindgen]
#[wasm_bindgen(js_name = "dropPlayer")]
pub async fn drop_player(game: JsValue, player: JsValue) -> JsValue {
let game = get_game(game);
let player_index = player.as_f64().expect("player index should be a number") as usize;
@ -64,8 +64,8 @@ pub async fn drop_player(game: JsValue, player: JsValue) -> JsValue {
from_game(game)
}
#[wasm_bindgen]
pub async fn current_player(game: JsValue) -> JsValue {
#[wasm_bindgen(js_name = "currentPlayer")]
pub fn current_player(game: JsValue) -> JsValue {
let game = get_game(game);
let player_index = game_api::current_player(&game);
match player_index {
@ -74,22 +74,22 @@ pub async fn current_player(game: JsValue) -> JsValue {
}
}
#[wasm_bindgen]
pub async fn log_length(game: JsValue) -> JsValue {
#[wasm_bindgen(js_name = "logLength")]
pub fn log_length(game: JsValue) -> JsValue {
let game = get_game(game);
let log_length = game_api::log_length(&game);
JsValue::from_f64(log_length as f64)
}
#[wasm_bindgen]
pub async fn log_slice(game: JsValue, options: JsValue) -> JsValue {
#[wasm_bindgen(js_name = "logSlice")]
pub fn log_slice(game: JsValue, options: JsValue) -> JsValue {
let game = get_game(game);
let options = serde_wasm_bindgen::from_value(options).expect("options should be serializable");
let log = game_api::log_slice(&game, &options);
serde_wasm_bindgen::to_value(&log).expect("log should be serializable")
}
#[wasm_bindgen]
#[wasm_bindgen(js_name = "setPlayerMetaData")]
pub fn set_player_meta_data(game: JsValue, player_index: JsValue, meta_data: JsValue) -> JsValue {
let game = get_game(game);
let player_index = player_index
@ -109,7 +109,7 @@ pub fn rankings(game: JsValue) -> JsValue {
serde_wasm_bindgen::to_value(&rankings).expect("rankings should be serializable")
}
#[wasm_bindgen]
#[wasm_bindgen(js_name = "roundNumber")]
pub fn round_number(game: JsValue) -> JsValue {
let game = get_game(game);
let round = game_api::round(&game);
@ -126,7 +126,7 @@ pub fn factions(game: JsValue) -> JsValue {
serde_wasm_bindgen::to_value(&factions).expect("faction list should be serializable")
}
#[wasm_bindgen]
#[wasm_bindgen(js_name = "stripSecret")]
pub fn strip_secret(game: JsValue, player: JsValue) -> JsValue {
let game = get_game(game);
let player_index = player.as_f64().map(|player_index| player_index as usize);