1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Smr\Pages\Admin; |
4
|
|
|
|
5
|
|
|
use DummyShip; |
6
|
|
|
use Smr\Page\AccountPageProcessor; |
7
|
|
|
use Smr\Request; |
8
|
|
|
use SmrAccount; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @param array<int, \AbstractSmrPlayer> $realAttackers |
12
|
|
|
* @param array<int, \AbstractSmrPlayer> $realDefenders |
13
|
|
|
* @return array<string, mixed> |
14
|
|
|
*/ |
15
|
|
|
function runAnAttack(array $realAttackers, array $realDefenders): array { |
16
|
|
|
$results = [ |
17
|
|
|
'Attackers' => ['Traders' => [], 'TotalDamage' => 0], |
18
|
|
|
'Defenders' => ['Traders' => [], 'TotalDamage' => 0], |
19
|
|
|
]; |
20
|
|
|
foreach ($realAttackers as $teamPlayer) { |
21
|
|
|
$playerResults = $teamPlayer->shootPlayers($realDefenders); |
22
|
|
|
$results['Attackers']['Traders'][] = $playerResults; |
23
|
|
|
$results['Attackers']['TotalDamage'] += $playerResults['TotalDamage']; |
24
|
|
|
} |
25
|
|
|
foreach ($realDefenders as $teamPlayer) { |
26
|
|
|
$playerResults = $teamPlayer->shootPlayers($realAttackers); |
27
|
|
|
$results['Defenders']['Traders'][] = $playerResults; |
28
|
|
|
$results['Defenders']['TotalDamage'] += $playerResults['TotalDamage']; |
29
|
|
|
} |
30
|
|
|
return $results; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
class CombatSimulatorProcessor extends AccountPageProcessor { |
34
|
|
|
|
35
|
|
|
public function build(SmrAccount $account): never { |
36
|
|
|
$usedNames = []; |
37
|
|
|
|
38
|
|
|
$i = 1; |
39
|
|
|
$attackers = []; |
40
|
|
|
if (Request::has('attackers')) { |
41
|
|
|
foreach (Request::getArray('attackers') as $attackerName) { |
42
|
|
|
if ($attackerName == 'none') { |
43
|
|
|
continue; |
44
|
|
|
} |
45
|
|
|
if (isset($usedNames[$attackerName])) { |
46
|
|
|
create_error('Duplicate name used: ' . $attackerName); |
47
|
|
|
} |
48
|
|
|
$usedNames[$attackerName] = true; |
49
|
|
|
$attackers[$i] = DummyShip::getCachedDummyShip($attackerName)->getPlayer(); |
50
|
|
|
++$i; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$i = 1; |
55
|
|
|
$defenders = []; |
56
|
|
|
foreach (Request::getArray('defenders') as $defenderName) { |
57
|
|
|
if ($defenderName == 'none') { |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
if (isset($usedNames[$defenderName])) { |
61
|
|
|
create_error('Duplicate name used: ' . $defenderName); |
62
|
|
|
} |
63
|
|
|
$usedNames[$defenderName] = true; |
64
|
|
|
$defenders[$i] = DummyShip::getCachedDummyShip($defenderName)->getPlayer(); |
65
|
|
|
++$i; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (Request::has('repair')) { |
69
|
|
|
foreach ([...$attackers, ...$defenders] as $player) { |
70
|
|
|
$player->setDead(false); |
71
|
|
|
$player->getShip()->setHardwareToMax(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$results = null; |
76
|
|
|
if (Request::has('run') || Request::has('death_run')) { |
77
|
|
|
if (Request::has('death_run')) { |
78
|
|
|
$maxRounds = 100; |
79
|
|
|
} else { |
80
|
|
|
$maxRounds = 1; |
81
|
|
|
} |
82
|
|
|
$attackersLeft = $attackers; |
83
|
|
|
$defendersLeft = $defenders; |
84
|
|
|
for ($round = 0; $round < $maxRounds; $round++) { |
85
|
|
|
foreach ($attackersLeft as $key => $teamPlayer) { |
86
|
|
|
if ($teamPlayer->isDead()) { |
87
|
|
|
unset($attackersLeft[$key]); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
foreach ($defendersLeft as $key => $teamPlayer) { |
91
|
|
|
if ($teamPlayer->isDead()) { |
92
|
|
|
unset($defendersLeft[$key]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
if (count($attackersLeft) == 0 || count($defendersLeft) == 0) { |
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
$results = runAnAttack($attackersLeft, $defendersLeft); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Save ships unless we're just updating the dummy list |
103
|
|
|
if (!Request::has('update')) { |
104
|
|
|
DummyShip::saveDummyShips(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$container = new CombatSimulator($results, $attackers, $defenders); |
108
|
|
|
$container->go(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|