Skip to content

Commit 0834ec9

Browse files
authored
game api for wasm (#54)
1 parent 7ebf0d7 commit 0834ec9

File tree

4 files changed

+81
-18
lines changed

4 files changed

+81
-18
lines changed

README.md

+62-6
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,71 @@
66

77
### Run native client
88

9-
In the `client` directory, run `cargo run`.
9+
- `cd client`
10+
- `cargo run`.
1011

1112
### Run local web client
1213

13-
`./wasm-bindgen-macroquad.sh local_client`
14-
In the `client` directory, run `basic-http-server .` in `dist/` and open `http://localhost:4000` in a browser.
14+
- `cd client`
15+
- `./wasm-bindgen-macroquad.sh local_client`
16+
In the `client` directory, run `basic-http-server .` in `dist/` and open `http://localhost:4000` in a browser.
17+
18+
### Server wrapper
19+
20+
- `cd server`
21+
- `cargo install wasm-pack` (if you haven't already)
22+
- `./build-wasm.sh`
1523

1624
# Notes
1725

18-
- For random, use https://github.com/not-fl3/quad-rand
19-
- boardgamers-mono
20-
- https://github.com/boardgamers/boardgamers-mono/blob/683f4d473586ffe359ad7e58f7bf08d95c96d821/.gitpod.yml#L12-L18
26+
- https://stackoverflow.com/questions/40102686/how-to-install-package-with-local-path-by-yarn-it-couldnt-find-package
27+
28+
## Boardgamers Mono
29+
30+
- `docker run -d -p 27017:27017 mongo:4.4`
31+
- `cd apps/api && pnpm seed && echo cron=1 > .env`
32+
- `pnpm dev --filter @bgs/api --filter @bgs/game-server --filter @bgs/web --filter @bgs/admin`
33+
- admin: http://localhost:3000 ([email protected]/password)
34+
- user: http://localhost:8612/ ([email protected]/password)
35+
36+
old
37+
38+
- https://github.com/boardgamers/boardgamers-mono/blob/683f4d473586ffe359ad7e58f7bf08d95c96d821/.gitpod.yml#L12-L18 (if
39+
you have't already)
40+
- This will create three users, with emails [email protected], [email protected] and [email protected], and "password"
41+
password".
42+
43+
.tool-versions
44+
45+
```
46+
nodejs 14.21.3
47+
pnpm 6.14.1
48+
```
49+
50+
### Bypass npm publish
51+
52+
```
53+
Index: apps/game-server/app/services/engines.ts
54+
IDEA additional info:
55+
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
56+
<+>UTF-8
57+
===================================================================
58+
diff --git a/apps/game-server/app/services/engines.ts b/apps/game-server/app/services/engines.ts
59+
--- a/apps/game-server/app/services/engines.ts (revision 741eecd403ed7c5b38b3b98b1e26be8a502cafc0)
60+
+++ b/apps/game-server/app/services/engines.ts (date 1726905060117)
61+
@@ -7,9 +7,7 @@
62+
const engines = {};
63+
64+
async function requirePath(name: string, version: number) {
65+
- const entryPoint = (await GameInfo.findById({ game: name, version }, "engine.entryPoint", { lean: true })).engine
66+
- .entryPoint;
67+
- return `../../games/node_modules/${name}_${version}/${entryPoint}`;
68+
+ return `/home/gregor/source/clash/server/pkg`;
69+
}
70+
71+
export async function getEngine(name: string, version: number): Promise<Engine> {
72+
```
73+
74+
## Docs
75+
76+
- [Game API](https://docs.boardgamers.space/guide/engine-api.html)

server/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77

88
[lib]
99
name = "server"
10-
crate-type = ["lib"]
10+
crate-type = ["cdylib", "rlib"]
1111

1212
[dependencies]
1313
hex2d = "1.1.0"

server/build-wasm.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
wasm-pack build --target nodejs
6+
7+
echo "Done!"

server/src/game_api_wrapper.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub async fn init(
3434
from_game(game)
3535
}
3636

37-
#[wasm_bindgen]
37+
#[wasm_bindgen(js_name = move)]
3838
pub fn execute_move(game: JsValue, move_data: JsValue, player: JsValue) -> JsValue {
3939
let game = get_game(game);
4040
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 {
5656
serde_wasm_bindgen::to_value(&scores).expect("scores should be serializable")
5757
}
5858

59-
#[wasm_bindgen]
59+
#[wasm_bindgen(js_name = "dropPlayer")]
6060
pub async fn drop_player(game: JsValue, player: JsValue) -> JsValue {
6161
let game = get_game(game);
6262
let player_index = player.as_f64().expect("player index should be a number") as usize;
6363
let game = game_api::drop_player(game, player_index);
6464
from_game(game)
6565
}
6666

67-
#[wasm_bindgen]
68-
pub async fn current_player(game: JsValue) -> JsValue {
67+
#[wasm_bindgen(js_name = "currentPlayer")]
68+
pub fn current_player(game: JsValue) -> JsValue {
6969
let game = get_game(game);
7070
let player_index = game_api::current_player(&game);
7171
match player_index {
@@ -74,22 +74,22 @@ pub async fn current_player(game: JsValue) -> JsValue {
7474
}
7575
}
7676

77-
#[wasm_bindgen]
78-
pub async fn log_length(game: JsValue) -> JsValue {
77+
#[wasm_bindgen(js_name = "logLength")]
78+
pub fn log_length(game: JsValue) -> JsValue {
7979
let game = get_game(game);
8080
let log_length = game_api::log_length(&game);
8181
JsValue::from_f64(log_length as f64)
8282
}
8383

84-
#[wasm_bindgen]
85-
pub async fn log_slice(game: JsValue, options: JsValue) -> JsValue {
84+
#[wasm_bindgen(js_name = "logSlice")]
85+
pub fn log_slice(game: JsValue, options: JsValue) -> JsValue {
8686
let game = get_game(game);
8787
let options = serde_wasm_bindgen::from_value(options).expect("options should be serializable");
8888
let log = game_api::log_slice(&game, &options);
8989
serde_wasm_bindgen::to_value(&log).expect("log should be serializable")
9090
}
9191

92-
#[wasm_bindgen]
92+
#[wasm_bindgen(js_name = "setPlayerMetaData")]
9393
pub fn set_player_meta_data(game: JsValue, player_index: JsValue, meta_data: JsValue) -> JsValue {
9494
let game = get_game(game);
9595
let player_index = player_index
@@ -109,7 +109,7 @@ pub fn rankings(game: JsValue) -> JsValue {
109109
serde_wasm_bindgen::to_value(&rankings).expect("rankings should be serializable")
110110
}
111111

112-
#[wasm_bindgen]
112+
#[wasm_bindgen(js_name = "roundNumber")]
113113
pub fn round_number(game: JsValue) -> JsValue {
114114
let game = get_game(game);
115115
let round = game_api::round(&game);
@@ -126,7 +126,7 @@ pub fn factions(game: JsValue) -> JsValue {
126126
serde_wasm_bindgen::to_value(&factions).expect("faction list should be serializable")
127127
}
128128

129-
#[wasm_bindgen]
129+
#[wasm_bindgen(js_name = "stripSecret")]
130130
pub fn strip_secret(game: JsValue, player: JsValue) -> JsValue {
131131
let game = get_game(game);
132132
let player_index = player.as_f64().map(|player_index| player_index as usize);

0 commit comments

Comments
 (0)