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