aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/Database/SQLite/dbCiphers.pm
blob: f257b7b975199499eaff9e648a3e8e5f36a04330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package MasterServer::Database::SQLite::dbCiphers;

use strict;
use warnings;
use Exporter 'import';

our @EXPORT = qw| check_cipher_count
                  clear_ciphers 
                  insert_cipher 
                  get_game_props 
                  get_gamenames |;

################################################################################
## Check if ciphers exist
################################################################################
sub check_cipher_count {
  my $self = shift;
  return $self->db_all('SELECT count(*) as num from games')->[0]->{num};
}

################################################################################
## 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 $s = shift;
  my %o = (
    sort => '', 
    @_
  );
  
  my %where = (
    $o{gamename}      ? ('gamename = ?'       => lc $o{gamename})   : (),
    $o{cipher}        ? ('cipher = ?'         => $o{cipher})        : (),
    $o{description}   ? ('description = ?'    => $o{description})   : (),
    $o{default_qport} ? ('default_qport = ?'  => $o{default_qport}) : (),
    $o{num_uplink}    ? ('num_uplink = ?'     => $o{num_uplink})    : (),
    $o{num_total}     ? ('num_total = ?'      => $o{num_total})     : (),
    $o{num_gt}        ? ('num_total >= ?'     => $o{num_gt})        : (),
  );
  
  my @select = ( qw| gamename cipher description default_qport num_uplink num_total|,);
  my $order = sprintf {
      gamename      => 'gamename %s',
      cipher        => 'cipher %s',
      description   => 'description %s',
      default_qport => 'default_qport %s',
      num_uplink    => 'num_uplink %s',
      num_total     => 'num_total %s',
  }->{ $o{sort}||'gamename' }, $o{reverse} ? 'DESC' : 'ASC';

  return $s->db_all( q|
    SELECT !s FROM games
      !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;