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

Alpha SDK and example for Node.js (Player tracking) #1658

Merged
merged 19 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions build/build-sdk-images/node/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@ header() {
sdk=/go/src/agones.dev/agones/proto/sdk
googleapis=/go/src/agones.dev/agones/proto/googleapis

mkdir -p ./sdks/nodejs/lib/alpha

cd /go/src/agones.dev/agones

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
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

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
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

header ./sdks/nodejs/lib/sdk_pb.js
header ./sdks/nodejs/lib/google/api/annotations_pb.js
header ./sdks/nodejs/lib/google/api/http_pb.js

header ./sdks/nodejs/lib/alpha/alpha_pb.js
header ./sdks/nodejs/lib/alpha/google/api/annotations_pb.js
header ./sdks/nodejs/lib/alpha/google/api/http_pb.js
2 changes: 1 addition & 1 deletion examples/nodejs-simple/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ root_path = $(realpath $(project_path)/../..)
# |_|\__,_|_| \__, |\___|\__|___/
# |___/

# build the cpp-simple binary
# build the nodejs-simple binary
markmandel marked this conversation as resolved.
Show resolved Hide resolved
build:
cd $(root_path) && docker build -f $(project_path)/Dockerfile --tag=$(server_tag) .

Expand Down
20 changes: 18 additions & 2 deletions examples/nodejs-simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ It will:
- After the shutdown duration (default 60 seconds), shut the server down
- Parse options to get help or set the shutdown timeout duration

If alpha features are enabled it will additionally:
markmandel marked this conversation as resolved.
Show resolved Hide resolved
- Set and get the player capacity (this is not enforced)
- Add, get and remove players, and test if they are present

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/).

## Building
Expand Down Expand Up @@ -56,7 +60,7 @@ Connecting to the SDK server...
...connected to SDK server
```

To see help, pass `--help` as the argument (all are equivalent):
To see help, pass `--help` as the argument (use the preferred command below, all are equivalent):
```
$ make args="--help" run
$ docker run --network=host gcr.io/agones-images/nodejs-simple-server:0.5 --help
Expand All @@ -76,4 +80,16 @@ To make run indefinitely use the special timeout value of 0:
$ make args="--timeout=0" run
$ docker run --network=host gcr.io/agones-images/nodejs-simple-server:0.5 --timeout=0
$ npm start -- --timeout=0
```
```

To enable alpha features ensure the feature gate is enabled:
```bash
$ cd ../../build; make run-sdk-conformance-local TIMEOUT=120 FEATURE_GATES="PlayerTracking=true" TESTS=ready,watch,health,gameserver
```

Then enable the alpha suite:
```
$ make args="--alpha" run
$ docker run --network=host gcr.io/agones-images/nodejs-simple-server:0.5 --alpha
$ npm start -- --alpha
```
137 changes: 113 additions & 24 deletions examples/nodejs-simple/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/nodejs-simple/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"dependencies": {
"@google-cloud/agones-sdk": "1.6.0"
"@google-cloud/agones-sdk": "file:../../sdks/nodejs"
},
"description": "replace @google-cloud/agones-sdk dependency with the latest version once published",
"scripts": {
"start": "node src/index.js"
}
Expand Down
87 changes: 81 additions & 6 deletions examples/nodejs-simple/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ const util = require('util');

const sleep = util.promisify(setTimeout);

const agonesSDK = new AgonesSDK();

const DEFAULT_TIMEOUT = 60;
const MAX_TIMEOUT = 2147483;

const connect = async (timeout) => {
const connect = async (timeout, enableAlpha) => {
let agonesSDK = new AgonesSDK();

let lifetimeInterval;
let healthInterval;

Expand All @@ -41,7 +41,16 @@ const connect = async (timeout) => {
console.log('Health ping sent');
}, 20000);
agonesSDK.watchGameServer((result) => {
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(' & ')}`);
let output = `GameServer Update:
name: ${result.objectMeta.name}
state: ${result.status.state}
labels: ${result.objectMeta.labelsMap.join(' & ')}
annotations: ${result.objectMeta.annotationsMap.join(' & ')}`;
if (enableAlpha) {
output += `
players: ${result.status.players.count}/${result.status.players.capacity} [${result.status.players.idsList}]`;
}
console.log(output);
});

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

if (enableAlpha) {
console.log('Running alpha suite');
await runAlphaSuite(agonesSDK);
}

if (timeout === 0) {
do {
await sleep(MAX_TIMEOUT);
Expand All @@ -91,16 +105,72 @@ const connect = async (timeout) => {
console.error(error);
clearInterval(healthInterval);
clearInterval(lifetimeInterval);
process.exit(0);
}
};

const runAlphaSuite = async (agonesSDK) => {
await sleep(10000);
console.log('Setting capacity');
await agonesSDK.alpha.setPlayerCapacity(64);

await sleep(10000);
console.log('Getting capacity');
let result = await agonesSDK.alpha.getPlayerCapacity();
console.log(`result: ${result}`);

await sleep(10000);
console.log('Connecting a player');
result = await agonesSDK.alpha.playerConnect('firstPlayerID');
console.log(`result: ${result}`);

await sleep(10000);
console.log('Connecting a duplicate player');
result = await agonesSDK.alpha.playerConnect('firstPlayerID');
console.log(`result: ${result}`);

await sleep(10000);
console.log('Connecting another player');
await agonesSDK.alpha.playerConnect('secondPlayerID');

await sleep(10000);
console.log('Getting player count');
result = await agonesSDK.alpha.getPlayerCount();
console.log(`result: ${result}`);

await sleep(10000);
console.log('Finding if firstPlayerID connected');
result = await agonesSDK.alpha.isPlayerConnected('firstPlayerID');
console.log(`result: ${result}`);

await sleep(10000);
console.log('Getting connected players');
result = await agonesSDK.alpha.getConnectedPlayers();
console.log(`result: ${result}`);

await sleep(10000);
console.log('Disconnecting a player');
result = await agonesSDK.alpha.playerDisconnect('firstPlayerID');
console.log(`result: ${result}`);

await sleep(10000);
console.log('Disconnecting the same player');
result = await agonesSDK.alpha.playerDisconnect('firstPlayerID');
console.log(`result: ${result}`);
};

let args = process.argv.slice(2);
let timeout = DEFAULT_TIMEOUT;
let enableAlpha = false;

for (let arg of args) {
let [argName, argValue] = arg.split('=');
if (argName === '--help') {
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`);
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.

Options:
--timeout=...\t\tshutdown timeout in seconds. Use 0 to never shut down
--alpha\t\t\tenable alpha features`);
return;
}
if (argName === '--timeout') {
Expand All @@ -116,6 +186,11 @@ for (let arg of args) {
console.log(`Using shutdown timeout of ${timeout} seconds`);
}
}

if (argName === '--alpha') {
console.log('Enabling alpha features!');
enableAlpha = true;
}
}

connect(timeout);
connect(timeout, enableAlpha);
2 changes: 2 additions & 0 deletions sdks/nodejs/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"ecmaVersion": 2018
},
"rules": {
"indent": ["error", "tab"],
"lines-between-class-members": "error",
"quotes": ["error", "single"],
"semi": "error",
"space-before-blocks": "error"
}
Expand Down
Loading