| 1 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package Net::OTServ; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Retrieve status information about Open Tibia Servers | ||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.004'; # VERSION | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use IO::Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use IO::Socket::Timeout; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use XML::Hash::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =pod | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =encoding utf8 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Net::OTServ - Retrieve status information about Open Tibia Servers | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | use Net::OTServ; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | my $status = Net::OTServ::status("127.0.0.1", 7171); | ||||||||||||||||||||||||||||
| 27 | print "We got $status->{players}{online} players online!\n" | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Open Tibia servers offer a XML interface to query online count, client version and other information. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 METHODS AND ARGUMENTS | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =over 4 | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =item status($ip [, $port]) | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Retrieves the status of specified OTServ as a hash reference. If C<$port> is omitted, the default 7171 is assumed. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | sub status { | ||||||||||||||||||||||||||
|
Subroutine
status: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 4 | my $ip = shift; | |||||||||||||||||||||||||||
| 46 | 4 | ▶ | my $port = shift || 7171; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 7171
MC/DC100%1/1
shift() || 7171
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 47 | 4 | my $timeout = 1; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 4 | ▶ | my $ot = IO::Socket::INET->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | PeerAddr => $ip, | ||||||||||||||||||||||||||||
| 51 | PeerPort => $port, | ||||||||||||||||||||||||||||
| 52 | Proto => 'tcp', | ||||||||||||||||||||||||||||
| 53 | Timeout => $timeout, # connection timeout | ||||||||||||||||||||||||||||
| 54 | ) or croak "OTServ at $ip:$port is offline."; | ||||||||||||||||||||||||||||
| 55 | 2 | IO::Socket::Timeout->enable_timeouts_on($ot); | |||||||||||||||||||||||||||
| 56 | 2 | $ot->read_timeout($timeout); | |||||||||||||||||||||||||||
| 57 | 2 | $ot->write_timeout($timeout); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | $ot->write("\x06\x00\xFF\xFF\x69\x6E\x66\x6F"); | |||||||||||||||||||||||||||
| 60 | 2 | my $xml; $ot->recv($xml, 1500); | |||||||||||||||||||||||||||
| 61 | 2 | ▶ | $xml or croak "Server at $ip:$port doesn't reply."; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 2 | my $status; eval { $status = xml2hash $xml }; | |||||||||||||||||||||||||||
| 63 | 2 | ▶ | $status and !$@ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status and not $@
MC/DC50%1/2
$status and not $@
$status
not $@
Truth table66%2/3
A and not(B)
A= $statusB= not $@
| |||||||||||||||||||||||||||||
| 64 | or croak "Server at $ip:$port doesn't reply in XML."; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | return $status; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 2 | 1; | |||||||||||||||||||||||||||
| 73 | __END__ | ||||||||||||||||||||||||||||