Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
done pongSocket and unit tests. Ready for integration tests with pong…
… engine and client
  • Loading branch information
bestander committed Jan 8, 2013
1 parent 3e5a8f1 commit d346425
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 92 deletions.
3 changes: 3 additions & 0 deletions .idea/codeStyleSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/pong-mmo-server.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 43 additions & 9 deletions game/socket/pongSocket.js
Expand Up @@ -12,6 +12,7 @@
* --------
* Copyright 2012 Konstantin Raev (bestander@gmail.com)
*/
// TODO test garbage collection of sockets and games
'use strict';

function PongSocket (socket, lobby){
Expand All @@ -26,13 +27,14 @@ function PongSocket (socket, lobby){
this._lobby = lobby;
this._game = null;
this._playerId = null;
this._matchStarted = false;
this._defineCommandsHandlers();
}

module.exports = PongSocket;

// world update rate in milliseconds
PongSocket.prototype.GAME_UPDATE_PERIOD_MILLIS = 1000;
// match update rate in milliseconds
PongSocket.prototype.MATCH_UPDATE_PERIOD_MILLIS = 1000;

PongSocket.prototype._defineCommandsHandlers = function () {
var that = this;
Expand All @@ -41,40 +43,72 @@ PongSocket.prototype._defineCommandsHandlers = function () {
// TODO will be async I'm pretty sure
that._game = that._lobby.getGame();
that._playerId = that._game.joinPlayer();
that._socket.emit('ENTERED_GAME', that._game.getParametersAndState());
that._socket.emit('GAME_ENTERED', that._game.getFieldParams());
that._defineGameEventsHandlers();
}
});
this._socket.on('LAG_CHECK', function () {
that._socket.emit('LAG_CHECK_RESPONSE', new Date().getTime());
that._socket.emit('LAG_RESPONSE', new Date().getTime());
});
this._socket.on('READY', function () {
if (that._isJoinedToGame()) {
that._game.handlePlayerCommand(that._playerId, 'READY');
that._startClientNotificationLoop();
}
});
this._socket.on('PLAYER_COMMAND', function (data) {
if (that._isJoinedToGame()) {
that._game.handlePlayerCommand(that._playerId, data);
}
});
this._socket.on('disconnect', function () {
if (that._isJoinedToGame()) {
that._game.quitPlayer(that._playerId);
that._game.quitPlayer(that._playerId);
}
});
};

PongSocket.prototype._defineGameEventsHandlers = function () {
var that = this;
this._game.getEventEmitter().on('PLAYER_JOINED', function (data) {
that._socket.emit('PLAYER_JOINED', data);
});
this._game.getEventEmitter().on('PLAYER_QUIT', function (data) {
that._socket.emit('PLAYER_QUIT', data);
});
this._game.getEventEmitter().on('PLAYER_READY', function (data) {
that._socket.emit('PLAYER_READY', data);
});
this._game.getEventEmitter().on('PLAYER_SCORED', function (data) {
that._socket.emit('PLAYER_SCORED', data);
});
this._game.getEventEmitter().on('MATCH_STARTED', function (data) {
that._matchStarted = true;
that._startClientNotificationLoop();
that._socket.emit('MATCH_STARTED', data);
});
this._game.getEventEmitter().on('MATCH_STOPPED', function (data) {
that._matchStarted = false;
that._socket.emit('MATCH_STOPPED', data);
});
};

PongSocket.prototype._isJoinedToGame = function () {
return this._game && this._playerId;
};

PongSocket.prototype._isMatchStarted = function () {
return this._isJoinedToGame() && this._matchStarted;
};

PongSocket.prototype._startClientNotificationLoop = function () {
this._boundLoopCall = this._boundLoopCall || this._startClientNotificationLoop.bind(this);

if(this._isJoinedToGame()){
if(this._isMatchStarted()){
this._socket.emit('GAME_UPDATE', {
'objects': this._game.getObjectPositions(),
'time': new Date().getTime()
});
setTimeout(this._boundLoopCall, this.GAME_UPDATE_PERIOD_MILLIS);

setTimeout(this._boundLoopCall, this.MATCH_UPDATE_PERIOD_MILLIS);
}
};

Expand Down

0 comments on commit d346425

Please sign in to comment.