GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 11c0a1...856a6b )
by Jacky
34s
created

PlayerRoutine::cmpFight()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace Asylamba\Modules\Atlas\Routine;
4
5
use Asylamba\Modules\Athena\Helper\OrbitalBaseHelper;
6
use Asylamba\Modules\Athena\Resource\OrbitalBaseResource;
7
use Asylamba\Classes\Library\Game;
8
9
use Asylamba\Modules\Athena\Resource\ShipResource;
10
11
use Asylamba\Classes\Library\DataAnalysis;
12
13
use Asylamba\Modules\Atlas\Manager\PlayerRankingManager;
14
use Asylamba\Modules\Atlas\Repository\PlayerRankingRepository;
15
16
use Asylamba\Modules\Atlas\Model\Ranking;
17
use Asylamba\Modules\Atlas\Model\PlayerRanking;
18
19
class PlayerRoutine extends AbstractRoutine
20
{
21
	/** @var array **/
22
	protected $results;
23
	
24
	const COEF_RESOURCE = 0.001;
25
	
26
	public function execute(
27
		$players,
28
		\PDOStatement $resourcesStatement,
29
		\PDOStatement $resourcesDataStatement,
30
		\PDOStatement $generalStatement,
31
		\PDOStatement $armiesStatement,
32
		\PDOStatement $planetStatement,
33
		\PDOStatement $tradeRoutesStatement,
34
		\PDOStatement $linkedTradeRoutesStatement,
35
		\PDOStatement $attackersStatement,
36
		\PDOStatement $defendersStatement,
37
		OrbitalBaseHelper $orbitalBaseHelper
38
	)
39
	{
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
40
		$this->results = [];
41
		# create an array with all the players
42
		foreach ($players as $player) {
43
			$this->results[$player->id] = [
44
				'general' => 0, 
45
				'resources' => 0,
46
				'experience' => 0, 
47
				'victory' => 0,
48
				'defeat' => 0,
49
				'fight' => 0,
50
				'armies' => 0,
51
				'butcher' => 0,
52
				'butcherDestroyedPEV' => 0,
53
				'butcherLostPEV' => 0,
54
				'trader' => 0,
55
56
				'DA_Resources' => 0,
57
				'DA_PlanetNumber' => 0
58
			];
59
		}
60
		$this->calculateResources($resourcesStatement, $orbitalBaseHelper);
61
		$this->calculateDataResources($resourcesDataStatement);
62
		$this->calculatePlanetRanking($planetStatement);
63
		$this->calculateGeneralRanking($generalStatement);
64
		$this->calculateArmiesRanking($armiesStatement);
65
		$this->calculateTradeRanking($tradeRoutesStatement, $linkedTradeRoutesStatement);
66
		$this->calculateButcherRanking($attackersStatement, $defendersStatement);
67
	}
68
	
69
	public function processResults(Ranking $ranking, $players, PlayerRankingManager $playerRankingManager, PlayerRankingRepository $playerRankingRepository)
70
	{
71
		foreach ($players as $playerId) {
72
			if (isset($this->results[$playerId->id])) {
73
				# add the points to the list
74
				$this->results[$playerId->id]['experience'] += $playerId->experience;
75
				$this->results[$playerId->id]['victory'] += $playerId->victory;
76
				$this->results[$playerId->id]['defeat'] += $playerId->defeat;
77
				$this->results[$playerId->id]['fight'] += $playerId->victory - $playerId->defeat;
78
			}
79
		}
80
81
		# copy the arrays
82
		$listG = $this->results;
83
		$listR = $this->results;
84
		$listE = $this->results;
85
		$listF = $this->results;
86
		$listA = $this->results;
87
		$listB = $this->results;
88
		$listT = $this->results;
89
90
		# sort all the copies
91
		uasort($listG, [$this, 'cmpGeneral']);
92
		uasort($listR, [$this, 'cmpResources']);
93
		uasort($listE, [$this, 'cmpExperience']);
94
		uasort($listF, [$this, 'cmpFight']);
95
		uasort($listA, [$this, 'cmpArmies']);
96
		uasort($listB, [$this, 'cmpButcher']);
97
		uasort($listT, [$this, 'cmpTrader']);
98
99
		/*foreach ($list as $key => $value) {
100
			echo $key . ' => ' . $value['general'] . '<br/>';
101
		}*/
102
103
		# put the position in each array
104
		$position = 1;
105
		foreach ($listG as $key => $value) { $listG[$key]['position'] = $position++;}
106
		$position = 1;
107
		foreach ($listR as $key => $value) { $listR[$key]['position'] = $position++;}
108
		$position = 1;
109
		foreach ($listE as $key => $value) { $listE[$key]['position'] = $position++;}
110
		$position = 1;
111
		foreach ($listF as $key => $value) { $listF[$key]['position'] = $position++;}
112
		$position = 1;
113
		foreach ($listA as $key => $value) { $listA[$key]['position'] = $position++;}
114
		$position = 1;
115
		foreach ($listB as $key => $value) { $listB[$key]['position'] = $position++;}
116
		$position = 1;
117
		foreach ($listT as $key => $value) { $listT[$key]['position'] = $position++;}
118
119
		foreach ($players as $player) {
120
			$playerId = $player->getId();
121
			
122
			$pr = new PlayerRanking();
123
			$pr->rRanking = $ranking->getId();
124
			$pr->rPlayer = $playerId; 
125
126
			# voir s'il faut améliorer (p.ex. : stocker le tableau des objets et supprimer chaque objet utilisé pour que la liste se rapetisse)
127
			$firstRanking = true;
128
			for ($i = 0; $i < $playerRankingManager->size(); $i++) {
129
				if ($playerRankingManager->get($i)->rPlayer == $playerId) {
130
					$firstRanking = false;
131
					$oldRanking = $playerRankingManager->get($i);
132
					break;
133
				}
134
			}
135
136
			$pr->general = $listG[$playerId]['general'];
137
			$pr->generalPosition = $listG[$playerId]['position'];
138
			$pr->generalVariation = $firstRanking ? 0 : $oldRanking->generalPosition - $pr->generalPosition;
0 ignored issues
show
Bug introduced by
The variable $oldRanking does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
139
			$player->factionPoint = $pr->general;
140
141
			$pr->resources = $listR[$playerId]['resources'];
142
			$pr->resourcesPosition = $listR[$playerId]['position'];
143
			$pr->resourcesVariation = $firstRanking ? 0 : $oldRanking->resourcesPosition - $pr->resourcesPosition;
144
145
			$pr->experience = $listE[$playerId]['experience'];
146
			$pr->experiencePosition = $listE[$playerId]['position'];
147
			$pr->experienceVariation = $firstRanking ? 0 : $oldRanking->experiencePosition - $pr->experiencePosition;
148
149
			$pr->fight = ($listF[$playerId]['fight'] >= 0) ? $listF[$playerId]['fight'] : 0;
150
			$pr->victories = $listF[$playerId]['victory'];
151
			$pr->defeat = $listF[$playerId]['defeat'];
152
			$pr->fightPosition = $listF[$playerId]['position'];
153
			$pr->fightVariation = $firstRanking ? 0 : $oldRanking->fightPosition - $pr->fightPosition;
154
155
			$pr->armies = $listA[$playerId]['armies'];
156
			$pr->armiesPosition = $listA[$playerId]['position'];
157
			$pr->armiesVariation = $firstRanking ? 0 : $oldRanking->armiesPosition - $pr->armiesPosition;
158
159
			$pr->butcher = ($listB[$playerId]['butcher'] >= 0) ? $listB[$playerId]['butcher'] : 0;
160
			$pr->butcherDestroyedPEV = $listB[$playerId]['butcherDestroyedPEV'];
161
			$pr->butcherLostPEV = $listB[$playerId]['butcherLostPEV'];
162
			$pr->butcherPosition = $listB[$playerId]['position'];
163
			$pr->butcherVariation = $firstRanking ? 0 : $oldRanking->butcherPosition - $pr->butcherPosition;
164
165
			$pr->trader = $listT[$playerId]['trader'];
166
			$pr->traderPosition = $listT[$playerId]['position'];
167
			$pr->traderVariation = $firstRanking ? 0 : $oldRanking->traderPosition - $pr->traderPosition;
168
169
			$playerRankingManager->add($pr);
170
171
			if (DATA_ANALYSIS) {
172
				$playerRankingRepository->insertDataAnalysis(
173
					$player,
174
					$pr,
175
					$this->results[$player]['DA_Resources'],
176
					$this->results[$player]['DA_PlanetNumber']
177
				);
178
			}
179
		}
180
	}
181
	
182
	protected function calculateResources(\PDOStatement $statement, OrbitalBaseHelper $orbitalBaseHelper)
183
	{
184
		while ($row = $statement->fetch()) {
185
			if (isset($this->results[$row['player']])) {
186
				$resourcesProd = Game::resourceProduction($orbitalBaseHelper->getBuildingInfo(OrbitalBaseResource::REFINERY, 'level', $row['levelRefinery'], 'refiningCoefficient'), $row['coefResources']);
187
				$this->results[$row['player']]['resources'] += $resourcesProd;
188
			}
189
		}
190
		$statement->closeCursor();
191
	}
192
	
193
	protected function calculateDataResources(\PDOStatement $statement)
194
	{
195
		while ($row = $statement->fetch()) {
196
			if (isset($this->results[$row['player']])) {
197
				$this->results[$row['player']]['DA_Resources'] += DataAnalysis::resourceToStdUnit($row['sumResources']);
198
			}
199
		}
200
		$statement->closeCursor();
201
	}
202
	
203
	protected function calculatePlanetRanking(\PDOStatement $statement)
204
	{
205
		while ($row = $statement->fetch()) {
206
			if (isset($this->results[$row['player']])) {
207
				$this->results[$row['player']]['DA_PlanetNumber'] += $row['sumPlanets'];
208
			}
209
		}
210
		$statement->closeCursor();
211
	}
212
	
213
	protected function calculateGeneralRanking(\PDOStatement $statement)
214
	{
215
		while ($row = $statement->fetch()) {
216
			if (isset($this->results[$row['player']])) {
217
				$shipPrice = 0;
218
				$shipPrice += ShipResource::getInfo(0, 'resourcePrice') * $row['s0'];
219
				$shipPrice += ShipResource::getInfo(1, 'resourcePrice') * $row['s1'];
220
				$shipPrice += ShipResource::getInfo(2, 'resourcePrice') * $row['s2'];
221
				$shipPrice += ShipResource::getInfo(3, 'resourcePrice') * $row['s3'];
222
				$shipPrice += ShipResource::getInfo(4, 'resourcePrice') * $row['s4'];
223
				$shipPrice += ShipResource::getInfo(5, 'resourcePrice') * $row['s5'];
224
				$shipPrice += ShipResource::getInfo(6, 'resourcePrice') * $row['s6'];
225
				$shipPrice += ShipResource::getInfo(7, 'resourcePrice') * $row['s7'];
226
				$shipPrice += ShipResource::getInfo(8, 'resourcePrice') * $row['s8'];
227
				$shipPrice += ShipResource::getInfo(9, 'resourcePrice') * $row['s9'];
228
				$shipPrice += ShipResource::getInfo(10, 'resourcePrice') * $row['s10'];
229
				$shipPrice += ShipResource::getInfo(11, 'resourcePrice') * $row['s11'];
230
				$points = round($shipPrice * self::COEF_RESOURCE);
231
				$points += $row['points'];
232
				$points += round($row['resources'] * self::COEF_RESOURCE);
233
				$this->results[$row['player']]['general'] += $points;
234
235
				$pevQuantity = 0;
236
				$pevQuantity += ShipResource::getInfo(0, 'pev') * $row['s0'];
237
				$pevQuantity += ShipResource::getInfo(1, 'pev') * $row['s1'];
238
				$pevQuantity += ShipResource::getInfo(2, 'pev') * $row['s2'];
239
				$pevQuantity += ShipResource::getInfo(3, 'pev') * $row['s3'];
240
				$pevQuantity += ShipResource::getInfo(4, 'pev') * $row['s4'];
241
				$pevQuantity += ShipResource::getInfo(5, 'pev') * $row['s5'];
242
				$pevQuantity += ShipResource::getInfo(6, 'pev') * $row['s6'];
243
				$pevQuantity += ShipResource::getInfo(7, 'pev') * $row['s7'];
244
				$pevQuantity += ShipResource::getInfo(8, 'pev') * $row['s8'];
245
				$pevQuantity += ShipResource::getInfo(9, 'pev') * $row['s9'];
246
				$pevQuantity += ShipResource::getInfo(10, 'pev') * $row['s10'];
247
				$pevQuantity += ShipResource::getInfo(11, 'pev') * $row['s11'];
248
				$this->results[$row['player']]['armies'] += $pevQuantity;
249
			}
250
		}
251
		$statement->closeCursor();
252
	}
253
	
254
	protected function calculateArmiesRanking(\PDOStatement $statement)
255
	{
256
		while ($row = $statement->fetch()) {
257
			if (isset($this->results[$row['player']])) {
258
				$shipPrice = 0;
259
				$shipPrice += ShipResource::getInfo(0, 'resourcePrice') * $row['s0'];
260
				$shipPrice += ShipResource::getInfo(1, 'resourcePrice') * $row['s1'];
261
				$shipPrice += ShipResource::getInfo(2, 'resourcePrice') * $row['s2'];
262
				$shipPrice += ShipResource::getInfo(3, 'resourcePrice') * $row['s3'];
263
				$shipPrice += ShipResource::getInfo(4, 'resourcePrice') * $row['s4'];
264
				$shipPrice += ShipResource::getInfo(5, 'resourcePrice') * $row['s5'];
265
				$shipPrice += ShipResource::getInfo(6, 'resourcePrice') * $row['s6'];
266
				$shipPrice += ShipResource::getInfo(7, 'resourcePrice') * $row['s7'];
267
				$shipPrice += ShipResource::getInfo(8, 'resourcePrice') * $row['s8'];
268
				$shipPrice += ShipResource::getInfo(9, 'resourcePrice') * $row['s9'];
269
				$shipPrice += ShipResource::getInfo(10, 'resourcePrice') * $row['s10'];
270
				$shipPrice += ShipResource::getInfo(11, 'resourcePrice') * $row['s11'];
271
				$points = round($shipPrice * self::COEF_RESOURCE);
272
				$this->results[$row['player']]['general'] += $points;
273
274
				$pevQuantity = 0;
275
				$pevQuantity += ShipResource::getInfo(0, 'pev') * $row['s0'];
276
				$pevQuantity += ShipResource::getInfo(1, 'pev') * $row['s1'];
277
				$pevQuantity += ShipResource::getInfo(2, 'pev') * $row['s2'];
278
				$pevQuantity += ShipResource::getInfo(3, 'pev') * $row['s3'];
279
				$pevQuantity += ShipResource::getInfo(4, 'pev') * $row['s4'];
280
				$pevQuantity += ShipResource::getInfo(5, 'pev') * $row['s5'];
281
				$pevQuantity += ShipResource::getInfo(6, 'pev') * $row['s6'];
282
				$pevQuantity += ShipResource::getInfo(7, 'pev') * $row['s7'];
283
				$pevQuantity += ShipResource::getInfo(8, 'pev') * $row['s8'];
284
				$pevQuantity += ShipResource::getInfo(9, 'pev') * $row['s9'];
285
				$pevQuantity += ShipResource::getInfo(10, 'pev') * $row['s10'];
286
				$pevQuantity += ShipResource::getInfo(11, 'pev') * $row['s11'];
287
				$this->results[$row['player']]['armies'] += $pevQuantity;
288
			}
289
		}
290
		$statement->closeCursor();
291
	}
292
	
293
	protected function calculateTradeRanking(\PDOStatement $routesStatement, \PDOStatement $linkedRoutesStatement)
294
	{
295
		while ($row = $routesStatement->fetch()) {
296
			if (isset($this->results[$row['player']])) {
297
				$this->results[$row['player']]['trader'] += $row['income'];
298
			}
299
		}
300
		$routesStatement->closeCursor();
301
		while ($row = $linkedRoutesStatement->fetch()) {
302
			if (isset($this->results[$row['player']])) {
303
				$this->results[$row['player']]['trader'] += $row['income'];
304
			}
305
		}
306
		$linkedRoutesStatement->closeCursor();
307
	}
308
	
309
	protected function calculateButcherRanking(\PDOStatement $attackerStatement, \PDOStatement $defenderStatement)
310
	{
311
		while ($row = $attackerStatement->fetch()) {
312
			if (isset($this->results[$row['player']])) {
313
				$this->results[$row['player']]['butcherDestroyedPEV'] += $row['destroyedPEV'];
314
				$this->results[$row['player']]['butcherLostPEV'] += $row['lostPEV'];
315
				$this->results[$row['player']]['butcher'] += $row['destroyedPEV'] - $row['lostPEV'];
316
			}
317
		}
318
		$attackerStatement->closeCursor();
319
		while ($row = $defenderStatement->fetch()) {
320
			if (isset($this->results[$row['player']])) {
321
				$this->results[$row['player']]['butcherDestroyedPEV'] += $row['destroyedPEV'];
322
				$this->results[$row['player']]['butcherLostPEV'] += $row['lostPEV'];
323
				$this->results[$row['player']]['butcher'] += $row['destroyedPEV'] - $row['lostPEV'];
324
			}
325
		}
326
		$defenderStatement->closeCursor();
327
	}
328
329
	protected function cmpGeneral($a, $b)
330
	{
331
		if($a['general'] == $b['general']) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
332
			return 0;
333
		}
334
		return ($a['general'] > $b['general']) ? -1 : 1;
335
	}
