Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — master ( 287493...da8ddf )
by Dan
52s queued 13s
created

SmrForce::killForcesByPlayer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 3
c 4
b 2
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
class SmrForce {
4
	protected static $CACHE_FORCES = array();
5
	protected static $CACHE_SECTOR_FORCES = array();
6
	protected static $TIDIED_UP = array();
7
8
	const LOWEST_MAX_EXPIRE_SCOUTS_ONLY = 432000; // 5 days
9
	const TIME_PER_SCOUT_ONLY = 86400; // 1 = 1 day
10
	const TIME_PERCENT_PER_SCOUT = 0.02; // 1/50th
11
	const TIME_PERCENT_PER_COMBAT = 0.02; // 1/50th
12
	const TIME_PERCENT_PER_MINE = 0.02; // 1/50th
13
	const REFRESH_ALL_TIME_PER_STACK = 1; // 1 second
14
15
	protected static $refreshAllHREF;
16
17
	protected $db;
18
	protected $SQL;
19
20
	protected $ownerID;
21
	protected $sectorID;
22
	protected $gameID;
23
	protected $combatDrones=0;
24
	protected $scoutDrones=0;
25
	protected $mines=0;
26
	protected $expire=0;
27
28
	protected $isNew;
29
	protected $hasChanged = false;
30
31
	public static function refreshCache() {
32
		foreach(self::$CACHE_FORCES as $gameID => &$gameForces) {
33
			foreach($gameForces as $sectorID => &$gameSectorForces) {
34
				foreach($gameSectorForces as $ownerID => &$forces) {
35
					$forces = self::getForce($gameID,$sectorID,$ownerID,true);
36
				}
37
			}
38
		}
39
	}
40
41
	public static function clearCache() {
42
		self::$CACHE_FORCES = array();
43
		self::$CACHE_SECTOR_FORCES = array();
44
	}
45
46
	public static function saveForces() {
47
		foreach(self::$CACHE_FORCES as $gameForces) {
48
			foreach($gameForces as $gameSectorForces) {
49
				foreach($gameSectorForces as $forces) {
50
					$forces->update();
51
				}
52
			}
53
		}
54
	}
55
56
	public static function &getSectorForces($gameID,$sectorID,$forceUpdate = false) {
57
		if($forceUpdate || !isset(self::$CACHE_SECTOR_FORCES[$gameID][$sectorID])) {
58
			self::tidyUpForces(SmrGalaxy::getGalaxyContaining($gameID,$sectorID));
59
			$db = new SmrMySqlDatabase();
60
			$db->query('SELECT * FROM sector_has_forces WHERE sector_id = ' . $db->escapeNumber($sectorID) . ' AND game_id=' . $db->escapeNumber($gameID) . ' ORDER BY expire_time ASC');
61
			$forces = array();
62
			while($db->nextRecord()) {
63
				$ownerID = $db->getInt('owner_id');
64
				$forces[$ownerID] = self::getForce($gameID, $sectorID, $ownerID, $forceUpdate, $db);
65
			}
66
			self::$CACHE_SECTOR_FORCES[$gameID][$sectorID] = $forces;
67
		}
68
		return self::$CACHE_SECTOR_FORCES[$gameID][$sectorID];
69
	}
70
71
	public static function &getForce($gameID, $sectorID, $ownerID, $forceUpdate=false, $db=null) {
72
		if($forceUpdate || !isset(self::$CACHE_FORCES[$gameID][$sectorID][$ownerID])) {
73
			self::tidyUpForces(SmrGalaxy::getGalaxyContaining($gameID,$sectorID));
74
			$p = new SmrForce($gameID, $sectorID, $ownerID, $db);
75
			self::$CACHE_FORCES[$gameID][$sectorID][$ownerID] = $p;
76
		}
77
		return self::$CACHE_FORCES[$gameID][$sectorID][$ownerID];
78
	}
79
80
	public static function tidyUpForces(SmrGalaxy $galaxyToTidy) {
81
		if(!isset(self::$TIDIED_UP[$galaxyToTidy->getGameID()][$galaxyToTidy->getGalaxyID()])) {
82
			self::$TIDIED_UP[$galaxyToTidy->getGameID()][$galaxyToTidy->getGalaxyID()] = true;
83
			$db = new SmrMySqlDatabase();
84
			$db->query('UPDATE sector_has_forces
85
						SET refresher=0,
86
							expire_time = (refresh_at + if(combat_drones+mines=0,
87
															LEAST('.$db->escapeNumber(self::LOWEST_MAX_EXPIRE_SCOUTS_ONLY).', scout_drones*'.$db->escapeNumber(self::TIME_PER_SCOUT_ONLY).'),
88
															LEAST('.$db->escapeNumber($galaxyToTidy->getMaxForceTime()).', (combat_drones*'.$db->escapeNumber(self::TIME_PERCENT_PER_COMBAT).'+scout_drones*'.$db->escapeNumber(self::TIME_PERCENT_PER_SCOUT).'+mines*'.$db->escapeNumber(self::TIME_PERCENT_PER_MINE).')*'.$db->escapeNumber($galaxyToTidy->getMaxForceTime()).')
89
														))
90
						WHERE game_id = '.$db->escapeNumber($galaxyToTidy->getGameID()).' AND sector_id >= '.$db->escapeNumber($galaxyToTidy->getStartSector()).' AND sector_id <= '.$db->escapeNumber($galaxyToTidy->getEndSector()).' AND refresher != 0 AND refresh_at <= '.$db->escapeNumber(TIME));
91
			$db->query('DELETE FROM sector_has_forces WHERE expire_time < ' . $db->escapeNumber(TIME));
92
		}
93
	}
94
95
	protected function __construct($gameID, $sectorID, $ownerID, $db=null) {
96
		$this->db = new SmrMySqlDatabase();
97
		$this->SQL = 'game_id = '.$this->db->escapeNumber($gameID).'
98
		              AND sector_id = '.$this->db->escapeNumber($sectorID).'
99
		              AND owner_id = '.$this->db->escapeNumber($ownerID);
100
101
		if (isset($db)) {
102
			$this->isNew = false;
103
		} else {
104
			$db = $this->db;
105
			$this->db->query('SELECT * FROM sector_has_forces WHERE ' . $this->SQL);
106
			$this->isNew = !$db->nextRecord();
107
		}
108
109
		$this->gameID = $gameID;
110
		$this->ownerID = $ownerID;
111
		$this->sectorID = $sectorID;
112
		if (!$this->isNew) {
113
			$this->combatDrones = $db->getInt('combat_drones');
114
			$this->scoutDrones = $db->getInt('scout_drones');
115
			$this->mines = $db->getInt('mines');
116
			$this->expire = $db->getInt('expire_time');
117
		}
118
	}
119
120
	public function exists() {
121
		return ($this->hasCDs()||$this->hasSDs()||$this->hasMines())&&!$this->hasExpired();
122
	}
123
124
	public function canAcceptCDs() {
125
		return $this->getCDs()<50;
126
	}
127
128
	public function canAcceptSDs() {
129
		return $this->getSDs()<5;
130
	}
131
132
	public function canAcceptMines() {
133
		return $this->getMines()<50;
134
	}
135
136
	public function hasCDs() {
137
		return $this->getCDs()>0;
138
	}
139
140
	public function hasSDs() {
141
		return $this->getSDs()>0;
142
	}
143
144
	public function hasMines() {
145
		return $this->getMines()>0;
146
	}
147
148
	public function getCDs() {
149
		return $this->combatDrones;
150
	}
151
152
	public function getSDs() {
153
		return $this->scoutDrones;
154
	}
155
156
	public function getMines() {
157
		return $this->mines;
158
	}
159
160
	public function addMines($amount) {
161
		if($amount<0) {
162
			throw new Exception('Cannot add negative mines.');
163
		}
164
		$this->setMines($this->getMines() + $amount);
165
	}
166
167
	public function addCDs($amount) {
168
		if($amount<0) {
169
			throw new Exception('Cannot add negative CDs.');
170
		}
171
		$this->setCDs($this->getCDs() + $amount);
172
	}
173
174
	public function addSDs($amount) {
175
		if($amount<0) {
176
			throw new Exception('Cannot add negative SDs.');
177
		}
178
		$this->setSDs($this->getSDs() + $amount);
179
	}
180
181
	public function takeMines($amount) {
182
		if($amount<0) {
183
			throw new Exception('Cannot take negative mines.');
184
		}
185
		$this->setMines($this->getMines() - $amount);
186
	}
187
188
	public function takeCDs($amount) {
189
		if($amount<0) {
190
			throw new Exception('Cannot take negative CDs.');
191
		}
192
		$this->setCDs($this->getCDs() - $amount);
193
	}
194
195
	public function takeSDs($amount) {
196
		if($amount<0) {
197
			throw new Exception('Cannot take negative SDs.');
198
		}
199
		$this->setSDs($this->getSDs() - $amount);
200
	}
201
202
	public function setMines($amount) {
203
		if($amount<0) {
204
			throw new Exception('Cannot set negative mines.');
205
		}
206
		if($amount==$this->getMines()) {
207
			return;
208
		}
209
		$this->hasChanged = true;
210
		$this->mines = $amount;
211
	}
212
213
	public function setCDs($amount) {
214
		if($amount<0) {
215
			throw new Exception('Cannot set negative CDs.');
216
		}
217
		if($amount==$this->getCDs()) {
218
			return;
219
		}
220
		$this->hasChanged = true;
221
		$this->combatDrones = $amount;
222
	}
223
224
	public function setSDs($amount) {
225
		if($amount<0) {
226
			throw new Exception('Cannot set negative SDs.');
227
		}
228
		if($amount==$this->getSDs()) {
229
			return;
230
		}
231
		$this->hasChanged = true;
232
		$this->scoutDrones = $amount;
233
	}
234
235
	public function hasExpired() {
236
		return $this->expire<TIME;
237
	}
238
239
	public function getExpire() {
240
		return $this->expire;
241
	}
242
243
	public function setExpire($time) {
244
		if($time<0) {
245
			throw new Exception('Cannot set negative expiry.');
246
		}
247
		if($time==$this->getExpire()) {
248
			return;
249
		}
250
		if($time > TIME + $this->getMaxExpireTime()) {
251
			$time = TIME + $this->getMaxExpireTime();
252
		}
253
		$this->hasChanged = true;
254
		$this->expire = $time;
255
		if(!$this->isNew)
256
			$this->update();
257
	}
258
259
	public function updateExpire() {
260
		// Changed (26/10/05) - scout drones count * 2
261
		if($this->getCDs() == 0 && $this->getMines() == 0 && $this->getSDs() > 0) {
262
			$time = self::TIME_PER_SCOUT_ONLY*$this->getSDs();
263
		}
264
		else {
265
			$time = ($this->getCDs()*self::TIME_PERCENT_PER_COMBAT + $this->getSDs()*self::TIME_PERCENT_PER_SCOUT + $this->getMines()*self::TIME_PERCENT_PER_MINE) * $this->getMaxGalaxyExpireTime();
266
		}
267
		$this->setExpire(TIME + $time);
268
	}
269
270
	public function getMaxExpireTime() {
271
		if($this->hasCDs()||$this->hasMines()) {
272
			return $this->getMaxGalaxyExpireTime();
273
		}
274
		if(!$this->hasCDs() && !$this->hasMines() && $this->hasSDs()) {
275
			return max(self::LOWEST_MAX_EXPIRE_SCOUTS_ONLY,$this->getMaxGalaxyExpireTime());
276
		}
277
		return 0;
278
	}
279
280
	public function getMaxGalaxyExpireTime() {
281
		return $this->getGalaxy()->getMaxForceTime();
282
	}
283
284
	public function getBumpTurnCost(AbstractSmrShip $ship) {
285
		$mines = $this->getMines();
286
		if($mines <= 1) {
287
			return 0;
288
		}
289
		if($mines < 10) {
290
			$turns = 1;
291
		}
292
		else if($mines < 25) {
293
			$turns = 2;
294
		}
295
		else {
296
			$turns = 3;
297
		}
298
		if($ship->isFederal() || $ship->hasDCS()) {
299
			$turns -= 1;
300
		}
301
		return $turns;
302
	}
303
304
	public function getAttackTurnCost(AbstractSmrShip $ship) {
305
		if($ship->isFederal() || $ship->hasDCS()) {
306
			return 2;
307
		}
308
		return 3;
309
	}
310
311
	public function getOwnerID() {
312
		return $this->ownerID;
313
	}
314
315
	public function getGameID() {
316
		return $this->gameID;
317
	}
318
319
	public function &getSector() {
320
		return SmrSector::getSector($this->getGameID(),$this->getSectorID());
321
	}
322
323
	public function getSectorID() {
324
		return $this->sectorID;
325
	}
326
327
	public function ping($pingMessage, AbstractSmrPlayer $playerPinging, $skipCheck=false) {
328
		if(!$this->hasSDs() && !$skipCheck) {
329
			return;
330
		}
331
		$owner = $this->getOwner();
332
		if(!$playerPinging->sameAlliance($owner)) {
333
			$playerPinging->sendMessage($owner->getAccountID(), MSG_SCOUT, $pingMessage,false);
0 ignored issues
show
Bug introduced by
The method sendMessage() does not exist on AbstractSmrPlayer. It seems like you code against a sub-type of AbstractSmrPlayer such as SmrPlayer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

333
			$playerPinging->/** @scrutinizer ignore-call */ 
334
                   sendMessage($owner->getAccountID(), MSG_SCOUT, $pingMessage,false);
Loading history...
334
		}
335
	}
336
337
	function &getGalaxy() {
338
		return SmrGalaxy::getGalaxyContaining($this->getGameID(),$this->getSectorID());
339
	}
340
341
	function &getOwner() {
342
		return SmrPlayer::getPlayer($this->getOwnerID(),$this->getGameID());
343
	}
344
345
	public function update() {
346
		if(!$this->isNew) {
347
			if (!$this->exists()) {
348
				$this->db->query('DELETE FROM sector_has_forces WHERE ' . $this->SQL);
349
				$this->isNew=true;
350
			}
351
			else if ($this->hasChanged) {
352
				$this->db->query('UPDATE sector_has_forces SET combat_drones = '.$this->db->escapeNumber($this->combatDrones).', scout_drones = ' . $this->db->escapeNumber($this->scoutDrones) . ', mines = ' . $this->db->escapeNumber($this->mines) . ', expire_time = ' . $this->db->escapeNumber($this->expire) . ' WHERE ' . $this->SQL);
353
			}
354
		}
355
		else if($this->exists()) {
356
			$this->db->query('INSERT INTO sector_has_forces (game_id, sector_id, owner_id, combat_drones, scout_drones, mines, expire_time)
357
								VALUES('.$this->db->escapeNumber($this->gameID).', '.$this->db->escapeNumber($this->sectorID).', '.$this->db->escapeNumber($this->ownerID).', '.$this->db->escapeNumber($this->combatDrones).', '.$this->db->escapeNumber($this->scoutDrones).', '.$this->db->escapeNumber($this->mines).', '.$this->db->escapeNumber($this->expire).')');
358
			$this->isNew=false;
359
		}
360
		// This instance is now in sync with the database
361
		$this->hasChanged = false;
362
	}
363
364
	/**
365
	 * Update the table fields associated with using Refresh All
366
	 */
367
	public function updateRefreshAll(SmrPlayer $player, $refreshTime) {
368
		$this->db->query('UPDATE sector_has_forces SET refresh_at=' . $this->db->escapeNumber($refreshTime) . ', refresher=' . $this->db->escapeNumber($player->getAccountID()) . ' WHERE ' . $this->SQL);
369
	}
370
371
	public function getExamineDropForcesHREF() {
372
		$container = create_container('skeleton.php', 'forces_drop.php');
373
		$container['owner_id']	= $this->getOwnerID();
374
		return SmrSession::getNewHREF($container);
375
	}
376
377
	public function getAttackForcesHREF() {
378
		$container = create_container('forces_attack_processing.php');
379
		$container['action'] = 'attack';
380
		$container['owner_id']	= $this->getOwnerID();
381
		return SmrSession::getNewHREF($container);
382
	}
383
384
	public function getRefreshHREF() {
385
		$container = create_container('forces_refresh_processing.php');
386
		$container['owner_id']	= $this->getOwnerID();
387
		return SmrSession::getNewHREF($container);
388
	}
389
390
	protected function getDropContainer() {
391
		return array('url' => 'forces_drop_processing.php', 'owner_id' => $this->getOwnerID());
392
	}
393
394
	public function getDropSDHREF() {
395
		$container = $this->getDropContainer();
396
		$container['drop_scout_drones']	= 1;
397
		return SmrSession::getNewHREF($container);
398
	}
399
400
	public function getTakeSDHREF() {
401
		$container = $this->getDropContainer();
402
		$container['take_scout_drones']	= 1;
403
		return SmrSession::getNewHREF($container);
404
	}
405
406
	public function getDropCDHREF() {
407
		$container = $this->getDropContainer();
408
		$container['drop_combat_drones']	= 1;
409
		return SmrSession::getNewHREF($container);
410
	}
411
412
	public function getTakeCDHREF() {
413
		$container = $this->getDropContainer();
414
		$container['take_combat_drones']	= 1;
415
		return SmrSession::getNewHREF($container);
416
	}
417
418
	public function getDropMineHREF() {
419
		$container = $this->getDropContainer();
420
		$container['drop_mines']	= 1;
421
		return SmrSession::getNewHREF($container);
422
	}
423
424
	public function getTakeMineHREF() {
425
		$container = $this->getDropContainer();
426
		$container['take_mines']	= 1;
427
		return SmrSession::getNewHREF($container);
428
	}
429
430
	public static function getRefreshAllHREF() {
431
		$container = array();
432
		$container['url']		= 'forces_mass_refresh.php';
433
		return SmrSession::getNewHREF($container);
434
	}
435
436
	public function &shootPlayer(AbstractSmrPlayer $targetPlayer,$minesAreAttacker) {
437
		return $this->shootPlayers(array($targetPlayer),$minesAreAttacker);
438
	}
439
440
	public function &shootPlayers(array $targetPlayers,$minesAreAttacker) {
441
		$results = array('TotalDamage' => 0);
442
		if(!$this->exists()) {
443
			$results['DeadBeforeShot'] = true;
444
			return $results;
445
		}
446
		$results['DeadBeforeShot'] = false;
447
448
		if($this->hasMines()) {
449
			$thisMines = new SmrMines($this->getGameID(),$this->getMines());
450
			$results['Results']['Mines'] =& $thisMines->shootPlayerAsForce($this, $targetPlayers[array_rand($targetPlayers)],$minesAreAttacker);
451
			$results['TotalDamage'] += $results['Results']['Mines']['ActualDamage']['TotalDamage'];
452
		}
453
454
		if($this->hasCDs()) {
455
			$thisCDs = new SmrCombatDrones($this->getGameID(),$this->getCDs());
456
			$results['Results']['Drones'] =& $thisCDs->shootPlayerAsForce($this, $targetPlayers[array_rand($targetPlayers)]);
457
			$results['TotalDamage'] += $results['Results']['Drones']['ActualDamage']['TotalDamage'];
458
		}
459
460
		if(!$minesAreAttacker) {
461
			if($this->hasSDs()) {
462
				$thisSDs = new SmrScoutDrones($this->getGameID(),$this->getSDs());
463
				$results['Results']['Scouts'] =& $thisSDs->shootPlayerAsForce($this, $targetPlayers[array_rand($targetPlayers)]);
464
				$results['TotalDamage'] += $results['Results']['Scouts']['ActualDamage']['TotalDamage'];
465
			}
466
		}
467
468
		$results['ForcesDestroyed'] = !$this->exists();
469
		return $results;
470
	}
471
472
	public function &doWeaponDamage(array $damage) {
473
		$alreadyDead = !$this->exists();
474
		$minesDamage = 0;
475
		$cdDamage = 0;
476
		$sdDamage = 0;
477
		if(!$alreadyDead) {
478
			$minesDamage = $this->doMinesDamage(min($damage['MaxDamage'],$damage['Armour']));
479
			$damage['Armour'] -= $minesDamage;
480
			$damage['MaxDamage'] -= $minesDamage;
481
			if(!$this->hasMines() && ($minesDamage == 0 || $damage['Rollover'])) {
482
				$cdDamage = $this->doCDDamage(min($damage['MaxDamage'],$damage['Armour']));
483
				$damage['Armour'] -= $cdDamage;
484
				$damage['MaxDamage'] -= $cdDamage;
485
				if(!$this->hasCDs() && ($cdDamage == 0 || $damage['Rollover'])) {
486
					$sdDamage = $this->doSDDamage(min($damage['MaxDamage'],$damage['Armour']));
487
				}
488
			}
489
		}
490
		$return = array(
491
						'KillingShot' => !$alreadyDead && !$this->exists(),
492
						'TargetAlreadyDead' => $alreadyDead,
493
						'Mines' => $minesDamage,
494
						'NumMines' => $minesDamage/MINE_ARMOUR,
495
						'HasMines' => $this->hasMines(),
496
						'CDs' => $cdDamage,
497
						'NumCDs' => $cdDamage/CD_ARMOUR,
498
						'HasCDs' => $this->hasCDs(),
499
						'SDs' => $sdDamage,
500
						'NumSDs' => $sdDamage/SD_ARMOUR,
501
						'HasSDs' => $this->hasSDs(),
502
						'TotalDamage' => $minesDamage+$cdDamage+$sdDamage
503
		);
504
		return $return;
505
	}
506
507
	protected function doMinesDamage($damage) {
508
		$actualDamage = min($this->getMines(),floor($damage/MINE_ARMOUR));
509
		$this->takeMines($actualDamage);
510
		return $actualDamage*MINE_ARMOUR;
511
	}
512
513
	protected function doCDDamage($damage) {
514
		$actualDamage = min($this->getCDs(),floor($damage/CD_ARMOUR));
515
		$this->takeCDs($actualDamage);
516
		return $actualDamage*CD_ARMOUR;
517
	}
518
519
	protected function doSDDamage($damage) {
520
		$actualDamage = min($this->getSDs(),floor($damage/SD_ARMOUR));
521
		$this->takeSDs($actualDamage);
522
		return $actualDamage*SD_ARMOUR;
523
	}
524
525
	public function &killForcesByPlayer(AbstractSmrPlayer $killer) {
0 ignored issues
show
Unused Code introduced by
The parameter $killer is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

525
	public function &killForcesByPlayer(/** @scrutinizer ignore-unused */ AbstractSmrPlayer $killer) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
526
		$return = array();
527
		return $return;
528
	}
529
}
530