]> git.sesse.net Git - remoteglot/blobdiff - Position.pm
Count connected viewers a bit more precisely; in particular, rely more on sleeping...
[remoteglot] / Position.pm
index 5d63ae2ff662d9abce22ba9bbe40a729ea7b8f8d..6b772a67b82fafe4bcffb5b44a09849a8fb49eeb 100644 (file)
@@ -4,6 +4,7 @@
 #
 use strict;
 use warnings;
+use MIME::Base64;
 
 require 'Board.pm';
 
@@ -28,7 +29,13 @@ sub new {
        $pos->{'player_w'} =~ s/^W?[FCIG]M//;
        $pos->{'player_b'} =~ s/^W?[FCIG]M//;
        $pos->{'move_num'} = $x[26];
+       if ($x[27] =~ /([a-h][1-8])-([a-h][1-8])/) {
+               $pos->{'last_move_uci'} = $1 . $2;
+       } else {
+               $pos->{'last_move_uci'} = undef;
+       }
        $pos->{'last_move'} = $x[29];
+       $pos->{'prettyprint_cache'} = {};
 
        bless $pos, $class;
        return $pos;
@@ -36,6 +43,8 @@ sub new {
 
 sub start_pos {
        my ($class, $white, $black) = @_;
+       $white = "base64:" . MIME::Base64::encode_base64($white);
+       $black = "base64:" . MIME::Base64::encode_base64($black);
        return $class->new("<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 dummygamenum $white $black -2 dummytime dummyincrement 39 39 dummytime dummytime 1 none (0:00) none 0 0 0");
 }
 
@@ -103,7 +112,14 @@ sub fen {
 
 sub to_json_hash {
        my $pos = shift;
-       return { %$pos, board => undef, fen => $pos->fen() };
+       my $json = { %$pos, board => undef, prettyprint_cache => undef, fen => $pos->fen() };
+       if ($json->{'player_w'} =~ /^base64:(.*)$/) {
+               $json->{'player_w'} = MIME::Base64::decode_base64($1);
+       }
+       if ($json->{'player_b'} =~ /^base64:(.*)$/) {
+               $json->{'player_b'} = MIME::Base64::decode_base64($1);
+       }
+       return $json;
 }
 
 sub parse_pretty_move {
@@ -167,7 +183,10 @@ sub make_move {
        }
        $np->{'player_w'} = $pos->{'player_w'};
        $np->{'player_b'} = $pos->{'player_b'};
-       $np->{'last_move'} = '(move)';  # FIXME
+       my ($move, $nb) = $pos->{'board'}->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo);
+       $np->{'last_move'} = $move;
+       $np->{'last_move_uci'} = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
+
        return bless $np;
 }