X-Git-Url: https://git.sesse.net/?p=remoteglot;a=blobdiff_plain;f=remoteglot.pl;h=5a5d4c556ad4d6607111f12a8012ed79cf37eccc;hp=7846e83ef46f5d4090f50103a395e5a50c7ce767;hb=b643b81793e1457b373cd9f24635ae0757621ab7;hpb=30d4e71134ad769aacb702a556e0129eec117998 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);