-
Notifications
You must be signed in to change notification settings - Fork 18
Automated Players v7
- status: complete
- version: 7.x
- follows from: Levels
Automated players are created as new client types
by adding a new file the game/client_types/
folder.
They can be created from scratch, or they can extend an existing one, like in the example below.
module.exports = function(treatmentName, settings, stager, setup, gameRoom) {
var node = gameRoom.node;
var ngc = require('nodegame-client');
var game, stager;
// Get a copy of the 'player' client type and rename it.
game = gameRoom.getClientType('player');
game.nodename = 'autoplay';
// Create a new stager object and start modifying it.
stager = ngc.getStager(game.plot);
// Steps can be extended individually, but it is often easier
// to extend them all at the same time.
stager.extendAllSteps(function(o) {
// Store a reference to previous step callback.
o._cb = o.cb;
// Create a new step callback.
o.cb = function() {
var _cb, stepObj, id;
// Get the previous step callback and execute it.
stepObj = this.getCurrentStepObj();
_cb = stepObj._cb;
_cb.call(this);
// Performs automatic play depending on the step.
id = stepObj.id
if (id === 'xxx') {
// Do something.
}
else if (id === 'yyy') {
// Do something else.
}
else if (id === 'zzz') {
// Do yet another automated thing.
}
// Call node.done after a random time interval.
node.timer.randomDone();
};
// Return the extended step.
return o;
});
game.plot = stager.getState();
return game;
};
Furthermore, in the step callback of the automated player, it is possible to simulate game events, such as disconnections and timeups.
// Instead of calling node.timer.randomDone()
// Simulate a disconnection and reconnection
// (authentication must be enabled in the channel to work).
if (Math.random() < 0.5) {
node.socket.disconnect();
node.game.stop();
node.timer.randomExec(function() {
node.socket.reconnect();
}, 4000);
}
else {
// Simulate a timeup.
if (Math.random() < 0.5) {
node.timer.randomDone(1500);
}
}
Automated players can be launched from the monitor interface, via the server API, or by specifying the client type as a query parameter in the url in the address bar of the browser, e.g:
http://localhost:8080/?clientType=autoplay
- Writing an autoplay player when roles are defined, see the Ultimatum Game
Go back to the wiki Home.
Copyright (C) 2021 Stefano Balietti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.