Add local time management, for when incoming time is bad or nonexistent.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 15 May 2015 23:10:16 +0000 (01:10 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 15 May 2015 23:10:16 +0000 (01:10 +0200)
config.pm
remoteglot.pl

index e036a4d..4a65363 100644 (file)
--- a/config.pm
+++ b/config.pm
@@ -16,6 +16,23 @@ our $pgn_filter = undef;
 #      return $pgn->round() eq '7' && $pgn->white eq 'Carlsen,M';
 #};
 
+# Set to non-undef to override the clock information with our own calculations.
+# The example implements a simple 60+60 (with bonus added before the move).
+our $adjust_clocks_before_move = undef;
+#our $adjust_clocks_before_move = sub {
+#        my ($white_clock_left, $black_clock_left, $move, $toplay) = @_;
+#
+#        if (!defined($$white_clock_left) || !defined($$black_clock_left)) {
+#                $$white_clock_left = 3600;
+#                $$black_clock_left = 3600;
+#        }
+#        if ($toplay eq 'W') {
+#                $$white_clock_left += 60;
+#        } else {
+#                $$black_clock_left += 60;
+#        }
+#};
+
 our $json_output = "/srv/analysis.sesse.net/www/analysis.json";
 our $json_history_dir = "/srv/analysis.sesse.net/www/history/";  # undef for none.
 
index a94a618..7b3d967 100755 (executable)
@@ -326,7 +326,7 @@ sub handle_pgn {
 
 sub handle_position {
        my ($pos) = @_;
-       find_clock_start($pos);
+       find_clock_start($pos, $pos_calculating);
                
        # if this is already in the queue, ignore it (just update the result)
        if (defined($pos_waiting) && $pos->fen() eq $pos_waiting->fen()) {
@@ -1033,7 +1033,7 @@ sub hms_to_sec {
 }
 
 sub find_clock_start {
-       my $pos = shift;
+       my ($pos, $prev_pos) = @_;
 
        # If the game is over, the clock is stopped.
        if (exists($pos->{'result'}) &&
@@ -1045,6 +1045,9 @@ sub find_clock_start {
 
        # When we don't have any moves, we assume the clock hasn't started yet.
        if ($pos->{'move_num'} == 1 && $pos->{'toplay'} eq 'W') {
+               if (defined($remoteglotconf::adjust_clocks_before_move)) {
+                       &$remoteglotconf::adjust_clocks_before_move(\$pos->{'white_clock'}, \$pos->{'black_clock'}, 1, 'W');
+               }
                return;
        }
 
@@ -1066,6 +1069,23 @@ sub find_clock_start {
 
        # OK, we haven't seen this position before, so we assume the move
        # happened right now.
+
+       # See if we should do our own clock management (ie., clock information
+       # is spurious or non-existent).
+       if (defined($remoteglotconf::adjust_clocks_before_move)) {
+               my $wc = $pos->{'white_clock'} // $prev_pos->{'white_clock'};
+               my $bc = $pos->{'black_clock'} // $prev_pos->{'black_clock'};
+               if (defined($prev_pos->{'white_clock_target'})) {
+                       $wc = $prev_pos->{'white_clock_target'} - time;
+               }
+               if (defined($prev_pos->{'black_clock_target'})) {
+                       $bc = $prev_pos->{'black_clock_target'} - time;
+               }
+               &$remoteglotconf::adjust_clocks_before_move(\$wc, \$bc, $pos->{'move_num'}, $pos->{'toplay'});
+               $pos->{'white_clock'} = $wc;
+               $pos->{'black_clock'} = $bc;
+       }
+
        my $key = ($pos->{'toplay'} eq 'W') ? 'white_clock' : 'black_clock';
        if (!exists($pos->{$key})) {
                # No clock information.
@@ -1075,8 +1095,10 @@ sub find_clock_start {
        $clock_target_for_pos{$id} = time + $time_left;
        if ($pos->{'toplay'} eq 'W') {
                $pos->{'white_clock_target'} = $clock_target_for_pos{$id};
+               delete $pos->{'black_clock_target'};
        } else {
                $pos->{'black_clock_target'} = $clock_target_for_pos{$id};
+               delete $pos->{'white_clock_target'};
        }
 }