1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This symfony command will recalculate Elo matches |
4
|
|
|
* |
5
|
|
|
* @package BZiON |
6
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace BZIon\Command; |
10
|
|
|
|
11
|
|
|
use Match; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
class RecalculateEloCommand extends Command |
17
|
|
|
{ |
18
|
|
|
protected function configure() |
19
|
|
|
{ |
20
|
|
|
$this |
21
|
|
|
->setName('bzion:recalc-elo') |
22
|
|
|
->setDescription('Recalculate Elos for a given match and all of the matches proceeding it.') |
23
|
|
|
->addArgument('matchID', InputArgument::REQUIRED, 'The ID of the first match that needs to be recalculated.') |
24
|
|
|
; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
28
|
|
|
{ |
29
|
|
|
$id = $input->getArgument('matchID'); |
30
|
|
|
$match = Match::get($id); |
31
|
|
|
|
32
|
|
|
if (!$match->isValid()) { |
33
|
|
|
$output->writeln(sprintf('No match found with id: %d', $id)); |
34
|
|
|
exit(1); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$output->writeln(sprintf('Starting Elo recalculation with match #%d...', $id)); |
38
|
|
|
$output->writeln(sprintf(' (this may take a while)')); |
39
|
|
|
|
40
|
|
|
try { |
41
|
|
|
Match::recalculateMatchesSince($match, function ($event) use ($output) { |
42
|
|
|
switch ($event['type']) { |
43
|
|
|
case 'recalculation.count': |
44
|
|
|
$output->writeln(sprintf("\nDetected %d matches needing recalculations\n", $event['value'])); |
45
|
|
|
$this->initProgress($output, $event['value']); |
46
|
|
|
break; |
47
|
|
|
|
48
|
|
|
case 'recalculation.progress': |
49
|
|
|
$this->advance(); |
50
|
|
|
break; |
51
|
|
|
|
52
|
|
|
case 'recalculation.complete': |
53
|
|
|
$this->finishProgress(); |
54
|
|
|
break; |
55
|
|
|
|
56
|
|
|
default: |
57
|
|
|
$output->writeln(sprintf('Received unknown recalculation event: %s', $event['type'])); |
58
|
|
|
} |
59
|
|
|
}); |
60
|
|
|
} |
61
|
|
|
catch (\Exception $e) { |
62
|
|
|
$errMsg = $e->getMessage(); |
63
|
|
|
|
64
|
|
|
$output->writeln(<<<EXCEPTION |
65
|
|
|
|
66
|
|
|
<bg=red;options=bold> |
67
|
|
|
[ERROR] An exception occurred with the following message: |
68
|
|
|
${$errMsg} |
69
|
|
|
</> |
70
|
|
|
EXCEPTION |
71
|
|
|
); |
72
|
|
|
exit(1); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$output->writeln(<<<SUCCESS |
76
|
|
|
|
77
|
|
|
<bg=green;options=bold> |
78
|
|
|
|
79
|
|
|
[OK] Match Elos have been successfully recalculated. |
80
|
|
|
</> |
81
|
|
|
SUCCESS |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.