diff options
| author | Darkelarious <darkelarious@333networks.com> | 2016-11-19 20:56:04 +0100 |
|---|---|---|
| committer | Darkelarious <darkelarious@333networks.com> | 2016-11-19 20:56:04 +0100 |
| commit | c3f8d65a4fb1f5674557ee67cf7f74369df86ad1 (patch) | |
| tree | 92aab2a394bda28da0ed7c7c75e633fdf386fc71 /lib/MasterServer/Database | |
| parent | 1de3da4b8027508a91144639455c934fd6ccb9b7 (diff) | |
| download | MasterServer-Perl-c3f8d65a4fb1f5674557ee67cf7f74369df86ad1.tar.gz MasterServer-Perl-c3f8d65a4fb1f5674557ee67cf7f74369df86ad1.zip | |
Massive improvements on efficiency, robustness, security, reliability and more
Diffstat (limited to 'lib/MasterServer/Database')
22 files changed, 836 insertions, 1649 deletions
diff --git a/lib/MasterServer/Database/Pg/dbBeacon.pm b/lib/MasterServer/Database/Pg/dbBeacon.pm deleted file mode 100755 index 735eaf9..0000000 --- a/lib/MasterServer/Database/Pg/dbBeacon.pm +++ /dev/null @@ -1,203 +0,0 @@ - -package MasterServer::Database::Pg::dbBeacon; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| add_beacon - add_pending - remove_pending - get_pending_beacon - get_pending_info - get_next_pending |; - -################################################################################ -## Update beacon in serverlist or pending list. Add if beacon does not exist in -## either list. Return 0,1,2 if success in adding or -1 on error. -################################################################################ -sub add_beacon { - my ($self, $ip, $beaconport, $heartbeat, $gamename, $secure) = @_; - - # if address is in list, update the timestamp - my $u = $self->{dbh}->do( - "UPDATE serverlist - SET beacon = NOW(), - updated = NOW(), - gamename = ?, - b333ms = TRUE - WHERE ip = ? - AND port = ?", - undef, lc $gamename, $ip, $heartbeat); - - # notify - $self->log("update", "beacon heartbeat for $ip:$heartbeat") if ($u > 0); - - # if serverlist was updated return 0 - return 0 if ($u > 0); - - # if it is already in the pending list, update it with a new challenge - $u = $self->{dbh}->do( - "UPDATE pending - SET added = NOW(), - beaconport = ?, - gamename = ?, - secure = ? - WHERE ip = ? - AND heartbeat = ?", - undef, $beaconport, lc $gamename, $secure, $ip, $heartbeat); - - # notify - $self->log("update", "beacon heartbeat $ip:$beaconport pending $gamename:$heartbeat") if ($u > 0); - - # beacon was already in pending list and was updated - return 1 if ($u > 0); - - # if not found, add it - $u = $self->{dbh}->do( - "INSERT INTO pending ( - ip, - beaconport, - heartbeat, - gamename, - secure) - SELECT ?, ?, ?, ?, ?", - undef, $ip, $beaconport, $heartbeat, lc $gamename, $secure); - - # notify - $self->log("add", "beacon heartbeat $ip:$beaconport pending $gamename:$heartbeat") if ($u > 0); - - # it was added to pending - return 2 if ($u > 0); - - # or else report error - $self->log("error", "an error occurred adding beacon $ip:$beaconport with $gamename:$heartbeat to the pending list"); - return -1; -} - -################################################################################ -## Add an address to the database that was obtained via a method other than -## an udp beacon. Return 0,1,2 if success in adding or -1 on error. -################################################################################ -sub add_pending { - my ($self, $ip, $port, $gamename, $secure) = @_; - - # if address is in list, update the timestamp - my $u = $self->{dbh}->do( - "UPDATE serverlist - SET updated = NOW() - WHERE ip = ? - AND port = ?", - undef, $ip, $port); - - # notify - $self->log("update", "updated serverlist with $ip:$port") if ($u > 0); - - # if updated, return 0 - return 0 if ($u > 0); - - # if it is already in the pending list, update it with a new challenge - $u = $self->{dbh}->do( - "UPDATE pending - SET added = NOW(), - secure = ? - WHERE ip = ? - AND heartbeat = ?", - undef, $secure, $ip, $port); - - # notify - $self->log("update", "updated pending with $ip:$port") if ($u > 0); - - # return 1 if updated - return 1 if ($u > 0); - - # if not found, add it - $u = $self->{dbh}->do( - "INSERT INTO pending ( - ip, - heartbeat, - gamename, - secure) - SELECT ?, ?, ?, ?", - undef, $ip, $port, $gamename, $secure); - - # notify - $self->log("add", "$ip:$port added pending $gamename") if ($u > 0); - - # return 2 if added new - return 2 if ($u > 0); - - # else - return -1; -} - -################################################################################ -## Remove an entry from the pending list. Returns 0 if removed or -1 in case -## of error(s). -################################################################################ -sub remove_pending { - my ($self, $id) = @_; - - # if address is in list, update the timestamp - my $u = $self->{dbh}->do( - "DELETE FROM pending - WHERE id = ?", - undef, $id); - - # notify - $self->log("delete", "removed pending id $id from pending") if ($u > 0); - - # it was removed from pending - return 2 if ($u > 0); - - # or else report error - $self->log("error", "error deleting server $id from pending"); - return -1; -} - -################################################################################ -## Get pending server by ip, beacon port. Returns * or undef -################################################################################ -sub get_pending_beacon { - my ($self, $ip, $port) = @_; - - # if address is in list, update the timestamp - return $self->{dbh}->selectall_arrayref( - "SELECT * FROM pending - WHERE ip = ? - AND beaconport = ?", - undef, $ip, $port)->[0]; -} - -################################################################################ -## Get pending server by ip, heartbeat port. Returns * or undef -################################################################################ -sub get_pending_info { - my ($self, $ip, $port) = @_; - - # if address is in list, update the timestamp - return $self->{dbh}->selectall_arrayref( - "SELECT * FROM pending - WHERE ip = ? - AND heartbeat = ?", - undef, $ip, $port)->[0]; -} - -################################################################################ -## Get server info from any entry with an id higher than the provided one. The -## server is added to pending at least 15 seconds ago. Returns info or undef. -################################################################################ -sub get_next_pending { - my ($self, $id) = @_; - - # get 1 pending id that is older than 15s - return $self->{dbh}->selectall_arrayref( - "SELECT id, ip, heartbeat, secure FROM pending - WHERE added < (NOW() - INTERVAL '15 SECONDS') - AND id > ? - ORDER BY id ASC LIMIT 1", - undef, $id)->[0]; -} - - -1; diff --git a/lib/MasterServer/Database/Pg/dbCiphers.pm b/lib/MasterServer/Database/Pg/dbCiphers.pm deleted file mode 100755 index e2e8191..0000000 --- a/lib/MasterServer/Database/Pg/dbCiphers.pm +++ /dev/null @@ -1,98 +0,0 @@ - -package MasterServer::Database::Pg::dbCiphers; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| clear_ciphers - insert_cipher - get_cipher - get_default_port |; - -################################################################################ -## Clear all existing ciphers from the database -################################################################################ -sub clear_ciphers { - my $self = shift; - - # delete ALL entries - my $u = $self->{dbh}->do("DELETE FROM games"); - - # notify - $self->log("delete", "Removed all ciphers") if ($u > 0); - - # removed from games - return 2 if ($u > 0); - - # or else report notice - $self->log("notice", "No ciphers deleted!"); - return -1; - -} - -################################################################################ -## Insert the list of supported games and their ciphers / default ports / -## descriptions included from the data/supportedgames.pl file. -################################################################################ -sub insert_cipher { - my ($self, %opt) = @_; - - # insert a single cipher/key combo - my $u = $self->{dbh}->do( - "INSERT INTO games ( - gamename, - cipher, - description, - default_qport) - VALUES(?, ?, ?, ?)", undef, - $opt{gamename}, $opt{cipher}, $opt{description}, $opt{default_qport}); - - # notify - $self->log("add", "Added cipher for $opt{gamename}") if ($u and $u > 0); - return 1 if ($u and $u > 0); - - # or else report error - $self->log("error", "An error occurred adding a cipher for $opt{gamename}"); - return -1; - -} - - -################################################################################ -## get the cipher that goes with gamename -################################################################################ -sub get_cipher { - my ($self, $gn) = @_; - - # no gamename specified? "undef" is not a known cipher, so send that instead. - return 'undef' if !$gn; - - # get cipher from db if gamename exists - my $cipher = $self->{dbh}->selectall_arrayref( - 'SELECT cipher FROM games WHERE gamename = ?', undef, - lc $gn)->[0]->[0]; - - # return a non-zero-length string - return ($cipher ? $cipher : 'undef'); -} - -################################################################################ -## get the default query port that goes with gamename -################################################################################ -sub get_default_port { - my ($self, $gn) = @_; - - # no gamename specified? default port is 0 - return 0 if !$gn; - - # get port from db if gamename exists - my $p = $self->{dbh}->selectall_arrayref( - 'SELECT default_qport FROM games WHERE gamename = ?', undef, - lc $gn)->[0]->[0]; - - # return port or zero - return $p || 0; -} - -1; diff --git a/lib/MasterServer/Database/Pg/dbClientList.pm b/lib/MasterServer/Database/Pg/dbClientList.pm deleted file mode 100755 index 718bf8a..0000000 --- a/lib/MasterServer/Database/Pg/dbClientList.pm +++ /dev/null @@ -1,45 +0,0 @@ - -package MasterServer::Database::Pg::dbClientList; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| get_gamenames - get_game_list |; - - -################################################################################ -## get a list of distinct gamenames currently in the database. it does not -## matter whether they are recent or old, as long as the game is currently in -## the database. -## -## returns: hashref of gamenames -################################################################################ -sub get_gamenames { - my $self = shift; - - return $self->{dbh}->selectall_arrayref( - "SELECT distinct gamename - FROM serverlist"); -} - -################################################################################ -## get the list of games of a certain $gamename, excluding the ones excempted -## via the blacklist -## only returns server addresses that are no more than 1 hours old -################################################################################ -sub get_game_list { - my ($self, $gamename) = @_; - - return $self->{dbh}->selectall_arrayref( - "SELECT ip, port - FROM serverlist - WHERE updated > (NOW() - INTERVAL '1 HOUR') - AND gamename = ? - AND NOT blacklisted", - undef, lc $gamename); -} - - -1; diff --git a/lib/MasterServer/Database/Pg/dbCore.pm b/lib/MasterServer/Database/Pg/dbCore.pm deleted file mode 100755 index 5cd194b..0000000 --- a/lib/MasterServer/Database/Pg/dbCore.pm +++ /dev/null @@ -1,44 +0,0 @@ - -package MasterServer::Database::Pg::dbCore; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| database_login |; - -################################################################################ -## login to the database with credentials provided in the config file. -## returns dbh object or quits application on error. -################################################################################ -sub database_login { - my $self = shift; - - # create the dbi object - my $dbh = DBI->connect(@{$self->{dblogin}}, {PrintError => $self->{db_print}}); - - # verify that the database connected - if (defined $dbh) { - - # log the event - $self->log("load","Connected to the Postgres database."); - - # turn on error printing - $dbh->{printerror} = 1; - - # return the dbi object for further use - return $dbh; - } - else { - # fatal error - $self->log("fatal", "$DBI::errstr!"); - - # end program - $self->halt(); - } - - # unreachable - return undef; -} - -1; diff --git a/lib/MasterServer/Database/Pg/dbServerlist.pm b/lib/MasterServer/Database/Pg/dbServerlist.pm deleted file mode 100755 index 5a58717..0000000 --- a/lib/MasterServer/Database/Pg/dbServerlist.pm +++ /dev/null @@ -1,152 +0,0 @@ - -package MasterServer::Database::Pg::dbServerlist; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| add_to_serverlist - update_serverlist - syncer_add - get_next_server |; - -################################################################################ -## beacon was verified or otherwise accepted and will now be added to the -## serverlist. -################################################################################ -sub add_to_serverlist { - my ($self, $ip, $port, $gamename) = @_; - - # update or add server to serverlist - my $u = $self->{dbh}->do("UPDATE serverlist - SET updated = NOW() - WHERE ip = ? - AND port = ?", - undef, $ip, $port); - - # notify - $self->log("update", "$ip:$port timestamp updated") if ($u > 0); - - # if found, updated; done - return 0 if ($u > 0); - - # if not found, add it. - $u = $self->{dbh}->do("INSERT INTO serverlist (ip, port, gamename, country) - SELECT ?, ?, ?, ?", - undef, $ip, $port, $gamename, $self->ip2country($ip)); - - # notify - $self->log("add", "$ip:$port added to serverlist") if ($u > 0); - - # return added - return 1 if ($u > 0); - - # or else report error - $self->log("error", "an error occurred adding server $ip:$port ($gamename) to the serverlist"); - return -1; -} - -################################################################################ -## same as add_to_serverlist above, but does not add the server to serverlist -## if it does not exist in serverlist. it must be added by another function -## first. -################################################################################ -sub update_serverlist { - my ($self, $ip, $port, $s) = @_; - - # update server info - my $u = $self->{dbh}->do( - 'UPDATE serverlist - SET updated = NOW(), - gamename = ?, - gamever = ?, - hostname = ?, - hostport = ? - WHERE ip = ? - AND port = ?', undef, - $s->{gamename}, $s->{gamever}, $s->{hostname}, $s->{hostport}, - $ip, $port); - - # notify - $self->log("update", "server $ip:$port info updated") if ($u > 0); - - # return 0 if updated - return 0 if ($u > 0); - - # or else report error - $self->log("error", "an error occurred updating server $ip:$port in the serverlist"); - return -1; -} - - -################################################################################ -## add new addresses to the pending list, but do not update timestamps. masters -## that sync with each other would otherwise update the timestamp for a server -## which is no longer online. -################################################################################ -sub syncer_add { - my ($self, $ip, $port, $gamename, $secure) = @_; - - # if address is in list, update the timestamp - my $u = $self->{dbh}->do( - "SELECT * FROM serverlist - WHERE ip = ? - AND port = ?", - undef, $ip, $port); - - # notify - $self->log("read","syncer found entry for $ip:$port") if ($u > 0); - - # if found, return 0 - return 0 if ($u > 0); - - # if it is already in the pending list, update it with a new challenge - $u = $self->{dbh}->do( - "UPDATE pending - SET secure = ? - WHERE ip = ? - AND heartbeat = ?", - undef, $secure, $ip, $port); - - # notify - $self->log("update","$ip:$port was updated by syncer", - $self->{log_settings}->{db_updated}) if ($u > 0); - - # return 1 if found - return 1 if ($u > 0); - - # if not found, add it - $u = $self->{dbh}->do( - "INSERT INTO pending (ip, heartbeat, gamename, secure) - SELECT ?, ?, ?, ?", - undef, $ip, $port, $gamename, $secure); - - # notify - $self->log("add","beacon: $ip:$port was added for $gamename after sync") if ($u > 0); - - # return 2 if added new - return 2 if ($u > 0); - - # or else report error - $self->log("error", "an error occurred adding $ip:$port after sync"); - return -1; -} - -################################################################################ -## get a server address of the next server in line to be queried for game info. -## query must be older than 30 seconds (in case it just got added) and not -## older than 3 hours. -################################################################################ -sub get_next_server { - my ($self, $id) = @_; - - return $self->{dbh}->selectall_arrayref( - "SELECT id, ip, port FROM serverlist - WHERE added < (NOW() - INTERVAL '15 SECONDS') - AND updated > (NOW() - INTERVAL '3 HOUR') - AND id > ? - AND NOT blacklisted - ORDER BY id ASC LIMIT 1", undef, $id)->[0]; -} - -1; diff --git a/lib/MasterServer/Database/SQLite/dbBeacon.pm b/lib/MasterServer/Database/SQLite/dbBeacon.pm deleted file mode 100755 index 09eeec3..0000000 --- a/lib/MasterServer/Database/SQLite/dbBeacon.pm +++ /dev/null @@ -1,203 +0,0 @@ - -package MasterServer::Database::SQLite::dbBeacon; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| add_beacon - add_pending - remove_pending - get_pending_beacon - get_pending_info - get_next_pending |; - -################################################################################ -## Update beacon in serverlist or pending list. Add if beacon does not exist in -## either list. Return 0,1,2 if success in adding or -1 on error. -################################################################################ -sub add_beacon { - my ($self, $ip, $beaconport, $heartbeat, $gamename, $secure) = @_; - - # if address is in list, update the timestamp - my $u = $self->{dbh}->do( - "UPDATE serverlist - SET beacon = CURRENT_TIMESTAMP, - updated = CURRENT_TIMESTAMP, - gamename = ?, - b333ms = 1 - WHERE ip = ? - AND port = ?", - undef, lc $gamename, $ip, $heartbeat); - - # notify - $self->log("update", "beacon heartbeat for $ip:$heartbeat") if ($u > 0); - - # if serverlist was updated return 0 - return 0 if ($u > 0); - - # if it is already in the pending list, update it with a new challenge - $u = $self->{dbh}->do( - "UPDATE pending - SET added = CURRENT_TIMESTAMP, - beaconport = ?, - gamename = ?, - secure = ? - WHERE ip = ? - AND heartbeat = ?", - undef, $beaconport, lc $gamename, $secure, $ip, $heartbeat); - - # notify - $self->log("update", "beacon heartbeat $ip:$beaconport pending $gamename:$heartbeat") if ($u > 0); - - # beacon was already in pending list and was updated - return 1 if ($u > 0); - - # if not found, add it - $u = $self->{dbh}->do( - "INSERT INTO pending ( - ip, - beaconport, - heartbeat, - gamename, - secure) - SELECT ?, ?, ?, ?, ?", - undef, $ip, $beaconport, $heartbeat, lc $gamename, $secure); - - # notify - $self->log("add", "beacon heartbeat $ip:$beaconport pending $gamename:$heartbeat") if ($u > 0); - - # it was added to pending - return 2 if ($u > 0); - - # or else report error - $self->log("error", "an error occurred adding beacon $ip:$beaconport with $gamename:$heartbeat to the pending list"); - return -1; -} - -################################################################################ -## Add an address to the database that was obtained via a method other than -## an udp beacon. Return 0,1,2 if success in adding or -1 on error. -################################################################################ -sub add_pending { - my ($self, $ip, $port, $gamename, $secure) = @_; - - # if address is in list, update the timestamp - my $u = $self->{dbh}->do( - "UPDATE serverlist - SET updated = CURRENT_TIMESTAMP - WHERE ip = ? - AND port = ?", - undef, $ip, $port); - - # notify - $self->log("update", "updated serverlist with $ip:$port") if ($u > 0); - - # if updated, return 0 - return 0 if ($u > 0); - - # if it is already in the pending list, update it with a new challenge - $u = $self->{dbh}->do( - "UPDATE pending - SET added = CURRENT_TIMESTAMP, - secure = ? - WHERE ip = ? - AND heartbeat = ?", - undef, $secure, $ip, $port); - - # notify - $self->log("update", "updated pending with $ip:$port") if ($u > 0); - - # return 1 if updated - return 1 if ($u > 0); - - # if not found, add it - $u = $self->{dbh}->do( - "INSERT INTO pending ( - ip, - heartbeat, - gamename, - secure) - SELECT ?, ?, ?, ?", - undef, $ip, $port, $gamename, $secure); - - # notify - $self->log("add", "$ip:$port added pending $gamename") if ($u > 0); - - # return 2 if added new - return 2 if ($u > 0); - - # else - return -1; -} - -################################################################################ -## Remove an entry from the pending list. Returns 0 if removed or -1 in case -## of error(s). -################################################################################ -sub remove_pending { - my ($self, $id) = @_; - - # if address is in list, update the timestamp - my $u = $self->{dbh}->do( - "DELETE FROM pending - WHERE id = ?", - undef, $id); - - # notify - $self->log("delete", "removed pending id $id from pending") if ($u > 0); - - # it was removed from pending - return 2 if ($u > 0); - - # or else report error - $self->log("error", "error deleting server $id from pending"); - return -1; -} - -################################################################################ -## Get pending server by ip, beacon port. Returns * or undef -################################################################################ -sub get_pending_beacon { - my ($self, $ip, $port) = @_; - - # if address is in list, update the timestamp - return $self->{dbh}->selectall_arrayref( - "SELECT * FROM pending - WHERE ip = ? - AND beaconport = ?", - undef, $ip, $port)->[0]; -} - -################################################################################ -## Get pending server by ip, heartbeat port. Returns * or undef -################################################################################ -sub get_pending_info { - my ($self, $ip, $port) = @_; - - # if address is in list, update the timestamp - return $self->{dbh}->selectall_arrayref( - "SELECT * FROM pending - WHERE ip = ? - AND heartbeat = ?", - undef, $ip, $port)->[0]; -} - -################################################################################ -## Get server info from any entry with an id higher than the provided one. The -## server is added to pending at least 15 seconds ago. Returns info or undef. -################################################################################ -sub get_next_pending { - my ($self, $id) = @_; - - # get 1 pending id that is older than 15s - return $self->{dbh}->selectall_arrayref( - "SELECT id, ip, heartbeat, secure FROM pending - WHERE added < datetime(CURRENT_TIMESTAMP, '-15 seconds') - AND id > ? - ORDER BY id ASC LIMIT 1", - undef, $id)->[0]; -} - - -1; diff --git a/lib/MasterServer/Database/SQLite/dbCiphers.pm b/lib/MasterServer/Database/SQLite/dbCiphers.pm deleted file mode 100755 index 0dc388d..0000000 --- a/lib/MasterServer/Database/SQLite/dbCiphers.pm +++ /dev/null @@ -1,98 +0,0 @@ - -package MasterServer::Database::SQLite::dbCiphers; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| clear_ciphers - insert_cipher - get_cipher - get_default_port |; - -################################################################################ -## Clear all existing ciphers from the database -################################################################################ -sub clear_ciphers { - my $self = shift; - - # delete ALL entries - my $u = $self->{dbh}->do("DELETE FROM games"); - - # notify - $self->log("delete", "Removed all ciphers") if ($u > 0); - - # removed from games - return 2 if ($u > 0); - - # or else report notice - $self->log("notice", "No ciphers deleted!"); - return -1; - -} - -################################################################################ -## Insert the list of supported games and their ciphers / default ports / -## descriptions included from the data/supportedgames.pl file. -################################################################################ -sub insert_cipher { - my ($self, %opt) = @_; - - # insert a single cipher/key combo - my $u = $self->{dbh}->do( - "INSERT INTO games ( - gamename, - cipher, - description, - default_qport) - VALUES(?, ?, ?, ?)", undef, - $opt{gamename}, $opt{cipher}, $opt{description}, $opt{default_qport}); - - # notify - $self->log("add", "Added cipher for $opt{gamename}") if ($u and $u > 0); - return 1 if ($u and $u > 0); - - # or else report error - $self->log("error", "An error occurred adding a cipher for $opt{gamename}"); - return -1; - -} - - -################################################################################ -## get the cipher that goes with gamename -################################################################################ -sub get_cipher { - my ($self, $gn) = @_; - - # no gamename specified? "undef" is not a known cipher, so send that instead. - return 'undef' if !$gn; - - # get cipher from db if gamename exists - my $cipher = $self->{dbh}->selectall_arrayref( - 'SELECT cipher FROM games WHERE gamename = ?', undef, - lc $gn)->[0]->[0]; - - # return a non-zero-length string - return ($cipher ? $cipher : 'undef'); -} - -################################################################################ -## get the default query port that goes with gamename -################################################################################ -sub get_default_port { - my ($self, $gn) = @_; - - # no gamename specified? default port is 0 - return 0 if !$gn; - - # get port from db if gamename exists - my $p = $self->{dbh}->selectall_arrayref( - 'SELECT default_qport FROM games WHERE gamename = ?', undef, - lc $gn)->[0]->[0]; - - # return port or zero - return $p || 0; -} - -1; diff --git a/lib/MasterServer/Database/SQLite/dbClientList.pm b/lib/MasterServer/Database/SQLite/dbClientList.pm deleted file mode 100755 index 58c1392..0000000 --- a/lib/MasterServer/Database/SQLite/dbClientList.pm +++ /dev/null @@ -1,45 +0,0 @@ - -package MasterServer::Database::SQLite::dbClientList; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| get_gamenames - get_game_list |; - - -################################################################################ -## get a list of distinct gamenames currently in the database. it does not -## matter whether they are recent or old, as long as the game is currently in -## the database. -## -## returns: hashref of gamenames -################################################################################ -sub get_gamenames { - my $self = shift; - - return $self->{dbh}->selectall_arrayref( - "SELECT distinct gamename - FROM serverlist"); -} - -################################################################################ -## get the list of games of a certain $gamename, excluding the ones excempted -## via the blacklist -## only returns server addresses that are no more than 1 hours old -################################################################################ -sub get_game_list { - my ($self, $gamename) = @_; - - return $self->{dbh}->selectall_arrayref( - "SELECT ip, port - FROM serverlist - WHERE updated > datetime(CURRENT_TIMESTAMP, '-3600 seconds') - AND gamename = ? - AND NOT blacklisted", - undef, lc $gamename); -} - - -1; diff --git a/lib/MasterServer/Database/SQLite/dbCore.pm b/lib/MasterServer/Database/SQLite/dbCore.pm deleted file mode 100755 index 27c9b35..0000000 --- a/lib/MasterServer/Database/SQLite/dbCore.pm +++ /dev/null @@ -1,66 +0,0 @@ - -package MasterServer::Database::SQLite::dbCore; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| database_login |; - -################################################################################ -## login to the database with credentials provided in the config file. -## returns dbh object or quits application on error. -################################################################################ -sub database_login { - my $self = shift; - - # check if database file exists - my $db_file = [split(':', $self->{dblogin}->[0])]->[2]; - $db_file =~ s/dbname=//i; - - unless (-e $db_file) { - # fatal error - $self->log("fatal", "Database file $db_file does not exist!"); - - # end program - $self->halt(); - } - - # create the dbi object - my $dbh = DBI->connect(@{$self->{dblogin}}, {PrintError => $self->{db_print}}); - - # verify that the database connected - if (defined $dbh) { - # log the event - $self->log("load","Connected to the SQLite database."); - - # turn on error printing - $dbh->{printerror} = 1; - - # synchronous read/writing to the sql file OFF. That means: when the script - # shuts down unexpectedly, i.e. because of power failure or a crash, changes - # to the database are NOT SAVED. However, if this setting is not turned OFF, - # it takes too long to write to the database, which means that new beacons, - # requests and servers cannot be processed. You don't have a choice, really.. - $dbh->do("PRAGMA synchronous = OFF"); - - # allow the use of foreign keys (referencing) - $dbh->do("PRAGMA foreign_keys = ON"); - - # return the dbi object for further use - return $dbh; - } - else { - # fatal error - $self->log("fatal", "$DBI::errstr!"); - - # end program - $self->halt(); - } - - # unreachable - return undef; - -} - -1; diff --git a/lib/MasterServer/Database/SQLite/dbServerlist.pm b/lib/MasterServer/Database/SQLite/dbServerlist.pm deleted file mode 100755 index 436a788..0000000 --- a/lib/MasterServer/Database/SQLite/dbServerlist.pm +++ /dev/null @@ -1,152 +0,0 @@ - -package MasterServer::Database::SQLite::dbServerlist; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| add_to_serverlist - update_serverlist - syncer_add - get_next_server |; - -################################################################################ -## beacon was verified or otherwise accepted and will now be added to the -## serverlist. -################################################################################ -sub add_to_serverlist { - my ($self, $ip, $port, $gamename) = @_; - - # update or add server to serverlist - my $u = $self->{dbh}->do("UPDATE serverlist - SET updated = CURRENT_TIMESTAMP - WHERE ip = ? - AND port = ?", - undef, $ip, $port); - - # notify - $self->log("update", "$ip:$port timestamp updated") if ($u > 0); - - # if found, updated; done - return 0 if ($u > 0); - - # if not found, add it. - $u = $self->{dbh}->do("INSERT INTO serverlist (ip, port, gamename, country) - SELECT ?, ?, ?, ?", - undef, $ip, $port, $gamename, $self->ip2country($ip)); - - # notify - $self->log("add", "$ip:$port added to serverlist") if ($u > 0); - - # return added - return 1 if ($u > 0); - - # or else report error - $self->log("error", "an error occurred adding server $ip:$port ($gamename) to the serverlist"); - return -1; -} - -################################################################################ -## same as add_to_serverlist above, but does not add the server to serverlist -## if it does not exist in serverlist. it must be added by another function -## first. -################################################################################ -sub update_serverlist { - my ($self, $ip, $port, $s) = @_; - - # update server info - my $u = $self->{dbh}->do( - 'UPDATE serverlist - SET updated = CURRENT_TIMESTAMP, - gamename = ?, - gamever = ?, - hostname = ?, - hostport = ? - WHERE ip = ? - AND port = ?', undef, - $s->{gamename}, $s->{gamever}, $s->{hostname}, $s->{hostport}, - $ip, $port); - - # notify - $self->log("update", "server $ip:$port info updated") if ($u > 0); - - # return 0 if updated - return 0 if ($u > 0); - - # or else report error - $self->log("error", "an error occurred updating server $ip:$port in the serverlist"); - return -1; -} - - -################################################################################ -## add new addresses to the pending list, but do not update timestamps. masters -## that sync with each other would otherwise update the timestamp for a server -## which is no longer online. -################################################################################ -sub syncer_add { - my ($self, $ip, $port, $gamename, $secure) = @_; - - # if address is in list, update the timestamp - my $u = $self->{dbh}->do( - "SELECT * FROM serverlist - WHERE ip = ? - AND port = ?", - undef, $ip, $port); - - # notify - $self->log("read","syncer found entry for $ip:$port") if ($u > 0); - - # if found, return 0 - return 0 if ($u > 0); - - # if it is already in the pending list, update it with a new challenge - $u = $self->{dbh}->do( - "UPDATE pending - SET secure = ? - WHERE ip = ? - AND heartbeat = ?", - undef, $secure, $ip, $port); - - # notify - $self->log("update","$ip:$port was updated by syncer", - $self->{log_settings}->{db_updated}) if ($u > 0); - - # return 1 if found - return 1 if ($u > 0); - - # if not found, add it - $u = $self->{dbh}->do( - "INSERT INTO pending (ip, heartbeat, gamename, secure) - SELECT ?, ?, ?, ?", - undef, $ip, $port, $gamename, $secure); - - # notify - $self->log("add","beacon: $ip:$port was added for $gamename after sync") if ($u > 0); - - # return 2 if added new - return 2 if ($u > 0); - - # or else report error - $self->log("error", "an error occurred adding $ip:$port after sync"); - return -1; -} - -################################################################################ -## get a server address of the next server in line to be queried for game info. -## query must be older than 30 seconds (in case it just got added) and not -## older than 3 hours. -################################################################################ -sub get_next_server { - my ($self, $id) = @_; - - return $self->{dbh}->selectall_arrayref( - "SELECT id, ip, port FROM serverlist - WHERE added < datetime(CURRENT_TIMESTAMP, '-15 seconds') - AND updated > datetime(CURRENT_TIMESTAMP, '-10800 seconds') - AND id > ? - AND NOT blacklisted - ORDER BY id ASC LIMIT 1", undef, $id)->[0]; -} - -1; diff --git a/lib/MasterServer/Database/dbAddServers.pm b/lib/MasterServer/Database/dbAddServers.pm new file mode 100755 index 0000000..4898316 --- /dev/null +++ b/lib/MasterServer/Database/dbAddServers.pm @@ -0,0 +1,162 @@ + +package MasterServer::Database::dbAddServers; + +use strict; +use warnings; +use MasterServer::Core::Util 'sqlprint'; +use Exporter 'import'; + +our @EXPORT = qw| add_server_new + add_server_list + update_server_list + syncer_add |; + +################################################################################ +## Update an existing address or add a new address to the pending list. +## opts: direct beacon, set update, game +################################################################################ +sub add_server_new { + my $self = shift; + my %o = ( + updated => time, + @_); + + # try updating it in serverlist + my %H = ( + $o{direct} ? ( 'b333ms = CAST(? AS BOOLEAN)' => $o{direct}) : (), + $o{updated} ? ( 'updated = to_timestamp(?)' => $o{updated}) : (), + $o{beacon} ? ( 'beacon = to_timestamp(?)' => $o{beacon}) : (), + $o{gamename} ? ('gamename = ?' => lc $o{gamename}) : (), + ); + + my($q, @p) = sqlprint("UPDATE serverlist !H + WHERE ip = ? AND port = ?", \%H, $o{ip}, $o{heartbeat}); + + my $n = $self->{dbh}->do($q, undef, @p); + + # if serverlist was updated + return 0 if ($n > 0); + + + # try updating it in pending + %H = ( + $o{added} ? ( 'added = ?' => $o{added}) : (), + $o{secure} ? ( 'secure = ?' => $o{secure}) : (), + $o{gamename} ? ( 'gamename = ?' => lc $o{gamename}) : (), + $o{beaconport} ? ('beaconport = ?' => $o{beaconport}) : (), + ); + + ($q, @p) = sqlprint("UPDATE pending !H + WHERE ip = ? AND heartbeat = ?", \%H, $o{ip}, $o{heartbeat}); + + # exec query + $n = $self->{dbh}->do($q, undef, @p); + + # if beacon was updated + return 1 if ($n > 0); + + # if not found at all, add to pending + $n = $self->{dbh}->do( + "INSERT INTO pending ( + ip, + beaconport, + heartbeat, + gamename, + secure) + SELECT ?, ?, ?, ?, ?", + undef, $o{ip}, $o{beaconport}, $o{heartbeat}, lc $o{gamename}, $o{secure}); + + # it was added to pending + return 2 if ($n > 0); +} + +################################################################################ +## Update the server info in the serverlist +################################################################################ +sub update_server_list { + my $self = shift; + my %o = ( + updated => time, + @_); + + # try updating it in serverlist + my %H = ( + $o{gamename} ? ('gamename = ?' => lc $o{gamename}) : (), + $o{gamever} ? ( 'gamever = ?' => $o{gamever}) : (), + $o{hostname} ? ('hostname = ?' => $o{hostname}) : (), + $o{hostport} ? ('hostport = ?' => $o{hostport}) : (), + $o{updated} ? ( 'updated = to_timestamp(?)' => $o{updated}) : (), + ); + + my($q, @p) = sqlprint("UPDATE serverlist !H + WHERE ip = ? AND port = ?", \%H, $o{ip}, $o{port}); + + return $self->{dbh}->do($q, undef, @p); +} + +################################################################################ +## beacon was verified or otherwise accepted and will now be added to the +## serverlist. +################################################################################ +sub add_server_list { + my $self = shift; + my %o = @_; + + # insert basic data + return $self->{dbh}->do("INSERT INTO serverlist (ip, port, gamename, country) + SELECT ?, ?, ?, ?", undef, + $o{ip}, $o{port}, lc $o{gamename}, $self->ip2country($o{ip})); +} + +################################################################################ +## add new addresses to the pending list, but do not update timestamps. masters +## that sync with each other would otherwise update the timestamp for a server +## which is no longer online. +################################################################################ +sub syncer_add { + my ($self, $ip, $port, $gamename, $secure) = @_; + + # if address is in the list AND up to date, + # acknowledge its existance but don't do anything with it + my $u = $self->{dbh}->do( + "SELECT * FROM serverlist + WHERE ip = ? + AND port = ? + AND updated > to_timestamp(?)", + undef, $ip, $port, time-7200); + + # if found, return 0 + return 0 if ($u > 0); + + # if it is already in the pending list, update it with a new challenge + $u = $self->{dbh}->do( + "UPDATE pending + SET secure = ? + WHERE ip = ? + AND heartbeat = ?", + undef, $secure, $ip, $port); + + # notify + $self->log("update","$ip:$port was updated by syncer") if ($u > 0); + + # return 1 if found + return 1 if ($u > 0); + + # if not found or out of date, add it to pending to be checked again + $u = $self->{dbh}->do( + "INSERT INTO pending (ip, heartbeat, gamename, secure) + SELECT ?, ?, ?, ?", + undef, $ip, $port, lc $gamename, $secure); + + # notify + $self->log("add","beacon: $ip:$port was added for $gamename after sync") if ($u > 0); + + # return 2 if added new + return 2 if ($u > 0); + + # or else report error + $self->log("error", "an error occurred adding $ip:$port after sync"); + return -1; +} + +1; diff --git a/lib/MasterServer/Database/dbCiphers.pm b/lib/MasterServer/Database/dbCiphers.pm new file mode 100755 index 0000000..e62552d --- /dev/null +++ b/lib/MasterServer/Database/dbCiphers.pm @@ -0,0 +1,57 @@ + +package MasterServer::Database::dbCiphers; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT = qw| clear_ciphers + insert_cipher + get_game_props |; + +################################################################################ +## Clear all existing ciphers from the database +################################################################################ +sub clear_ciphers { + my $self = shift; + + # delete ALL entries + my $u = $self->{dbh}->do("DELETE FROM games"); +} + +################################################################################ +## Insert the list of supported games and their ciphers / default ports / +## descriptions included from the data/supportedgames.pl file. +################################################################################ +sub insert_cipher { + my ($self, %opt) = @_; + + # insert a single cipher/key combo + my $u = $self->{dbh}->do( + "INSERT INTO games ( + gamename, + cipher, + description, + default_qport) + VALUES(?, ?, ?, ?)", undef, + lc $opt{gamename}, $opt{cipher}, $opt{description}, $opt{default_qport}); + return 1 if ($u and $u > 0); + + # or else report error + $self->log("error", "An error occurred adding a cipher for $opt{gamename}"); +} + +################################################################################ +## get the cipher, description and default port that goes with given gamename +################################################################################ +sub get_game_props { + my ($self, $gn) = @_; + + # get cipher from db if gamename exists + return $self->{dbh}->selectall_arrayref( + 'SELECT * FROM games WHERE gamename = ?', + {Slice=>{}}, + lc $gn)->[0]; +} + +1; diff --git a/lib/MasterServer/Database/dbCore.pm b/lib/MasterServer/Database/dbCore.pm new file mode 100755 index 0000000..4ea0c79 --- /dev/null +++ b/lib/MasterServer/Database/dbCore.pm @@ -0,0 +1,83 @@ + +package MasterServer::Database::dbCore; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT = qw| database_login |; + +################################################################################ +## login to the database with credentials provided in the config file. +## returns dbh object or quits application on error. +## +## Recommended database types: Postgresql, MySQL or SQLite. Warranty void if +## other database types are used. Use at your own risk. +################################################################################ +sub database_login { + my $self = shift; + + # read db type from db login + my @db_type = split(':', $self->{dblogin}->[0]); + + # if the type is an SQLite database, check if the database file exists + if ( "SQLite" =~ m/$db_type[1]/i) { + + # check if database file exists + my $db_file = [split(':', $self->{dblogin}->[0])]->[2]; + $db_file =~ s/dbname=//i; + + unless (-e $db_file) { + # fatal error + $self->log("fatal", "Database file $db_file does not exist!"); + + # end program + $self->halt(); + } + } + + # inform what DB we try to load + # $self->log("info","Database: $db_type[1]"); + + # create the dbi object + my $dbh = DBI->connect(@{$self->{dblogin}}, {PrintError => $self->{db_print}}); + + # verify that the database connected + if (defined $dbh) { + + # log the event + $self->log("info","Connected to the $db_type[1] database."); + + # turn on error printing + $dbh->{printerror} = 1; + + # if the type is an SQLite database, disable sync rw. + if ( "SQLite" =~ m/$db_type[1]/i) { + + # synchronous read/writing to the SQLite file OFF. That means: when the script + # shuts down unexpectedly, i.e. because of power failure or a crash, changes + # to the database are NOT SAVED. However, if this setting is not turned OFF, + # it takes too long to write to the database, which means that new beacons, + # requests and servers cannot be processed. You don't have a choice, really.. + $dbh->do("PRAGMA synchronous = OFF"); + + # allow the use of foreign keys (referencing) + $dbh->do("PRAGMA foreign_keys = ON"); + } + + # return the dbi object for further use + return $dbh; + } + else { + # fatal error + $self->log("fatal", "$DBI::errstr!"); + + # end program + $self->halt(); + } + + # in case of any other error, return undef. + return undef; +} + +1; diff --git a/lib/MasterServer/Database/dbGetServers.pm b/lib/MasterServer/Database/dbGetServers.pm new file mode 100755 index 0000000..5069462 --- /dev/null +++ b/lib/MasterServer/Database/dbGetServers.pm @@ -0,0 +1,139 @@ +package MasterServer::Database::dbGetServers; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT = qw| get_server + get_pending + get_gamenames |; + +################################################################################ +## get server details for one or multiple servers +## opts: limit, see $order +################################################################################ +sub get_server { + my $s = shift; + my %o = ( + sort => '', + @_ + ); + + my %where = ( + $o{next_id} ? ( 'id > ?' => $o{next_id}) : (), + $o{id} ? ( 'id = ?' => $o{id}) : (), + $o{ip} ? ( 'ip = ?' => $o{ip}) : (), + $o{port} ? ( 'port = ?' => $o{port}) : (), + $o{gamename} ? ( 'gamename = ?' => lc $o{gamename}) : (), + $o{gamever} ? ( 'gamever = ?' => $o{gamever}) : (), + $o{hostname} ? ( 'hostname = ?' => $o{hostname}) : (), + $o{hostport} ? ( 'hostport = ?' => $o{hostport}) : (), + $o{country} ? ( 'country = ?' => $o{country}) : (), + $o{b333ms} ? ( 'b333ms = ?' => $o{b333ms}) : (), + $o{blacklisted} ? ('blacklisted = ?' => $o{blacklisted}) : (), + $o{added} ? ( 'added < to_timestamp(?)' => (time-$o{added})) : (), + $o{beacon} ? ( 'beacon > to_timestamp(?)' => (time-$o{beacon})) : (), + $o{updated} ? ('updated > to_timestamp(?)' => (time-$o{updated})) : (), + $o{before} ? ('updated < to_timestamp(?)' => (time-$o{before})) : (), + ); + + my @select = ( qw| + id + ip + port + gamename + gamever + hostname + hostport + country + b333ms + blacklisted + added + beacon + updated + |); + + my $order = sprintf { + id => 'id %s', + ip => 'ip %s', + port => 'port %s', + gamename => 'gamename %s', + gamever => 'gamever %s', + hostname => 'hostname %s', + hostport => 'hostport %s', + country => 'country %s', + b333ms => 'b333ms %s', + blacklisted => 'blacklisted %s', + added => 'added %s', + beacon => 'beacon %s', + updated => 'updated %s', + }->{ $o{sort}||'id' }, $o{reverse} ? 'DESC' : 'ASC'; + + return $s->db_all( q| + SELECT !s FROM serverlist + !W + ORDER BY !s| + .($o{limit} ? " LIMIT ?" : ""), + join(', ', @select), \%where, $order, ($o{limit} ? $o{limit} : ()), + ); +} + +################################################################################ +## get server details for one or multiple pending servers +## opts: limit, next_id, beaconport, heartbeat, gamename, secure, enctype, added +################################################################################ +sub get_pending { + my $s = shift; + my %o = ( + sort => '', + @_ + ); + + my %where = ( + $o{next_id} ? ( 'id > ?' => $o{next_id}) : (), + $o{id} ? ( 'id = ?' => $o{id}) : (), + $o{ip} ? ( 'ip = ?' => $o{ip}) : (), + $o{beaconport} ? ('beaconport = ?' => $o{beaconport}) : (), + $o{heartbeat} ? ( 'heartbeat = ?' => $o{heartbeat}) : (), + $o{gamename} ? ( 'gamename = ?' => lc $o{gamename}) : (), + $o{secure} ? ( 'secure = ?' => $o{secure}) : (), + $o{enctype} ? ( 'enctype = ?' => $o{enctype}) : (), + $o{added} ? ('added < to_timestamp(?)' => (time-$o{added})) : (), + $o{after} ? ('added > to_timestamp(?)' => (time-$o{after})) : (), + ); + + my @select = ( qw| id ip beaconport heartbeat gamename secure enctype added |,); + my $order = sprintf { + id => 'id %s', + ip => 'ip %s', + beaconport => 'beaconport %s', + heartbeat => 'heartbeat %s', + gamename => 'gamename %s', + secure => 'secure %s', + enctype => 'enctype %s', + added => 'added %s', + }->{ $o{sort}||'id' }, $o{reverse} ? 'DESC' : 'ASC'; + + return $s->db_all( q| + SELECT !s FROM pending + !W + ORDER BY !s| + .($o{limit} ? " LIMIT ?" : ""), + join(', ', @select), \%where, $order, ($o{limit} ? $o{limit} : ()), + ); +} + +################################################################################ +## get a list of distinct gamenames currently in the database. it does not +## matter whether they are recent or old, as long as the game is currently in +## the database. +################################################################################ +sub get_gamenames { + my $self = shift; + + return $self->{dbh}->selectall_arrayref( + "SELECT distinct gamename + FROM serverlist"); +} + +1; diff --git a/lib/MasterServer/Database/dbMaintenance.pm b/lib/MasterServer/Database/dbMaintenance.pm new file mode 100755 index 0000000..41ea93a --- /dev/null +++ b/lib/MasterServer/Database/dbMaintenance.pm @@ -0,0 +1,40 @@ +package MasterServer::Database::dbMaintenance; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT = qw| delete_old_pending + remove_pending |; + +################################################################################ +## delete unresponsive servers from the pending list +## where the server is unresponsive for more than 1 hour +################################################################################ +sub delete_old_pending { + my ($self) = shift; + + # remove servers + my $u = $self->{dbh}->do( + "DELETE FROM pending + WHERE added < to_timestamp(?)", undef, time-3600); + + # notify + $self->log("delete", "Removed $u entries from pending.") if ($u > 0); +} + +################################################################################ +## Remove an entry from the pending list. Returns 0 if removed or -1 in case +## of error(s). +################################################################################ +sub remove_pending { + my ($self, $id) = @_; + + # if address is in list, update the timestamp + my $u = $self->{dbh}->do("DELETE FROM pending WHERE id = ?", undef, $id); + + # notify + $self->log("delete", "removed pending id $id from pending") if ($u > 0); +} + +1; diff --git a/lib/MasterServer/Database/dbStats.pm b/lib/MasterServer/Database/dbStats.pm new file mode 100755 index 0000000..70962dc --- /dev/null +++ b/lib/MasterServer/Database/dbStats.pm @@ -0,0 +1,117 @@ +package MasterServer::Database::dbStats; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT = qw| get_gamelist_stats + write_direct_beacons + write_stat + write_kfstats |; + +################################################################################ +# calculate stats for all individual games +################################################################################ +sub get_gamelist_stats { + my $self = shift; + + return $self->{dbh}->selectall_arrayref( + "SELECT DISTINCT gamename AS gamename, + COUNT(NULLIF(b333ms AND updated > to_timestamp(?), FALSE)) AS numdirect, + COUNT(NULLIF(updated > to_timestamp(?), FALSE)) AS numtotal + FROM serverlist + GROUP BY gamename", undef, time-7200, time-7200); +} + +################################################################################ +# Determine from the last beacon whether the server is still uplinking +# directly to us, or whether it stopped uplinking and is now artificially +# kept in the database. +################################################################################ +sub write_direct_beacons { + my $self = shift; + my $u = $self->{dbh}->do( + "UPDATE serverlist + SET b333ms = CAST(0 AS BOOLEAN) + WHERE beacon < to_timestamp(?) AND b333ms", + undef, time-3600); + + # notify + $self->log("unset", "Lost $u direct beacons.") if ($u > 0); +} + +################################################################################ +# Write the stats to the games table +# A stat can not exist without existing gamename. Was inserted by cipher loader. +################################################################################ +sub write_stat { + my ($self, %opt) = @_; + + # if it is already in the pending list, update it with a new challenge + my $u = $self->{dbh}->do( + "UPDATE games + SET num_uplink = ?, + num_total = ? + WHERE gamename = ?", + undef, $opt{num_uplink}, $opt{num_total}, lc $opt{gamename}); + + # notify + $self->log("update", "Updated stats for $opt{gamename}.") if ($u > 0); + +} + +################################################################################ +## Write the KFStats to the database +################################################################################ +sub write_kfstats { + my ($self, $h) = @_; + + # check if entry already excists. + my $u = $self->{dbh}->selectall_arrayref( + "SELECT * FROM kfstats WHERE UTkey = ? ", undef, $h->{UTkey}); + + if ( !defined $u->[0] ) { + $u = $self->{dbh}->do( + "INSERT INTO kfstats (UTkey, Username) VALUES (?,?)", + undef, $h->{UTkey}, $h->{Username}); + + # notify + $self->log("kfnew", "New KF Player $h->{Username} added"); + } + + # update existing information + $u = $self->{dbh}->do("UPDATE kfstats SET + Username = ?, + CurrentVeterancy = ?, + TotalKills = ?, + DecaptedKills = ?, + TotalMeleeDamage = ?, + MeleeKills = ?, + PowerWpnKills = ?, + BullpupDamage = ?, + StalkerKills = ?, + TotalWelded = ?, + TotalHealed = ?, + TotalPlaytime =?, + GamesWon = ?, + GamesLost = ? + WHERE UTkey = ?", undef, + $h->{Username}, + $h->{CurrentVeterancy}, + $h->{TotalKills}, + $h->{DecaptedKills}, + $h->{TotalMeleeDamage}, + $h->{MeleeKills}, + $h->{PowerWpnKills}, + $h->{BullpupDamage}, + $h->{StalkerKills}, + $h->{TotalWelded}, + $h->{TotalHealed}, + $h->{TotalPlaytime}, + $h->{GamesWon}, + $h->{GamesLost}, + $h->{UTkey} + ); +} + +1; diff --git a/lib/MasterServer/Database/dbUTServerInfo.pm b/lib/MasterServer/Database/dbUTServerInfo.pm new file mode 100755 index 0000000..9352e38 --- /dev/null +++ b/lib/MasterServer/Database/dbUTServerInfo.pm @@ -0,0 +1,238 @@ + +package MasterServer::Database::dbUTServerInfo; + +use strict; +use warnings; +use MasterServer::Core::Util 'sqlprint'; +use Exporter 'import'; + +our @EXPORT = qw| get_utserver + add_utserver + update_utserver + delete_utplayers + insert_utplayer |; + +################################################################################ +## get server details for one or multiple UT servers +## opts: limit, see $order +################################################################################ +sub get_utserver { + my $s = shift; + my %o = ( + sort => '', + @_ + ); + + my %where = ( + $o{id} ? ('server_id = ?' => $o{id}) : (), + $o{minnetver} ? ('minnetver = ?' => $o{minnetver}) : (), + $o{gamever} ? ('gamever = ?' => $o{gamever}) : (), + $o{location} ? ('location = ?' => $o{location}) : (), + $o{listenserver} ? ('listenserver = ?' => $o{listenserver}) : (), + $o{hostport} ? ('hostport = ?' => $o{hostport}) : (), + $o{hostname} ? ('hostname = ?' => $o{hostname}) : (), + $o{adminname} ? ('adminname = ?' => $o{adminname}) : (), + $o{adminemail} ? ('adminemail = ?' => $o{adminemail}) : (), + $o{password} ? ('password = ?' => $o{password}) : (), + $o{gametype} ? ('gametype = ?' => $o{gametype}) : (), + $o{gamestyle} ? ('gamestyle = ?' => $o{gamestyle}) : (), + $o{changelevels} ? ('changelevels = ?' => $o{changelevels}) : (), + $o{maptitle} ? ('maptitle = ?' => $o{maptitle}) : (), + $o{mapname} ? ('mapname = ?' => $o{mapname}) : (), + $o{numplayers} ? ('numplayers = ?' => $o{numplayers}) : (), + $o{maxplayers} ? ('maxplayers = ?' => $o{maxplayers}) : (), + $o{minplayers} ? ('minplayers = ?' => $o{minplayers}) : (), + $o{botskill} ? ('botskill = ?' => $o{botskill}) : (), + $o{balanceteams} ? ('balanceteams = ?' => $o{balanceteams}) : (), + $o{playersbalanceteams} ? ('playersbalanceteams = ?' => $o{playersbalanceteams}) : (), + $o{friendlyfire} ? ('friendlyfire = ?' => $o{friendlyfire}) : (), + $o{maxteams} ? ('maxteams = ?' => $o{maxteams}) : (), + $o{timelimit} ? ('timelimit = ?' => $o{timelimit}) : (), + $o{goalteamscore} ? ('goalteamscore = ?' => $o{goalteamscore}) : (), + $o{fraglimit} ? ('fraglimit = ?' => $o{fraglimit}) : (), + $o{mutators} ? ('hostname ILIKE ?' => "%$o{mutators}%") : (), + $o{updated} ? ('updated > to_timestamp(?)'=> (time-$o{updated})) : (), + ); + + my @select = ( qw| + server_id + minnetver + gamever + location + listenserver + hostport + hostname + adminname + adminemail + password + gametype + gamestyle + changelevels + maptitle + mapname + numplayers + maxplayers + minplayers + botskill + balanceteams + playersbalanceteams + friendlyfire + maxteams + timelimit + goalteamscore + fraglimit + mutators + updated + |); + + my $order = sprintf { + server_id => 'server_id %s', + minnetver => 'minnetver %s', + gamever => 'gamever %s', + location => 'location %s', + listenserver => 'listenserver %s', + hostport => 'hostport %s', + hostname => 'hostname %s', + adminname => 'adminname %s', + adminemail => 'adminemail %s', + password => 'password %s', + gametype => 'gametype %s', + gamestyle => 'gamestyle %s', + changelevels => 'changelevels %s', + maptitle => 'maptitle %s', + mapname => 'mapname %s', + numplayers => 'numplayers %s', + maxplayers => 'maxplayers %s', + minplayers => 'minplayers %s', + botskill => 'botskill %s', + balanceteams => 'balanceteams %s', + playersbalanceteams => 'playersbalanceteams %s', + friendlyfire => 'friendlyfire %s', + maxteams => 'maxteams %s', + timelimit => 'timelimit %s', + goalteamscore => 'goalteamscore %s', + fraglimit => 'fraglimit %s', + mutators => 'mutators %s', + updated => 'updated %s', + }->{ $o{sort}||'server_id' }, $o{reverse} ? 'DESC' : 'ASC'; + + return $s->db_all( q| + SELECT !s FROM utserver_info + !W + ORDER BY !s| + .($o{limit} ? " LIMIT ?" : ""), + join(', ', @select), \%where, $order, ($o{limit} ? $o{limit} : ()), + ); +} + + +################################################################################ +## Update serverinfo for an existing address to the utserver list. +## opts: all server info data fields. +################################################################################ +sub update_utserver { + my $self = shift; + my $id = shift; + my %s = ( + # defaults + updated => time, + @_); + + # try updating it in serverlist + my %H = ( + $s{minnetver} ? ( 'minnetver = ?' => $s{minnetver} ) : (), + $s{gamever} ? ( 'gamever = ?' => int( $s{gamever}) ) : (), + $s{location} ? ( 'location = ?' => $s{location} ) : (), + $s{listenserver} ? ( 'listenserver = ?' => ( $s{listenserver} ? 1 : 0) ) : (), + $s{hostport} ? ( 'hostport = ?' => $s{hostport}) : (), + $s{hostname} ? ( 'hostname = ?' => $s{hostname}) : (), + $s{AdminName} ? ( 'adminname = ?' => $s{AdminName}) : (), + $s{AdminEMail} ? ( 'adminemail = ?' => $s{AdminEMail}) : (), + $s{password} ? ( 'password = ?' => ( $s{password} ? 1 : 0) ) : (), + $s{gametype} ? ( 'gametype = ?' => $s{gametype}) : (), + $s{gamestyle} ? ( 'gamestyle = ?' => $s{gamestyle}) : (), + $s{changelevels} ? ( 'changelevels = ?' => ( $s{changelevels} ? 1 : 0) ) : (), + $s{maptitle} ? ( 'maptitle = ?' => $s{maptitle}) : (), + $s{mapname} ? ( 'mapname = ?' => $s{mapname}) : (), + $s{numplayers} ? ( 'numplayers = ?' => $s{numplayers}) : ('numplayers = ?' => 0), + $s{maxplayers} ? ( 'maxplayers = ?' => $s{maxplayers}) : ('maxplayers = ?' => 0), + $s{minplayers} ? ( 'minplayers = ?' => $s{minplayers}) : ('minplayers = ?' => 0), + $s{botskill} ? ( 'botskill = ?' => $s{botskill}) : (), + $s{balanceteams} ? ( 'balanceteams = ?' => ( $s{balanceteams} ? 1 : 0) ) : (), + $s{playersbalanceteams} ? ( 'playersbalanceteams = ?' => ( $s{playersbalanceteams} ? 1 : 0) ) : (), + $s{friendlyfire} ? ( 'friendlyfire = ?' => $s{friendlyfire}) : (), + $s{maxteams} ? ( 'maxteams = ?' => $s{maxteams}) : (), + $s{timelimit} ? ( 'timelimit = ?' => $s{timelimit}) : (), + $s{goalteamscore} ? ( 'goalteamscore = ?' => int( $s{goalteamscore}) ) : (), + $s{fraglimit} ? ( 'fraglimit = ?' => int( $s{fraglimit}) ) : (), + $s{mutators} ? ( 'mutators = ?' => $s{mutators}) : ('mutators = ?' => "None"), + $s{updated} ? ('updated = to_timestamp(?)' => $s{updated}) : (), + ); + + my($q, @p) = sqlprint("UPDATE utserver_info !H WHERE server_id = ?", \%H, $id); + return $self->{dbh}->do($q, undef, @p); +} + + +################################################################################ +## Add a new utserver and trigger the update routine above. +## opts: id, server info data +################################################################################ +sub add_utserver { + my ($self, $ip, $port) = @_; + + # create new entry + return $self->{dbh}->do( + "INSERT INTO utserver_info (server_id) + SELECT (SELECT id FROM serverlist WHERE ip = ? AND port = ?)", + undef, $ip, $port); +} + + +################################################################################ +## Delete all players from a certain server ID +## opts: server id +################################################################################ +sub delete_utplayers { + my ($self, $sid) = @_; + + # delete players for server_id + return $self->{dbh}->do( + "DELETE FROM utplayer_info WHERE server_id = ?", + undef, $sid); +} + +################################################################################ +## Insert player info for a single player in server sid +## opts: server id, player info +################################################################################ +sub insert_utplayer { + my $self = shift; + my $sid = shift; + my %s = ( + updated => time, + @_); + + # apparently useless chunk of code + # FIXME move to site part + my %H = ( + $s{server_id} ? ( 'server_id = ?' => $s{server_id}) : (), + $s{player} ? ( 'player = ?' => $s{player}) : (), + $s{team} ? ( 'team = ?' => int( $s{team})) : (), + $s{frags} ? ( 'frags = ?' => int( $s{frags})) : (), + $s{mesh} ? ( 'mesh = ?' => $s{mesh}) : (), + $s{skin} ? ( 'skin = ?' => $s{skin}) : (), + $s{face} ? ( 'face = ?' => $s{face}) : (), + $s{ping} ? ( 'ping = ?' => int( $s{ping})) : (), + $s{ngsecret} ? ( 'ngsecret = ?' => $s{ngsecret}) : (), + $s{updated} ? ('updated = to_timestamp(?)' => $s{updated}) : (), + ); + + # insert + return $self->{dbh}->do( + "INSERT INTO utplayer_info (server_id, player, team, frags, mesh, skin, face, ping, ngsecret) + VALUES (?,?,?,?,?,?,?,?,?)", + undef, $sid, $s{player}, $s{team}, $s{frags}, $s{mesh}, $s{skin}, $s{face}, $s{ping}, $s{ngsecret}); +} + +1; diff --git a/lib/MasterServer/Database/mysql/dbBeacon.pm b/lib/MasterServer/Database/mysql/dbBeacon.pm deleted file mode 100755 index fd956b7..0000000 --- a/lib/MasterServer/Database/mysql/dbBeacon.pm +++ /dev/null @@ -1,203 +0,0 @@ - -package MasterServer::Database::mysql::dbBeacon; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| add_beacon - add_pending - remove_pending - get_pending_beacon - get_pending_info - get_next_pending |; - -################################################################################ -## Update beacon in serverlist or pending list. Add if beacon does not exist in -## either list. Return 0,1,2 if success in adding or -1 on error. -################################################################################ -sub add_beacon { - my ($self, $ip, $beaconport, $heartbeat, $gamename, $secure) = @_; - - # if address is in list, update the timestamp - my $u = $self->{dbh}->do( - "UPDATE serverlist - SET beacon = NOW(), - updated = NOW(), - gamename = ?, - b333ms = 1 - WHERE ip = ? - AND port = ?", - undef, lc $gamename, $ip, $heartbeat); - - # notify - $self->log("update", "beacon heartbeat for $ip:$heartbeat") if ($u > 0); - - # if serverlist was updated return 0 - return 0 if ($u > 0); - - # if it is already in the pending list, update it with a new challenge - $u = $self->{dbh}->do( - "UPDATE pending - SET added = NOW(), - beaconport = ?, - gamename = ?, - secure = ? - WHERE ip = ? - AND heartbeat = ?", - undef, $beaconport, lc $gamename, $secure, $ip, $heartbeat); - - # notify - $self->log("update", "beacon heartbeat $ip:$beaconport pending $gamename:$heartbeat") if ($u > 0); - - # beacon was already in pending list and was updated - return 1 if ($u > 0); - - # if not found, add it - $u = $self->{dbh}->do( - "INSERT INTO pending ( - ip, - beaconport, - heartbeat, - gamename, - secure) - SELECT ?, ?, ?, ?, ?", - undef, $ip, $beaconport, $heartbeat, lc $gamename, $secure); - - # notify - $self->log("add", "beacon heartbeat $ip:$beaconport pending $gamename:$heartbeat") if ($u > 0); - - # it was added to pending - return 2 if ($u > 0); - - # or else report error - $self->log("error", "an error occurred adding beacon $ip:$beaconport with $gamename:$heartbeat to the pending list"); - return -1; -} - -################################################################################ -## Add an address to the database that was obtained via a method other than -## an udp beacon. Return 0,1,2 if success in adding or -1 on error. -################################################################################ -sub add_pending { - my ($self, $ip, $port, $gamename, $secure) = @_; - - # if address is in list, update the timestamp - my $u = $self->{dbh}->do( - "UPDATE serverlist - SET updated = NOW() - WHERE ip = ? - AND port = ?", - undef, $ip, $port); - - # notify - $self->log("update", "updated serverlist with $ip:$port") if ($u > 0); - - # if updated, return 0 - return 0 if ($u > 0); - - # if it is already in the pending list, update it with a new challenge - $u = $self->{dbh}->do( - "UPDATE pending - SET added = NOW(), - secure = ? - WHERE ip = ? - AND heartbeat = ?", - undef, $secure, $ip, $port); - - # notify - $self->log("update", "updated pending with $ip:$port") if ($u > 0); - - # return 1 if updated - return 1 if ($u > 0); - - # if not found, add it - $u = $self->{dbh}->do( - "INSERT INTO pending ( - ip, - heartbeat, - gamename, - secure) - SELECT ?, ?, ?, ?", - undef, $ip, $port, $gamename, $secure); - - # notify - $self->log("add", "$ip:$port added pending $gamename") if ($u > 0); - - # return 2 if added new - return 2 if ($u > 0); - - # else - return -1; -} - -################################################################################ -## Remove an entry from the pending list. Returns 0 if removed or -1 in case -## of error(s). -################################################################################ -sub remove_pending { - my ($self, $id) = @_; - - # if address is in list, update the timestamp - my $u = $self->{dbh}->do( - "DELETE FROM pending - WHERE id = ?", - undef, $id); - - # notify - $self->log("delete", "removed pending id $id from pending") if ($u > 0); - - # it was removed from pending - return 2 if ($u > 0); - - # or else report error - $self->log("error", "error deleting server $id from pending"); - return -1; -} - -################################################################################ -## Get pending server by ip, beacon port. Returns * or undef -################################################################################ -sub get_pending_beacon { - my ($self, $ip, $port) = @_; - - # if address is in list, update the timestamp - return $self->{dbh}->selectall_arrayref( - "SELECT * FROM pending - WHERE ip = ? - AND beaconport = ?", - undef, $ip, $port)->[0]; -} - -################################################################################ -## Get pending server by ip, heartbeat port. Returns * or undef -################################################################################ -sub get_pending_info { - my ($self, $ip, $port) = @_; - - # if address is in list, update the timestamp - return $self->{dbh}->selectall_arrayref( - "SELECT * FROM pending - WHERE ip = ? - AND heartbeat = ?", - undef, $ip, $port)->[0]; -} - -################################################################################ -## Get server info from any entry with an id higher than the provided one. The -## server is added to pending at least 15 seconds ago. Returns info or undef. -################################################################################ -sub get_next_pending { - my ($self, $id) = @_; - - # get 1 pending id that is older than 15s - return $self->{dbh}->selectall_arrayref( - "SELECT id, ip, heartbeat, secure FROM pending - WHERE added < NOW() - INTERVAL 15 SECOND - AND id > ? - ORDER BY id ASC LIMIT 1", - undef, $id)->[0]; -} - - -1; diff --git a/lib/MasterServer/Database/mysql/dbCiphers.pm b/lib/MasterServer/Database/mysql/dbCiphers.pm deleted file mode 100755 index 31db78b..0000000 --- a/lib/MasterServer/Database/mysql/dbCiphers.pm +++ /dev/null @@ -1,98 +0,0 @@ - -package MasterServer::Database::mysql::dbCiphers; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| clear_ciphers - insert_cipher - get_cipher - get_default_port |; - -################################################################################ -## Clear all existing ciphers from the database -################################################################################ -sub clear_ciphers { - my $self = shift; - - # delete ALL entries - my $u = $self->{dbh}->do("DELETE FROM games"); - - # notify - $self->log("delete", "Removed all ciphers") if ($u > 0); - - # removed from games - return 2 if ($u > 0); - - # or else report notice - $self->log("notice", "No ciphers deleted!"); - return -1; - -} - -################################################################################ -## Insert the list of supported games and their ciphers / default ports / -## descriptions included from the data/supportedgames.pl file. -################################################################################ -sub insert_cipher { - my ($self, %opt) = @_; - - # insert a single cipher/key combo - my $u = $self->{dbh}->do( - "INSERT INTO games ( - gamename, - cipher, - description, - default_qport) - VALUES(?, ?, ?, ?)", undef, - $opt{gamename}, $opt{cipher}, $opt{description}, $opt{default_qport}); - - # notify - $self->log("add", "Added cipher for $opt{gamename}") if ($u and $u > 0); - return 1 if ($u and $u > 0); - - # or else report error - $self->log("error", "An error occurred adding a cipher for $opt{gamename}"); - return -1; - -} - - -################################################################################ -## get the cipher that goes with gamename -################################################################################ -sub get_cipher { - my ($self, $gn) = @_; - - # no gamename specified? "undef" is not a known cipher, so send that instead. - return 'undef' if !$gn; - - # get cipher from db if gamename exists - my $cipher = $self->{dbh}->selectall_arrayref( - 'SELECT cipher FROM games WHERE gamename = ?', undef, - lc $gn)->[0]->[0]; - - # return a non-zero-length string - return ($cipher ? $cipher : 'undef'); -} - -################################################################################ -## get the default query port that goes with gamename -################################################################################ -sub get_default_port { - my ($self, $gn) = @_; - - # no gamename specified? default port is 0 - return 0 if !$gn; - - # get port from db if gamename exists - my $p = $self->{dbh}->selectall_arrayref( - 'SELECT default_qport FROM games WHERE gamename = ?', undef, - lc $gn)->[0]->[0]; - - # return port or zero - return $p || 0; -} - -1; diff --git a/lib/MasterServer/Database/mysql/dbClientList.pm b/lib/MasterServer/Database/mysql/dbClientList.pm deleted file mode 100755 index 898eb28..0000000 --- a/lib/MasterServer/Database/mysql/dbClientList.pm +++ /dev/null @@ -1,45 +0,0 @@ - -package MasterServer::Database::mysql::dbClientList; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| get_gamenames - get_game_list |; - - -################################################################################ -## get a list of distinct gamenames currently in the database. it does not -## matter whether they are recent or old, as long as the game is currently in -## the database. -## -## returns: hashref of gamenames -################################################################################ -sub get_gamenames { - my $self = shift; - - return $self->{dbh}->selectall_arrayref( - "SELECT distinct gamename - FROM serverlist"); -} - -################################################################################ -## get the list of games of a certain $gamename, excluding the ones excempted -## via the blacklist -## only returns server addresses that are no more than 1 hours old -################################################################################ -sub get_game_list { - my ($self, $gamename) = @_; - - return $self->{dbh}->selectall_arrayref( - "SELECT ip, port - FROM serverlist - WHERE updated > NOW() - INTERVAL 10800 SECOND - AND gamename = ? - AND NOT blacklisted", - undef, lc $gamename); -} - - -1; diff --git a/lib/MasterServer/Database/mysql/dbCore.pm b/lib/MasterServer/Database/mysql/dbCore.pm deleted file mode 100755 index 30fe3ef..0000000 --- a/lib/MasterServer/Database/mysql/dbCore.pm +++ /dev/null @@ -1,44 +0,0 @@ - -package MasterServer::Database::mysql::dbCore; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| database_login |; - -################################################################################ -## login to the database with credentials provided in the config file. -## returns dbh object or quits application on error. -################################################################################ -sub database_login { - my $self = shift; - - # create the dbi object - my $dbh = DBI->connect(@{$self->{dblogin}}, {PrintError => $self->{db_print}}); - - # verify that the database connected - if (defined $dbh) { - - # log the event - $self->log("load","Connected to the MySQL database."); - - # turn on error printing - $dbh->{printerror} = 1; - - # return the dbi object for further use - return $dbh; - } - else { - # fatal error - $self->log("fatal", "$DBI::errstr!"); - - # end program - $self->halt(); - } - - # unreachable - return undef; -} - -1; diff --git a/lib/MasterServer/Database/mysql/dbServerlist.pm b/lib/MasterServer/Database/mysql/dbServerlist.pm deleted file mode 100755 index 5bc1cd3..0000000 --- a/lib/MasterServer/Database/mysql/dbServerlist.pm +++ /dev/null @@ -1,153 +0,0 @@ - -package MasterServer::Database::mysql::dbServerlist; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT = qw| add_to_serverlist - update_serverlist - syncer_add - get_next_server |; - -################################################################################ -## beacon was verified or otherwise accepted and will now be added to the -## serverlist. -################################################################################ -sub add_to_serverlist { - my ($self, $ip, $port, $gamename) = @_; - - # update or add server to serverlist - my $u = $self->{dbh}->do("UPDATE serverlist - SET updated = NOW() - WHERE ip = ? - AND port = ?", - undef, $ip, $port); - - # notify - $self->log("update", "$ip:$port timestamp updated") if ($u > 0); - - # if found, updated; done - return 0 if ($u > 0); - - # if not found, add it. - $u = $self->{dbh}->do("INSERT INTO serverlist (ip, port, gamename, country) - SELECT ?, ?, ?, ?", - undef, $ip, $port, $gamename, $self->ip2country($ip)); - - # notify - $self->log("add", "$ip:$port added to serverlist") if ($u > 0); - - # return added - return 1 if ($u > 0); - - # or else report error - $self->log("error", "an error occurred adding server $ip:$port ($gamename) to the serverlist"); - return -1; -} - -################################################################################ -## same as add_to_serverlist above, but does not add the server to serverlist -## if it does not exist in serverlist. it must be added by another function -## first. -################################################################################ -sub update_serverlist { - my ($self, $ip, $port, $s) = @_; - - # update server info - my $u = $self->{dbh}->do( - 'UPDATE serverlist - SET updated = NOW(), - gamename = ?, - gamever = ?, - hostname = ?, - hostport = ? - WHERE ip = ? - AND port = ?', undef, - $s->{gamename}, $s->{gamever}, $s->{hostname}, $s->{hostport}, - $ip, $port); - - # notify - $self->log("update", "server $ip:$port info updated") if ($u > 0); - - # return 0 if updated - return 0 if ($u > 0); - - # or else report error - $self->log("error", "an error occurred updating server $ip:$port in the serverlist"); - return -1; -} - - -################################################################################ -## add new addresses to the pending list, but do not update timestamps. masters -## that sync with each other would otherwise update the timestamp for a server -## which is no longer online. -################################################################################ -sub syncer_add { - my ($self, $ip, $port, $gamename, $secure) = @_; - - # if address is in list, update the timestamp - my $u = $self->{dbh}->do( - "SELECT * FROM serverlist - WHERE ip = ? - AND port = ?", - undef, $ip, $port); - - # notify - $self->log("read","syncer found entry for $ip:$port") if ($u > 0); - - # if found, return 0 - return 0 if ($u > 0); - - # if it is already in the pending list, update it with a new challenge - $u = $self->{dbh}->do( - "UPDATE pending - SET secure = ? - WHERE ip = ? - AND heartbeat = ?", - undef, $secure, $ip, $port); - - # notify - $self->log("update","$ip:$port was updated by syncer", - $self->{log_settings}->{db_updated}) if ($u > 0); - - # return 1 if found - return 1 if ($u > 0); - - # if not found, add it - $u = $self->{dbh}->do( - "INSERT INTO pending (ip, heartbeat, gamename, secure) - SELECT ?, ?, ?, ?", - undef, $ip, $port, $gamename, $secure); - - # notify - $self->log("add","beacon: $ip:$port was added for $gamename after sync") if ($u > 0); - - # return 2 if added new - return 2 if ($u > 0); - - # or else report error - $self->log("error", "an error occurred adding $ip:$port after sync"); - return -1; -} - -################################################################################ -## get a server address of the next server in line to be queried for game info. -## query must be older than 30 seconds (in case it just got added) and not -## older than 3 hours. FIXME: now older servers are ignored! -################################################################################ -sub get_next_server { - my ($self, $id) = @_; - - return $self->{dbh}->selectall_arrayref( - "SELECT id, ip, port FROM serverlist - WHERE added < NOW() - INTERVAL 15 SECOND - AND updated > NOW() - INTERVAL 10800 SECOND - AND id > ? - AND NOT blacklisted - ORDER BY id ASC LIMIT 1", undef, $id)->[0]; - -} - -1; |
