smrealms /
smr
We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | use Smr\Database; |
||
| 4 | use Smr\DatabaseRecord; |
||
| 5 | use Smr\Epoch; |
||
| 6 | use Smr\Exceptions\AllianceInvitationNotFound; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Object interfacing with the alliance_invites_player table. |
||
| 10 | */ |
||
| 11 | class SmrInvitation { |
||
| 12 | |||
| 13 | private readonly int $allianceID; |
||
| 14 | private readonly int $gameID; |
||
| 15 | private readonly int $receiverAccountID; |
||
| 16 | private readonly int $senderAccountID; |
||
| 17 | private readonly int $messageID; |
||
| 18 | private readonly int $expires; |
||
| 19 | |||
| 20 | public static function send(int $allianceID, int $gameID, int $receiverAccountID, int $senderAccountID, int $messageID, int $expires): void { |
||
| 21 | $db = Database::getInstance(); |
||
| 22 | $db->insert('alliance_invites_player', [ |
||
| 23 | 'game_id' => $db->escapeNumber($gameID), |
||
| 24 | 'account_id' => $db->escapeNumber($receiverAccountID), |
||
| 25 | 'alliance_id' => $db->escapeNumber($allianceID), |
||
| 26 | 'invited_by_id' => $db->escapeNumber($senderAccountID), |
||
| 27 | 'expires' => $db->escapeNumber($expires), |
||
| 28 | 'message_id' => $db->escapeNumber($messageID), |
||
| 29 | ]); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Get all unexpired invitations for the given alliance |
||
| 34 | * |
||
| 35 | * @return array<self> |
||
| 36 | */ |
||
| 37 | public static function getAll(int $allianceID, int $gameID): array { |
||
| 38 | // Remove any expired invitations |
||
| 39 | $db = Database::getInstance(); |
||
| 40 | $db->write('DELETE FROM alliance_invites_player WHERE expires < ' . $db->escapeNumber(Epoch::time())); |
||
| 41 | |||
| 42 | $dbResult = $db->read('SELECT * FROM alliance_invites_player WHERE alliance_id=' . $db->escapeNumber($allianceID) . ' AND game_id=' . $db->escapeNumber($gameID)); |
||
| 43 | $invites = []; |
||
| 44 | foreach ($dbResult->records() as $dbRecord) { |
||
| 45 | $invites[] = new self($dbRecord); |
||
| 46 | } |
||
| 47 | return $invites; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the alliance invitation for a single recipient, if not expired |
||
| 52 | */ |
||
| 53 | public static function get(int $allianceID, int $gameID, int $receiverAccountID): self { |
||
| 54 | // Remove any expired invitations |
||
| 55 | $db = Database::getInstance(); |
||
| 56 | $db->write('DELETE FROM alliance_invites_player WHERE expires < ' . $db->escapeNumber(Epoch::time())); |
||
| 57 | |||
| 58 | $dbResult = $db->read('SELECT * FROM alliance_invites_player WHERE alliance_id=' . $db->escapeNumber($allianceID) . ' AND game_id=' . $db->escapeNumber($gameID) . ' AND account_id=' . $db->escapeNumber($receiverAccountID)); |
||
| 59 | if ($dbResult->hasRecord()) { |
||
| 60 | return new self($dbResult->record()); |
||
| 61 | } |
||
| 62 | throw new AllianceInvitationNotFound(); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function __construct(DatabaseRecord $dbRecord) { |
||
| 66 | $this->allianceID = $dbRecord->getInt('alliance_id'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 67 | $this->gameID = $dbRecord->getInt('game_id'); |
||
|
0 ignored issues
–
show
|
|||
| 68 | $this->receiverAccountID = $dbRecord->getInt('account_id'); |
||
|
0 ignored issues
–
show
|
|||
| 69 | $this->senderAccountID = $dbRecord->getInt('invited_by_id'); |
||
|
0 ignored issues
–
show
|
|||
| 70 | $this->messageID = $dbRecord->getInt('message_id'); |
||
|
0 ignored issues
–
show
|
|||
| 71 | $this->expires = $dbRecord->getInt('expires'); |
||
|
0 ignored issues
–
show
|
|||
| 72 | } |
||
| 73 | |||
| 74 | public function delete(): void { |
||
| 75 | $db = Database::getInstance(); |
||
| 76 | $db->write('DELETE FROM alliance_invites_player WHERE alliance_id=' . $db->escapeNumber($this->allianceID) . ' AND game_id=' . $db->escapeNumber($this->gameID) . ' AND account_id=' . $db->escapeNumber($this->receiverAccountID)); |
||
| 77 | $db->write('DELETE FROM message WHERE message_id=' . $db->escapeNumber($this->messageID)); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function getSender(): AbstractSmrPlayer { |
||
| 81 | return SmrPlayer::getPlayer($this->senderAccountID, $this->gameID); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function getReceiver(): AbstractSmrPlayer { |
||
| 85 | return SmrPlayer::getPlayer($this->receiverAccountID, $this->gameID); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function getExpires(): int { |
||
| 89 | return $this->expires; |
||
| 90 | } |
||
| 91 | |||
| 92 | } |
||
| 93 |