|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Asylamba\Modules\Atlas\Routine; |
|
4
|
|
|
|
|
5
|
|
|
use Asylamba\Modules\Atlas\Model\Ranking; |
|
6
|
|
|
use Asylamba\Modules\Atlas\Model\FactionRanking; |
|
7
|
|
|
use Asylamba\Modules\Demeter\Model\Color; |
|
8
|
|
|
|
|
9
|
|
|
use Asylamba\Classes\Library\Utils; |
|
10
|
|
|
|
|
11
|
|
|
class FactionRoutine extends AbstractRoutine |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Contains results of all alive factions |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $results = []; |
|
19
|
|
|
/** @var bool **/ |
|
20
|
|
|
protected $isGameOver = false; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param Color $faction |
|
24
|
|
|
* @param array $playerRankings |
|
25
|
|
|
* @param array $routesIncome |
|
26
|
|
|
* @param array $sectors |
|
27
|
|
|
*/ |
|
28
|
|
|
public function execute(Color $faction, $playerRankings, $routesIncome, $sectors) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->results[$faction->getId()] = array( |
|
31
|
|
|
'general' => 0, |
|
32
|
|
|
'wealth' => 0, |
|
33
|
|
|
'territorial' => 0, |
|
34
|
|
|
'points' => $faction->rankingPoints); |
|
35
|
|
|
if ($faction->isWinner == 1) { |
|
36
|
|
|
$this->isGameOver = true; |
|
37
|
|
|
} |
|
38
|
|
|
$this->calculateGeneralRanking($playerRankings); |
|
39
|
|
|
$this->calculateWealthRanking($faction, $routesIncome); |
|
40
|
|
|
$this->calculateTerritorialRanking($faction, $sectors); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function processResults(Ranking $ranking, $factions, $factionRankingManager) |
|
44
|
|
|
{ |
|
45
|
|
|
#---------------- COMPUTING -------------------# |
|
46
|
|
|
|
|
47
|
|
|
# copy the arrays |
|
48
|
|
|
$listG = $this->results; |
|
49
|
|
|
$listW = $this->results; |
|
50
|
|
|
$listT = $this->results; |
|
51
|
|
|
|
|
52
|
|
|
# sort all the copies |
|
53
|
|
|
uasort($listG, [$this, 'cmpFactionGeneral']); |
|
54
|
|
|
uasort($listW, [$this, 'cmpWealth']); |
|
55
|
|
|
uasort($listT, [$this, 'cmpTerritorial']); |
|
56
|
|
|
|
|
57
|
|
|
/*foreach ($list as $key => $value) { |
|
58
|
|
|
echo $key . ' => ' . $value['general'] . '<br/>'; |
|
59
|
|
|
}*/ |
|
60
|
|
|
|
|
61
|
|
|
# put the position in each array |
|
62
|
|
|
$listG = $this->setPositions($listG, 'general'); |
|
63
|
|
|
$listW = $this->setPositions($listW, 'wealth'); |
|
64
|
|
|
$listT = $this->setPositions($listT, 'territorial'); |
|
65
|
|
|
|
|
66
|
|
|
#-------------------------------- POINTS RANKING -----------------------------# |
|
67
|
|
|
|
|
68
|
|
|
# faire ce classement uniquement après x jours de jeu |
|
69
|
|
|
if (Utils::interval(SERVER_START_TIME, Utils::now(), 'h') > HOURS_BEFORE_START_OF_RANKING) { |
|
70
|
|
|
# points qu'on gagne en fonction de sa place dans le classement |
|
71
|
|
|
$pointsToEarn = [40, 30, 20, 10, 0, 0, 0, 0, 0, 0, 0]; |
|
72
|
|
|
$coefG = 0.1; # 4 3 2 1 0 ... |
|
73
|
|
|
$coefW = 0.4; # 16 12 8 4 0 ... |
|
74
|
|
|
$coefT = 0.5; # 20 15 10 5 0 ... |
|
75
|
|
|
|
|
76
|
|
|
foreach ($factions as $faction) { |
|
77
|
|
|
$factionId = $faction->id; |
|
78
|
|
|
$additionalPoints = 0; |
|
79
|
|
|
|
|
80
|
|
|
# general |
|
81
|
|
|
$additionalPoints += intval($pointsToEarn[$listG[$factionId]['position'] - 1] * $coefG); |
|
82
|
|
|
|
|
83
|
|
|
# wealth |
|
84
|
|
|
$additionalPoints += intval($pointsToEarn[$listW[$factionId]['position'] - 1] * $coefW); |
|
85
|
|
|
|
|
86
|
|
|
# territorial |
|
87
|
|
|
$additionalPoints += intval($pointsToEarn[$listT[$factionId]['position'] - 1] * $coefT); |
|
88
|
|
|
|
|
89
|
|
|
$this->results[$factionId]['points'] += $additionalPoints; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
#---------------- LAST COMPUTING -------------------# |
|
95
|
|
|
|
|
96
|
|
|
$listP = $this->results; |
|
97
|
|
|
uasort($listP, [$this, 'cmpPoints']); |
|
98
|
|
|
|
|
99
|
|
|
$position = 1; |
|
100
|
|
|
foreach ($listP as $key => $value) { $listP[$key]['position'] = $position++;} |
|
101
|
|
|
|
|
102
|
|
|
#---------------- SAVING -------------------# |
|
103
|
|
|
|
|
104
|
|
|
$rankings = []; |
|
105
|
|
|
|
|
106
|
|
|
foreach ($factions as $faction) { |
|
107
|
|
|
$factionId = $faction->getId(); |
|
108
|
|
|
$fr = new FactionRanking(); |
|
109
|
|
|
$fr->rRanking = $ranking->getId(); |
|
110
|
|
|
$fr->rFaction = $factionId; |
|
111
|
|
|
|
|
112
|
|
|
$firstRanking = true; |
|
113
|
|
|
for ($i = 0; $i < $factionRankingManager->size(); $i++) { |
|
114
|
|
|
if ($factionRankingManager->get($i)->rFaction == $factionId) { |
|
115
|
|
|
$oldRanking = $factionRankingManager->get($i); |
|
116
|
|
|
$firstRanking = false; |
|
117
|
|
|
break; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$fr->general = $listG[$factionId]['general']; |
|
122
|
|
|
$fr->generalPosition = $listG[$factionId]['position']; |
|
123
|
|
|
$fr->generalVariation = $firstRanking ? 0 : $oldRanking->generalPosition - $fr->generalPosition; |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
$fr->wealth = $listW[$factionId]['wealth']; |
|
126
|
|
|
$fr->wealthPosition = $listW[$factionId]['position']; |
|
127
|
|
|
$fr->wealthVariation = $firstRanking ? 0 : $oldRanking->wealthPosition - $fr->wealthPosition; |
|
128
|
|
|
|
|
129
|
|
|
$fr->territorial = $listT[$factionId]['territorial']; |
|
130
|
|
|
$fr->territorialPosition = $listT[$factionId]['position']; |
|
131
|
|
|
$fr->territorialVariation = $firstRanking ? 0 : $oldRanking->territorialPosition - $fr->territorialPosition; |
|
132
|
|
|
|
|
133
|
|
|
if ($this->isGameOver === true) { |
|
134
|
|
|
$fr->points = $oldRanking->points; |
|
135
|
|
|
$fr->pointsPosition = $oldRanking->pointsPosition; |
|
136
|
|
|
$fr->pointsVariation = 0; |
|
137
|
|
|
$fr->newPoints = 0; |
|
138
|
|
|
} else { |
|
139
|
|
|
$fr->points = $listP[$factionId]['points']; |
|
140
|
|
|
$fr->pointsPosition = $listP[$factionId]['position']; |
|
141
|
|
|
$fr->pointsVariation = $firstRanking ? 0 : $oldRanking->pointsPosition - $fr->pointsPosition; |
|
142
|
|
|
$fr->newPoints = $firstRanking ? $fr->points : $fr->points - $oldRanking->points; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
# update faction infos |
|
146
|
|
|
$faction->rankingPoints = $listP[$factionId]['points']; |
|
147
|
|
|
$faction->points = $listG[$factionId]['general']; |
|
148
|
|
|
$faction->sectors = $listT[$factionId]['territorial']; |
|
149
|
|
|
|
|
150
|
|
|
//$this->colorManager->updateInfos($faction); |
|
151
|
|
|
|
|
152
|
|
|
$rankings[] = $fr; |
|
153
|
|
|
$factionRankingManager->add($fr); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
if ($this->isGameOver === false) { |
|
157
|
|
|
# check if a faction wins the game |
|
158
|
|
|
$winRanking = NULL; |
|
159
|
|
|
foreach ($rankings as $ranking) { |
|
160
|
|
|
if ($ranking->points >= POINTS_TO_WIN) { |
|
161
|
|
|
if ($winRanking !== NULL) { |
|
162
|
|
|
if ($winRanking->points < $ranking->points) { |
|
163
|
|
|
return $ranking->rFaction; |
|
164
|
|
|
} |
|
165
|
|
|
} else { |
|
166
|
|
|
return $ranking->rFaction; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
return null; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
protected function calculateGeneralRanking($playerRankings) |
|
175
|
|
|
{ |
|
176
|
|
|
foreach ($playerRankings as $playerRanking) { |
|
177
|
|
|
$player = $playerRanking->getPlayer(); |
|
178
|
|
|
|
|
179
|
|
|
if (isset($player->rColor)) { |
|
180
|
|
|
$this->results[$player->rColor]['general'] += $playerRanking->general; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
protected function calculateWealthRanking(Color $faction, $routesIncome) |
|
186
|
|
|
{ |
|
187
|
|
|
|
|
188
|
|
|
if ($routesIncome['income'] == NULL) { |
|
189
|
|
|
$income = 0; |
|
190
|
|
|
} else { |
|
191
|
|
|
$income = $routesIncome['income']; |
|
192
|
|
|
} |
|
193
|
|
|
$this->results[$faction->getId()]['wealth'] = $income; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
protected function calculateTerritorialRanking($faction, $sectors) |
|
197
|
|
|
{ |
|
198
|
|
|
foreach ($sectors as $sector) { |
|
199
|
|
|
if ($sector->rColor === $faction->getId()) { |
|
200
|
|
|
$this->results[$sector->rColor]['territorial'] += $sector->points; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
protected function cmpFactionGeneral($a, $b) { |
|
206
|
|
|
if ($a['general'] == $b['general']) { |
|
207
|
|
|
return 0; |
|
208
|
|
|
} |
|
209
|
|
|
return ($a['general'] > $b['general']) ? -1 : 1; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
protected function cmpWealth($a, $b) { |
|
213
|
|
|
if ($a['wealth'] == $b['wealth']) { |
|
214
|
|
|
return 0; |
|
215
|
|
|
} |
|
216
|
|
|
return ($a['wealth'] > $b['wealth']) ? -1 : 1; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
protected function cmpTerritorial($a, $b) { |
|
220
|
|
|
if ($a['territorial'] == $b['territorial']) { |
|
221
|
|
|
return 0; |
|
222
|
|
|
} |
|
223
|
|
|
return ($a['territorial'] > $b['territorial']) ? -1 : 1; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
protected function cmpPoints($a, $b) { |
|
227
|
|
|
if ($a['points'] == $b['points']) { |
|
228
|
|
|
return 0; |
|
229
|
|
|
} |
|
230
|
|
|
return ($a['points'] > $b['points']) ? -1 : 1; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
protected function setPositions($list, $attribute) { |
|
234
|
|
|
$position = 1; |
|
235
|
|
|
$index = 1; |
|
236
|
|
|
$previous = PHP_INT_MAX; |
|
237
|
|
|
foreach ($list as $key => $value) { |
|
238
|
|
|
if ($previous > $list[$key][$attribute]) { |
|
239
|
|
|
$position = $index; |
|
240
|
|
|
} |
|
241
|
|
|
$list[$key]['position'] = $position; |
|
242
|
|
|
$index++; |
|
243
|
|
|
$previous = $list[$key][$attribute]; |
|
244
|
|
|
} |
|
245
|
|
|
return $list; |
|
246
|
|
|
} |
|
247
|
|
|
} |
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: