Complex classes like Player often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Player, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Player extends AvatarModel implements NamedModel, DuplexUrlInterface, EloInterface |
||
18 | { |
||
19 | /** |
||
20 | * These are built-in roles that cannot be deleted via the web interface so we will be storing these values as |
||
21 | * constant variables. Hopefully, a user won't be silly enough to delete them manually from the database. |
||
22 | * |
||
23 | * @TODO Deprecate these and use the Role constants |
||
24 | */ |
||
25 | const DEVELOPER = Role::DEVELOPER; |
||
26 | const ADMIN = Role::ADMINISTRATOR; |
||
27 | const COP = Role::COP; |
||
28 | const REFEREE = Role::REFEREE; |
||
29 | const S_ADMIN = Role::SYSADMIN; |
||
30 | const PLAYER = Role::PLAYER; |
||
31 | const PLAYER_NO_PM = Role::PLAYER_NO_PM; |
||
32 | |||
33 | /** |
||
34 | * The bzid of the player |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $bzid; |
||
38 | |||
39 | /** |
||
40 | * The id of the player's team |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $team; |
||
44 | |||
45 | /** |
||
46 | * The player's status |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $status; |
||
50 | |||
51 | /** |
||
52 | * The player's e-mail address |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $email; |
||
56 | |||
57 | /** |
||
58 | * Whether the player has verified their e-mail address |
||
59 | * @var bool |
||
60 | */ |
||
61 | protected $verified; |
||
62 | |||
63 | /** |
||
64 | * What kind of events the player should be e-mailed about |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $receives; |
||
68 | |||
69 | /** |
||
70 | * A confirmation code for the player's e-mail address verification |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $confirmCode; |
||
74 | |||
75 | /** |
||
76 | * Whether the callsign of the player is outdated |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $outdated; |
||
80 | |||
81 | /** |
||
82 | * The player's profile description |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $description; |
||
86 | |||
87 | /** |
||
88 | * The id of the player's country |
||
89 | * @var int |
||
90 | */ |
||
91 | protected $country; |
||
92 | |||
93 | /** |
||
94 | * The player's timezone PHP identifier, e.g. "Europe/Paris" |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $timezone; |
||
98 | |||
99 | /** |
||
100 | * The date the player joined the site |
||
101 | * @var TimeDate |
||
102 | */ |
||
103 | protected $joined; |
||
104 | |||
105 | /** |
||
106 | * The date of the player's last login |
||
107 | * @var TimeDate |
||
108 | */ |
||
109 | protected $last_login; |
||
110 | |||
111 | /** |
||
112 | * The date of the player's last match |
||
113 | * @var Match |
||
114 | */ |
||
115 | protected $last_match; |
||
116 | |||
117 | /** |
||
118 | * The roles a player belongs to |
||
119 | * @var Role[] |
||
120 | */ |
||
121 | protected $roles; |
||
122 | |||
123 | /** |
||
124 | * The permissions a player has |
||
125 | * @var Permission[] |
||
126 | */ |
||
127 | protected $permissions; |
||
128 | |||
129 | /** |
||
130 | * A section for admins to write notes about players |
||
131 | * @var string |
||
132 | */ |
||
133 | protected $admin_notes; |
||
134 | |||
135 | /** |
||
136 | * The ban of the player, or null if the player is not banned |
||
137 | * @var Ban|null |
||
138 | */ |
||
139 | protected $ban; |
||
140 | |||
141 | /** |
||
142 | * Cached results for match summaries |
||
143 | * |
||
144 | * @var array |
||
145 | */ |
||
146 | private $cachedMatchSummary; |
||
147 | |||
148 | /** |
||
149 | * The cached match count for a player |
||
150 | * |
||
151 | * @var int |
||
152 | */ |
||
153 | private $cachedMatchCount = null; |
||
154 | |||
155 | private $eloSeason; |
||
156 | private $eloSeasonHistory; |
||
157 | |||
158 | private $matchActivity; |
||
159 | |||
160 | /** |
||
161 | * The name of the database table used for queries |
||
162 | */ |
||
163 | const TABLE = "players"; |
||
164 | |||
165 | /** |
||
166 | * The location where avatars will be stored |
||
167 | */ |
||
168 | const AVATAR_LOCATION = "/web/assets/imgs/avatars/players/"; |
||
169 | |||
170 | const EDIT_PERMISSION = Permission::EDIT_USER; |
||
171 | const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_USER; |
||
172 | const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_USER; |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | 70 | protected function assignResult($player) |
|
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | 70 | protected function assignLazyResult($player) |
|
214 | |||
215 | /** |
||
216 | * Add a player a new role |
||
217 | * |
||
218 | * @param Role|int $role_id The role ID to add a player to |
||
219 | * |
||
220 | * @return bool Whether the operation was successful or not |
||
221 | */ |
||
222 | 70 | public function addRole($role_id) |
|
242 | |||
243 | /** |
||
244 | * Get the notes admins have left about a player |
||
245 | * @return string The notes |
||
246 | */ |
||
247 | public function getAdminNotes() |
||
253 | |||
254 | /** |
||
255 | * Get the player's BZID |
||
256 | * @return int The BZID |
||
257 | */ |
||
258 | 22 | public function getBZID() |
|
262 | |||
263 | /** |
||
264 | * Get the country a player belongs to |
||
265 | * |
||
266 | * @return Country The country belongs to |
||
267 | */ |
||
268 | 1 | public function getCountry() |
|
272 | |||
273 | /** |
||
274 | * Get the e-mail address of the player |
||
275 | * |
||
276 | * @return string The address |
||
277 | */ |
||
278 | public function getEmailAddress() |
||
284 | |||
285 | /** |
||
286 | * Build a key that we'll use for caching season Elo data in this model |
||
287 | * |
||
288 | * @param string|null $season The season to get |
||
289 | * @param int|null $year The year of the season to get |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | 27 | private function buildSeasonCacheKey(&$season, &$year) |
|
305 | |||
306 | /** |
||
307 | * Get the Elo changes for a player for a given season |
||
308 | * |
||
309 | * @param string|null $season The season to get |
||
310 | * @param int|null $year The year of the season to get |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | 27 | public function getEloSeasonHistory($season = null, $year = null) |
|
350 | |||
351 | /** |
||
352 | * Get the player's Elo for a season. |
||
353 | * |
||
354 | * With the default arguments, it will fetch the Elo for the current season. |
||
355 | * |
||
356 | * @param string|null $season The season we're looking for: winter, spring, summer, or fall |
||
357 | * @param int|null $year The year of the season we're looking for |
||
358 | * |
||
359 | * @return int The player's Elo |
||
360 | */ |
||
361 | 27 | public function getElo($season = null, $year = null) |
|
362 | { |
||
363 | 27 | $this->getEloSeasonHistory($season, $year); |
|
364 | 27 | $seasonKey = $this->buildSeasonCacheKey($season, $year); |
|
365 | |||
366 | 27 | return $this->eloSeason[$seasonKey]; |
|
367 | } |
||
368 | |||
369 | /** |
||
370 | * Adjust the Elo of a player for the current season based on a Match |
||
371 | * |
||
372 | * **Warning:** If $match is null, the Elo for the player will be modified but the value will not be persisted to |
||
373 | * the database. |
||
374 | * |
||
375 | * @param int $adjust The value to be added to the current ELO (negative to subtract) |
||
376 | * @param Match|null $match The match where this Elo change took place |
||
377 | */ |
||
378 | 27 | public function adjustElo($adjust, Match $match = null) |
|
379 | { |
||
380 | 27 | $timestamp = ($match !== null) ? $match->getTimestamp() : (Carbon::now()); |
|
381 | 27 | $seasonInfo = Season::getSeason($timestamp); |
|
382 | |||
383 | // Get the current Elo for the player, even if it's the default 1200. We need the value for adjusting |
||
384 | 27 | $elo = $this->getElo($seasonInfo['season'], $seasonInfo['year']); |
|
385 | 27 | $seasonKey = sprintf('%s-%s', $seasonInfo['year'], $seasonInfo['season']); |
|
386 | |||
387 | 27 | $this->eloSeason[$seasonKey] += $adjust; |
|
388 | |||
389 | 27 | if ($match !== null && $this->isValid()) { |
|
390 | 26 | $this->db->execute(' |
|
391 | INSERT INTO player_elo VALUES (?, ?, ?, ?, ?, ?) |
||
392 | 26 | ', [ $this->getId(), $match->getId(), $seasonInfo['season'], $seasonInfo['year'], $elo, $this->eloSeason[$seasonKey] ]); |
|
393 | 26 | } |
|
394 | 27 | } |
|
395 | |||
396 | /** |
||
397 | * Returns whether the player has verified their e-mail address |
||
398 | * |
||
399 | * @return bool `true` for verified players |
||
400 | */ |
||
401 | public function isVerified() |
||
402 | { |
||
403 | $this->lazyLoad(); |
||
404 | |||
405 | return $this->verified; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Returns the confirmation code for the player's e-mail address verification |
||
410 | * |
||
411 | * @return string The player's confirmation code |
||
412 | */ |
||
413 | public function getConfirmCode() |
||
414 | { |
||
415 | $this->lazyLoad(); |
||
416 | |||
417 | return $this->confirmCode; |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Returns what kind of events the player should be e-mailed about |
||
422 | * |
||
423 | * @return string The type of notifications |
||
424 | */ |
||
425 | public function getReceives() |
||
426 | { |
||
427 | $this->lazyLoad(); |
||
428 | |||
429 | return $this->receives; |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Finds out whether the specified player wants and can receive an e-mail |
||
434 | * message |
||
435 | * |
||
436 | * @param string $type |
||
437 | * @return bool `true` if the player should be sent an e-mail |
||
438 | */ |
||
439 | 1 | public function canReceive($type) |
|
440 | { |
||
441 | 1 | $this->lazyLoad(); |
|
442 | |||
443 | 1 | if (!$this->email || !$this->isVerified()) { |
|
444 | // Unverified e-mail means the user will receive nothing |
||
445 | 1 | return false; |
|
446 | } |
||
447 | |||
448 | if ($this->receives == 'everything') { |
||
449 | return true; |
||
450 | } |
||
451 | |||
452 | return $this->receives == $type; |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Find out whether the specified confirmation code is correct |
||
457 | * |
||
458 | * This method protects against timing attacks |
||
459 | * |
||
460 | * @param string $code The confirmation code to check |
||
461 | * @return bool `true` for a correct e-mail verification code |
||
462 | */ |
||
463 | public function isCorrectConfirmCode($code) |
||
464 | { |
||
465 | $this->lazyLoad(); |
||
466 | |||
467 | if ($this->confirmCode === null) { |
||
468 | return false; |
||
469 | } |
||
470 | |||
471 | return StringUtils::equals($code, $this->confirmCode); |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Get the player's sanitized description |
||
476 | * @return string The description |
||
477 | */ |
||
478 | public function getDescription() |
||
479 | { |
||
480 | $this->lazyLoad(); |
||
481 | |||
482 | return $this->description; |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Get the joined date of the player |
||
487 | * @return TimeDate The joined date of the player |
||
488 | */ |
||
489 | public function getJoinedDate() |
||
490 | { |
||
491 | $this->lazyLoad(); |
||
492 | |||
493 | return $this->joined->copy(); |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Get all of the known IPs used by the player |
||
498 | * |
||
499 | * @return string[][] An array containing IPs and hosts |
||
500 | */ |
||
501 | public function getKnownIPs() |
||
502 | { |
||
503 | return $this->db->query( |
||
504 | 'SELECT DISTINCT ip, host FROM visits WHERE player = ? GROUP BY ip, host ORDER BY MAX(timestamp) DESC LIMIT 10', |
||
505 | array($this->getId()) |
||
506 | ); |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Get the last login for a player |
||
511 | * @return TimeDate The date of the last login |
||
512 | */ |
||
513 | public function getLastLogin() |
||
514 | { |
||
515 | $this->lazyLoad(); |
||
516 | |||
517 | return $this->last_login->copy(); |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Get the last match |
||
522 | * @return Match |
||
523 | */ |
||
524 | public function getLastMatch() |
||
525 | { |
||
526 | $this->lazyLoad(); |
||
527 | |||
528 | return $this->last_match; |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Get all of the callsigns a player has used to log in to the website |
||
533 | * @return string[] An array containing all of the past callsigns recorded for a player |
||
534 | */ |
||
535 | public function getPastCallsigns() |
||
536 | { |
||
537 | return self::fetchIds("WHERE player = ?", array($this->id), "past_callsigns", "username"); |
||
538 | } |
||
539 | |||
540 | /** |
||
541 | * Get the player's team |
||
542 | * @return Team The object representing the team |
||
543 | */ |
||
544 | 23 | public function getTeam() |
|
545 | { |
||
546 | 23 | return Team::get($this->team); |
|
547 | } |
||
548 | |||
549 | /** |
||
550 | * Get the player's timezone PHP identifier (example: "Europe/Paris") |
||
551 | * @return string The timezone |
||
552 | */ |
||
553 | 1 | public function getTimezone() |
|
554 | { |
||
555 | 1 | $this->lazyLoad(); |
|
556 | |||
557 | 1 | return ($this->timezone) ?: date_default_timezone_get(); |
|
558 | } |
||
559 | |||
560 | /** |
||
561 | * Get the roles of the player |
||
562 | * @return Role[] |
||
563 | */ |
||
564 | public function getRoles() |
||
565 | { |
||
566 | $this->lazyLoad(); |
||
567 | |||
568 | return $this->roles; |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Rebuild the list of permissions a user has been granted |
||
573 | */ |
||
574 | 70 | private function updateUserPermissions() |
|
575 | { |
||
576 | 70 | $this->roles = Role::getRoles($this->id); |
|
|
|||
577 | 70 | $this->permissions = array(); |
|
578 | |||
579 | 70 | foreach ($this->roles as $role) { |
|
580 | 70 | $this->permissions = array_merge($this->permissions, $role->getPerms()); |
|
581 | 70 | } |
|
582 | 70 | } |
|
583 | |||
584 | /** |
||
585 | * Check if a player has a specific permission |
||
586 | * |
||
587 | * @param string|null $permission The permission to check for |
||
588 | * |
||
589 | * @return bool Whether or not the player has the permission |
||
590 | */ |
||
591 | 2 | public function hasPermission($permission) |
|
592 | { |
||
593 | 2 | if ($permission === null) { |
|
594 | 1 | return false; |
|
595 | } |
||
596 | |||
597 | 2 | $this->lazyLoad(); |
|
598 | |||
599 | 2 | return isset($this->permissions[$permission]); |
|
600 | } |
||
601 | |||
602 | /** |
||
603 | * Check whether or not a player been in a match or has logged on in the specified amount of time to be considered |
||
604 | * active |
||
605 | * |
||
606 | * @return bool True if the player has been active |
||
607 | */ |
||
608 | public function hasBeenActive() |
||
609 | { |
||
610 | $this->lazyLoad(); |
||
611 | |||
612 | $interval = Service::getParameter('bzion.miscellaneous.active_interval'); |
||
613 | $lastLogin = $this->last_login->copy()->modify($interval); |
||
614 | |||
615 | $hasBeenActive = (TimeDate::now() <= $lastLogin); |
||
616 | |||
617 | if ($this->last_match->isValid()) { |
||
618 | $lastMatch = $this->last_match->getTimestamp()->copy()->modify($interval); |
||
619 | $hasBeenActive = ($hasBeenActive || TimeDate::now() <= $lastMatch); |
||
620 | } |
||
621 | |||
622 | return $hasBeenActive; |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Check whether the callsign of the player is outdated |
||
627 | * |
||
628 | * Returns true if this player has probably changed their callsign, making |
||
629 | * the current username stored in the database obsolete |
||
630 | * |
||
631 | * @return bool Whether or not the player is disabled |
||
632 | */ |
||
633 | public function isOutdated() |
||
634 | { |
||
635 | $this->lazyLoad(); |
||
636 | |||
637 | return $this->outdated; |
||
638 | } |
||
639 | |||
640 | /** |
||
641 | * Check if a player's account has been disabled |
||
642 | * |
||
643 | * @return bool Whether or not the player is disabled |
||
644 | */ |
||
645 | public function isDisabled() |
||
646 | { |
||
647 | return $this->status == "disabled"; |
||
648 | } |
||
649 | |||
650 | /** |
||
651 | * Check if everyone can log in as this user on a test environment |
||
652 | * |
||
653 | * @return bool |
||
654 | */ |
||
655 | 1 | public function isTestUser() |
|
656 | { |
||
657 | 1 | return $this->status == "test"; |
|
658 | } |
||
659 | |||
660 | /** |
||
661 | * Check if a player is teamless |
||
662 | * |
||
663 | * @return bool True if the player is teamless |
||
664 | */ |
||
665 | 49 | public function isTeamless() |
|
666 | { |
||
667 | 49 | return empty($this->team); |
|
668 | } |
||
669 | |||
670 | /** |
||
671 | * Mark a player's account as banned |
||
672 | */ |
||
673 | 1 | public function markAsBanned() |
|
674 | { |
||
675 | 1 | if ($this->status != 'active') { |
|
676 | return $this; |
||
677 | } |
||
678 | |||
679 | 1 | return $this->updateProperty($this->status, "status", "banned"); |
|
680 | } |
||
681 | |||
682 | /** |
||
683 | * Mark a player's account as unbanned |
||
684 | */ |
||
685 | public function markAsUnbanned() |
||
686 | { |
||
687 | if ($this->status != 'banned') { |
||
688 | return $this; |
||
689 | } |
||
690 | |||
691 | return $this->updateProperty($this->status, "status", "active"); |
||
692 | } |
||
693 | |||
694 | /** |
||
695 | * Find out if a player is banned |
||
696 | * |
||
697 | * @return bool |
||
698 | */ |
||
699 | 2 | public function isBanned() |
|
700 | { |
||
701 | 2 | return Ban::getBan($this->id) !== null; |
|
702 | } |
||
703 | |||
704 | /** |
||
705 | * Get the ban of the player |
||
706 | * |
||
707 | * This method performs a load of all the lazy parameters of the Player |
||
708 | * |
||
709 | * @return Ban|null The current ban of the player, or null if the player is |
||
710 | * is not banned |
||
711 | */ |
||
712 | public function getBan() |
||
713 | { |
||
714 | $this->lazyLoad(); |
||
715 | |||
716 | return $this->ban; |
||
717 | } |
||
718 | |||
719 | /** |
||
720 | * Remove a player from a role |
||
721 | * |
||
722 | * @param int $role_id The role ID to add or remove |
||
723 | * |
||
724 | * @return bool Whether the operation was successful or not |
||
725 | */ |
||
726 | public function removeRole($role_id) |
||
727 | { |
||
728 | $status = $this->modifyRole($role_id, "remove"); |
||
729 | $this->refresh(); |
||
730 | |||
731 | return $status; |
||
732 | } |
||
733 | |||
734 | /** |
||
735 | * Set the player's email address and reset their verification status |
||
736 | * @param string $email The address |
||
737 | */ |
||
738 | public function setEmailAddress($email) |
||
739 | { |
||
740 | $this->lazyLoad(); |
||
741 | |||
742 | if ($this->email == $email) { |
||
743 | // The e-mail hasn't changed, don't do anything |
||
744 | return; |
||
745 | } |
||
746 | |||
747 | $this->setVerified(false); |
||
748 | $this->generateNewConfirmCode(); |
||
749 | |||
750 | $this->updateProperty($this->email, 'email', $email); |
||
751 | } |
||
752 | |||
753 | /** |
||
754 | * Set whether the player has verified their e-mail address |
||
755 | * |
||
756 | * @param bool $verified Whether the player is verified or not |
||
757 | * @return self |
||
758 | */ |
||
759 | public function setVerified($verified) |
||
760 | { |
||
761 | $this->lazyLoad(); |
||
762 | |||
763 | if ($verified) { |
||
764 | $this->setConfirmCode(null); |
||
765 | } |
||
766 | |||
767 | return $this->updateProperty($this->verified, 'verified', $verified); |
||
768 | } |
||
769 | |||
770 | /** |
||
771 | * Generate a new random confirmation token for e-mail address verification |
||
772 | * |
||
773 | * @return self |
||
774 | */ |
||
775 | public function generateNewConfirmCode() |
||
776 | { |
||
777 | $generator = new SecureRandom(); |
||
778 | $random = $generator->nextBytes(16); |
||
779 | |||
780 | return $this->setConfirmCode(bin2hex($random)); |
||
781 | } |
||
782 | |||
783 | /** |
||
784 | * Set the confirmation token for e-mail address verification |
||
785 | * |
||
786 | * @param string $code The confirmation code |
||
787 | * @return self |
||
788 | */ |
||
789 | private function setConfirmCode($code) |
||
790 | { |
||
791 | $this->lazyLoad(); |
||
792 | |||
793 | return $this->updateProperty($this->confirmCode, 'confirm_code', $code); |
||
794 | } |
||
795 | |||
796 | /** |
||
797 | * Set what kind of events the player should be e-mailed about |
||
798 | * |
||
799 | * @param string $receives The type of notification |
||
800 | * @return self |
||
801 | */ |
||
802 | public function setReceives($receives) |
||
803 | { |
||
804 | $this->lazyLoad(); |
||
805 | |||
806 | return $this->updateProperty($this->receives, 'receives', $receives); |
||
807 | } |
||
808 | |||
809 | /** |
||
810 | * Set whether the callsign of the player is outdated |
||
811 | * |
||
812 | * @param bool $outdated Whether the callsign is outdated |
||
813 | * @return self |
||
814 | */ |
||
815 | 70 | public function setOutdated($outdated) |
|
816 | { |
||
817 | 70 | $this->lazyLoad(); |
|
818 | |||
819 | 70 | return $this->updateProperty($this->outdated, 'outdated', $outdated); |
|
820 | } |
||
821 | |||
822 | /** |
||
823 | * Set the player's description |
||
824 | * @param string $description The description |
||
825 | */ |
||
826 | public function setDescription($description) |
||
827 | { |
||
828 | $this->updateProperty($this->description, "description", $description); |
||
829 | } |
||
830 | |||
831 | /** |
||
832 | * Set the player's timezone |
||
833 | * @param string $timezone The timezone |
||
834 | */ |
||
835 | public function setTimezone($timezone) |
||
836 | { |
||
837 | $this->updateProperty($this->timezone, "timezone", $timezone); |
||
838 | } |
||
839 | |||
840 | /** |
||
841 | * Set the player's team |
||
842 | * @param int $team The team's ID |
||
843 | */ |
||
844 | 49 | public function setTeam($team) |
|
845 | { |
||
846 | 49 | $this->updateProperty($this->team, "team", $team); |
|
847 | 49 | } |
|
848 | |||
849 | /** |
||
850 | * Set the match the player last participated in |
||
851 | * |
||
852 | * @param int $match The match's ID |
||
853 | */ |
||
854 | 38 | public function setLastMatch($match) |
|
855 | { |
||
856 | 38 | $this->updateProperty($this->last_match, 'last_match', $match); |
|
857 | 38 | } |
|
858 | |||
859 | /** |
||
860 | * Set the player's status |
||
861 | * @param string $status The new status |
||
862 | */ |
||
863 | public function setStatus($status) |
||
864 | { |
||
865 | $this->updateProperty($this->status, 'status', $status); |
||
866 | } |
||
867 | |||
868 | /** |
||
869 | * Set the player's admin notes |
||
870 | * @param string $admin_notes The new admin notes |
||
871 | * @return self |
||
872 | */ |
||
873 | public function setAdminNotes($admin_notes) |
||
874 | { |
||
875 | return $this->updateProperty($this->admin_notes, 'admin_notes', $admin_notes); |
||
876 | } |
||
877 | |||
878 | /** |
||
879 | * Set the player's country |
||
880 | * @param int $country The ID of the new country |
||
881 | * @return self |
||
882 | */ |
||
883 | public function setCountry($country) |
||
884 | { |
||
885 | return $this->updateProperty($this->country, 'country', $country); |
||
886 | } |
||
887 | |||
888 | /** |
||
889 | * Updates this player's last login |
||
890 | */ |
||
891 | public function updateLastLogin() |
||
892 | { |
||
893 | $this->update("last_login", TimeDate::now()->toMysql()); |
||
894 | } |
||
895 | |||
896 | /** |
||
897 | * Get the player's username |
||
898 | * @return string The username |
||
899 | */ |
||
900 | 1 | public function getUsername() |
|
901 | { |
||
902 | 1 | return $this->name; |
|
903 | } |
||
904 | |||
905 | /** |
||
906 | * Get the player's username, safe for use in your HTML |
||
907 | * @return string The username |
||
908 | */ |
||
909 | 1 | public function getEscapedUsername() |
|
910 | { |
||
911 | 1 | return $this->getEscapedName(); |
|
912 | } |
||
913 | |||
914 | /** |
||
915 | * Alias for Player::setUsername() |
||
916 | * |
||
917 | * @param string $username The new username |
||
918 | * @return self |
||
919 | */ |
||
920 | public function setName($username) |
||
921 | { |
||
922 | return $this->setUsername($username); |
||
923 | } |
||
924 | |||
925 | /** |
||
926 | * Mark all the unread messages of a player as read |
||
927 | * |
||
928 | * @return void |
||
929 | */ |
||
930 | public function markMessagesAsRead() |
||
931 | { |
||
932 | $this->db->execute( |
||
933 | "UPDATE `player_conversations` SET `read` = 1 WHERE `player` = ? AND `read` = 0", |
||
934 | array($this->id) |
||
935 | ); |
||
936 | } |
||
937 | |||
938 | /** |
||
939 | * Set the roles of a user |
||
940 | * |
||
941 | * @todo Is it worth making this faster? |
||
942 | * @param Role[] $roles The new roles of the user |
||
943 | * @return self |
||
944 | */ |
||
945 | public function setRoles($roles) |
||
946 | { |
||
947 | $this->lazyLoad(); |
||
948 | |||
949 | $oldRoles = Role::mapToIds($this->roles); |
||
950 | $this->roles = $roles; |
||
951 | $roleIds = Role::mapToIds($roles); |
||
952 | |||
953 | $newRoles = array_diff($roleIds, $oldRoles); |
||
954 | $removedRoles = array_diff($oldRoles, $roleIds); |
||
955 | |||
956 | foreach ($newRoles as $role) { |
||
957 | $this->modifyRole($role, 'add'); |
||
958 | } |
||
959 | |||
960 | foreach ($removedRoles as $role) { |
||
961 | $this->modifyRole($role, 'remove'); |
||
962 | } |
||
963 | |||
964 | $this->refresh(); |
||
965 | |||
966 | return $this; |
||
967 | } |
||
968 | |||
969 | /** |
||
970 | * Give or remove a role to/form a player |
||
971 | * |
||
972 | * @param int $role_id The role ID to add or remove |
||
973 | * @param string $action Whether to "add" or "remove" a role for a player |
||
974 | * |
||
975 | * @return bool Whether the operation was successful or not |
||
976 | */ |
||
977 | 70 | private function modifyRole($role_id, $action) |
|
978 | { |
||
979 | 70 | $role = Role::get($role_id); |
|
980 | |||
981 | 70 | if ($role->isValid()) { |
|
982 | 70 | if ($action == "add") { |
|
983 | 70 | $this->db->execute("INSERT INTO player_roles (user_id, role_id) VALUES (?, ?)", array($this->getId(), $role_id)); |
|
984 | 70 | } elseif ($action == "remove") { |
|
985 | $this->db->execute("DELETE FROM player_roles WHERE user_id = ? AND role_id = ?", array($this->getId(), $role_id)); |
||
986 | } else { |
||
987 | throw new Exception("Unrecognized role action"); |
||
988 | } |
||
989 | |||
990 | 70 | return true; |
|
991 | } |
||
992 | |||
993 | return false; |
||
994 | } |
||
995 | |||
996 | /** |
||
997 | * Given a player's BZID, get a player object |
||
998 | * |
||
999 | * @param int $bzid The player's BZID |
||
1000 | * @return Player |
||
1001 | */ |
||
1002 | 23 | public static function getFromBZID($bzid) |
|
1003 | { |
||
1004 | 23 | return self::get(self::fetchIdFrom($bzid, "bzid")); |
|
1005 | } |
||
1006 | |||
1007 | /** |
||
1008 | * Get a single player by their username |
||
1009 | * |
||
1010 | * @param string $username The username to look for |
||
1011 | * @return Player |
||
1012 | */ |
||
1013 | 1 | public static function getFromUsername($username) |
|
1014 | { |
||
1015 | 1 | $player = static::get(self::fetchIdFrom($username, 'username')); |
|
1016 | |||
1017 | 1 | return $player->inject('name', $username); |
|
1018 | } |
||
1019 | |||
1020 | /** |
||
1021 | * Get all the players in the database that have an active status |
||
1022 | * @return Player[] An array of player BZIDs |
||
1023 | */ |
||
1024 | public static function getPlayers() |
||
1025 | { |
||
1026 | return self::arrayIdToModel( |
||
1027 | self::fetchIdsFrom("status", array("active", "test"), false) |
||
1028 | ); |
||
1029 | } |
||
1030 | |||
1031 | /** |
||
1032 | * Show the number of notifications the user hasn't read yet |
||
1033 | * @return int |
||
1034 | */ |
||
1035 | 1 | public function countUnreadNotifications() |
|
1036 | { |
||
1037 | 1 | return Notification::countUnreadNotifications($this->id); |
|
1038 | } |
||
1039 | |||
1040 | /** |
||
1041 | * Count the number of matches a player has participated in |
||
1042 | * @return int |
||
1043 | */ |
||
1044 | public function getMatchCount() |
||
1045 | { |
||
1046 | if ($this->cachedMatchCount === null) { |
||
1047 | $this->cachedMatchCount = Match::getQueryBuilder() |
||
1048 | ->active() |
||
1049 | ->with($this) |
||
1050 | ->count(); |
||
1051 | } |
||
1052 | |||
1053 | return $this->cachedMatchCount; |
||
1054 | } |
||
1055 | |||
1056 | /** |
||
1057 | * Get the (victory/total matches) ratio of the player |
||
1058 | * @return float |
||
1059 | */ |
||
1060 | public function getMatchWinRatio() |
||
1061 | { |
||
1062 | $count = $this->getMatchCount(); |
||
1063 | |||
1064 | if ($count == 0) { |
||
1065 | return 0; |
||
1066 | } |
||
1067 | |||
1068 | $wins = Match::getQueryBuilder() |
||
1069 | ->active() |
||
1070 | ->with($this, 'win') |
||
1071 | ->count(); |
||
1072 | |||
1073 | return $wins / $count; |
||
1074 | } |
||
1075 | |||
1076 | /** |
||
1077 | * Get the (total caps made by team/total matches) ratio of the player |
||
1078 | * @return float |
||
1079 | */ |
||
1080 | public function getMatchAverageCaps() |
||
1081 | { |
||
1082 | $count = $this->getMatchCount(); |
||
1083 | |||
1084 | if ($count == 0) { |
||
1085 | return 0; |
||
1086 | } |
||
1087 | |||
1088 | // Get the sum of team A points if the player was in team A, team B |
||
1089 | // points if the player was in team B, and their average if the player |
||
1090 | // was on both teams for some reason |
||
1091 | $query = $this->db->query( |
||
1092 | "SELECT SUM( |
||
1093 | IF( |
||
1094 | FIND_IN_SET(?, team_a_players) AND FIND_IN_SET(?, team_b_players), |
||
1095 | (team_a_points+team_b_points)/2, |
||
1096 | IF(FIND_IN_SET(?, team_a_players), team_a_points, team_b_points) |
||
1097 | ) |
||
1098 | ) AS sum FROM matches WHERE status='entered' AND (FIND_IN_SET(?, team_a_players) OR FIND_IN_SET(?, team_b_players))", |
||
1099 | array_fill(0, 5, $this->id) |
||
1100 | ); |
||
1101 | |||
1102 | return $query[0]['sum'] / $count; |
||
1103 | } |
||
1104 | |||
1105 | /** |
||
1106 | * Get the match activity in matches per day for a player |
||
1107 | * |
||
1108 | * @return float |
||
1109 | */ |
||
1110 | public function getMatchActivity() |
||
1111 | { |
||
1112 | if ($this->matchActivity !== null) { |
||
1113 | return $this->matchActivity; |
||
1114 | } |
||
1115 | |||
1116 | $activity = 0.0; |
||
1117 | |||
1118 | $matches = Match::getQueryBuilder() |
||
1119 | ->active() |
||
1120 | ->with($this) |
||
1121 | ->where('time')->isAfter(TimeDate::from('45 days ago')) |
||
1122 | ->getModels($fast = true); |
||
1123 | |||
1124 | foreach ($matches as $match) { |
||
1125 | $activity += $match->getActivity(); |
||
1126 | } |
||
1127 | |||
1128 | return $activity; |
||
1129 | } |
||
1130 | |||
1131 | /** |
||
1132 | * Return an array of matches this player participated in per month. |
||
1133 | * |
||
1134 | * ``` |
||
1135 | * ['yyyy-mm'] = <number of matches> |
||
1136 | * ``` |
||
1137 | * |
||
1138 | * @param TimeDate|string $timePeriod |
||
1139 | * |
||
1140 | * @return int[] |
||
1141 | */ |
||
1142 | public function getMatchSummary($timePeriod = '1 year ago') |
||
1143 | { |
||
1144 | $since = ($timePeriod instanceof TimeDate) ? $timePeriod : TimeDate::from($timePeriod); |
||
1145 | |||
1146 | if (!isset($this->cachedMatchSummary[(string)$timePeriod])) { |
||
1147 | $this->cachedMatchSummary[(string)$timePeriod] = Match::getQueryBuilder() |
||
1148 | ->active() |
||
1149 | ->with($this) |
||
1150 | ->where('time')->isAfter($since) |
||
1151 | ->getSummary($since) |
||
1152 | ; |
||
1153 | } |
||
1154 | |||
1155 | return $this->cachedMatchSummary[(string)$timePeriod]; |
||
1156 | } |
||
1157 | |||
1158 | /** |
||
1159 | * Show the number of messages the user hasn't read yet |
||
1160 | * @return int |
||
1161 | */ |
||
1162 | 1 | public function countUnreadMessages() |
|
1163 | { |
||
1164 | 1 | return $this->fetchCount("WHERE `player` = ? AND `read` = 0", |
|
1165 | 1 | $this->id, 'player_conversations' |
|
1166 | 1 | ); |
|
1167 | } |
||
1168 | |||
1169 | /** |
||
1170 | * Routine maintenance for a player when they participate in a match. |
||
1171 | * |
||
1172 | * This function updates the last match played, changes the ELO, and creates a historic ELO change. |
||
1173 | * |
||
1174 | * @param Match $match |
||
1175 | * @param int $eloDiff |
||
1176 | */ |
||
1177 | 38 | public function setMatchParticipation(Match $match, $eloDiff) |
|
1178 | { |
||
1179 | 38 | $this->setLastMatch($match->getId()); |
|
1180 | |||
1181 | 38 | if ($match->isOfficial() && $eloDiff !== null) { |
|
1182 | 26 | $this->adjustElo($eloDiff, $match); |
|
1183 | 26 | } |
|
1184 | 38 | } |
|
1185 | |||
1186 | /** |
||
1187 | * Get all of the members belonging to a team |
||
1188 | * @param int $teamID The ID of the team to fetch the members of |
||
1189 | * @return Player[] An array of Player objects of the team members |
||
1190 | */ |
||
1191 | 2 | public static function getTeamMembers($teamID) |
|
1192 | { |
||
1193 | 2 | return self::arrayIdToModel( |
|
1194 | 2 | self::fetchIds("WHERE team = ?", array($teamID)) |
|
1195 | 2 | ); |
|
1196 | } |
||
1197 | |||
1198 | /** |
||
1199 | * {@inheritdoc} |
||
1200 | */ |
||
1201 | 1 | public static function getActiveStatuses() |
|
1202 | { |
||
1203 | 1 | return array('active', 'reported', 'test'); |
|
1204 | } |
||
1205 | |||
1206 | /** |
||
1207 | * {@inheritdoc} |
||
1208 | */ |
||
1209 | 70 | public static function getEagerColumns($prefix = null) |
|
1210 | { |
||
1211 | $columns = [ |
||
1212 | 70 | 'id', |
|
1213 | 70 | 'bzid', |
|
1214 | 70 | 'team', |
|
1215 | 70 | 'username', |
|
1216 | 70 | 'alias', |
|
1217 | 70 | 'status', |
|
1218 | 70 | 'avatar', |
|
1219 | 70 | 'country', |
|
1220 | 70 | ]; |
|
1221 | |||
1222 | 70 | return self::formatColumns($prefix, $columns); |
|
1223 | } |
||
1224 | |||
1225 | /** |
||
1226 | * {@inheritdoc} |
||
1227 | */ |
||
1228 | 70 | public static function getLazyColumns($prefix = null) |
|
1229 | { |
||
1230 | $columns = [ |
||
1231 | 70 | 'email', |
|
1232 | 70 | 'verified', |
|
1233 | 70 | 'receives', |
|
1234 | 70 | 'confirm_code', |
|
1235 | 70 | 'outdated', |
|
1236 | 70 | 'description', |
|
1237 | 70 | 'timezone', |
|
1238 | 70 | 'joined', |
|
1239 | 70 | 'last_login', |
|
1240 | 70 | 'last_match', |
|
1241 | 70 | 'admin_notes', |
|
1242 | 70 | ]; |
|
1243 | |||
1244 | 70 | return self::formatColumns($prefix, $columns); |
|
1245 | } |
||
1246 | |||
1247 | /** |
||
1248 | * Get a query builder for players |
||
1249 | * @return PlayerQueryBuilder |
||
1250 | */ |
||
1251 | public static function getQueryBuilder() |
||
1252 | { |
||
1253 | return new PlayerQueryBuilder('Player', array( |
||
1254 | 'columns' => array( |
||
1255 | 'name' => 'username', |
||
1256 | 'team' => 'team', |
||
1257 | 'outdated' => 'outdated', |
||
1258 | 'status' => 'status', |
||
1259 | ), |
||
1260 | 'name' => 'name', |
||
1261 | )); |
||
1262 | } |
||
1263 | |||
1264 | /** |
||
1265 | * Enter a new player to the database |
||
1266 | * @param int $bzid The player's bzid |
||
1267 | * @param string $username The player's username |
||
1268 | * @param int $team The player's team |
||
1269 | * @param string $status The player's status |
||
1270 | * @param int $role_id The player's role when they are first created |
||
1271 | * @param string $avatar The player's profile avatar |
||
1272 | * @param string $description The player's profile description |
||
1273 | * @param int $country The player's country |
||
1274 | * @param string $timezone The player's timezone |
||
1275 | * @param string|\TimeDate $joined The date the player joined |
||
1276 | * @param string|\TimeDate $last_login The timestamp of the player's last login |
||
1277 | * @return Player An object representing the player that was just entered |
||
1278 | */ |
||
1279 | 70 | public static function newPlayer($bzid, $username, $team = null, $status = "active", $role_id = self::PLAYER, $avatar = "", $description = "", $country = 1, $timezone = null, $joined = "now", $last_login = "now") |
|
1280 | { |
||
1281 | 70 | $joined = TimeDate::from($joined); |
|
1282 | 70 | $last_login = TimeDate::from($last_login); |
|
1283 | 70 | $timezone = ($timezone) ?: date_default_timezone_get(); |
|
1284 | |||
1285 | 70 | $player = self::create(array( |
|
1286 | 70 | 'bzid' => $bzid, |
|
1287 | 70 | 'team' => $team, |
|
1288 | 70 | 'username' => $username, |
|
1289 | 70 | 'alias' => self::generateAlias($username), |
|
1290 | 70 | 'status' => $status, |
|
1291 | 70 | 'avatar' => $avatar, |
|
1292 | 70 | 'description' => $description, |
|
1293 | 70 | 'country' => $country, |
|
1294 | 70 | 'timezone' => $timezone, |
|
1295 | 70 | 'joined' => $joined->toMysql(), |
|
1296 | 70 | 'last_login' => $last_login->toMysql(), |
|
1297 | 70 | )); |
|
1298 | |||
1299 | 70 | $player->addRole($role_id); |
|
1300 | 70 | $player->getIdenticon($player->getId()); |
|
1301 | 70 | $player->setUsername($username); |
|
1302 | |||
1303 | 70 | return $player; |
|
1304 | } |
||
1305 | |||
1306 | /** |
||
1307 | * Determine if a player exists in the database |
||
1308 | * @param int $bzid The player's bzid |
||
1309 | * @return bool Whether the player exists in the database |
||
1310 | */ |
||
1311 | public static function playerBZIDExists($bzid) |
||
1312 | { |
||
1313 | return self::getFromBZID($bzid)->isValid(); |
||
1314 | } |
||
1315 | |||
1316 | /** |
||
1317 | * Change a player's callsign and add it to the database if it does not |
||
1318 | * exist as a past callsign |
||
1319 | * |
||
1320 | * @param string $username The new username of the player |
||
1321 | * @return self |
||
1322 | */ |
||
1323 | 70 | public function setUsername($username) |
|
1324 | { |
||
1325 | // The player's username was just fetched from BzDB, it's definitely not |
||
1326 | // outdated |
||
1327 | 70 | $this->setOutdated(false); |
|
1328 | |||
1329 | // Players who have this player's username are considered outdated |
||
1330 | 70 | $this->db->execute("UPDATE {$this->table} SET outdated = 1 WHERE username = ? AND id != ?", array($username, $this->id)); |
|
1331 | |||
1332 | 70 | if ($username === $this->name) { |
|
1333 | // The player's username hasn't changed, no need to do anything |
||
1334 | 70 | return $this; |
|
1335 | } |
||
1336 | |||
1337 | // Players who used to have our player's username are not outdated anymore, |
||
1338 | // unless they are more than one. |
||
1339 | // Even though we are sure that the old and new usernames are not equal, |
||
1340 | // MySQL makes a different type of string equality tests, which is why we |
||
1341 | // also check IDs to make sure not to affect our own player's outdatedness. |
||
1342 | $this->db->execute(" |
||
1343 | UPDATE {$this->table} SET outdated = |
||
1344 | (SELECT (COUNT(*)>1) FROM (SELECT 1 FROM {$this->table} WHERE username = ? AND id != ?) t) |
||
1345 | WHERE username = ? AND id != ?", |
||
1346 | array($this->name, $this->id, $this->name, $this->id)); |
||
1347 | |||
1348 | $this->updateProperty($this->name, 'username', $username); |
||
1349 | $this->db->execute("INSERT IGNORE INTO past_callsigns (player, username) VALUES (?, ?)", array($this->id, $username)); |
||
1350 | $this->resetAlias(); |
||
1351 | |||
1352 | return $this; |
||
1353 | } |
||
1354 | |||
1355 | /** |
||
1356 | * Alphabetical order function for use in usort (case-insensitive) |
||
1357 | * @return Closure The sort function |
||
1358 | */ |
||
1359 | public static function getAlphabeticalSort() |
||
1365 | |||
1366 | /** |
||
1367 | * {@inheritdoc} |
||
1368 | * @todo Add a constraint that does this automatically |
||
1369 | */ |
||
1370 | 70 | public function wipe() |
|
1371 | { |
||
1372 | 70 | $this->db->execute("DELETE FROM past_callsigns WHERE player = ?", $this->id); |
|
1373 | |||
1374 | 70 | parent::wipe(); |
|
1375 | 70 | } |
|
1376 | |||
1377 | /** |
||
1378 | * Find whether the player can delete a model |
||
1379 | * |
||
1380 | * @param PermissionModel $model The model that will be seen |
||
1381 | * @param bool $showDeleted Whether to show deleted models to admins |
||
1382 | * @return bool |
||
1383 | */ |
||
1384 | 1 | public function canSee($model, $showDeleted = false) |
|
1385 | { |
||
1386 | 1 | return $model->canBeSeenBy($this, $showDeleted); |
|
1387 | } |
||
1388 | |||
1389 | /** |
||
1390 | * Find whether the player can delete a model |
||
1391 | * |
||
1392 | * @param PermissionModel $model The model that will be deleted |
||
1393 | * @param bool $hard Whether to check for hard-delete perms, as opposed |
||
1394 | * to soft-delete ones |
||
1395 | * @return bool |
||
1396 | */ |
||
1397 | 1 | public function canDelete($model, $hard = false) |
|
1398 | { |
||
1399 | 1 | if ($hard) { |
|
1400 | return $model->canBeHardDeletedBy($this); |
||
1401 | } else { |
||
1402 | 1 | return $model->canBeSoftDeletedBy($this); |
|
1403 | } |
||
1404 | } |
||
1405 | |||
1406 | /** |
||
1407 | * Find whether the player can create a model |
||
1408 | * |
||
1409 | * @param string $modelName The PHP class identifier of the model type |
||
1410 | * @return bool |
||
1411 | */ |
||
1412 | 1 | public function canCreate($modelName) |
|
1413 | { |
||
1414 | 1 | return $modelName::canBeCreatedBy($this); |
|
1416 | |||
1417 | /** |
||
1418 | * Find whether the player can edit a model |
||
1419 | * |
||
1420 | * @param PermissionModel $model The model which will be edited |
||
1421 | * @return bool |
||
1422 | */ |
||
1423 | 1 | public function canEdit($model) |
|
1427 | } |
||
1428 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..