Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

game api for wasm #54

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 62 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ edition = "2021"

[lib]
name = "server"
crate-type = ["lib"]
crate-type = ["cdylib", "rlib"]

[dependencies]
hex2d = "1.1.0"
7 changes: 7 additions & 0 deletions server/build-wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -e

wasm-pack build --target nodejs

echo "Done!"
22 changes: 11 additions & 11 deletions server/src/game_api_wrapper.rs
Original file line number Diff line number Diff line change
@@ -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,16 +56,16 @@ 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;
let game = game_api::drop_player(game, player_index);
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);