aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/TCP/ListCompiler.pm
diff options
context:
space:
mode:
authorDarkelarious <darkelarious@333networks.com>2017-08-22 11:00:13 +0200
committerDarkelarious <darkelarious@333networks.com>2017-08-22 11:00:13 +0200
commitc06322da38b4cb76b2036af1a5448083adb8ff20 (patch)
tree189c9f0fec3325be927f763aba23cf18aa68cfe4 /lib/MasterServer/TCP/ListCompiler.pm
parente0d727670cbeda0db0812c5c9efc503d75f8d0a4 (diff)
downloadMasterServer-Perl-c06322da38b4cb76b2036af1a5448083adb8ff20.tar.gz
MasterServer-Perl-c06322da38b4cb76b2036af1a5448083adb8ff20.zip
new server checking mechanism, complete recode of major functionsv2.4.0
Diffstat (limited to 'lib/MasterServer/TCP/ListCompiler.pm')
-rwxr-xr-xlib/MasterServer/TCP/ListCompiler.pm133
1 files changed, 44 insertions, 89 deletions
diff --git a/lib/MasterServer/TCP/ListCompiler.pm b/lib/MasterServer/TCP/ListCompiler.pm
index c035008..edced5e 100755
--- a/lib/MasterServer/TCP/ListCompiler.pm
+++ b/lib/MasterServer/TCP/ListCompiler.pm
@@ -3,120 +3,75 @@ package MasterServer::TCP::ListCompiler;
use strict;
use warnings;
use Exporter 'import';
-
-our @EXPORT = qw| compile_list compile_list_cmp compile_sync |;
+our @EXPORT = qw| generate_list generate_sync compile_sync |;
################################################################################
## compile the list of \ip\ip:port\ addresses and parse them into the
-## plaintext return string.
+## plaintext or compressed address string.
################################################################################
-sub compile_list {
- my ($self, $gamename) = @_;
+sub generate_list {
+ # gamename and \list\(|cmp)
+ my ($self, $gamename, $cmp) = @_;
# get the list from database
my $serverlist = $self->get_server(
- updated => 3600,
- gamename => $gamename,
- );
-
- # prepare empty return string
- my $response_string = "";
-
- # add address as regular \ip\127.0.0.1:7777\ format
- for (@{$serverlist}){
-
- # append \ip\ip:port to string
- $response_string .= "\\ip\\$_->{ip}:$_->{port}";
- }
+ updated => 3600,
+ gamename => $gamename,
+ );
- # return the string with data
- return $response_string;
-}
-
-################################################################################
-## compile the list of binary ip:port addresses and parse them into the
-## ABCDE return string.
-################################################################################
-sub compile_list_cmp {
- my ($self, $gamename) = @_;
-
- # get the list from database
- my $serverlist = $self->get_server(
- updated => 3600,
- gamename => $gamename,
- );
-
- # prepare empty return string
- my $response_string = "";
-
- # compile a return string
- for (@{$serverlist}){
-
- # convert ip address to ABCDEF mode
- my ($A, $B, $C, $D) = ($_->{ip} =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/);
- my ($E, $F) = ($_->{port} >> 8, $_->{port} & 0xFF);
-
- # print as chr string of 6 bytes long
- my $bin = ""; $bin .= (chr $A) . (chr $B) . (chr $C) . (chr $D) . (chr $E) . (chr $F);
-
- # append to list of addresses
- $response_string .= $bin;
+ my $list = "";
+ # which format?
+ if ($cmp eq "cmp") {
+ # compressed format (ABCDEF format)
+ for (@{$serverlist}) {
+ my ($A, $B, $C, $D) = ($_->{ip} =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/);
+ my ($E, $F) = ($_->{port} >> 8, $_->{port} & 0xFF);
+ my $bin = ""; $bin .= (chr $A) . (chr $B) . (chr $C) . (chr $D) . (chr $E) . (chr $F);
+ $list .= $bin;}
}
-
- # return the string with data
- return $response_string;
+ else {
+ # normal format (regular \ip\127.0.0.1:7777\ format)
+ for (@{$serverlist}) {
+ $list .= "\\ip\\$_->{ip}:$_->{port}";}
+ }
+
+ # list ready
+ return $list;
}
-
################################################################################
-## compile a list of all requested games --or-- if not specified, a list of
-## all games
+## compile a list of addresses for all available or requested games.
+## opts: all | list of games
################################################################################
-sub compile_sync {
+sub generate_sync {
my ($self, $sync) = @_;
-
- # prepare empty return string
- my $response_string = "";
- my @games;
-
- # client requests to sync all games
+ my $list = "";
+ my %games = ();
+
+ # prepare list of games
+ my $avail = $self->get_gamenames();
if ($sync eq "all") {
- # get array of gamenames from db
- my $sg = $self->get_gamenames();
- for (@{$sg}) {push @games, $_->[0];}
- }
- # only selected games
+ # if "all" is requested, check which games we have available
+ $games{$_->[0]} = 1 for (@{$avail}); }
else {
- # split request into array
- @games = split " ", $sync;
- }
+ # otherwise, see which of the requested addresses match our db
+ for (@{$avail}) {$games{$_->[0]} = 1 if ($sync =~ m/$_->[0]/i); } }
- # only get unique values from array
- my %games = map { $_ => 1 } @games;
-
# get the list for every requested gamename
- for my $g (keys %games) {
-
- # $g is now a gamename -- check if it's supported. Else ignore.
- if ($self->get_game_props(gamename => $g)) {
+ for my $gamename (keys %games) {
# get list from database
- my $list = $self->get_server(
- updated => 7200,
- gamename => $g,
- );
+ my $listref = $self->get_server(gamename => $gamename, updated => 3600);
# add all games to string separated by spaces
- my $gamestring = "";
- foreach $_ (@{$list}) {$gamestring .= "$_->{ip}:$_->{port} ";}
+ my $addresses = "";
+ foreach (@{$listref}) {$addresses .= "$_->{ip}:$_->{port} ";}
# if it contains at least one entry, add the list to the response list
- $response_string .= "\\$g\\$gamestring" if (length $gamestring >= 7);
- }
+ $list .= "\\$gamename\\$addresses" if (length $addresses >= 7);
}
-
- # return \gamename\addresses\gamename2\addresses2 list
- return $response_string;
+ # list ready
+ return $list;
}
1;