Commit 1827acee authored by Monika's avatar Monika
Browse files

whoIsOnTurn

parent e6e66931
......@@ -41,6 +41,18 @@ module.exports = function (PlayersColliction, Score) {
return next;
},
resetTeamAccordingServe: function(id, team) {
this.resetTeamStack();
_whichTeam = team;
if (_whichTeam) {
var index = _team1.indexOf(id);
_team1.splice(index, 1);
} else {
var index = _team0.indexOf(id);
_team0.splice(index, 1);
}
},
/**
* return user.Id
* @param type
......@@ -51,6 +63,7 @@ module.exports = function (PlayersColliction, Score) {
} else {
_whichTeam = 1;
}
console.log(this.getNext(), 'turn');
return this.getNext();
}
};
......
......@@ -49,6 +49,8 @@ function ClientCollision(Ball, Racket, User) {
if (
attributes.racket.getIsActive()
&&
attributes.user.getIsTurn()
&&
(ballAxis.x >= (racketAxis.x - ballDimension.width))
&&
(ballAxis.x <= (racketAxis.x + racketDimension.width))
......
......@@ -92,12 +92,11 @@ $(function () {
if(!connected){
user.setId(data.id);
//console.log(data, 'turn');
if(data.id == data.whoIsOnTurn){
user.setIsTurn(true);
}
for (var i=0; i < data.allIds.length; i++){// if we need later to send all players whe can directly from them take ids
$rackets.append("<div class='racket racket" + data.allIds[i] + "'></div>")
}
drawAllRackets(data);
racket.setEl($('div.racket' + data.id));
connected = true;
}
......@@ -105,14 +104,21 @@ $(function () {
});
socket.on('who is turn', function (data) {
//console.log(data);
log.add("On turn: " + data.username);
//console.log(data, 'turn');
if(data.userId == user.getId()){
user.setIsTurn( true );
} else {
user.setIsTurn( false );
}
})
socket.on('change score', function (data) {
score.updateScore(data);
score.updateScore(data.score);
//console.log(data, 'serve');
if(data.userId == user.getId()){
user.setIsTurn( true );
} else {
user.setIsTurn( false );
}
})
socket.on('ball position', function (data) {
......@@ -129,7 +135,8 @@ $(function () {
log.add(data.username + ' joined');
log.add(data.username + ' is team ' + data.team);
user.setTeam(data.team);
console.log(data);
drawAllRackets(data);
//console.log(data);
});
// Whenever the server emits 'user left', log it in the chat body
......@@ -141,6 +148,12 @@ $(function () {
$('div.racket' + data.id).css({left: data.x, top: data.y});
}
function drawAllRackets(data) {
for (var i=0; i < data.allIds.length; i++){// if we need later to send all players whe can directly from them take ids
$rackets.append("<div class='racket racket" + data.allIds[i] + "'></div>")
}
}
$('.logout').click(function () {
socket.emit('disconnectUser', user);
$gamePage.fadeOut();
......
......@@ -16,6 +16,7 @@ var score = require('./Score.js')(ball, playgroundDimmesion);
var collision = require('./Collision.js')(playgroundDimmesion, ball);
var whoIsTurn = require('./WhoIsTurn.js')(PlayersCollection, score);
var whoIsOnServe = require('./whoIsOnServe.js')(PlayersCollection, score);
server.listen(port, function () {
console.log('Server listening at port %d', port);
......@@ -23,6 +24,7 @@ server.listen(port, function () {
var isIntervalInit = false;
var newAxis = {};
var firstTurn = false;
setInterval(function () {
if (isIntervalInit) {
......@@ -30,7 +32,11 @@ setInterval(function () {
ball.calculateNewAxis();
if(score.collision()){
io.emit('change score', score.getScore());
io.emit('change score', {
'score': score.getScore(),
'userId': whoIsOnServe.getWhoIsOnServe()
});
whoIsTurn.resetTeamAccordingServe(whoIsOnServe.getActId(), whoIsOnServe.getTeam());
}
var newAxis = ball.getAxis();
......@@ -54,6 +60,9 @@ io.on('connection', function (socket) {
socket.on('change ball vector', function (ballVector) {
ball.setVectors(ballVector.vx, ballVector.vy);
socket.broadcast.emit('who is turn', {
'userId': whoIsTurn.getWhoIsTurn()
});
});
// when the client emits 'add user', this listens and executes
......@@ -67,12 +76,19 @@ io.on('connection', function (socket) {
PlayersCollection.addPlayer(newPlayer);
if(!firstTurn){
var whoIsOnTurnId = whoIsTurn.getWhoIsTurn();
var whoIsOnServeId = whoIsOnServe.getWhoIsOnServe();
firstTurn = true;
}
socket.emit('login', {
numUsers: PlayersCollection.getCount(),
username: newPlayer.getName(),
id: newPlayer.getId(),
allIds: PlayersCollection.getIds(),
whoIsOnTurn: whoIsTurn.getWhoIsTurn()
whoIsOnTurn: whoIsOnTurnId,
whoIsOnServe: whoIsOnServeId
});
//// echo globally (all clients) that a person has connected
socket.broadcast.emit('user joined', {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment