nodegame-game-template
Version:
Template files for a standard nodeGame game
145 lines (116 loc) • 4.2 kB
JavaScript
/**
* # Requirements Room
* Copyright(c) {YEAR} {AUTHOR} <{AUTHOR_EMAIL}>
* MIT Licensed
*
* Validates the requirements of incoming connections
*
* http://nodegame.org
* ---
*/
module.exports = function(settings, room, runtimeConf) {
var node = room.node;
var channel = room.channel;
var registry = channel.registry;
// Creates a stager object to define the game stages.
var stager = new node.Stager();
var nextRoom;
if ('undefined' === typeof settings.nextRoom) {
// This should be safe, because waitRoom is created before.
if (channel.waitingRoom) {
nextRoom = channel.waitingRoom.name;
}
else {
throw new Error('requirements.room: no nextRoom provided, and ' +
'no waiting room found.');
}
}
else if ('string' === typeof settings.nextRoom &&
settings.nextRoom !== '') {
nextRoom = settings.nextRoom;
}
else {
throw new TypeError('requirements.room: nextRoom must be undefined ' +
'or non-empty string. Found: ' + settings.nextRoom);
}
function connectingPlayer(p, recon) {
console.log('Player connected to Requirements room.', p.id);
setTimeout(function() {
node.remoteSetup('page', p.id, {
clearBody: true,
title: { title: 'Welcome!', addToBody: true }
});
node.remoteSetup('widgets', p.id, {
append: { 'Requirements': {
root: 'widgets_div',
sayResults: true
} }
});
node.remoteSetup('requirements', p.id, settings);
}, 500);
room.logClientEvent(p, (recon ? 're' : '') + 'connect');
}
function monitorReconnects(p) {
node.game.ml.add(p);
}
// Define stager.
stager.setOnInit(function init() {
var that, i, len;
that = this;
node.on.preconnect(function(p) {
console.log('Player re-connected to Requirements room.');
node.game.pl.add(p);
connectingPlayer(p, true);
});
node.on.pconnect(connectingPlayer);
node.on.pdisconnect(function(p) {
console.log('Player disconnected from Requirements room: ' + p.id);
room.logClientEvent(p, 'disconnect');
});
// This must be done manually for now.
// (maybe will change in the future).
node.on.mreconnect(monitorReconnects);
// Results of the requirements check.
node.on.data('requirements', function(msg) {
if (msg.data.success) {
console.log('requirements passed', msg.from);
// Mark client as requirements passed.
registry.updateClient(msg.from, {apt: true});
setTimeout(function() {
channel.moveClient(msg.from, nextRoom);
}, 1000);
}
else {
console.log('requirements failed', msg.from);
i = -1, len = msg.data.results.length;
for ( ; ++i < len ; ) {
if (!msg.data.results[i].success) {
console.log(msg.data.results[i]);
}
}
}
});
});
stager.setDefaultProperty('publishLevel', 0);
stager.next({
id: 'requirements',
cb: function() {
var str;
str = 'Requirements room created: ' + channel.name;
if (channel.defaultChannel) str += ' (**Default**)';
console.log(str);
}
});
// Return the game.
game = {};
game.metadata = {
name: 'Requirements check room',
description: 'Validates clients requirements.',
version: '0.3.0'
};
game.debug = settings.debug || false;
game.plot = stager.getState();
game.nodename = 'requirements';
game.verbosity = 0;
return game;
};