336
337
	protected function cmpResources($a, $b) {
338
		if($a['resources'] == $b['resources']) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
339
			return 0;
340
		}
341
		return ($a['resources'] > $b['resources']) ? -1 : 1;
342
	}
343
344
	protected function cmpExperience($a, $b) {
345
		if($a['experience'] == $b['experience']) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
346
			return 0;
347
		}
348
		return ($a['experience'] > $b['experience']) ? -1 : 1;
349
	}
350
351
	protected function cmpFight($a, $b) {
352
		if($a['fight'] == $b['fight']) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
353
			return 0;
354
		}
355
		return ($a['fight'] > $b['fight']) ? -1 : 1;
356
	}
357
358
	protected function cmpArmies($a, $b) {
359
		if($a['armies'] == $b['armies']) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
360
			return 0;
361
		}
362
		return ($a['armies'] > $b['armies']) ? -1 : 1;
363
	}
364
365
	protected function cmpButcher($a, $b) {
366
		if($a['butcher'] == $b['butcher']) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
367
			return 0;
368
		}
369
		return ($a['butcher'] > $b['butcher']) ? -1 : 1;
370
	}
371
372
	protected function cmpTrader($a, $b) {
373
		if($a['trader'] == $b['trader']) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
374
			return 0;
375
		}
376
		return ($a['trader'] > $b['trader']) ? -1 : 1;
377
	}
378
}