Add some support for listing games in progress, and switching between them. (Proper...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 27 Jun 2015 23:27:39 +0000 (01:27 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 27 Jun 2015 23:27:39 +0000 (01:27 +0200)
remoteglot.pl
remoteglot.sql
www/css/remoteglot.css
www/index.html
www/js/remoteglot.js

index 7846e83..5a5d4c5 100755 (executable)
@@ -15,12 +15,11 @@ use AnyEvent::HTTP;
 use Chess::PGN::Parse;
 use EV;
 use Net::Telnet;
-use FileHandle;
+use File::Slurp;
 use IPC::Open2;
 use Time::HiRes;
 use JSON::XS;
 use URI::Escape;
-use Tie::Persistent;
 use DBI;
 use DBD::Pg;
 require 'Position.pm';
@@ -823,6 +822,37 @@ sub output_json {
                $json->{'score_history'} = \%score_history;
        }
 
+       # Give out a list of other games going on. (Empty is fine.)
+       if (!$historic_json_only) {
+               my @games = ();
+
+               my $q = $dbh->prepare('SELECT * FROM current_games ORDER BY priority DESC, id');
+               $q->execute;
+               while (my $ref = $q->fetchrow_hashref) {
+                       eval {
+                               my $other_game_contents = File::Slurp::read_file($ref->{'json_path'});
+                               my $other_game_json = JSON::XS::decode_json($other_game_contents);
+
+                               die "Missing position" if (!exists($other_game_json->{'position'}));
+                               my $white = $other_game_json->{'position'}{'player_w'} // die 'Missing white';
+                               my $black = $other_game_json->{'position'}{'player_b'} // die 'Missing black';
+
+                               push @games, {
+                                       id => $ref->{'id'},
+                                       name => "$white–$black",
+                                       url => $ref->{'url'}
+                               };
+                       };
+                       if ($@) {
+                               warn "Could not add external game " . $ref->{'json_path'} . ": $@";
+                       }
+               }
+
+               if (scalar @games > 0) {
+                       $json->{'games'} = \@games;
+               }
+       }
+
        my $json_enc = JSON::XS->new;
        $json_enc->canonical(1);
        my $encoded = $json_enc->encode($json);
index ed5d1d8..4ad748c 100644 (file)
@@ -14,3 +14,10 @@ CREATE TABLE clock_info (
        white_clock_target integer,  -- FIXME: really timestamp with time zone
        black_clock_target integer   -- FIXME: really timestamp with time zone
 );
+
+CREATE TABLE current_games (
+       id varchar not null primary key,
+       json_path varchar not null,
+       url varchar not null,
+       priority integer not null default 0,
+);
index 6232f06..21a3a85 100644 (file)
@@ -1,6 +1,9 @@
 body {
        font-family: sans-serif;
 }
+h1 {
+       margin-top: 0em;
+}
 h3 {
        margin-top: 1em;
        margin-bottom: 0;
@@ -175,3 +178,17 @@ a.move:hover {
 #linenav {
        display: none;
 }
+
+#games {
+       font-size: smaller;
+       margin-bottom: 0.5em;
+}
+.game {
+       padding: 0.25em 1em 0.25em 1em;
+       margin: 0;
+       background: #eee;
+       border-right: 1px solid #bbb;
+}
+.game:last-of-type {
+       border-right: none;
+}
index 4d13cce..3c1eb85 100644 (file)
@@ -14,6 +14,8 @@
   <source src="ding.opus" type="audio/ogg; codecs=opus" />
   <source src="ding.mp3" type="audio/mp3" />
 </audio>
+<div id="games">
+</div>
 <h1 id="headline">Analysis</h1>
 <div id="boardcontainer">
   <div id="board"></div>
index 12b349a..0462252 100644 (file)
@@ -738,6 +738,28 @@ var update_board = function(current_data, display_data) {
        }
        update_history();
 
+       // Games currently in progress, if any.
+       $("#games").text("");
+       if (current_data['games']) {
+               var games_div = document.getElementById('games');
+               for (var game_num = 0; game_num < current_data['games'].length; ++game_num) {
+                       var game = current_data['games'][game_num];
+                       var game_span = document.createElement("span");
+                       game_span.setAttribute("class", "game");
+
+                       var game_name = document.createTextNode(game['name']);
+                       if (game['url'] === backend_url) {
+                               game_span.appendChild(game_name);
+                       } else {
+                               var game_a = document.createElement("a");
+                               game_a.setAttribute("href", "javascript:switch_backend('" + game['url'] + "')");
+                               game_a.appendChild(game_name);
+                               game_span.appendChild(game_a);
+                       }
+                       games_div.appendChild(game_span);
+               }
+       }
+
        // The headline. Names are always fetched from current_data;
        // the rest can depend a bit.
        var headline;