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
Branch master (dec958)
by Jacky
07:11 queued 02:46
created

system/Classes/Library/Game.php (9 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Asylamba\Classes\Library;
4
5
use Asylamba\Modules\Ares\Model\Commander;
6
7
use Asylamba\Modules\Athena\Model\CommercialRoute;
8
use Asylamba\Modules\Athena\Model\Transaction;
9
use Asylamba\Modules\Athena\Resource\ShipResource;
10
11
use Asylamba\Modules\Gaia\Resource\PlaceResource;
12
13
use Asylamba\Modules\Artemis\Model\SpyReport;
14
15
use Asylamba\Modules\Zeus\Model\PlayerBonus;
16
17
18
class Game {
19
20
	const COMMERCIAL_TIME_TRAVEL = 0.2;
21
22
	public static function convertPlaceType($type) {
23
		switch ($type) {
24
			case 1 : return 'planète tellurique';
0 ignored issues
show
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
25
			case 2 : return 'géante gazeuse';
26
			case 3 : return 'ruine';
0 ignored issues
show
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
27
			case 4 : return 'poche de gaz';
28
			case 5 : return 'ceinture d\'astéroïdes';
0 ignored issues
show
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
29
			case 6 : return 'zone vide';
30
			default: return 'rien';
31
		}
32
	}
33
34
	public static function getSizeOfPlanet($population) {
35
		if ($population < 100) {
36
			return 1;
37
		} elseif ($population < 200) {
38
			return 2;
39
		} else {
40
			return 3;
41
		}
42
	}
43
44
	public static function formatCoord($xCoord, $yCoord, $planetPosition = 0, $sectorLocation = 0) {
45
		if ($sectorLocation > 0) {
46
			return '⟨' . $sectorLocation . '⟩ ' . $xCoord . ':' . $yCoord . ':' . $planetPosition . '';
47
		} elseif ($planetPosition > 0) {
48
			return $xCoord . ':' . $yCoord . ':' . $planetPosition;
49
		} else {
50
			return $xCoord . ':' . $yCoord;
51
		}
52
	}
53
54
	public static function resourceProduction($coeffRefinery, $coeffPlanet) {
55
		return $coeffRefinery * $coeffPlanet;
56
	}
57
58
	public static function getDistance($xa, $xb, $ya, $yb) {
59
		$distance = floor((sqrt((($xa - $xb) * ($xa - $xb)) + (($ya - $yb) * ($ya - $yb)))));
60
		return ($distance < 1) ? 1 : $distance;
61
	}
62
63
	public static function getFleetSpeed($bonus) {
64
		$b = $bonus != NULL
65
			? Commander::FLEETSPEED * (3 * ($bonus->get(PlayerBonus::SHIP_SPEED) / 100)) : 0;
66
		return Commander::FLEETSPEED + $b;
67
	}
68
69
	public static function getMaxTravelDistance($bonus) {
70
		return Commander::DISTANCEMAX;
71
	}
72
73
	public static function getTimeToTravelCommercial($startPlace, $destinationPlace, $bonus = NULL) {
74
		return round(self::getTimeToTravel($startPlace, $destinationPlace, $bonus) * self::COMMERCIAL_TIME_TRAVEL);
75
	}
76
77
	public static function getTimeToTravel($startPlace, $destinationPlace, $bonus = NULL) {
78
		# $startPlace and $destinationPlace are instance of Place
79
		return self::getTimeTravel(
80
			$startPlace->getRSystem(),
81
			$startPlace->getPosition(),
82
			$startPlace->getXSystem(),
83
			$startPlace->getYSystem(),
84
			$destinationPlace->getRSystem(),
85
			$destinationPlace->getPosition(),
86
			$destinationPlace->getXSystem(),
87
			$destinationPlace->getYSystem(),
88
			$bonus
89
		);
90
	}
91
92
	public static function getTimeTravelCommercial($systemFrom, $positionFrom, $xFrom, $yFrom, $systemTo, $positionTo, $xTo, $yTo, $bonus = NULL) {
93
		return round(self::getTimeTravel($systemFrom, $positionFrom, $xFrom, $yFrom, $systemTo, $positionTo, $xTo, $yTo, $bonus) * self::COMMERCIAL_TIME_TRAVEL);
94
	}
95
96
	public static function getTimeTravel($systemFrom, $positionFrom, $xFrom, $yFrom, $systemTo, $positionTo, $xTo, $yTo, $bonus = NULL) {
97
		return $systemFrom == $systemTo
98
			? Game::getTimeTravelInSystem($positionFrom, $positionTo)
99
			: Game::getTimeTravelOutOfSystem($bonus, $xFrom, $yFrom, $xTo, $yTo);
100
	}
101
102
	public static function getTimeTravelInSystem($startPosition, $destinationPosition) {
103
		$distance = abs($startPosition - $destinationPosition);
104
		return round((Commander::COEFFMOVEINSYSTEM * $distance) * ((40 - $distance) / 50) + 180);
105
	}
106
107
	public static function getTimeTravelOutOfSystem($bonus, $startX, $startY, $destinationX, $destinationY) {
108
		$distance = self::getDistance($startX, $destinationX, $startY, $destinationY);
109
		$time  = Commander::COEFFMOVEOUTOFSYSTEM;
110
		$time += round((Commander::COEFFMOVEINTERSYSTEM * $distance) / self::getFleetSpeed($bonus));
111
		return $time;
112
	}
113
114
	public static function getRCPrice($distance) {
115
		return $distance * CommercialRoute::COEF_PRICE;
116
	}
117
118
	public static function getRCIncome($distance, $bonusA = 1, $bonusB = 1) {
119
		$income = CommercialRoute::COEF_INCOME_2 * sqrt($distance * CommercialRoute::COEF_INCOME_1);
120
		$maxIncome = CommercialRoute::COEF_INCOME_2 * sqrt(100 * CommercialRoute::COEF_INCOME_1);
121
		if ($income > $maxIncome) {
122
			$income = $maxIncome;
123
		}
124
		return round($income * $bonusA * $bonusB);
125
	}
126
127
 	public static function getTaxFromPopulation($population, $typeOfBase, $taxCoeff) {
128
		$tax  = ((180 * $population) + 1500) * $taxCoeff;
129
		$tax *= PlaceResource::get($typeOfBase, 'tax');
130
		return $tax;
131
	}
132
133
	public static function getAntiSpyRadius($investment, $mode = ANTISPY_DISPLAY_MODE) {
134
		return $mode == ANTISPY_DISPLAY_MODE
135
			# en pixels : sert à l'affichage
136
			? sqrt($investment / 3.14) * 20
137
			# en position du jeu (250x250)
138
			: sqrt($investment / 3.14);
139
	}
140
141
	public static function antiSpyArea($startPlace, $destinationPlace, $arrivalDate) {
142
		# dans le même système
143
		if ($startPlace->getRSystem() == $destinationPlace->getRSystem()) {
144
			return ANTISPY_LITTLE_CIRCLE;
145
		} else {
146
			$duration = self::getTimeToTravel($startPlace, $destinationPlace);
147
148
			$secRemaining = strtotime($arrivalDate) - strtotime(Utils::now());
149
			$ratioRemaining = $secRemaining / $duration;
150
151
			$distance = self::getDistance($startPlace->getXSystem(), $destinationPlace->getXSystem(), $startPlace->getYSystem(), $destinationPlace->getYSystem());
152
			$distanceRemaining = $distance * $ratioRemaining;
153
154
			$antiSpyRadius = self::getAntiSpyRadius($destinationPlace->getAntiSpyInvest(), 1);
155
156
			if ($antiSpyRadius >= $distanceRemaining) {
157
				if ($distanceRemaining < $antiSpyRadius / 3) {
158
					return ANTISPY_LITTLE_CIRCLE;
159
				} elseif ($distanceRemaining < $antiSpyRadius / 3 * 2) {
160
					return ANTISPY_MIDDLE_CIRCLE;
161
				} else {
162
					return ANTISPY_BIG_CIRCLE;
163
				}
164
			} else {
165
				return ANTISPY_OUT_OF_CIRCLE;
166
			}
167
		}
168
	}
169
170
	public static function getAntiSpyEntryTime($startPlace, $destinationPlace, $arrivalDate) {
171
		# dans le même système
172
		if ($startPlace->getRSystem() == $destinationPlace->getRSystem()) {
173
			return array(TRUE, TRUE, TRUE);
174
		} else {
175
			$duration = self::getTimeToTravel($startPlace, $destinationPlace);
176
177
			$secRemaining = strtotime($arrivalDate) - strtotime(Utils::now());
178
			$ratioRemaining = $secRemaining / $duration;
179
180
			$distance = self::getDistance($startPlace->getXSystem(), $destinationPlace->getXSystem(), $startPlace->getYSystem(), $destinationPlace->getYSystem());
181
			$distanceRemaining = $distance * $ratioRemaining;
182
183
			$antiSpyRadius = self::getAntiSpyRadius($destinationPlace->getAntiSpyInvest(), 1);
184
185
			if ($distanceRemaining < $antiSpyRadius / 3) {
186
				return array(TRUE, TRUE, TRUE);
187
			} elseif ($distanceRemaining < $antiSpyRadius / 3 * 2) {
188
				$ratio = ($antiSpyRadius / 3) / $distanceRemaining;
189
				$sec = $ratio * $secRemaining;
190
				$newDate = Utils::addSecondsToDate($arrivalDate, -$sec);
191
192
				return array(TRUE, TRUE, $newDate);
193
			} elseif ($distanceRemaining < $antiSpyRadius) {
194
				$ratio = ($antiSpyRadius / 3 * 2) / $distanceRemaining;
195
				$sec = $ratio * $secRemaining;
196
				$newDate1 = Utils::addSecondsToDate($arrivalDate, -$sec);
197
198
				$ratio = ($antiSpyRadius / 3) / $distanceRemaining;
199
				$sec = $ratio * $secRemaining;
200
				$newDate2 = Utils::addSecondsToDate($arrivalDate, -$sec);
201
202
				return array(TRUE, $newDate1, $newDate2);
203
			} else {
204
				$ratio = $antiSpyRadius / $distanceRemaining;
205
				$sec = $ratio * $secRemaining;
206
				$newDate1 = Utils::addSecondsToDate($arrivalDate, -$sec);
207
208
				$ratio = ($antiSpyRadius / 3 * 2) / $distanceRemaining;
209
				$sec = $ratio * $secRemaining;
210
				$newDate2 = Utils::addSecondsToDate($arrivalDate, -$sec);
211
212
				$ratio = ($antiSpyRadius / 3) / $distanceRemaining;
213
				$sec = $ratio * $secRemaining;
214
				$newDate3 = Utils::addSecondsToDate($arrivalDate, -$sec);
215
216
				return array($newDate1, $newDate2, $newDate3);
217
			}
218
		}
219
	}
220
221
	public static function getCommercialShipQuantityNeeded($transactionType, $quantity, $identifier = 0) {
222
		switch ($transactionType) {
223
			case Transaction::TYP_RESOURCE :
224
				# 1000 ressources => 1 commercialShip
225
				$needed = ceil($quantity / 1000);
226
				break;
227
			case Transaction::TYP_SHIP :
228
				# 1 PEV => 1 commercialShip
229
				if (ShipResource::isAShip($identifier) AND $quantity > 0) {
230
					$needed = $quantity * ShipResource::getInfo($identifier, 'pev');
231
				} else {
232
					$needed = FALSE;
233
				}
234
				break;
235
			case Transaction::TYP_COMMANDER :
236
				# 1 commander => 1 commercialShip
237
				$needed = 1;
238
				break;
239
			default :
240
				$needed = FALSE;
241
				break;
242
		}
243
		return $needed;
244
	}
245
246
	public static function calculateCurrentRate($currentRate, $transactionType, $quantity, $identifier, $price) {
247
		# calculate the new rate (when a transaction is accepted)
248
		switch ($transactionType) {
249
			case Transaction::TYP_RESOURCE :
250
				# 1 resource = x credit
251
				$thisRate = $price / $quantity;
252
				# dilution of 1%
253
				$newRate = (($quantity * $thisRate) + (50000 * (99 * $currentRate)) / 100) / (50000 + $quantity);
254
				return max($newRate, Transaction::MIN_RATE_RESOURCE);
255
				break;
256
			case Transaction::TYP_SHIP :
257
				# 1 resource = x credit
258
				if (ShipResource::isAShip($identifier)) {
259
					$resourceQuantity = ShipResource::getInfo($identifier, 'resourcePrice') * $quantity;
260
					$thisRate = $price / $resourceQuantity;
261
					# dilution of 1%
262
					$newRate = (($resourceQuantity * $thisRate) + (50000 * (99 * $currentRate)) / 100) / (50000 + $resourceQuantity);
263
					return max($newRate, Transaction::MIN_RATE_SHIP);
264
				} else {
265
					return FALSE;
266
				}
267
				break;
268
			case Transaction::TYP_COMMANDER :
269
				# 1 experience = x credit
270
				$thisRate = $price / $quantity;
271
				# dilution of 1%
272
				$newRate = (($quantity * $thisRate) + (50000 * (99 * $currentRate)) / 100) / (50000 + $quantity);
273
				return max($newRate, Transaction::MIN_RATE_COMMANDER);
274
				break;
275
			default :
276
				return 0;
277
				break;
278
		}
279
	}
280
281
	public static function calculateRate($transactionType, $quantity, $identifier, $price) {
282
		switch ($transactionType) {
283
			case Transaction::TYP_RESOURCE :
284
				# 1 resource = x credit
285
				return $price / $quantity;
286
				break;
287
			case Transaction::TYP_SHIP :
288
				# 1 resource = x credit
289
				if (ShipResource::isAShip($identifier)) {
290
					$resourceQuantity = ShipResource::getInfo($identifier, 'resourcePrice') * $quantity;
291
					return $price / $resourceQuantity;
292
				} else {
293
					return FALSE;
294
				}
295
				break;
296
			case Transaction::TYP_COMMANDER :
297
				# 1 experience = x credit
298
				return $price / $quantity;
299
				break;
300
			default :
301
				return FALSE;
302
				break;
303
		}
304
	}
305
306
	public static function getMinPriceRelativeToRate($transactionType, $quantity, $identifier = FALSE) {
307
		switch ($transactionType) {
308
			case Transaction::TYP_RESOURCE:
309
				$minRate = Transaction::MIN_RATE_RESOURCE;
310
				break;
311
			case Transaction::TYP_SHIP:
312
				$minRate = Transaction::MIN_RATE_SHIP;
313
				$quantity = ShipResource::getInfo($identifier, 'resourcePrice') * $quantity;
314
				break;
315
			case Transaction::TYP_COMMANDER:
316
				$minRate = Transaction::MIN_RATE_COMMANDER;
317
				break;
318
			default:
319
				return FALSE;
320
		}
321
322
		$price = round($quantity * $minRate);
323
		if ($price < 1) {
324
			$price = 1;
325
		}
326
		return $price;
327
	}
328
329
	public static function getMaxPriceRelativeToRate($transactionType, $quantity, $identifier = FALSE) {
330
		switch ($transactionType) {
331
			case Transaction::TYP_RESOURCE:
332
				$minRate = Transaction::MAX_RATE_RESOURCE;
333
				break;
334
			case Transaction::TYP_SHIP:
335
				$minRate = Transaction::MAX_RATE_SHIP;
336
				$quantity = ShipResource::getInfo($identifier, 'resourcePrice') * $quantity;
337
				break;
338
			case Transaction::TYP_COMMANDER:
339
				$minRate = Transaction::MAX_RATE_COMMANDER;
340
				break;
341
			default:
342
				return FALSE;
343
		}
344
345
		$price = $quantity * $minRate;
346
		return round($price);
347
	}
348
349
	public static function getSpySuccess($antiSpy, $priceInvested) {
350
		# spy success must be between 0 and 100
351
		$antiSpy = $antiSpy == 0 ? 1 : $antiSpy;
352
		$ratio   = $priceInvested / $antiSpy;
353
		$percent = round($ratio * 33);
354
		# ça veut dire qu'il payer 3x plus que ce que le gars investi pour tout voir
355
		if ($percent > 100) {
356
			$percent = 100;
357
		}
358
		return $percent;
359
	}
360
361
	public static function getTypeOfSpy($success, $antiSpy) {
362
		if ($antiSpy < 1000) {
363
			return SpyReport::TYP_NOT_CAUGHT;
364
		}
365
366
		$percent = rand(0, 100);
367
		if ($success < 40) {
368
			if ($percent < 5) {
369
				return SpyReport::TYP_NOT_CAUGHT;			# 5%
370
			} elseif ($percent < 50) {
371
				return SpyReport::TYP_ANONYMOUSLY_CAUGHT;	# 45%
372
			} else {
373
				return SpyReport::TYP_CAUGHT;				# 50%
374
			}
375
		} else if ($success < 80) {
376
			if ($percent < 30) {
377
				return SpyReport::TYP_NOT_CAUGHT;			# 30%
378
			} elseif ($percent < 60) {
379
				return SpyReport::TYP_ANONYMOUSLY_CAUGHT;	# 30%
380
			} else {
381
				return SpyReport::TYP_CAUGHT;				# 40%
382
			}
383
		} else if ($success < 100) {
384
			if ($percent < 50) {
385
				return SpyReport::TYP_NOT_CAUGHT;			# 50%
386
			} elseif ($percent < 80) {
387
				return SpyReport::TYP_ANONYMOUSLY_CAUGHT;	# 30%
388
			} else {
389
				return SpyReport::TYP_CAUGHT;				# 20%
390
			}
391
		} else { # success == 100
392
			if ($percent < 70) {
393
				return SpyReport::TYP_NOT_CAUGHT;			# 70%
394
			} elseif ($percent < 90) {
395
				return SpyReport::TYP_ANONYMOUSLY_CAUGHT;	# 20%
396
			} else {
397
				return SpyReport::TYP_CAUGHT;				# 10%
398
			}
399
		}
400
	}
401
402
	public static function getImprovementFromScientificCoef($coef) {
403
		# transform scientific coefficient of a place
404
		# into improvement coefficient for the technosphere
405
		if ($coef < 10) {
406
			return 0;
407
		} elseif ($coef >= 100) {
408
			return 40;
409
		} else {
410
			return ceil(0.004 * $coef * $coef - 0.01 * $coef + 0.7);
411
		}
412
	}
413
414
	public static function getFleetCost($ships, $affected = TRUE) {
415
		$cost = 0;
416
		for ($i = 0; $i < ShipResource::SHIP_QUANTITY; $i++) {
417
			$cost += ShipResource::getInfo($i, 'cost') * $ships[$i];
418
		}
419
		if (!$affected) {
420
			$cost *= ShipResource::COST_REDUCTION;
421
		}
422
		return ceil($cost);
423
	}
424
}
425