Games::AssaultCube::MasterserverQuery::Response - Holds the various data from a MasterserverQuery response


Games-AssaultCube documentation Contained in the Games-AssaultCube distribution.

Index


Code Index:

NAME

Top

Games::AssaultCube::MasterserverQuery::Response - Holds the various data from a MasterserverQuery response

SYNOPSIS

Top

	use Games::AssaultCube::MasterserverQuery;
	my $query = Games::AssaultCube::MasterserverQuery->new;
	#my $query = Games::AssaultCube::MasterserverQuery->new( 'http://foo.com/get.do' );
	#my $query = Games::AssaultCube::MasterserverQuery->new({ server => 'http://foo.com/get.do', timeout => 5 });
	my $response = $query->run;
	if ( defined $response ) {
		print "There is a total of " . $response->num_servers " servers in the list!\n";
	} else {
		print "Masterserver is not responding!\n";
	}

ABSTRACT

Top

This module holds the various data from a MasterserverQuery response

DESCRIPTION

Top

This module holds the response data from an AssaultCube MasterserverQuery. Normally you will not use this class directly, but via the Games::AssaultCube::MasterserverQuery class.

Attributes

You can get the various data by fetching the attribute. Valid attributes are:

masterserver

The URI of the masterserver we queried

servers

An arrayref of hashrefs of servers in the list

The hashref contains the following keys: ip and port

num_servers

A convenience accessor returning the number of servers in the list

response

The HTTP::Response object in case you wanted to poke around

tohash

A convenience accessor returning "vital" data in a hashref for easy usage

AUTHOR

Top

Apocalypse <apocal@cpan.org>

Props goes to Getty and the BS clan for the support!

This project is sponsored by http://cubestats.net

COPYRIGHT AND LICENSE

Top


Games-AssaultCube documentation Contained in the Games-AssaultCube distribution.

# Declare our package
package Games::AssaultCube::MasterserverQuery::Response;

# import the Moose stuff
use Moose;
use MooseX::StrictConstructor;

# Initialize our version
use vars qw( $VERSION );
$VERSION = '0.04';

# get some utility stuff
use Games::AssaultCube::Utils qw( parse_masterserverresponse );

# TODO improve validation for everything here, ha!

has 'masterserver' => (
	isa		=> 'Str',
	is		=> 'ro',
	required	=> 1,
);

has 'servers' => (
	isa		=> 'ArrayRef[HashRef]',
	is		=> 'ro',
	default		=> sub { [] },
);

has 'num_servers' => (
	isa		=> 'Int',
	is		=> 'ro',
	lazy		=> 1,
	default		=> sub {
		my $self = shift;
		return scalar @{ $self->servers };
	},
);

has 'response' => (
	isa		=> 'HTTP::Response',
	is		=> 'ro',
	required	=> 1,
);

has 'tohash' => (
	isa		=> 'HashRef',
	is		=> 'ro',
	lazy		=> 1,
	default		=> sub {
		my $self = shift;
		my $data = {
			masterserver	=> $self->masterserver,
			servers		=> [ map { { ip => $_->{ip}, port => $_->{port} } } @{ $self->servers } ],
		};
		return $data;
	},
);

sub BUILDARGS {
	my $class = shift;

	# Normally, we would be created by Games::AssaultCube::MasterserverQuery and contain 2 args
	if ( @_ == 2 && ref $_[0] && $_[0]->isa( 'Games::AssaultCube::MasterserverQuery' ) ) {
		# call the parse method
		return {
			masterserver	=> $_[0]->server,
			servers		=> parse_masterserverresponse( $_[1] ),
			response	=> $_[1],
		};
	} else {
		return $class->SUPER::BUILDARGS(@_);
	}
}

# from Moose::Manual::BestPractices
no Moose;
__PACKAGE__->meta->make_immutable;

1;
__END__