#!/usr/bin/env perl

use strict;
use warnings;
use Mojo::UserAgent;
use AnyEvent::ReadLine::Gnu;
use EV;

my $ua = Mojo::UserAgent->new;
my $ws = shift @ARGV || 'wss://aitestbed.com:443/websocket';

$ua->websocket(
  $ws => sub {
    my ( $ua, $tx ) = @_;

    my $rl = new AnyEvent::ReadLine::Gnu
     prompt  => "$ws> ",
     on_line => sub {
      my $i = shift;
      exit 0 if $i =~ /quit/i;
      if ( $i && !$tx->is_finished ) {
        $tx->send($i);
      }
    };

    $tx->on(
      finish => sub {
        my ( $tx, $code ) = @_;
        AnyEvent::ReadLine::Gnu->print("WebSocket closed with code $code.");
        exit 0;
      }
    );

    $tx->on(
      message => sub {
        my ( $tx, $msg ) = @_;
        AnyEvent::ReadLine::Gnu->print("server: $msg\n");
      }
    );
    $tx->send('["guest_login"]');
  }
);

EV::run;

=head1 NAME

wssh.pl - WebSocket Shell

=head1 DESCRIPTION

This is a command line interface to the aitestbed.com poker server. It establishes a websocket connection to the server and allows you to receive and send JSON encoded messages from the command line.  

=head1 DEPENDENCIES

Mojo::UserAgent;
AnyEvent::ReadLine::Gnu;
EV;

=head1 LICENSE AND COPYRIGHT

Copyright 2016 Nathaniel Graham.

This program is free software; you can redistribute it and/or modify it
under the terms of the MIT license.

=cut
