agones.dev/agones@v1.53.0/examples/nodejs-simple/src/index.js (about) 1 // Copyright 2019 Google LLC All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 const AgonesSDK = require('@google-cloud/agones-sdk'); 16 const {setTimeout} = require('timers/promises'); 17 18 const DEFAULT_TIMEOUT = 60; 19 const MAX_TIMEOUT = 2147483; 20 21 const connect = async (timeout, enableAlpha, enableBeta) => { 22 let agonesSDK = new AgonesSDK(); 23 24 let lifetimeInterval; 25 let healthInterval; 26 27 try { 28 console.log(`Connecting to the SDK server...`); 29 await agonesSDK.connect(); 30 console.log('...connected to SDK server'); 31 32 let connectTime = Date.now(); 33 lifetimeInterval = setInterval(() => { 34 let lifetimeSeconds = Math.floor((Date.now() - connectTime) / 1000); 35 console.log(`Running for ${lifetimeSeconds} seconds`); 36 }, 10000); 37 healthInterval = setInterval(() => { 38 agonesSDK.health(); 39 console.log('Health ping sent'); 40 }, 20000); 41 agonesSDK.watchGameServer((result) => { 42 let output = `GameServer Update: 43 name: ${result.objectMeta.name} 44 state: ${result.status.state} 45 labels: ${result.objectMeta.labelsMap.join(' & ')} 46 annotations: ${result.objectMeta.annotationsMap.join(' & ')}`; 47 if (enableAlpha) { 48 output += ` 49 players: ${result.status.players.count}/${result.status.players.capacity} [${result.status.players.idsList}]`; 50 } 51 console.log(output); 52 }, (error) => { 53 console.error('Watch ERROR', error); 54 clearInterval(healthInterval); 55 clearInterval(lifetimeInterval); 56 process.exit(0); 57 }); 58 59 await setTimeout(5000); 60 console.log('Setting a label'); 61 await agonesSDK.setLabel('test-label', 'test-value'); 62 63 await setTimeout(5000); 64 console.log('Setting an annotation'); 65 await agonesSDK.setAnnotation('test-annotation', 'test value'); 66 67 await setTimeout(5000); 68 console.log('Marking server as ready...'); 69 await agonesSDK.ready(); 70 71 await setTimeout(5000); 72 console.log('Allocating'); 73 await agonesSDK.allocate(); 74 75 await setTimeout(5000); 76 console.log('Reserving for 10 seconds'); 77 await agonesSDK.reserve(10); 78 await setTimeout(15000); 79 80 if (enableAlpha) { 81 console.log('Running alpha suite'); 82 await runAlphaSuite(agonesSDK); 83 } 84 85 if (enableBeta) { 86 console.log('Running beta suite'); 87 await runBetaSuite(agonesSDK); 88 } 89 90 if (timeout === 0) { 91 do { 92 await setTimeout(MAX_TIMEOUT); 93 } while (true); 94 } 95 96 console.log(`Shutting down after timeout of ${timeout} seconds...`); 97 await setTimeout(timeout * 1000); 98 console.log('Shutting down...'); 99 agonesSDK.shutdown(); 100 101 await setTimeout(5000); 102 console.log('Closing connection to SDK server'); 103 clearInterval(healthInterval); 104 agonesSDK.close(); 105 106 await setTimeout(5000); 107 console.log('Exiting'); 108 clearInterval(lifetimeInterval); 109 110 // Some parts of the SDK are still using the event loop so must exit manually until fixed 111 process.exit(0); 112 } catch (error) { 113 console.error(error); 114 clearInterval(healthInterval); 115 clearInterval(lifetimeInterval); 116 process.exit(0); 117 } 118 }; 119 120 const runAlphaSuite = async (agonesSDK) => { 121 await setTimeout(5000); 122 console.log('Setting capacity'); 123 await agonesSDK.alpha.setPlayerCapacity(64); 124 125 await setTimeout(5000); 126 console.log('Getting capacity'); 127 let result = await agonesSDK.alpha.getPlayerCapacity(); 128 console.log(`result: ${result}`); 129 130 await setTimeout(5000); 131 console.log('Connecting a player'); 132 result = await agonesSDK.alpha.playerConnect('firstPlayerID'); 133 console.log(`result: ${result}`); 134 135 await setTimeout(5000); 136 console.log('Connecting a duplicate player'); 137 result = await agonesSDK.alpha.playerConnect('firstPlayerID'); 138 console.log(`result: ${result}`); 139 140 await setTimeout(5000); 141 console.log('Connecting another player'); 142 await agonesSDK.alpha.playerConnect('secondPlayerID'); 143 144 await setTimeout(5000); 145 console.log('Getting player count'); 146 result = await agonesSDK.alpha.getPlayerCount(); 147 console.log(`result: ${result}`); 148 149 await setTimeout(5000); 150 console.log('Finding if firstPlayerID connected'); 151 result = await agonesSDK.alpha.isPlayerConnected('firstPlayerID'); 152 console.log(`result: ${result}`); 153 154 await setTimeout(5000); 155 console.log('Getting connected players'); 156 result = await agonesSDK.alpha.getConnectedPlayers(); 157 console.log(`result: ${result}`); 158 159 await setTimeout(5000); 160 console.log('Disconnecting a player'); 161 result = await agonesSDK.alpha.playerDisconnect('firstPlayerID'); 162 console.log(`result: ${result}`); 163 164 await setTimeout(5000); 165 console.log('Disconnecting the same player'); 166 result = await agonesSDK.alpha.playerDisconnect('firstPlayerID'); 167 console.log(`result: ${result}`); 168 169 await setTimeout(5000); 170 console.log('Setting counter capacity'); 171 result = await agonesSDK.alpha.setCounterCapacity('testCounter', 10); 172 console.log(`result: ${result}`); 173 }; 174 175 const runBetaSuite = async (agonesSDK) => { 176 let result; 177 178 await setTimeout(5000); 179 console.log('Getting counter count'); 180 result = await agonesSDK.beta.getCounterCount('rooms'); 181 console.log(`result: ${result}`); 182 183 await setTimeout(5000); 184 console.log('Incrementing counter'); 185 await agonesSDK.beta.incrementCounter('rooms', 1); 186 187 await setTimeout(5000); 188 console.log('Decrementing counter'); 189 await agonesSDK.beta.decrementCounter('rooms', 1); 190 191 await setTimeout(5000); 192 console.log('Setting counter count'); 193 await agonesSDK.beta.setCounterCount('rooms', 2); 194 195 await setTimeout(5000); 196 console.log('Getting counter capacity'); 197 result = await agonesSDK.beta.getCounterCapacity('rooms'); 198 console.log(`result: ${result}`); 199 200 await setTimeout(5000); 201 console.log('Setting counter capacity'); 202 await agonesSDK.beta.setCounterCapacity('rooms', 200); 203 204 await setTimeout(5000); 205 console.log('Getting list capacity'); 206 result = await agonesSDK.beta.getListCapacity('players'); 207 console.log(`result: ${result}`); 208 209 await setTimeout(5000); 210 console.log('Setting list capacity'); 211 await agonesSDK.beta.setListCapacity('players', 10); 212 213 await setTimeout(5000); 214 console.log('Getting list contains'); 215 result = await agonesSDK.beta.listContains('players', 'test0'); 216 console.log(`result: ${result}`); 217 218 await setTimeout(5000); 219 console.log('Getting list length'); 220 result = await agonesSDK.beta.getListLength('players'); 221 console.log(`result: ${result}`); 222 223 await setTimeout(5000); 224 console.log('Getting list values'); 225 result = await agonesSDK.beta.getListValues('players'); 226 console.log(`result: ${result}`); 227 228 await setTimeout(5000); 229 console.log('Appending list value'); 230 await agonesSDK.beta.appendListValue('players', 'test3'); 231 232 await setTimeout(5000); 233 console.log('Deleting list value'); 234 await agonesSDK.beta.deleteListValue('players', 'test3'); 235 }; 236 237 let args = process.argv.slice(2); 238 let timeout = DEFAULT_TIMEOUT; 239 let enableAlpha = false; 240 let enableBeta = false; 241 242 for (let arg of args) { 243 let [argName, argValue] = arg.split('='); 244 if (argName === '--help') { 245 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. 246 247 Options: 248 --timeout=...\t\tshutdown timeout in seconds. Use 0 to never shut down 249 --alpha\t\t\tenable alpha features 250 --beta\t\t\tenable beta features`); 251 return; 252 } 253 if (argName === '--timeout') { 254 timeout = Number(argValue); 255 if (Number.isNaN(timeout)) { 256 timeout = DEFAULT_TIMEOUT; 257 } else if (timeout < 0 || timeout > MAX_TIMEOUT) { 258 timeout = 0; 259 } 260 if (timeout === 0) { 261 console.log(`Using shutdown timeout of never`); 262 } else { 263 console.log(`Using shutdown timeout of ${timeout} seconds`); 264 } 265 } 266 267 if (argName === '--alpha') { 268 console.log('Enabling alpha features!'); 269 enableAlpha = true; 270 } 271 272 if (argName === '--beta') { 273 console.log('Enabling beta features!'); 274 enableBeta = true; 275 } 276 } 277 278 connect(timeout, enableAlpha, enableBeta);