Skip to content

Commit 4225416

Browse files
Alpha SDK and example for Node.js (Player tracking) (agones-dev#1658)
* update dependencies in sdk and example * update build and generate pb * add alpha sdk * export sdk and alpha sdk * cleanup nodejs-simple * add nodejs-alpha example Co-authored-by: Mark Mandel <markmandel@google.com>
1 parent 0a40ebe commit 4225416

18 files changed

Lines changed: 2634 additions & 115 deletions

File tree

‎build/build-sdk-images/node/gen.sh‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,20 @@ header() {
2323
sdk=/go/src/agones.dev/agones/proto/sdk
2424
googleapis=/go/src/agones.dev/agones/proto/googleapis
2525

26+
mkdir -p ./sdks/nodejs/lib/alpha
27+
2628
cd /go/src/agones.dev/agones
2729

2830
grpc_tools_node_protoc --proto_path=${googleapis} --proto_path=${sdk} --js_out=import_style=commonjs,binary:./sdks/nodejs/lib google/api/annotations.proto google/api/http.proto
31+
grpc_tools_node_protoc --proto_path=${googleapis} --proto_path=${sdk}/alpha --js_out=import_style=commonjs,binary:./sdks/nodejs/lib/alpha google/api/annotations.proto google/api/http.proto
32+
2933
grpc_tools_node_protoc --proto_path=${googleapis} --proto_path=${sdk} --grpc_out=generate_package_definition:./sdks/nodejs/lib --js_out=import_style=commonjs,binary:./sdks/nodejs/lib sdk.proto
34+
grpc_tools_node_protoc --proto_path=${googleapis} --proto_path=${sdk}/alpha --grpc_out=generate_package_definition:./sdks/nodejs/lib/alpha --js_out=import_style=commonjs,binary:./sdks/nodejs/lib/alpha alpha.proto
3035

3136
header ./sdks/nodejs/lib/sdk_pb.js
3237
header ./sdks/nodejs/lib/google/api/annotations_pb.js
3338
header ./sdks/nodejs/lib/google/api/http_pb.js
39+
40+
header ./sdks/nodejs/lib/alpha/alpha_pb.js
41+
header ./sdks/nodejs/lib/alpha/google/api/annotations_pb.js
42+
header ./sdks/nodejs/lib/alpha/google/api/http_pb.js

‎examples/nodejs-simple/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ root_path = $(realpath $(project_path)/../..)
3838
# |_|\__,_|_| \__, |\___|\__|___/
3939
# |___/
4040

41-
# build the cpp-simple binary
41+
# build the nodejs-simple binary
4242
build:
4343
cd $(root_path) && docker build -f $(project_path)/Dockerfile --tag=$(server_tag) .
4444

‎examples/nodejs-simple/README.md‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ It will:
1212
- After the shutdown duration (default 60 seconds), shut the server down
1313
- Parse options to get help or set the shutdown timeout duration
1414

15+
If alpha features are enabled it will additionally:
16+
- Set and get the player capacity (this is not enforced)
17+
- Add, get and remove players, and test if they are present
18+
1519
To learn how to deploy this example service to GKE, please see the tutorial [Build and Run a Simple Gameserver (node.js)](https://agones.dev/site/docs/tutorials/simple-gameserver-nodejs/).
1620

1721
## Building
@@ -56,7 +60,7 @@ Connecting to the SDK server...
5660
...connected to SDK server
5761
```
5862

59-
To see help, pass `--help` as the argument (all are equivalent):
63+
To see help, pass `--help` as the argument (use the preferred command below, all are equivalent):
6064
```
6165
$ make args="--help" run
6266
$ docker run --network=host gcr.io/agones-images/nodejs-simple-server:0.5 --help
@@ -76,4 +80,16 @@ To make run indefinitely use the special timeout value of 0:
7680
$ make args="--timeout=0" run
7781
$ docker run --network=host gcr.io/agones-images/nodejs-simple-server:0.5 --timeout=0
7882
$ npm start -- --timeout=0
79-
```
83+
```
84+
85+
To enable alpha features ensure the feature gate is enabled:
86+
```bash
87+
$ cd ../../build; make run-sdk-conformance-local TIMEOUT=120 FEATURE_GATES="PlayerTracking=true" TESTS=ready,watch,health,gameserver
88+
```
89+
90+
Then enable the alpha suite:
91+
```
92+
$ make args="--alpha" run
93+
$ docker run --network=host gcr.io/agones-images/nodejs-simple-server:0.5 --alpha
94+
$ npm start -- --alpha
95+
```

‎examples/nodejs-simple/package-lock.json‎

Lines changed: 113 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/nodejs-simple/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"dependencies": {
3-
"@google-cloud/agones-sdk": "1.6.0"
3+
"@google-cloud/agones-sdk": "file:../../sdks/nodejs"
44
},
5+
"description": "replace @google-cloud/agones-sdk dependency with the latest version once published",
56
"scripts": {
67
"start": "node src/index.js"
78
}

‎examples/nodejs-simple/src/index.js‎

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ const util = require('util');
1717

1818
const sleep = util.promisify(setTimeout);
1919

20-
const agonesSDK = new AgonesSDK();
21-
2220
const DEFAULT_TIMEOUT = 60;
2321
const MAX_TIMEOUT = 2147483;
2422

25-
const connect = async (timeout) => {
23+
const connect = async (timeout, enableAlpha) => {
24+
let agonesSDK = new AgonesSDK();
25+
2626
let lifetimeInterval;
2727
let healthInterval;
2828

@@ -41,7 +41,16 @@ const connect = async (timeout) => {
4141
console.log('Health ping sent');
4242
}, 20000);
4343
agonesSDK.watchGameServer((result) => {
44-
console.log(`GameServer Update:\n\tname: ${result.objectMeta.name}\n\tstate: ${result.status.state}\n\tlabels: ${result.objectMeta.labelsMap.join(' & ')}\n\tannotations: ${result.objectMeta.annotationsMap.join(' & ')}`);
44+
let output = `GameServer Update:
45+
name: ${result.objectMeta.name}
46+
state: ${result.status.state}
47+
labels: ${result.objectMeta.labelsMap.join(' & ')}
48+
annotations: ${result.objectMeta.annotationsMap.join(' & ')}`;
49+
if (enableAlpha) {
50+
output += `
51+
players: ${result.status.players.count}/${result.status.players.capacity} [${result.status.players.idsList}]`;
52+
}
53+
console.log(output);
4554
});
4655

4756
await sleep(10000);
@@ -65,6 +74,11 @@ const connect = async (timeout) => {
6574
await agonesSDK.reserve(10);
6675
await sleep(20000);
6776

77+
if (enableAlpha) {
78+
console.log('Running alpha suite');
79+
await runAlphaSuite(agonesSDK);
80+
}
81+
6882
if (timeout === 0) {
6983
do {
7084
await sleep(MAX_TIMEOUT);
@@ -91,16 +105,72 @@ const connect = async (timeout) => {
91105
console.error(error);
92106
clearInterval(healthInterval);
93107
clearInterval(lifetimeInterval);
108+
process.exit(0);
94109
}
95110
};
96111

112+
const runAlphaSuite = async (agonesSDK) => {
113+
await sleep(10000);
114+
console.log('Setting capacity');
115+
await agonesSDK.alpha.setPlayerCapacity(64);
116+
117+
await sleep(10000);
118+
console.log('Getting capacity');
119+
let result = await agonesSDK.alpha.getPlayerCapacity();
120+
console.log(`result: ${result}`);
121+
122+
await sleep(10000);
123+
console.log('Connecting a player');
124+
result = await agonesSDK.alpha.playerConnect('firstPlayerID');
125+
console.log(`result: ${result}`);
126+
127+
await sleep(10000);
128+
console.log('Connecting a duplicate player');
129+
result = await agonesSDK.alpha.playerConnect('firstPlayerID');
130+
console.log(`result: ${result}`);
131+
132+
await sleep(10000);
133+
console.log('Connecting another player');
134+
await agonesSDK.alpha.playerConnect('secondPlayerID');
135+
136+
await sleep(10000);
137+
console.log('Getting player count');
138+
result = await agonesSDK.alpha.getPlayerCount();
139+
console.log(`result: ${result}`);
140+
141+
await sleep(10000);
142+
console.log('Finding if firstPlayerID connected');
143+
result = await agonesSDK.alpha.isPlayerConnected('firstPlayerID');
144+
console.log(`result: ${result}`);
145+
146+
await sleep(10000);
147+
console.log('Getting connected players');
148+
result = await agonesSDK.alpha.getConnectedPlayers();
149+
console.log(`result: ${result}`);
150+
151+
await sleep(10000);
152+
console.log('Disconnecting a player');
153+
result = await agonesSDK.alpha.playerDisconnect('firstPlayerID');
154+
console.log(`result: ${result}`);
155+
156+
await sleep(10000);
157+
console.log('Disconnecting the same player');
158+
result = await agonesSDK.alpha.playerDisconnect('firstPlayerID');
159+
console.log(`result: ${result}`);
160+
};
161+
97162
let args = process.argv.slice(2);
98163
let timeout = DEFAULT_TIMEOUT;
164+
let enableAlpha = false;
99165

100166
for (let arg of args) {
101167
let [argName, argValue] = arg.split('=');
102168
if (argName === '--help') {
103-
console.log(`Example to call each SDK feature in turn. Once complete will call shutdown and close after a default timeout of ${DEFAULT_TIMEOUT} seconds.\n\nOptions:\n\t--timeout=...\t\tshutdown timeout in seconds. Use 0 to never shut down`);
169+
console.log(`Example to call each SDK feature in turn. Once complete will call shutdown and close after a default timeout of ${DEFAULT_TIMEOUT} seconds.
170+
171+
Options:
172+
--timeout=...\t\tshutdown timeout in seconds. Use 0 to never shut down
173+
--alpha\t\t\tenable alpha features`);
104174
return;
105175
}
106176
if (argName === '--timeout') {
@@ -116,6 +186,11 @@ for (let arg of args) {
116186
console.log(`Using shutdown timeout of ${timeout} seconds`);
117187
}
118188
}
189+
190+
if (argName === '--alpha') {
191+
console.log('Enabling alpha features!');
192+
enableAlpha = true;
193+
}
119194
}
120195

121-
connect(timeout);
196+
connect(timeout, enableAlpha);

‎sdks/nodejs/.eslintrc.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"ecmaVersion": 2018
1010
},
1111
"rules": {
12+
"indent": ["error", "tab"],
1213
"lines-between-class-members": "error",
14+
"quotes": ["error", "single"],
1315
"semi": "error",
1416
"space-before-blocks": "error"
1517
}

0 commit comments

Comments
 (0)