gloat.pl (1728B) [raw]
1 #!/usr/bin/env perl 2 # gloat.pl -- the Server 3 # 4 # Those who Euchre Gloat never Win 5 # ~ Andy Karle 6 use Mojolicious::Lite; 7 use Mojo::JSON qw(decode_json); 8 use Mojo::IOLoop; 9 use FindBin; 10 use lib "$FindBin::RealBin/lib"; 11 12 $::LOG = app->log; # save off for global access, befroe loading Euchre::Host 13 use Euchre::Host; 14 15 # Always log in debug, regardless of prod vs preprod 16 app->log->level('debug'); 17 18 get '/' => sub { 19 my $c = shift; 20 $c->reply->static('index.html'); 21 }; 22 23 get '/game' => sub { 24 my $c = shift; 25 if (app->mode() eq 'production') { 26 $c->reply->static('prod.html'); 27 } else { 28 $c->reply->static('preprod.html'); 29 } 30 }; 31 32 get '/tables' => sub { 33 my $c = shift; 34 $c->render(json => { tables => list_tables }); 35 }; 36 37 get '/debug' => sub { 38 my $c = shift; 39 $c->reply->static('debug.html'); 40 }; 41 42 get '/stats' => sub { 43 my $c = shift; 44 $c->render(text => stats, format => 'txt'); 45 }; 46 47 websocket '/play' => sub { 48 my $c = shift; 49 50 my $id = ''.$c->tx; 51 app->log->debug("New player: $id"); 52 53 # Register the player with the server 54 register_player($c->tx); 55 56 $c->on(message => sub { 57 my ($c, $msg) = @_; 58 handle_msg($id, decode_json($msg)); 59 }); 60 61 $c->on(finish => sub { 62 gloaters_never_win($id); 63 }); 64 }; 65 66 my $cleanup_time = $ENV{DEBUG} ? 1 : 300; 67 Mojo::IOLoop->recurring($cleanup_time => sub { 68 prune_tables(); 69 prune_players(); 70 }); 71 72 # Our prod environment is a true daemon 73 if (app->mode() eq 'production') { 74 app->hook( 75 before_server_start => sub { 76 my ($server, $app) = @_; 77 $server->daemonize(); 78 app->log->path("/var/log/gloat/prod.log"); 79 } 80 ); 81 } 82 83 app->start;