Completed
Pull Request — master (#172)
by De Cramer
03:09
created

GenDummyRecordsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 1
eloc 7
nc 1
nop 0
crap 1
1
<?php
2
3
namespace eXpansion\Bundle\LocalRecords\Command;
4
5
use eXpansion\Bundle\LocalRecords\Model\Map\RecordTableMap;
6
use eXpansion\Bundle\LocalRecords\Model\Record;
7
use eXpansion\Bundle\LocalRecords\Model\RecordQueryBuilder;
8
use eXpansion\Bundle\Maps\Model\Base\MapQuery;
9
use eXpansion\Bundle\Maps\Model\MapQueryBuilder;
10
use eXpansion\Framework\Core\Services\Console;
11
use eXpansion\Framework\Core\Storage\MapStorage;
12
use eXpansion\Framework\PlayersBundle\Model\Map\PlayerTableMap;
13
use eXpansion\Framework\PlayersBundle\Model\Player;
14
use eXpansion\Framework\PlayersBundle\Model\PlayerQueryBuilder;
15
use Maniaplanet\DedicatedServer\Connection;
16
use Propel\Runtime\Propel;
17
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
18
use Symfony\Component\Console\Helper\ProgressBar;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
24
/**
25
 * Class Records
26
 *
27
 * @author  reaby
28
 */
29
class GenDummyRecordsCommand extends ContainerAwareCommand
30
{
31
    /**
32
     * @var Connection
33
     */
34
    private $connection;
35
    /**
36
     * @var MapStorage
37
     */
38
    private $mapStorage;
39
    /**
40
     * @var PlayerQueryBuilder
41
     */
42
    private $playerQueryBuilder;
43
    /**
44
     * @var RecordQueryBuilder
45
     */
46
    private $recordQueryBuilder;
47
    /**
48
     * @var Console
49
     */
50
    private $console;
51
    /**
52
     * @var MapQuery
53
     */
54
    private $mapQuery;
55
56
    /**
57
     * ScriptPanel constructor.
58
     *
59
     * @param Connection         $connection
60
     * @param MapStorage         $mapStorage
61
     * @param PlayerQueryBuilder $playerQueryBuilder
62
     * @param RecordQueryBuilder $recordQueryBuilder
63
     * @param MapQueryBuilder    $mapQuery
64
     * @param Console            $console
65
     */
66 131 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
        Connection $connection,
68
        MapStorage $mapStorage,
69
        PlayerQueryBuilder $playerQueryBuilder,
70
        RecordQueryBuilder $recordQueryBuilder,
71
        MapQueryBuilder $mapQuery,
72
        Console $console
73
    ) {
74 131
        parent::__construct();
75 131
        $this->mapStorage = $mapStorage;
76 131
        $this->playerQueryBuilder = $playerQueryBuilder;
77 131
        $this->recordQueryBuilder = $recordQueryBuilder;
78 131
        $this->mapQuery = $mapQuery;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mapQuery of type object<eXpansion\Bundle\...\Model\MapQueryBuilder> is incompatible with the declared type object<eXpansion\Bundle\Maps\Model\Base\MapQuery> of property $mapQuery.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79 131
        $this->console = $console;
80 131
        $this->connection = $connection;
81 131
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 131
    protected function configure()
87
    {
88
        $this
89
            // the name of the command (the part after "bin/console")
90 131
            ->setName('eXpansion:testing:generateTestData')
91
            // the short description shown while running "php bin/console list"
92 131
            ->setDescription('creates defined number of users with records to all maps.')
93
            // the full command description shown when running the command with
94
            // the "--help" option
95 131
            ->setHelp('This command generates defined number of users and records to each map...');
96
        $this
97
            // configure an argument
98 131
            ->addArgument('count', InputArgument::REQUIRED, 'The number of users and records to generate.');
99
100 131
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105
    public function execute(InputInterface $input, OutputInterface $output)
106
    {
107
        $this->console->init($output, null);
108
109
        $count = $input->getArgument('count');
110
        $this->console->writeln('Checking if there\'s enough players');
111
112
        $preAdd = $this->playerQueryBuilder->findDummy();
113
114
        if (count($preAdd) < $count) {
115
116
            $this->console->writeln("Generating missing players up to $count");
117
            $progress = new ProgressBar($output, $count);
118
            $progress->start();
119
            $progress->advance(count($preAdd));
120
121
            $con = Propel::getWriteConnection(PlayerTableMap::DATABASE_NAME);
122
            $con->beginTransaction();
123
124
            for ($x = count($preAdd); $x < $count; $x++) {
125
                $player = new Player();
126
                $login = "dummyplayer_$x";
127
                $player->setNickname($login);
128
                $player->setNicknameStripped($login);
129
                $player->setLogin($login);
130
                $player->setPath("World|Hello");
131
                $player->save();
132
                $progress->advance();
133
            }
134
            $con->commit();
135
            $progress->finish();
136
        }
137
138
        unset($preAdd);
139
140
        /** @var Player[] $players */
141
        $players = $this->playerQueryBuilder->findDummy();
142
143
        $this->console->writeln("Generating maximum of $count records for all maps on server");
144
        $maps = $this->mapQuery->getAllMaps();
145
146
        $con = Propel::getWriteConnection(RecordTableMap::DATABASE_NAME);
147
        $i = 1;
148
        foreach ($maps as $m => $map) {
149
            $records = $this->recordQueryBuilder->getMapRecords($map->getMapuid(), 1, "asc", 1000);
150
151
152
            $idsUsed = [];
153
            if (count($records) < $count) {
154
                foreach ($records as $record) {
155
                    $idsUsed[] = $record->getPlayerId();
156
                }
157
                $this->console->writeln("Generating records for map ".$i."/".count($maps)." -> ".$map->getName());
158
                $con->beginTransaction();
159
160
                $record = new Record();
161
                $record->setNblaps(1);
162
                $record->setNbFinish(1);
163
                $record->setMapuid($map->getMapuid());
164
                $record->setCreatedAt(new \DateTime());
165
                $record->setCheckpoints([]);
166
                $progress = new ProgressBar($output, $count - count($records));
167
                $progress->start();
168
                for ($x = (count($records) + 1); $x < $count; $x++) {
169
                    if (!in_array($x, $idsUsed)) {
170
                        $rec = clone $record;
171
                        $rec->setScore(mt_rand($map->getGoldtime(), $map->getGoldtime() * 5));
172
                        $rec->setPlayerId($players[$x-1]->getId());
173
                        $rec->save();
174
                        $progress->advance();
175
                    }
176
                }
177
                $con->commit();
178
                $progress->finish();
179
                $i++;
180
                RecordTableMap::clearInstancePool();
181
                RecordTableMap::clearRelatedInstancePool();
182
183
            } else {
184
                $this->console->writeln("skipping map, records alreaady set!");
185
            }
186
        }
187
        PlayerTableMap::clearInstancePool();
188
        PlayerTableMap::clearRelatedInstancePool();
189
    }
190
}
191