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

Completed
Push — master ( b09cbf...9d6a0e )
by Dan
19s queued 15s
created

SmrAlliance::setBank()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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