Make all clock data be in seconds internally, moving the clock formatting to the...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 15 May 2015 22:48:24 +0000 (00:48 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 15 May 2015 22:54:47 +0000 (00:54 +0200)
Also make the client show seconds when there are less than ten minutes left.

Position.pm
remoteglot.pl
www/js/remoteglot.js

index b720116..6840e7f 100644 (file)
@@ -28,8 +28,8 @@ sub new {
        $pos->{'player_b'} = $x[18];
        $pos->{'player_w'} =~ s/^W?[FCIG]M//;
        $pos->{'player_b'} =~ s/^W?[FCIG]M//;
-       $pos->{'white_clock'} = _parse_fics_clock($x[24]);
-       $pos->{'black_clock'} = _parse_fics_clock($x[25]);
+       $pos->{'white_clock'} = $x[24];
+       $pos->{'black_clock'} = $x[25];
        $pos->{'move_num'} = $x[26];
        if ($x[27] =~ /([a-h][1-8])-([a-h][1-8])/) {
                $pos->{'last_move_uci'} = $1 . $2;
@@ -300,18 +300,4 @@ sub _parse_uci_move {
         return ($from_row, $from_col, $to_row, $to_col, $promo);
 }
 
-sub _parse_fics_clock {
-       my $x = shift;
-       if ($x =~ /^\d+$/) {
-               my $s = $x % 60;
-               $x = ($x - $s) / 60;
-               my $m = $x % 60;
-               $x = ($x - $m) / 60;
-               my $h = $x;
-               return sprintf "%02d:%02d:%02d", $h, $m, $s;
-       } else {
-               return $x;
-       }
-}
-
 1;
index 86c4474..a94a618 100755 (executable)
@@ -996,11 +996,8 @@ sub extract_clock {
        # Look for extended PGN clock tags.
        my $tags = $pgn->tags;
        if (exists($tags->{'WhiteClock'}) && exists($tags->{'BlackClock'})) {
-               $pos->{'white_clock'} = $tags->{'WhiteClock'};
-               $pos->{'black_clock'} = $tags->{'BlackClock'};
-
-               $pos->{'white_clock'} =~ s/\b(\d)\b/0$1/g;
-               $pos->{'black_clock'} =~ s/\b(\d)\b/0$1/g;
+               $pos->{'white_clock'} = hms_to_sec($tags->{'WhiteClock'});
+               $pos->{'black_clock'} = hms_to_sec($tags->{'BlackClock'});
                return;
        }
 
@@ -1018,12 +1015,9 @@ sub extract_clock {
            $comments->{$white_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/ &&
            $comments->{$black_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/) {
                $comments->{$white_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/;
-               $pos->{'white_clock'} = $1;
+               $pos->{'white_clock'} = hms_to_sec($1);
                $comments->{$black_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/;
-               $pos->{'black_clock'} = $1;
-
-               $pos->{'white_clock'} =~ s/\b(\d)\b/0$1/g;
-               $pos->{'black_clock'} =~ s/\b(\d)\b/0$1/g;
+               $pos->{'black_clock'} = hms_to_sec($1);
                return;
        }
 
@@ -1031,6 +1025,13 @@ sub extract_clock {
        delete $pos->{'black_clock'};
 }
 
+sub hms_to_sec {
+       my $hms = shift;
+       return undef if (!defined($hms));
+       $hms =~ /(\d+):(\d+):(\d+)/;
+       return $1 * 3600 + $2 * 60 + $3;
+}
+
 sub find_clock_start {
        my $pos = shift;
 
@@ -1070,8 +1071,7 @@ sub find_clock_start {
                # No clock information.
                return;
        }
-       $pos->{$key} =~ /(\d+):(\d+):(\d+)/;
-       my $time_left = $1 * 3600 + $2 * 60 + $3;
+       my $time_left = $pos->{$key};
        $clock_target_for_pos{$id} = time + $time_left;
        if ($pos->{'toplay'} eq 'W') {
                $pos->{'white_clock_target'} = $clock_target_for_pos{$id};
index 8dbeccb..5d12c43 100644 (file)
@@ -894,15 +894,16 @@ var update_clock = function() {
                }
        }
 
-       var white_clock = "";
-       var black_clock = "";
+       var white_clock_ms = null;
+       var black_clock_ms = null;
+       var show_seconds = false;
 
        // Static clocks.
        if (data['position'] &&
            data['position']['white_clock'] &&
            data['position']['black_clock']) {
-               white_clock = data['position']['white_clock'].replace(/:[0-5][0-9]$/, "");
-               black_clock = data['position']['black_clock'].replace(/:[0-5][0-9]$/, "");
+               white_clock_ms = data['position']['white_clock'] * 1000;
+               black_clock_ms = data['position']['black_clock'] * 1000;
        }
 
        // Dynamic clock (only one, obviously).
@@ -919,30 +920,52 @@ var update_clock = function() {
                $("#whiteclock").removeClass("running-clock");
                $("#blackclock").removeClass("running-clock");
        }
+       var remaining_ms;
        if (color) {
                var now = new Date().getTime() + client_clock_offset_ms;
-               var remaining_ms = data['position'][color + '_clock_target'] * 1000 - now;
+               remaining_ms = data['position'][color + '_clock_target'] * 1000 - now;
                if (color === "white") {
-                       white_clock = format_clock(remaining_ms);
+                       white_clock_ms = remaining_ms;
                } else {
-                       black_clock = format_clock(remaining_ms);
+                       black_clock_ms = remaining_ms;
                }
+       }
+
+       if (white_clock_ms === null || black_clock_ms === null) {
+               $("#whiteclock").empty();
+               $("#blackclock").empty();
+               return;
+       }
+
+       // If either player has ten minutes or less left, add the second counters.
+       var show_seconds = (white_clock_ms < 60 * 10 * 1000 || black_clock_ms < 60 * 10 * 1000);
 
+       if (color) {
                // See when the clock will change next, and update right after that.
-               var next_update_ms = remaining_ms % 60000 + 100;
+               var next_update_ms;
+               if (show_seconds) {
+                       next_update_ms = remaining_ms % 1000 + 100;
+               } else {
+                       next_update_ms = remaining_ms % 60000 + 100;
+               }
                clock_timer = setTimeout(update_clock, next_update_ms);
        }
 
-       $("#whiteclock").text(white_clock);
-       $("#blackclock").text(black_clock);
+       $("#whiteclock").text(format_clock(white_clock_ms, show_seconds));
+       $("#blackclock").text(format_clock(black_clock_ms, show_seconds));
 }
 
 /**
  * @param {Number} remaining_ms
+ * @param {boolean} show_seconds
  */
-var format_clock = function(remaining_ms) {
+var format_clock = function(remaining_ms, show_seconds) {
        if (remaining_ms <= 0) {
-               return "00:00";
+               if (show_seconds) {
+                       return "00:00:00";
+               } else {
+                       return "00:00";
+               }
        }
 
        var remaining = Math.floor(remaining_ms / 1000);
@@ -951,7 +974,11 @@ var format_clock = function(remaining_ms) {
        var minutes = remaining % 60;
        remaining = (remaining - minutes) / 60;
        var hours = remaining;
-       return format_2d(hours) + ":" + format_2d(minutes);
+       if (show_seconds) {
+               return format_2d(hours) + ":" + format_2d(minutes) + ":" + format_2d(seconds);
+       } else {
+               return format_2d(hours) + ":" + format_2d(minutes);
+       }
 }
 
 /**