/
player_service
executable file
·44 lines (35 loc) · 1.06 KB
/
player_service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use HTTP::Daemon;
use HTTP::Response;
use HTTP::Status qw(:constants);
use JSON::MaybeXS;
use Player;
my $port = $ENV{PORT} || 1234;
my $daemon = HTTP::Daemon->new(
LocalPort => $port,
) || die "Can't start daemon: $!";
$SIG{INT} = sub { die 'Interrupted' };
my $player = Player->new;
while ( my $client = $daemon->accept ) {
while ( my $request = $client->get_request ) {
my $params = CGI->new( $request->decoded_content )->Vars;
my $response = HTTP::Response->new();
my $action = $params->{action};
my $game_state = decode_json( $params->{game_state} )
if defined $params->{game_state};
if ( $player->can($action) ) {
$response->code(HTTP_OK);
$response->content( $player->$action($game_state) );
}
else {
$response->code(HTTP_NOT_IMPLEMENTED);
$response->content('Unimplemented action');
}
$client->send_response($response);
}
$client->close;
undef($client);
}