Skip to content

Commit

Permalink
AbsoluteTimer: more precise, no rounding. Tests updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
darthpolly committed Oct 3, 2011
1 parent ede2d93 commit bd461fe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions time/js/board_emulator.js
Expand Up @@ -41,8 +41,8 @@ Board.prototype = {
},

update_clocks: function(remain) {
this.div_clocks[BLACK].innerHTML = remain[BLACK];
this.div_clocks[WHITE].innerHTML = remain[WHITE];
this.div_clocks[BLACK].innerHTML = Math.round(remain[BLACK]);
this.div_clocks[WHITE].innerHTML = Math.round(remain[WHITE]);
},

announce_loss: function(remain) {
Expand Down
10 changes: 6 additions & 4 deletions time/systems/absolute.js
Expand Up @@ -32,7 +32,7 @@ AbsoluteTimer.prototype = {
this.actual_color = color;
this.status = ST_COUNTING;
this.last_resume = new Date();
this.clock = window.setInterval(binder(this.tick, this), 1000);
this.clock = window.setInterval(this.binder(this.tick, this), 100);
}
},

Expand All @@ -42,7 +42,7 @@ AbsoluteTimer.prototype = {
this.last_pause = new Date();
window.clearInterval(this.clock);
this.status = ST_PAUSED;
this.remain[this.actual_color] -= Math.round((this.last_pause - this.last_resume) / 1000);
this.remain[this.actual_color] -= (this.last_pause - this.last_resume) / 1000;
return this.remain;
}
return false;
Expand All @@ -67,7 +67,7 @@ AbsoluteTimer.prototype = {
var tmp_remain = {};
tmp_remain[BLACK] = this.remain[BLACK];
tmp_remain[WHITE] = this.remain[WHITE];
tmp_remain[this.actual_color] = this.remain[this.actual_color] - Math.round((new Date() - this.last_resume) / 1000);
tmp_remain[this.actual_color] = this.remain[this.actual_color] - (new Date() - this.last_resume) / 1000;
this.game.update_clocks(tmp_remain);
if (tmp_remain[this.actual_color] <= 0) {
this.remain[this.actual_color] = 0;
Expand All @@ -76,5 +76,7 @@ AbsoluteTimer.prototype = {
}
},


binder: function (method, object, args) {
return function(orig_args) { method.apply(object, [orig_args].concat(args)); };
},
}
24 changes: 16 additions & 8 deletions time/tests/absolute.js
Expand Up @@ -62,8 +62,10 @@ test("Configure Timer", function() {
board2.play();
test_remain[BLACK] = STARTING_TIME - 6;
test_remain[WHITE] = STARTING_TIME;
deepEqual(board1.time.remain, test_remain, "B1: 6 seconds passed until B played.");
deepEqual(board2.time.remain, test_remain, "B2: 6 seconds passed until B played.");
equal(Math.round(board1.time.remain[WHITE]), test_remain[WHITE], "B1: 6 seconds passed until B played.");
equal(Math.round(board1.time.remain[BLACK]), test_remain[BLACK], "B1: 6 seconds passed until B played.");
equal(Math.round(board2.time.remain[WHITE]), test_remain[WHITE], "B2: 6 seconds passed until B played.");
equal(Math.round(board2.time.remain[BLACK]), test_remain[BLACK], "B2: 6 seconds passed until B played.");
equal(board1.time.actual_color, WHITE, "B1: Counting for White");
equal(board2.time.actual_color, WHITE, "B2: Counting for White");
start();
Expand All @@ -76,8 +78,10 @@ test("Configure Timer", function() {
board1.play();
test_remain[BLACK] = STARTING_TIME - 6;
test_remain[WHITE] = STARTING_TIME - 10;
deepEqual(board1.time.remain, test_remain, "B1: 10 seconds passed until W played.");
deepEqual(board2.time.remain, test_remain, "B2: 10 seconds passed until W played.");
equal(Math.round(board1.time.remain[WHITE]), test_remain[WHITE], "B1: 10 seconds passed until W played.");
equal(Math.round(board1.time.remain[BLACK]), test_remain[BLACK], "B1: 10 seconds passed until W played.");
equal(Math.round(board2.time.remain[WHITE]), test_remain[WHITE], "B2: 10 seconds passed until W played.");
equal(Math.round(board2.time.remain[BLACK]), test_remain[BLACK], "B2: 10 seconds passed until W played.");
equal(board1.time.actual_color, BLACK, "B1: Counting for Black");
equal(board2.time.actual_color, BLACK, "B2: Counting for Black");
start();
Expand All @@ -90,8 +94,10 @@ test("Configure Timer", function() {
board2.play();
test_remain[BLACK] = STARTING_TIME - 6 - 12;
test_remain[WHITE] = STARTING_TIME - 10;
deepEqual(board1.time.remain, test_remain, "B1: 12 seconds passed until B played again. It was a great move!");
deepEqual(board2.time.remain, test_remain, "B2: 12 seconds passed until B played again. It was a great move!");
equal(Math.round(board1.time.remain[WHITE]), test_remain[WHITE], "B1: 12 seconds passed until B played again. It was a great move!");
equal(Math.round(board1.time.remain[BLACK]), test_remain[BLACK], "B1: 12 seconds passed until B played again. It was a great move!");
equal(Math.round(board2.time.remain[WHITE]), test_remain[WHITE], "B2: 12 seconds passed until B played again. It was a great move!");
equal(Math.round(board2.time.remain[BLACK]), test_remain[BLACK], "B2: 12 seconds passed until B played again. It was a great move!");
equal(board1.time.actual_color, WHITE, "B1: Counting for White");
equal(board2.time.actual_color, WHITE, "B2: Counting for White");
start();
Expand All @@ -104,8 +110,10 @@ test("Configure Timer", function() {
board1.play();
test_remain[BLACK] = STARTING_TIME - 6 - 12;
test_remain[WHITE] = STARTING_TIME - 10 - 20;
deepEqual(board1.time.remain, test_remain, "B1: 10 seconds passed until W played.");
deepEqual(board2.time.remain, test_remain, "B2: 10 seconds passed until W played.");
equal(Math.round(board1.time.remain[WHITE]), test_remain[WHITE], "B1: 22 seconds passed until W played. Game was ended before, so no effect.");
equal(Math.round(board1.time.remain[BLACK]), test_remain[BLACK], "B1: 22 seconds passed until W played. Game was ended before, so no effect.");
equal(Math.round(board2.time.remain[WHITE]), test_remain[WHITE], "B2: 22 seconds passed until W played. Game was ended before, so no effect.");
equal(Math.round(board2.time.remain[BLACK]), test_remain[BLACK], "B2: 22 seconds passed until W played. Game was ended before, so no effect.");
equal(board1.time.status, ST_STOPED, "B1: Not counting");
equal(board2.time.status, ST_STOPED, "B2: Not counting");
equal(board1.time.actual_color, null, "B1: No actual color");
Expand Down

0 comments on commit bd461fe

Please sign in to comment.