| 1 | # Declare our package | ||||||||||||||||||||||||||||
| 2 | package Games::AssaultCube::MasterserverQuery; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # import the Moose stuff | ||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use MooseX::StrictConstructor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # Initialize our version | ||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # get some utility stuff | ||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Games::AssaultCube::MasterserverQuery::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # TODO make validation so we accept a *real* URI | ||||||||||||||||||||||||||||
| 18 | 2 | has 'server' => ( | |||||||||||||||||||||||||||
| 19 | isa => 'Str', | ||||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||
| 21 | default => 'http://masterserver.cubers.net/cgi-bin/AssaultCube.pl/retrieve.do?item=list', | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | has 'timeout' => ( | |||||||||||||||||||||||||||
| 25 | isa => 'Int', | ||||||||||||||||||||||||||||
| 26 | is => 'rw', | ||||||||||||||||||||||||||||
| 27 | default => 30, | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | has 'useragent' => ( | ||||||||||||||||||||||||||||
| 31 | isa => 'LWP::UserAgent', | ||||||||||||||||||||||||||||
| 32 | is => 'rw', | ||||||||||||||||||||||||||||
| 33 | 1 | ▶ | default => sub { return LWP::UserAgent->new }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 2 | ); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | has 'request' => ( | ||||||||||||||||||||||||||||
| 37 | isa => 'HTTP::Request', | ||||||||||||||||||||||||||||
| 38 | is => 'rw', | ||||||||||||||||||||||||||||
| 39 | lazy => 1, | ||||||||||||||||||||||||||||
| 40 | 0 | ▶ | default => sub { return HTTP::Request->new( GET => $_[0]->server ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 41 | 2 | ); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=3 SCAR=13.3
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | if ( @_ == 1 && ! ref $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ == 1 and not ref $_[0]
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1
not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
| |||||||||||||||||||||||||||||
| 47 | # set the server as the first argument | ||||||||||||||||||||||||||||
| 48 | 0 | return { server => $_[0] }; | |||||||||||||||||||||||||||
| 49 | } else { | ||||||||||||||||||||||||||||
| 50 | # normal hash/hashref way | ||||||||||||||||||||||||||||
| 51 | 1 | return $class->SUPER::BUILDARGS(@_); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # set the alarm, and wait for the response | ||||||||||||||||||||||||||||
| 59 | 0 | my( $res ); | |||||||||||||||||||||||||||
| 60 | 0 | eval { | |||||||||||||||||||||||||||
| 61 | # perldoc -f alarm says I need to put \n in the die... weird! | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | local $SIG{ALRM} = sub { die "alarm\n" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 63 | 0 | alarm $self->timeout; | |||||||||||||||||||||||||||
| 64 | 0 | $res = $self->useragent->request( $self->request ); | |||||||||||||||||||||||||||
| 65 | 0 | alarm 0; | |||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ( $@ =~ /^alarm/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | die "Unable to query server: Timed out"; | |||||||||||||||||||||||||||
| 70 | } else { | ||||||||||||||||||||||||||||
| 71 | 0 | die "Unable to query server: $@"; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ( defined $res and $res->is_success ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $res and $res->is_success
MC/DC0%0/2
defined $res and $res->is_success
defined $res
$res->is_success
Truth table0%0/3
A and B
A= defined $resB= $res->is_success
| |||||||||||||||||||||||||||||
| 75 | 0 | return Games::AssaultCube::MasterserverQuery::Response->new( $self, $res ); | |||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | 0 | return; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # from Moose::Manual::BestPractices | ||||||||||||||||||||||||||||
| 83 | 2 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | 1; | |||||||||||||||||||||||||||
| 87 | __END__ | ||||||||||||||||||||||||||||