nodePong/server/server_core.js

142 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-02-10 14:12:52 +01:00
/**
* @file server_core.js
* @author frtk
*/
/**
*
* nodePong Server Object
*
*/
2016-02-12 20:05:23 +01:00
var NPGServer = {
2016-02-10 14:12:52 +01:00
/*
* Data
*/
2016-02-12 20:05:23 +01:00
// app
2016-02-10 14:12:52 +01:00
version: '',
2016-02-12 20:05:23 +01:00
host: '',
port: 0,
2016-02-10 14:12:52 +01:00
// Users and Games
users: [],
games: [],
/*
*
*/
//--- init()
2016-02-12 20:05:23 +01:00
init: function(o) {
2016-02-10 14:12:52 +01:00
var self = this;
2016-02-12 20:05:23 +01:00
self.version = o.VERSION || '';
self.host = o.HTTP.host || '';
self.port = o.HTTP.port || 8042;
self.log('$ ##### nodePong - v'+self.version);
2016-02-10 14:12:52 +01:00
},
//--- 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());
},
2016-02-10 18:48:47 +01:00
2016-02-10 14:12:52 +01:00
2016-02-10 18:48:47 +01:00
/*
* Socket.io handling
*/
//---
socketHandling: function(io) {
var self = this;
io.sockets.on('connection', function (socket) {
2016-02-12 20:05:23 +01:00
//
self.log('$ User connected : id=' + socket.id);
2016-02-10 18:48:47 +01:00
2016-02-12 20:05:23 +01:00
// 'disconnect'
socket.on('disconnect', function () { self.log('$ User disconnected : id=' + socket.id);
});
2016-02-10 18:48:47 +01:00
2016-02-12 20:05:23 +01:00
//
2016-02-10 18:48:47 +01:00
});
},
2016-02-10 14:12:52 +01:00
};
/**
*
* nodePong User Object
*
*/
var User = function(name) {
};
/**
*
* nodePong Game Object
*
*/
var Game = function() {
};
/**
*
* EXPORT
*
*/
if (typeof exports !== "undefined") {
2016-02-12 20:05:23 +01:00
exports.NPGServer = NPGServer;
2016-02-10 14:12:52 +01:00
exports.User = User;
exports.Game = Game;
}