1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
use Smr\Database; |
4
|
|
|
use Smr\DatabaseRecord; |
5
|
|
|
use Smr\Epoch; |
6
|
|
|
use Smr\Exceptions\AllianceNotFound; |
7
|
|
|
use Smr\Exceptions\UserError; |
8
|
|
|
|
9
|
|
|
class SmrAlliance { |
10
|
|
|
|
11
|
|
|
/** @var array<int, array<int, self>> */ |
12
|
|
|
protected static array $CACHE_ALLIANCES = []; |
13
|
|
|
|
14
|
|
|
protected Database $db; |
15
|
|
|
protected readonly string $SQL; |
16
|
|
|
|
17
|
|
|
protected string $allianceName; |
18
|
|
|
protected ?string $description; |
19
|
|
|
protected string $password; |
20
|
|
|
protected bool $recruiting; |
21
|
|
|
protected int $leaderID; |
22
|
|
|
protected int $bank; |
23
|
|
|
protected int $kills; |
24
|
|
|
protected int $deaths; |
25
|
|
|
protected string $motd; |
26
|
|
|
protected string $imgSrc; |
27
|
|
|
protected ?string $discordServer; |
28
|
|
|
protected ?string $discordChannel; |
29
|
|
|
protected string $ircChannel; |
30
|
|
|
protected int $flagshipID; |
31
|
|
|
|
32
|
|
|
/** @var array<int> */ |
33
|
|
|
protected array $memberList; |
34
|
|
|
/** @var array<int> */ |
35
|
|
|
protected array $seedlist; |
36
|
|
|
|
37
|
|
|
// Recruit type constants |
38
|
|
|
public const RECRUIT_OPEN = 'open'; |
39
|
|
|
public const RECRUIT_CLOSED = 'closed'; |
40
|
|
|
public const RECRUIT_PASSWORD = 'password'; |
41
|
|
|
|
42
|
|
|
public static function clearCache(): void { |
43
|
|
|
self::$CACHE_ALLIANCES = []; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function getAlliance(int $allianceID, int $gameID, bool $forceUpdate = false, DatabaseRecord $dbRecord = null): self { |
47
|
|
|
if ($forceUpdate || !isset(self::$CACHE_ALLIANCES[$gameID][$allianceID])) { |
48
|
|
|
self::$CACHE_ALLIANCES[$gameID][$allianceID] = new self($allianceID, $gameID, $dbRecord); |
49
|
|
|
} |
50
|
|
|
return self::$CACHE_ALLIANCES[$gameID][$allianceID]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function getAllianceByDiscordChannel(string $channel, bool $forceUpdate = false): self { |
54
|
|
|
$db = Database::getInstance(); |
55
|
|
|
$dbResult = $db->read('SELECT alliance.* FROM alliance JOIN game USING(game_id) WHERE discord_channel = ' . $db->escapeString($channel) . ' AND game.end_time > ' . $db->escapeNumber(time()) . ' ORDER BY game_id DESC LIMIT 1'); |
56
|
|
|
if ($dbResult->hasRecord()) { |
57
|
|
|
$dbRecord = $dbResult->record(); |
58
|
|
|
return self::getAlliance($dbRecord->getInt('alliance_id'), $dbRecord->getInt('game_id'), $forceUpdate, $dbRecord); |
59
|
|
|
} |
60
|
|
|
throw new AllianceNotFound('Alliance Discord Channel not found'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function getAllianceByIrcChannel(string $channel, bool $forceUpdate = false): self { |
64
|
|
|
$db = Database::getInstance(); |
65
|
|
|
$dbResult = $db->read('SELECT alliance_id, game_id FROM irc_alliance_has_channel WHERE channel = ' . $db->escapeString($channel) . ' LIMIT 1'); |
66
|
|
|
if ($dbResult->hasRecord()) { |
67
|
|
|
$dbRecord = $dbResult->record(); |
68
|
|
|
return self::getAlliance($dbRecord->getInt('alliance_id'), $dbRecord->getInt('game_id'), $forceUpdate); |
69
|
|
|
} |
70
|
|
|
throw new AllianceNotFound('Alliance IRC Channel not found'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function getAllianceByName(string $name, int $gameID, bool $forceUpdate = false): self { |
74
|
|
|
$db = Database::getInstance(); |
75
|
|
|
$dbResult = $db->read('SELECT * FROM alliance WHERE alliance_name = ' . $db->escapeString($name) . ' AND game_id = ' . $db->escapeNumber($gameID)); |
76
|
|
|
if ($dbResult->hasRecord()) { |
77
|
|
|
$dbRecord = $dbResult->record(); |
78
|
|
|
return self::getAlliance($dbRecord->getInt('alliance_id'), $gameID, $forceUpdate, $dbRecord); |
79
|
|
|
} |
80
|
|
|
throw new AllianceNotFound('Alliance name not found'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function __construct( |
84
|
|
|
protected readonly int $allianceID, |
85
|
|
|
protected readonly int $gameID, |
86
|
|
|
DatabaseRecord $dbRecord = null |
87
|
|
|
) { |
88
|
|
|
if ($allianceID != 0) { |
89
|
|
|
$this->db = Database::getInstance(); |
90
|
|
|
$this->SQL = 'alliance_id=' . $this->db->escapeNumber($allianceID) . ' AND game_id=' . $this->db->escapeNumber($gameID); |
|
|
|
|
91
|
|
|
|
92
|
|
|
if ($dbRecord === null) { |
93
|
|
|
$dbResult = $this->db->read('SELECT * FROM alliance WHERE ' . $this->SQL); |
94
|
|
|
if ($dbResult->hasRecord()) { |
95
|
|
|
$dbRecord = $dbResult->record(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
if ($dbRecord === null) { |
99
|
|
|
throw new AllianceNotFound('Invalid allianceID: ' . $allianceID . ' OR gameID: ' . $gameID); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->allianceName = $dbRecord->getString('alliance_name'); |
103
|
|
|
$this->password = $dbRecord->getString('alliance_password'); |
104
|
|
|
$this->recruiting = $dbRecord->getBoolean('recruiting'); |
105
|
|
|
$this->description = $dbRecord->getNullableString('alliance_description'); |
106
|
|
|
$this->leaderID = $dbRecord->getInt('leader_id'); |
107
|
|
|
$this->bank = $dbRecord->getInt('alliance_account'); |
108
|
|
|
$this->kills = $dbRecord->getInt('alliance_kills'); |
109
|
|
|
$this->deaths = $dbRecord->getInt('alliance_deaths'); |
110
|
|
|
$this->motd = $dbRecord->getString('mod'); |
111
|
|
|
$this->imgSrc = $dbRecord->getString('img_src'); |
112
|
|
|
$this->discordServer = $dbRecord->getNullableString('discord_server'); |
113
|
|
|
$this->discordChannel = $dbRecord->getNullableString('discord_channel'); |
114
|
|
|
$this->flagshipID = $dbRecord->getInt('flagship_id'); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Create an alliance and return the new object. |
120
|
|
|
* Starts alliance with "closed" recruitment (for safety). |
121
|
|
|
*/ |
122
|
|
|
public static function createAlliance(int $gameID, string $name, bool $allowNHA = false): self { |
123
|
|
|
$db = Database::getInstance(); |
124
|
|
|
$db->lockTable('alliance'); |
125
|
|
|
|
126
|
|
|
// check if the alliance name already exists |
127
|
|
|
try { |
128
|
|
|
self::getAllianceByName($name, $gameID); |
129
|
|
|
$db->unlock(); |
130
|
|
|
throw new UserError('That alliance name already exists.'); |
131
|
|
|
} catch (AllianceNotFound) { |
132
|
|
|
// alliance with this name does not yet exist |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!$allowNHA && trim($name) === NHA_ALLIANCE_NAME) { |
136
|
|
|
$db->unlock(); |
137
|
|
|
throw new UserError('That alliance name is reserved.'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// get the next alliance id (start at 1 if there are no alliances yet) |
141
|
|
|
$dbResult = $db->read('SELECT IFNULL(max(alliance_id), 0) AS alliance_id FROM alliance WHERE game_id = ' . $db->escapeNumber($gameID)); |
142
|
|
|
$allianceID = $dbResult->record()->getInt('alliance_id') + 1; |
143
|
|
|
|
144
|
|
|
// actually create the alliance here |
145
|
|
|
$db->insert('alliance', [ |
146
|
|
|
'alliance_id' => $db->escapeNumber($allianceID), |
147
|
|
|
'game_id' => $db->escapeNumber($gameID), |
148
|
|
|
'alliance_name' => $db->escapeString($name), |
149
|
|
|
'alliance_password' => $db->escapeString(''), |
150
|
|
|
'recruiting' => $db->escapeBoolean(false), |
151
|
|
|
]); |
152
|
|
|
$db->unlock(); |
153
|
|
|
|
154
|
|
|
return self::getAlliance($allianceID, $gameID); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns true if the alliance ID is associated with allianceless players. |
159
|
|
|
*/ |
160
|
|
|
public function isNone(): bool { |
161
|
|
|
return $this->allianceID == 0; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns true if the alliance is the Newbie Help Alliance. |
166
|
|
|
*/ |
167
|
|
|
public function isNHA(): bool { |
168
|
|
|
return $this->allianceName === NHA_ALLIANCE_NAME; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getAllianceID(): int { |
172
|
|
|
return $this->allianceID; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getAllianceBBLink(): string { |
176
|
|
|
return '[alliance=' . $this->allianceID . ']'; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function getAllianceDisplayName(bool $linked = false, bool $includeAllianceID = false): string { |
180
|
|
|
$name = htmlentities($this->allianceName); |
181
|
|
|
if ($includeAllianceID) { |
182
|
|
|
$name .= ' (' . $this->allianceID . ')'; |
183
|
|
|
} |
184
|
|
|
if ($linked === true && !$this->hasDisbanded()) { |
185
|
|
|
return create_link(Globals::getAllianceRosterHREF($this->getAllianceID()), $name); |
186
|
|
|
} |
187
|
|
|
return $name; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns the alliance name. |
192
|
|
|
* Use getAllianceDisplayName for an HTML-safe version. |
193
|
|
|
*/ |
194
|
|
|
public function getAllianceName(): string { |
195
|
|
|
return $this->allianceName; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function getGameID(): int { |
199
|
|
|
return $this->gameID; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function getGame(): SmrGame { |
203
|
|
|
return SmrGame::getGame($this->gameID); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function hasDisbanded(): bool { |
207
|
|
|
return !$this->hasLeader(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function hasLeader(): bool { |
211
|
|
|
return $this->getLeaderID() != 0; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function getLeaderID(): int { |
215
|
|
|
return $this->leaderID; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function getLeader(): AbstractSmrPlayer { |
219
|
|
|
return SmrPlayer::getPlayer($this->getLeaderID(), $this->getGameID()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function setLeaderID(int $leaderID): void { |
223
|
|
|
$this->leaderID = $leaderID; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function getDiscordServer(): ?string { |
227
|
|
|
return $this->discordServer; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function setDiscordServer(string $serverId): void { |
231
|
|
|
$this->discordServer = $serverId; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function getDiscordChannel(): ?string { |
235
|
|
|
return $this->discordChannel; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function setDiscordChannel(?string $channelId): void { |
239
|
|
|
$this->discordChannel = $channelId; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function getIrcChannel(): string { |
243
|
|
|
if (!isset($this->ircChannel)) { |
244
|
|
|
$dbResult = $this->db->read('SELECT channel FROM irc_alliance_has_channel WHERE ' . $this->SQL); |
245
|
|
|
if ($dbResult->hasRecord()) { |
246
|
|
|
$this->ircChannel = $dbResult->record()->getString('channel'); |
247
|
|
|
} else { |
248
|
|
|
$this->ircChannel = ''; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
return $this->ircChannel; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function setIrcChannel(string $ircChannel): void { |
255
|
|
|
$this->getIrcChannel(); // to populate the class attribute |
256
|
|
|
if ($this->ircChannel == $ircChannel) { |
257
|
|
|
return; |
258
|
|
|
} |
259
|
|
|
if (strlen($ircChannel) > 0 && $ircChannel != '#') { |
260
|
|
|
if ($ircChannel[0] != '#') { |
261
|
|
|
$ircChannel = '#' . $ircChannel; |
262
|
|
|
} |
263
|
|
|
if ($ircChannel == '#smr' || $ircChannel == '#smr-bar') { |
264
|
|
|
throw new UserError('Please enter a valid irc channel for your alliance.'); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
$this->db->replace('irc_alliance_has_channel', [ |
268
|
|
|
'channel' => $this->db->escapeString($ircChannel), |
269
|
|
|
'alliance_id' => $this->db->escapeNumber($this->getAllianceID()), |
270
|
|
|
'game_id' => $this->db->escapeNumber($this->getGameID()), |
271
|
|
|
]); |
272
|
|
|
} else { |
273
|
|
|
$this->db->write('DELETE FROM irc_alliance_has_channel WHERE ' . $this->SQL); |
274
|
|
|
} |
275
|
|
|
$this->ircChannel = $ircChannel; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function hasImageURL(): bool { |
279
|
|
|
return strlen($this->imgSrc) && $this->imgSrc != 'http://'; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function getImageURL(): string { |
283
|
|
|
return $this->imgSrc; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public function setImageURL(string $url): void { |
287
|
|
|
if (preg_match('/"/', $url)) { |
288
|
|
|
throw new Exception('Tried to set an image url with ": ' . $url); |
289
|
|
|
} |
290
|
|
|
$this->imgSrc = htmlspecialchars($url); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get the total credits in the alliance bank account. |
295
|
|
|
*/ |
296
|
|
|
public function getBank(): int { |
297
|
|
|
return $this->bank; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Increases alliance bank account up to the maximum allowed credits. |
302
|
|
|
* Returns the amount that was actually added to handle overflow. |
303
|
|
|
*/ |
304
|
|
|
public function increaseBank(int $credits): int { |
305
|
|
|
$newTotal = min($this->bank + $credits, MAX_MONEY); |
306
|
|
|
$actualAdded = $newTotal - $this->bank; |
307
|
|
|
$this->setBank($newTotal); |
308
|
|
|
return $actualAdded; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function decreaseBank(int $credits): void { |
312
|
|
|
$newTotal = $this->bank - $credits; |
313
|
|
|
$this->setBank($newTotal); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function setBank(int $credits): void { |
317
|
|
|
$this->bank = $credits; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Get (HTML-safe) alliance Message of the Day for display. |
322
|
|
|
*/ |
323
|
|
|
public function getMotD(): string { |
324
|
|
|
return htmlentities($this->motd); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
public function setMotD(string $motd): void { |
328
|
|
|
$this->motd = $motd; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
public function getPassword(): string { |
332
|
|
|
return $this->password; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function isRecruiting(): bool { |
336
|
|
|
return $this->recruiting; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Set the password and recruiting attributes. |
341
|
|
|
* The input $password is ignored except for the "password" $type. |
342
|
|
|
*/ |
343
|
|
|
public function setRecruitType(string $type, string $password): void { |
344
|
|
|
if ($type == self::RECRUIT_CLOSED) { |
345
|
|
|
$this->recruiting = false; |
346
|
|
|
$this->password = ''; |
347
|
|
|
} elseif ($type == self::RECRUIT_OPEN) { |
348
|
|
|
$this->recruiting = true; |
349
|
|
|
$this->password = ''; |
350
|
|
|
} elseif ($type == self::RECRUIT_PASSWORD) { |
351
|
|
|
if (empty($password)) { |
352
|
|
|
throw new Exception('Password must not be empty here'); |
353
|
|
|
} |
354
|
|
|
$this->recruiting = true; |
355
|
|
|
$this->password = $password; |
356
|
|
|
} else { |
357
|
|
|
throw new Exception('Unknown recruit type: ' . $type); |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
public function getRecruitType(): string { |
362
|
|
|
return match (true) { |
363
|
|
|
!$this->isRecruiting() => self::RECRUIT_CLOSED, |
364
|
|
|
empty($this->getPassword()) => self::RECRUIT_OPEN, |
365
|
|
|
default => self::RECRUIT_PASSWORD, |
366
|
|
|
}; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* List of all recruitment types and their descriptions. |
371
|
|
|
* Do not change the order of elements in the list! |
372
|
|
|
* |
373
|
|
|
* @return array<string, string> |
374
|
|
|
*/ |
375
|
|
|
public static function allRecruitTypes(): array { |
376
|
|
|
// The first type is the default option when creating new alliances |
377
|
|
|
return [ |
378
|
|
|
self::RECRUIT_PASSWORD => 'Players can join by password or invitation', |
379
|
|
|
self::RECRUIT_CLOSED => 'Players can join by invitation only', |
380
|
|
|
self::RECRUIT_OPEN => 'Anyone can join (no password needed)', |
381
|
|
|
]; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
public function getKills(): int { |
385
|
|
|
return $this->kills; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public function getDeaths(): int { |
389
|
|
|
return $this->deaths; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Get (HTML-safe) alliance description for display. |
394
|
|
|
*/ |
395
|
|
|
public function getDescription(): string { |
396
|
|
|
if (empty($this->description)) { |
397
|
|
|
return ''; |
398
|
|
|
} |
399
|
|
|
return htmlentities($this->description); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
public function setAllianceDescription(string $description, AbstractSmrPlayer $player = null): void { |
403
|
|
|
$description = word_filter($description); |
404
|
|
|
if ($description == $this->description) { |
405
|
|
|
return; |
406
|
|
|
} |
407
|
|
|
if ($player !== null) { |
408
|
|
|
$boxDescription = 'Alliance ' . $this->getAllianceBBLink() . ' had their description changed to:' . EOL . EOL . $description; |
409
|
|
|
$player->sendMessageToBox(BOX_ALLIANCE_DESCRIPTIONS, $boxDescription); |
410
|
|
|
} |
411
|
|
|
$this->description = $description; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
public function hasFlagship(): bool { |
415
|
|
|
return $this->flagshipID != 0; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Get account ID of the player designated as the alliance flagship. |
420
|
|
|
* Returns 0 if no flagship. |
421
|
|
|
*/ |
422
|
|
|
public function getFlagshipID(): int { |
423
|
|
|
return $this->flagshipID; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Designate a player as the alliance flagship by their account ID. |
428
|
|
|
*/ |
429
|
|
|
public function setFlagshipID(int $accountID): void { |
430
|
|
|
if ($this->flagshipID == $accountID) { |
431
|
|
|
return; |
432
|
|
|
} |
433
|
|
|
$this->flagshipID = $accountID; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
public function getJoinRestriction(AbstractSmrPlayer $player, bool $doAllianceCheck = true, bool $doRecruitingCheck = true): string|false { |
437
|
|
|
if ($player->getGame()->isGameType(SmrGame::GAME_TYPE_DRAFT)) { |
438
|
|
|
return 'Alliance members will be selected by the Draft Leaders.'; |
439
|
|
|
} |
440
|
|
|
if (!$player->getAccount()->isValidated()) { |
441
|
|
|
return 'You cannot join an alliance until you validate your account.'; |
442
|
|
|
} |
443
|
|
|
if ($this->hasDisbanded()) { |
444
|
|
|
return 'This alliance has disbanded!'; |
445
|
|
|
} |
446
|
|
|
if ($doAllianceCheck && $player->hasAlliance()) { |
447
|
|
|
return 'You are already in an alliance!'; |
448
|
|
|
} |
449
|
|
|
if ($doRecruitingCheck && !$this->isRecruiting()) { |
450
|
|
|
return 'This alliance is not currently accepting new recruits.'; |
451
|
|
|
} |
452
|
|
|
if ($player->getAllianceJoinable() > Epoch::time()) { |
453
|
|
|
return 'You cannot join another alliance for ' . format_time($player->getAllianceJoinable() - Epoch::time()) . '.'; |
454
|
|
|
} |
455
|
|
|
if ($this->getNumMembers() < $this->getGame()->getAllianceMaxPlayers()) { |
456
|
|
|
if ($player->hasNewbieStatus()) { |
457
|
|
|
return false; |
458
|
|
|
} |
459
|
|
|
$maxVets = $this->getGame()->getAllianceMaxVets(); |
460
|
|
|
if ($this->getNumMembers() < $maxVets) { |
461
|
|
|
return false; |
462
|
|
|
} |
463
|
|
|
$dbResult = $this->db->read('SELECT status FROM player_joined_alliance WHERE account_id=' . $this->db->escapeNumber($player->getAccountID()) . ' AND ' . $this->SQL); |
464
|
|
|
if ($dbResult->hasRecord()) { |
465
|
|
|
if ($dbResult->record()->getString('status') == 'NEWBIE') { |
466
|
|
|
return false; |
467
|
|
|
} |
468
|
|
|
} |
469
|
|
|
$dbResult = $this->db->read('SELECT COUNT(*) AS num_orig_vets |
470
|
|
|
FROM player_joined_alliance |
471
|
|
|
JOIN player USING (account_id, alliance_id, game_id) |
472
|
|
|
WHERE ' . $this->SQL . ' AND status=\'VETERAN\''); |
473
|
|
|
if (!$dbResult->hasRecord() || $dbResult->record()->getInt('num_orig_vets') < $maxVets) { |
474
|
|
|
return false; |
475
|
|
|
} |
476
|
|
|
} |
477
|
|
|
return 'There is not currently enough room for you in this alliance.'; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
public function getNumVeterans(): int { |
481
|
|
|
$numVeterans = 0; |
482
|
|
|
foreach ($this->getMembers() as $player) { |
483
|
|
|
if (!$player->hasNewbieStatus()) { |
484
|
|
|
$numVeterans++; |
485
|
|
|
} |
486
|
|
|
} |
487
|
|
|
return $numVeterans; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
public function getNumMembers(): int { |
491
|
|
|
return count($this->getMemberIDs()); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
public function update(): void { |
495
|
|
|
$this->db->write('UPDATE alliance SET |
496
|
|
|
alliance_password = ' . $this->db->escapeString($this->password) . ', |
497
|
|
|
recruiting = ' . $this->db->escapeBoolean($this->recruiting) . ', |
498
|
|
|
alliance_account = ' . $this->db->escapeNumber($this->bank) . ', |
499
|
|
|
alliance_description = ' . $this->db->escapeString($this->description, true) . ', |
500
|
|
|
`mod` = ' . $this->db->escapeString($this->motd) . ', |
501
|
|
|
img_src = ' . $this->db->escapeString($this->imgSrc) . ', |
502
|
|
|
alliance_kills = ' . $this->db->escapeNumber($this->kills) . ', |
503
|
|
|
alliance_deaths = ' . $this->db->escapeNumber($this->deaths) . ', |
504
|
|
|
discord_server = ' . $this->db->escapeString($this->discordServer, true) . ', |
505
|
|
|
discord_channel = ' . $this->db->escapeString($this->discordChannel, true) . ', |
506
|
|
|
flagship_id = ' . $this->db->escapeNumber($this->flagshipID) . ', |
507
|
|
|
leader_id = ' . $this->db->escapeNumber($this->leaderID) . ' |
508
|
|
|
WHERE ' . $this->SQL); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Returns the members of this alliance as an array of SmrPlayer objects. |
513
|
|
|
* |
514
|
|
|
* @return array<int, SmrPlayer> |
515
|
|
|
*/ |
516
|
|
|
public function getMembers(): array { |
517
|
|
|
return SmrPlayer::getAlliancePlayers($this->getGameID(), $this->getAllianceID()); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* @return array<int> |
522
|
|
|
*/ |
523
|
|
|
public function getMemberIDs(): array { |
524
|
|
|
if (!isset($this->memberList)) { |
525
|
|
|
$dbResult = $this->db->read('SELECT account_id FROM player WHERE ' . $this->SQL); |
526
|
|
|
|
527
|
|
|
//we have the list of players put them in an array now |
528
|
|
|
$this->memberList = []; |
529
|
|
|
foreach ($dbResult->records() as $dbRecord) { |
530
|
|
|
$this->memberList[] = $dbRecord->getInt('account_id'); |
531
|
|
|
} |
532
|
|
|
} |
533
|
|
|
return $this->memberList; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* @return array<int> |
538
|
|
|
*/ |
539
|
|
|
public function getActiveIDs(): array { |
540
|
|
|
$activeIDs = []; |
541
|
|
|
|
542
|
|
|
$dbResult = $this->db->read('SELECT account_id |
543
|
|
|
FROM active_session |
544
|
|
|
JOIN player USING(account_id, game_id) |
545
|
|
|
WHERE ' . $this->SQL . ' AND last_accessed >= ' . $this->db->escapeNumber(Epoch::time() - TIME_BEFORE_INACTIVE)); |
546
|
|
|
|
547
|
|
|
foreach ($dbResult->records() as $dbRecord) { |
548
|
|
|
$activeIDs[] = $dbRecord->getInt('account_id'); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
return $activeIDs; |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
/** |
555
|
|
|
* Return all planets owned by members of this alliance. |
556
|
|
|
* |
557
|
|
|
* @return array<SmrPlanet> |
558
|
|
|
*/ |
559
|
|
|
public function getPlanets(): array { |
560
|
|
|
$dbResult = $this->db->read('SELECT planet.* |
561
|
|
|
FROM player |
562
|
|
|
JOIN planet ON player.game_id = planet.game_id AND player.account_id = planet.owner_id |
563
|
|
|
WHERE player.game_id=' . $this->db->escapeNumber($this->gameID) . ' |
564
|
|
|
AND player.alliance_id=' . $this->db->escapeNumber($this->allianceID) . ' |
565
|
|
|
ORDER BY planet.sector_id |
566
|
|
|
'); |
567
|
|
|
$planets = []; |
568
|
|
|
foreach ($dbResult->records() as $dbRecord) { |
569
|
|
|
$planets[] = SmrPlanet::getPlanet($this->gameID, $dbRecord->getInt('sector_id'), false, $dbRecord); |
570
|
|
|
} |
571
|
|
|
return $planets; |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* Return array of sector_id for sectors in the alliance seedlist. |
576
|
|
|
* |
577
|
|
|
* @return array<int> |
578
|
|
|
*/ |
579
|
|
|
public function getSeedlist(): array { |
580
|
|
|
if (!isset($this->seedlist)) { |
581
|
|
|
$dbResult = $this->db->read('SELECT sector_id FROM alliance_has_seedlist WHERE ' . $this->SQL); |
582
|
|
|
$this->seedlist = []; |
583
|
|
|
foreach ($dbResult->records() as $dbRecord) { |
584
|
|
|
$this->seedlist[] = $dbRecord->getInt('sector_id'); |
585
|
|
|
} |
586
|
|
|
} |
587
|
|
|
return $this->seedlist; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* Is the given sector in the alliance seedlist? |
592
|
|
|
*/ |
593
|
|
|
public function isInSeedlist(SmrSector $sector): bool { |
594
|
|
|
return in_array($sector->getSectorID(), $this->getSeedlist()); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Create the default roles for this alliance. |
599
|
|
|
* This should only be called once after the alliance is created. |
600
|
|
|
*/ |
601
|
|
|
public function createDefaultRoles(string $newMemberPermission = 'basic'): void { |
602
|
|
|
$db = $this->db; //for convenience |
603
|
|
|
|
604
|
|
|
// Create leader role |
605
|
|
|
$withPerDay = ALLIANCE_BANK_UNLIMITED; |
606
|
|
|
$removeMember = true; |
607
|
|
|
$changePass = true; |
608
|
|
|
$changeMOD = true; |
609
|
|
|
$changeRoles = true; |
610
|
|
|
$planetAccess = true; |
611
|
|
|
$exemptWith = true; |
612
|
|
|
$mbMessages = true; |
613
|
|
|
$sendAllMsg = true; |
614
|
|
|
$opLeader = true; |
615
|
|
|
$viewBonds = true; |
616
|
|
|
$db->insert('alliance_has_roles', [ |
617
|
|
|
'alliance_id' => $db->escapeNumber($this->getAllianceID()), |
618
|
|
|
'game_id' => $db->escapeNumber($this->getGameID()), |
619
|
|
|
'role_id' => $db->escapeNumber(ALLIANCE_ROLE_LEADER), |
620
|
|
|
'role' => $db->escapeString('Leader'), |
621
|
|
|
'with_per_day' => $db->escapeNumber($withPerDay), |
622
|
|
|
'remove_member' => $db->escapeBoolean($removeMember), |
623
|
|
|
'change_pass' => $db->escapeBoolean($changePass), |
624
|
|
|
'change_mod' => $db->escapeBoolean($changeMOD), |
625
|
|
|
'change_roles' => $db->escapeBoolean($changeRoles), |
626
|
|
|
'planet_access' => $db->escapeBoolean($planetAccess), |
627
|
|
|
'exempt_with' => $db->escapeBoolean($exemptWith), |
628
|
|
|
'mb_messages' => $db->escapeBoolean($mbMessages), |
629
|
|
|
'send_alliance_msg' => $db->escapeBoolean($sendAllMsg), |
630
|
|
|
'op_leader' => $db->escapeBoolean($opLeader), |
631
|
|
|
'view_bonds' => $db->escapeBoolean($viewBonds), |
632
|
|
|
]); |
633
|
|
|
|
634
|
|
|
// Create new member role |
635
|
|
|
switch ($newMemberPermission) { |
636
|
|
|
case 'full': |
637
|
|
|
//do nothing, perms already set above. |
638
|
|
|
break; |
639
|
|
|
case 'none': |
640
|
|
|
$withPerDay = 0; |
641
|
|
|
$removeMember = false; |
642
|
|
|
$changePass = false; |
643
|
|
|
$changeMOD = false; |
644
|
|
|
$changeRoles = false; |
645
|
|
|
$planetAccess = false; |
646
|
|
|
$exemptWith = false; |
647
|
|
|
$mbMessages = false; |
648
|
|
|
$sendAllMsg = false; |
649
|
|
|
$opLeader = false; |
650
|
|
|
$viewBonds = false; |
651
|
|
|
break; |
652
|
|
|
case 'basic': |
653
|
|
|
$withPerDay = ALLIANCE_BANK_UNLIMITED; |
654
|
|
|
$removeMember = false; |
655
|
|
|
$changePass = false; |
656
|
|
|
$changeMOD = false; |
657
|
|
|
$changeRoles = false; |
658
|
|
|
$planetAccess = true; |
659
|
|
|
$exemptWith = false; |
660
|
|
|
$mbMessages = false; |
661
|
|
|
$sendAllMsg = false; |
662
|
|
|
$opLeader = false; |
663
|
|
|
$viewBonds = false; |
664
|
|
|
break; |
665
|
|
|
} |
666
|
|
|
$db->insert('alliance_has_roles', [ |
667
|
|
|
'alliance_id' => $db->escapeNumber($this->getAllianceID()), |
668
|
|
|
'game_id' => $db->escapeNumber($this->getGameID()), |
669
|
|
|
'role_id' => $db->escapeNumber(ALLIANCE_ROLE_NEW_MEMBER), |
670
|
|
|
'role' => $db->escapeString('New Member'), |
671
|
|
|
'with_per_day' => $db->escapeNumber($withPerDay), |
672
|
|
|
'remove_member' => $db->escapeBoolean($removeMember), |
673
|
|
|
'change_pass' => $db->escapeBoolean($changePass), |
674
|
|
|
'change_mod' => $db->escapeBoolean($changeMOD), |
675
|
|
|
'change_roles' => $db->escapeBoolean($changeRoles), |
676
|
|
|
'planet_access' => $db->escapeBoolean($planetAccess), |
677
|
|
|
'exempt_with' => $db->escapeBoolean($exemptWith), |
678
|
|
|
'mb_messages' => $db->escapeBoolean($mbMessages), |
679
|
|
|
'send_alliance_msg' => $db->escapeBoolean($sendAllMsg), |
680
|
|
|
'op_leader' => $db->escapeBoolean($opLeader), |
681
|
|
|
'view_bonds' => $db->escapeBoolean($viewBonds), |
682
|
|
|
]); |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
} |
686
|
|
|
|