Skip to content

Commit

Permalink
fixing gamestate bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
meetar committed May 20, 2011
1 parent 81939f6 commit a9fe559
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 42 deletions.
83 changes: 58 additions & 25 deletions client.js
Expand Up @@ -144,10 +144,10 @@ var scores = {
7:[1,3,6],
8:[1,2,3,4,5,6,7],
9:[1,2,3,4,6,7]
}
};

// jQuery shortcuts
var p1 = $('#p1'), p2 = $('#p2'), xball = $('#xball'), ball = $('#ball'), court = $('#court'), $body = $('body');
var p1 = $('#p1'), p2 = $('#p2'), xball = $('#xball'), ball = $('#ball'), court = $('#court'), body = $('body');
var readout = $('#readout');
var readout2 = $('#readout2');

Expand Down Expand Up @@ -190,7 +190,15 @@ function command(msg){
break;

case 'playing':
socket.send({type:"log", what:"PLAYING"});
if (msg.paddle == 'p1' || msg.paddle == 'p2') {
if (testMode) readout.html('playing: '+msg.paddle);
// make sure to kill any previous game
ball.css('visibility', 'hidden');
xball.stop(true);
ball.stop(true);
xball.css('left',court.width()/2+"px")
ball.css('top',court.height()/2+"px")

// turn on mouse tracking
$(document).mousemove(function(e){ mouseY = e.pageY; });
Expand All @@ -212,7 +220,6 @@ function command(msg){
paddle.css('top', '50%');
//paddle.css('background-color', 'blue');
socket.send({type:'movePaddle', which:playing, pos:lastY, goal:lastY});

playLoop(msg.delay); // normally 1 - .8 seems to reduce lag?
}
break;
Expand Down Expand Up @@ -247,12 +254,27 @@ function command(msg){
break;

case 'moveBall': // move ball
// kill any existing animates()
xball.stop();
ball.stop();
//if (testMode) readout.html("moveBall: "+rnd(msg.startx)+", "+rnd(msg.starty)+'<br>end: '+msg.endx+', xTime: '+msg.xTime);
xball.css({'left': msg.startx+"%"});
ball.css({'visibility': 'visible', 'top': msg.starty+"%"});
if (!playing) {
//socket.send({type:"log", what:"MOVEBALL: BAIL"});
return false;
}
socket.send({type:"log", what:"MOVEBALL"});
// kill any existing or queued animates()
xball.stop(true);
ball.stop(true);
if (testMode) {
readout.html("");
readout.html('moveBall: '+rnd(msg.startx)+", "+rnd(msg.starty)+
'<br>end: '+rnd(msg.endx)+', xTime: '+rnd(msg.xTime));
}
//testing
//xball.css({'left': msg.startx+"%"});
startxpx = msg.startx/100*court.width();
xball.css({'left': startxpx+"px"});
//testing
//ball.css({'visibility': 'visible', 'top': msg.starty+"%"});
startypx = msg.starty/100*court.height();
ball.css({'visibility': 'visible', 'top': startypx+"px"});
ball.css({'background-color': 'white'});

deltax = msg.endx == 0 ? -1 : 1; // set direction
Expand All @@ -264,8 +286,11 @@ function command(msg){

/////////////////////////
// animate xball on the X
xball.animate({left: msg.endx+"%"}, {duration: msg.xTime, easing: 'linear',

//testing
//xball.animate({left: msg.endx+"%"}, {duration: msg.xTime, easing: 'linear',
endxpx = msg.endx/100*court.width();
xball.animate({left: endxpx+"px"}, {duration: msg.xTime, easing: 'linear',

step: function() {
//readout2.html("left: "+rnd(xball.position().left)+", top: "+rnd(xball.position().top));

Expand All @@ -284,8 +309,6 @@ function command(msg){
if (!returned && !scored) {
// P2 SCORED
scored = true;
//ball1.css('visibility', 'hidden');
//ball.css('background-color', 'red');

socket.send({type:'score', me:playing, which:'p2'});
//if (testMode) readout2.html("complete at 0: returned: "+returned+", scored: "+scored);
Expand All @@ -295,8 +318,6 @@ function command(msg){
if (!returned && !scored) {
// P1 SCORED
scored = true;
//ball1.css('visibility', 'hidden');
//ball.css('background-color', 'red');

socket.send({type:'score', me:playing, which:'p1'});
//if (testMode) readout2.html("complete at 97: returned: "+returned+", scored: "+scored);
Expand All @@ -313,7 +334,10 @@ function command(msg){
//if (testMode) readout.html('endy: '+endy+', inityTime: '+rnd(msg.inityTime)+', yTime: '+msg.yTime);
nextTime = msg.yTime * -1;

ball.animate({"top": endy+'%'}, {duration: msg.inityTime, easing: 'linear',
//testing
//ball.animate({"top": endy+'%'}, {duration: msg.inityTime, easing: 'linear',
endy = endy/100*court.height();
ball.animate({"top": endy+'px'}, {duration: msg.inityTime, easing: 'linear',
complete: function() {
bounceY(nextTime);
}
Expand Down Expand Up @@ -351,12 +375,17 @@ function command(msg){
break;

case 'endgame':
socket.send({type:"log", what:"ENDGAME"});
readout2.html("FORFEIT");
colliding = false;
playing = false;
paddle = '';
xball.stop();
ball.stop();
xball.stop(true);
ball.stop(true);
ball.css('visibility', 'hidden');
xball.css('left',court.width()/2+"px")
ball.css('top',court.height()/2+"px")


// turn off mouse tracking
$(document).mousemove(null);
Expand All @@ -378,19 +407,23 @@ function command(msg){
function bounceY(time) {
var top;
var thisTime, nextTime;

//ball.stop(true, true); // ie has problems with this
thisTime = Math.abs(time); // positive animate() durations only
nextTime = time * -1; // when done, head the other way

top = (time < 0 ? 0 : 96); // negative time = heading up, positive = down

//if (testMode) $("#returned").html('bounce, top: '+top+', time: '+time+', next: '+nextTime+', this: '+thisTime);
//if (testMode) readout2.html('top: '+top+', time'+time);
//if (testMode) readout2.html('top!: '+top+', thistime: '+thisTime);

//testing
//ball.animate({top: top+"%"},{duration: thisTime, easing: "linear", complete: function() {bounceY(nextTime);}});
topx = top/100*court.height();
ball.animate(
{top: top+"%", "background-color":"red"},
{top: topx+"px"},
{duration: thisTime, easing: "linear", complete: function() {bounceY(nextTime);}}
);

}

// main event loop
Expand Down Expand Up @@ -470,11 +503,11 @@ function collisionDetection() {
// a magnificent return!
if (returned) {
xball.stop();

// get relative y position so server can calculate english

// get position of ball as a proportion of paddle's height
var which = (returned == 'p1' ? p1 : p2);
var angle = ((ball.position().top + ball.height()/2 - which.position().top)/which.height())*100;
var angle = ((ball.position().top + ball.height()/2 - which.position().top)/paddle.height())*100;

//if (readout) $("#returned").html('ball.position().top: '+ball.position().top+'<br>ball.height()/2: '+ball.height()/2+ '<br>ball.position().top + ball.height(): '+(ball.position().top + ball.height()/2)+'<br>which.position().top: '+which.position().top+'<br>relative px: '+(ball.position().top + ball.height()/2 - which.position().top)+'<br>which.height(): '+which.height()+'<br>angle: '+rnd(angle));

Expand All @@ -484,7 +517,7 @@ function collisionDetection() {
starty: bally/court.height()*100,
which: returned,
angle: angle});
//if (testMode) readout2.html(playing+" returned! "+returned+", angle: "+angle);
if (testMode) readout2.html(playing+" returned! "+returned+", angle: "+rnd(angle));
}

lastbx = ballx;
Expand Down Expand Up @@ -531,7 +564,7 @@ function setBodyScale() {
if (fontSize > maxScale) fontSize = maxScale;
if (fontSize < minScale) fontSize = minScale; // Enforce the minimum and maximums

$body.css('font-size', fontSize + '%');
body.css('font-size', fontSize + '%');
}

// trigger when document has finished loading
Expand Down
7 changes: 4 additions & 3 deletions index.html
Expand Up @@ -10,7 +10,8 @@

<script type="text/javascript" src="/socket.io/socket.io.js"></script>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<!--script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script-->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>

<!-- don't serve to desktops: prevents opening links in new tab with keyboard modifiers -->

Expand Down Expand Up @@ -94,8 +95,8 @@ <h1 id="alert"></h1>
<div id="player1"></div>
<div id="player2"></div>
</div>
<div id="xball">
<div id="ball"></div>
<div id="xball">X
<div id="ball">Y</div>
</div>

<div id="scores">
Expand Down
34 changes: 20 additions & 14 deletions server.js
Expand Up @@ -155,7 +155,7 @@ io.on('connection', function(client){
if (client.sessionId == player2.id) {
if (p1heartBeat == false) {
p1skippedBeat++;
log('p1 SKIPPED: '+p1skippedBeat);
//log('p1 SKIPPED: '+p1skippedBeat);
} else {
p1skippedBeat = 0;
p1heartBeat = false;
Expand All @@ -169,7 +169,7 @@ io.on('connection', function(client){
if (client.sessionId == player1.id) {
if (p2heartBeat == false) {
p2skippedBeat++;
log('p2 SKIPPED: '+p2skippedBeat);
//log('p2 SKIPPED: '+p2skippedBeat);
} else {
p2skippedBeat = 0;
p2heartBeat = false;
Expand Down Expand Up @@ -370,9 +370,10 @@ io.on('connection', function(client){

// second player! start new game!
if (queue.length > 1 && !gameOn && !newgameID) {
log(' connect NEWGAME');
log('NEWGAME');
newgameID = setTimeout(function() {newgame(2)}, newgameDelay );
} else if (queue.length > 1 && !gameOn && newgame) { // claims game already triggered?
} else if (queue.length > 1 && !gameOn && newgameID) { // claims game already triggered?
log('NEWGAMEID ALREDY TRIGGERED');
setTimeout(function() { // give it 3 newgameDelays
if (newgameID) newgame(3); // if newgameID still not reset, try to start a new game anyway
else {
Expand Down Expand Up @@ -406,7 +407,7 @@ io.on('connection', function(client){
}

if (msg.type == 'log') {
log(parseInt(client.sessionId/100000000000000)+': '+msg.what);
log(":"+parseInt(client.sessionId/100000000000000)+': '+msg.what);
}

});
Expand Down Expand Up @@ -616,7 +617,7 @@ function log(x) {
function report(list) {
msg = ''
for (x in list) {
msg += list[x]+': '+eval(list[x])+' | ';
msg += list[x]+': '+eval(list[x])+', ';
}
log(msg);
}
Expand Down Expand Up @@ -654,6 +655,7 @@ function eliminateDuplicates(array) {
//////////////////////////////////
// NEW GAME

// newgame!
function newgame(id) {
if (sessions.length < 2 || queue.length < 2) {
log(' false start- sessions.length:'+sessions.length+', queue.length:'+queue.length);
Expand Down Expand Up @@ -718,12 +720,13 @@ function newgame(id) {
score2 = 0;
updateScores();

report(['playing', 'gameOn', 'point', 'player1.name', 'player2.name']);
if (!playing) {
//log(' !PLAYING');

getSet = true;
if (!playLoopID) {
log(' NO PLAYLOOP: starting');
log('NO PLAYLOOP: starting');
playLoopID = setTimeout(playLoop, delay, 'NEWGAME');
} else log('PROB: playloop already going');

Expand All @@ -733,10 +736,10 @@ function newgame(id) {
clearTimeout(resetID);
}
resetID = setTimeout( reset, resetDelay );
log("resetID: "+resetID);

} else {
log(' PROB: already playing');
report(['playing', 'gameOn', 'point', 'player1.id', 'player2.id']);
log('PROB: already playing, resetID: '+resetID);
//return false;
}

Expand Down Expand Up @@ -769,6 +772,8 @@ function playLoop(caller) {
return 0;
} else if (!resetID) {
resetID = setTimeout(reset, resetDelay);
log("resetID: "+resetID);

} else {
log('PROB: already resetting');
}
Expand All @@ -785,8 +790,7 @@ function playLoop(caller) {
playLoopID = false;
}

// GAME OVER
// type:'win'|'forfeit', player:[object Object]
// gameover! type: 'win' or 'forfeit', which: player object
function gameover(type, which) {
log('GAME OVER: '+type+', '+which.name);
gameOn = false;
Expand Down Expand Up @@ -867,10 +871,12 @@ function gameover(type, which) {

}

// reset!
function reset() {
log('\nRESET: playing: '+playing+', gameOn: '+gameOn+', newgame: '+(newgame != false));
//if (playing || !gameOn || !newgame) {
if ( (playing && !gameOn) || (playing && !newgame) ) {
log('\nRESET: playing: '+playing+', gameOn: '+gameOn+', newgameID: '+(newgameID != false));
//if (playing || !gameOn || !newgameID) {
//if ( (playing && !gameOn) || (playing && !newgameID) ) {
if (playing || !gameOn) {
log('> false reset');
return false;
}
Expand Down

0 comments on commit a9fe559

Please sign in to comment.