LeaderboardCheckCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 96
rs 10
wmc 13
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 4 1
C checkPlayerLeaderboards() 0 63 10
A configure() 0 10 1
A update() 0 6 1
1
<?php
2
3
namespace Ps2alerts\Api\Command;
4
5
use Ps2alerts\Api\Command\BaseCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class LeaderboardCheckCommand extends BaseCommand
10
{
11
    protected $config;
12
    protected $redis;
13
14
    protected function configure()
15
    {
16
        parent::configure(); // See BaseCommand.php
17
        $this
18
            ->setName('Leaderboards:Check')
19
            ->setDescription('Checks all leaderboards for updates');
20
21
        $this->config = $this->container->get('config');
22
        $this->redis = $this->container->get('redis');
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output)
26
    {
27
        $this->checkPlayerLeaderboards($output);
28
    }
29
30
    public function checkPlayerLeaderboards(OutputInterface $output)
31
    {
32
        $output->writeln("Checking Player Leaderboards");
33
        $date = date('U');
34
        $deadline = $date - 21600; // 6 hours ago
35
36
        $servers = $this->config['servers'];
37
        $servers[] = 0;
38
39
        foreach ($servers as $server) {
40
            $output->writeln("Checking Server {$server}");
41
42
            $key = "ps2alerts:api:leaderboards:status:{$server}";
43
            $resultKey = "ps2alerts:api:leaderboards:lastResult:{$server}";
44
45
            if (!$this->redis->exists($key)) {
46
                $output->writeln("Key doesn't exist for server {$server}! Forcing!");
47
                $this->update($server, $output);
48
                continue;
49
            }
50
51
            $data = json_decode($this->redis->get($key), true);
52
53
            if ($data['beingUpdated'] == 1) {
54
                $output->writeln("Server {$server} is currently being updated. Deferring.");
55
                continue;
56
            }
57
58
            $query = $this->auraFactory->newSelect();
59
            $query->cols(['ResultID']);
60
            $query->from('ws_results');
61
            if ($server !== 0) {
62
                $query->where('ResultServer = ?', $server);
63
            }
64
            $query->where('InProgress = ?', 0);
65
            $query->orderBy(['ResultID DESC']);
66
            $query->limit(1);
67
68
            $statement = $this->db->prepare($query->getStatement());
69
            $statement->execute($query->getBindValues());
70
71
            $row = $statement->fetch(\PDO::FETCH_OBJ);
72
            $force = false;
73
74
            if (!$this->redis->exists($resultKey)) {
75
                $force = true;
76
            } else {
77
                $lastResult = $this->redis->get($resultKey);
78
                if ($lastResult != $row->ResultID) {
79
                    $force = true;
80
                }
81
            }
82
83
            if ($force === true) {
84
                $output->writeln("Forcing...");
85
            }
86
87
            if ($data['lastUpdated'] <= $deadline || $force === true) {
88
                $this->update($server, $output);
89
                $this->redis->set($resultKey, $row->ResultID);
90
            }
91
        }
92
    }
93
94
    /**
95
     * @param integer $server
96
     * @param OutputInterface $output
97
     */
98
    public function update($server, $output) {
99
        $output->writeln("Executing update for server: {$server}");
100
101
        $command = "php {$this->config['commands_path']} Leaderboard:Players {$server}";
102
        exec($command);
103
    }
104
}
105