142 lines
2.4 KiB
JavaScript
142 lines
2.4 KiB
JavaScript
|
/**
|
||
|
* @file npg_server.js
|
||
|
* @author frtk
|
||
|
*/
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* nodePong Server Object
|
||
|
*
|
||
|
*/
|
||
|
var NPGServer = {
|
||
|
|
||
|
/*
|
||
|
* Data
|
||
|
*/
|
||
|
// app
|
||
|
version: '',
|
||
|
host: '',
|
||
|
port: 0,
|
||
|
|
||
|
// Users and Games
|
||
|
users: [],
|
||
|
games: [],
|
||
|
|
||
|
|
||
|
/*
|
||
|
*
|
||
|
*/
|
||
|
//--- init()
|
||
|
init: function(o) {
|
||
|
var self = this;
|
||
|
self.version = o.VERSION || '';
|
||
|
self.host = o.HTTP.host || '';
|
||
|
self.port = o.HTTP.port || 8042;
|
||
|
self.log('$ ##### nodePong - v'+self.version);
|
||
|
},
|
||
|
|
||
|
|
||
|
//--- setVersion(s)
|
||
|
setVersion: function(s) {
|
||
|
this.version = s;
|
||
|
},
|
||
|
|
||
|
|
||
|
/*
|
||
|
* Server Messages
|
||
|
*/
|
||
|
//---
|
||
|
log: function(msg) {
|
||
|
var self = this;
|
||
|
return console.log(self.newDateToString() + msg);
|
||
|
},
|
||
|
//--- dateToString
|
||
|
dateToString: function(date) {
|
||
|
var self = this;
|
||
|
return '[' + self.addZero(date.getHours(), 'h') + ':'
|
||
|
+ self.addZero(date.getMinutes(), 'm') + ':'
|
||
|
+ self.addZero(date.getSeconds(), 's') + ':'
|
||
|
+ self.addZero(date.getMilliseconds(), 'ms') + ']';
|
||
|
},
|
||
|
//--- addZero
|
||
|
addZero: function(value, type) {
|
||
|
switch(type) {
|
||
|
case 'h':
|
||
|
case 'm':
|
||
|
case 's':
|
||
|
if (value < 10) value = '0' + value;
|
||
|
break;
|
||
|
case 'ms':
|
||
|
if (value < 10) value = '00' + value;
|
||
|
else if (value >= 10 && value < 100) value = '0' + value;
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
};
|
||
|
return value;
|
||
|
},
|
||
|
//--- return new date string
|
||
|
newDateToString: function() {
|
||
|
var self = this;
|
||
|
return self.dateToString(new Date());
|
||
|
},
|
||
|
|
||
|
|
||
|
|
||
|
/*
|
||
|
* Socket.io handling
|
||
|
*/
|
||
|
//---
|
||
|
socketHandling: function(io) {
|
||
|
var self = this;
|
||
|
io.sockets.on('connection', function (socket) {
|
||
|
//
|
||
|
self.log('$ User connected : id=' + socket.id);
|
||
|
|
||
|
// 'disconnect'
|
||
|
socket.on('disconnect', function () { self.log('$ User disconnected : id=' + socket.id);
|
||
|
});
|
||
|
|
||
|
//
|
||
|
});
|
||
|
},
|
||
|
|
||
|
|
||
|
};
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* nodePong User Object
|
||
|
*
|
||
|
*/
|
||
|
var User = function(name) {
|
||
|
};
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* nodePong Game Object
|
||
|
*
|
||
|
*/
|
||
|
var Game = function() {
|
||
|
};
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* EXPORT
|
||
|
*
|
||
|
*/
|
||
|
if (typeof exports !== "undefined") {
|
||
|
exports.NPGServer = NPGServer;
|
||
|
exports.User = User;
|
||
|
exports.Game = Game;
|
||
|
}
|