Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for displaying time taken by each player & also Blitz mode Chess #6

Merged
merged 6 commits into from
Jan 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions public/javascripts/chesshub.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ $( document ).ready(function() {
* This name is then passed to the socket connection handshake query
*/
var username;
var time_out=300;//5 minutes in seconds
if($("#loggedUser").length) {
username = $("#loggedUser").data("user");
} else {
Expand Down Expand Up @@ -133,6 +134,47 @@ $( document ).ready(function() {
var side = $("#board").data('side');
var opponentSide = side === "black" ? "white" : "black";

/*
* Timer : displays time taken by each player while making moves
*/
var timer=function(time_set)
{
if(true)
{
if(game.turn().toString()=='w')
{
time_set[0]+=1;
if(time_set[0]>time_out)
{
//handle time out
$('#gameResult').html('TimeOut! Black Won !');
$('#gameResultPopup').modal({
keyboard: false,
backdrop: 'static'
});
clearInterval(timer_interval);
}
$("#timew").html(("00" + Math.floor(time_set[0]/60)).slice (-2)+":"+("00" + time_set[0]%60).slice (-2));
}
if(game.turn().toString()=='b')
{
time_set[1]+=1;
if(time_set[1]>time_out)
{
//handle time out
$('#gameResult').html('TimeOut! White Won !');
$('#gameResultPopup').modal({
keyboard: false,
backdrop: 'static'
});
clearInterval(timer_interval);
}
$("#timeb").html(("00" + Math.floor(time_set[1]/60)).slice (-2)+":"+("00" + time_set[1]%60).slice (-2));
}
}
return time_set;
};

/*
* When a piece is dragged, check if it the current player has the turn
*/
Expand Down Expand Up @@ -217,6 +259,9 @@ $( document ).ready(function() {
* A second player has joined the game => the game can start
*/
socket.on('ready', function (data) {
//intialize the timer
var time_sets=[0,0];
timer_interval=setInterval(function(){ time_sets=timer(time_sets)}, 1000);//repeat every second
$('#turn-w').addClass("fa fa-spinner");
$('#player-white').html(data.white);
$('#player-black').html(data.black);
Expand Down
2 changes: 2 additions & 0 deletions views/partials/game.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
<p >
<img src="/images/chesspieces/wikipedia/bK.png" class="side-img" alt="Black"/>
<span id="player-black"></span> <span class="turn" id="turn-b">&nbsp;<i class=""></i></span>
<span id="timeb" >{{time}}</span>
</p>
<p>
<img src="/images/chesspieces/wikipedia/wK.png" class="side-img" alt="White"/>
<span id="player-white"></span> <span class="turn" id="turn-w">&nbsp;<i class=""></i></span>
<span id="timew" >{{time}}</span>
</p>
</div>
</div>
Expand Down