From: Steinar H. Gunderson Date: Sat, 27 Jun 2015 23:27:39 +0000 (+0200) Subject: Add some support for listing games in progress, and switching between them. (Proper... X-Git-Url: https://git.sesse.net/?p=remoteglot;a=commitdiff_plain;h=b643b81793e1457b373cd9f24635ae0757621ab7 Add some support for listing games in progress, and switching between them. (Proper linking through #-URLs will come later.) --- diff --git a/remoteglot.pl b/remoteglot.pl index 7846e83..5a5d4c5 100755 --- a/remoteglot.pl +++ b/remoteglot.pl @@ -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); diff --git a/remoteglot.sql b/remoteglot.sql index ed5d1d8..4ad748c 100644 --- a/remoteglot.sql +++ b/remoteglot.sql @@ -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, +); diff --git a/www/css/remoteglot.css b/www/css/remoteglot.css index 6232f06..21a3a85 100644 --- a/www/css/remoteglot.css +++ b/www/css/remoteglot.css @@ -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; +} diff --git a/www/index.html b/www/index.html index 4d13cce..3c1eb85 100644 --- a/www/index.html +++ b/www/index.html @@ -14,6 +14,8 @@ +
+

Analysis

diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 12b349a..0462252 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -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;