From 3c422adae1d4b03531f1fd973c8149ffdd55bfbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lup=C4=8D=C3=ADk?= Date: Tue, 21 Jan 2025 11:51:56 +0100 Subject: [PATCH] Add player's awards and achievements. --- src/Models/Player.php | 44 +++++++ src/Models/PlayerAchievement.php | 112 ++++++++++++++++++ src/Models/PlayerAward.php | 94 +++++++++++++++ tests/Unit/Models/PlayerAchievementTest.php | 62 ++++++++++ tests/Unit/Models/PlayerAwardTest.php | 54 +++++++++ tests/Unit/Models/PlayerTest.php | 22 ++++ .../Models/fixtures/player.achievement.json | 7 ++ tests/Unit/Models/fixtures/player.award.json | 6 + tests/Unit/Models/fixtures/player.json | 24 ++++ tests/Unit/Requests/fixtures/player.json | 24 ++++ 10 files changed, 449 insertions(+) create mode 100644 src/Models/PlayerAchievement.php create mode 100644 src/Models/PlayerAward.php create mode 100644 tests/Unit/Models/PlayerAchievementTest.php create mode 100644 tests/Unit/Models/PlayerAwardTest.php create mode 100644 tests/Unit/Models/fixtures/player.achievement.json create mode 100644 tests/Unit/Models/fixtures/player.award.json diff --git a/src/Models/Player.php b/src/Models/Player.php index 5814d9d..1676505 100644 --- a/src/Models/Player.php +++ b/src/Models/Player.php @@ -183,6 +183,20 @@ class Player extends Model */ protected Collection $companyHistory; + /** + * The player's achievements sorted from the oldest. + * + * @var Collection + */ + protected Collection $achievements; + + /** + * The player's awards sorted from the oldest. + * + * @var Collection + */ + protected Collection $awards; + /** * Create a new Player instance. * @@ -216,6 +230,12 @@ public function __construct(Client $client, array $player) $history = new Collection($this->getValue('vtcHistory', [])); $this->companyHistory = $history->map(fn (array $entry) => new PlayerCompanyHistory($client, $entry)); + $achievements = new Collection($this->getValue('achievements', [])); + $this->achievements = $achievements->map(fn (array $entry) => new PlayerAchievement($client, $entry)); + + $awards = new Collection($this->getValue('awards', [])); + $this->awards = $awards->map(fn (array $entry) => new PlayerAward($client, $entry)); + $this->isStaff = $this->getValue('permissions.isStaff', false); $this->isUpperStaff = $this->getValue('permissions.isUpperStaff', false); $this->inGameAdmin = $this->getValue('permissions.isGameAdmin', false); @@ -480,6 +500,30 @@ public function getDiscordSnowflake(): ?string return $this->discordSnowflake; } + /** + * Get the player's achievements sorted from the oldest. + * + * Returns an empty collection if they are private. + * + * @return Collection + */ + public function getAchievements(): Collection + { + return $this->achievements; + } + + /** + * Get the player's awards sorted from the oldest. + * + * Returns an empty collection if they are private. + * + * @return Collection + */ + public function getAwards(): Collection + { + return $this->awards; + } + /** * Get the player's bans. * diff --git a/src/Models/PlayerAchievement.php b/src/Models/PlayerAchievement.php new file mode 100644 index 0000000..1546b1f --- /dev/null +++ b/src/Models/PlayerAchievement.php @@ -0,0 +1,112 @@ +id = $this->getValue('id'); + $this->title = $this->getValue('title'); + $this->description = $this->getValue('description'); + $this->imageUrl = $this->getValue('image_url'); + $this->achievedAt = new Carbon($this->getValue('achieved_at'), 'UTC'); + } + + /** + * Get the ID of the achievement. + * + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * Get the title of the achievement. + * + * @return string + */ + public function getTitle(): string + { + return $this->title; + } + + /** + * Get the description of the achievement. + * + * @return string + */ + public function getDescription(): string + { + return $this->description; + } + + /** + * Get the link to the achievement image. + * + * @return string + */ + public function getImageUrl(): string + { + return $this->imageUrl; + } + + /** + * Get the date and time the user was given the achievement (UTC). + * + * @return Carbon + */ + public function getAchievedAt(): Carbon + { + return $this->achievedAt; + } +} diff --git a/src/Models/PlayerAward.php b/src/Models/PlayerAward.php new file mode 100644 index 0000000..d554876 --- /dev/null +++ b/src/Models/PlayerAward.php @@ -0,0 +1,94 @@ +id = $this->getValue('id'); + $this->name = $this->getValue('name'); + $this->imageUrl = $this->getValue('image_url'); + $this->awardedAt = new Carbon($this->getValue('awarded_at'), 'UTC'); + } + + /** + * Get the ID of the award. + * + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * Get the name of the award. + * + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * Get the link to the award image. + * + * @return string + */ + public function getImageUrl(): string + { + return $this->imageUrl; + } + + /** + * Get the date and time the user was given the award (UTC). + * + * @return Carbon + */ + public function getAwardedAt(): Carbon + { + return $this->awardedAt; + } +} diff --git a/tests/Unit/Models/PlayerAchievementTest.php b/tests/Unit/Models/PlayerAchievementTest.php new file mode 100644 index 0000000..b4afdee --- /dev/null +++ b/tests/Unit/Models/PlayerAchievementTest.php @@ -0,0 +1,62 @@ +getFixtureData('player.achievement.json'); + + $this->achievement = new PlayerAchievement($this->client, $data); + } + + public function testItHasAnId() + { + $this->assertSame(1, $this->achievement->getId()); + } + + public function testItHasATitle() + { + $this->assertSame('Operation HQ', $this->achievement->getTitle()); + } + + public function testItHasADescription() + { + $this->assertSame( + 'Let\'s build a home for the TruckersMP community!', + $this->achievement->getDescription(), + ); + } + + public function testItHasAnImageUrl() + { + $this->assertSame( + 'https://static.truckersmp.com/images/achievements/achievement.png', + $this->achievement->getImageUrl(), + ); + } + + public function testItHasAnAchievedDate() + { + $this->assertDate('2022-02-14 12:42:24', $this->achievement->getAchievedAt()); + } +} diff --git a/tests/Unit/Models/PlayerAwardTest.php b/tests/Unit/Models/PlayerAwardTest.php new file mode 100644 index 0000000..22f0ab9 --- /dev/null +++ b/tests/Unit/Models/PlayerAwardTest.php @@ -0,0 +1,54 @@ +getFixtureData('player.award.json'); + + $this->award = new PlayerAward($this->client, $data); + } + + public function testItHasAnId() + { + $this->assertSame(2, $this->award->getId()); + } + + public function testItHasAName() + { + $this->assertSame('Easter Egg Hunt 2019', $this->award->getName()); + } + + public function testItHasAnImageUrl() + { + $this->assertSame( + 'https://static.truckersmp.com/images/awards/award.png', + $this->award->getImageUrl(), + ); + } + + public function testItHasAnAwardedDate() + { + $this->assertDate('2020-12-11 14:30:25', $this->award->getAwardedAt()); + } +} diff --git a/tests/Unit/Models/PlayerTest.php b/tests/Unit/Models/PlayerTest.php index f4bb05c..e8d3f87 100644 --- a/tests/Unit/Models/PlayerTest.php +++ b/tests/Unit/Models/PlayerTest.php @@ -12,6 +12,8 @@ use TruckersMP\APIClient\Models\Event; use TruckersMP\APIClient\Models\Patreon; use TruckersMP\APIClient\Models\Player; +use TruckersMP\APIClient\Models\PlayerAchievement; +use TruckersMP\APIClient\Models\PlayerAward; use TruckersMP\APIClient\Models\PlayerCompanyHistory; class PlayerTest extends TestCase @@ -124,6 +126,26 @@ public function testItHasACompanyHistory() $this->assertInstanceOf(PlayerCompanyHistory::class, $history->first()); } + public function testItHasAnAchievementsList() + { + $achievements = $this->player->getAchievements(); + + $this->assertInstanceOf(Collection::class, $achievements); + $this->assertCount(2, $achievements); + + $this->assertInstanceOf(PlayerAchievement::class, $achievements->first()); + } + + public function testItHasAnAwardList() + { + $awards = $this->player->getAwards(); + + $this->assertInstanceOf(Collection::class, $awards); + $this->assertCount(1, $awards); + + $this->assertInstanceOf(PlayerAward::class, $awards->first()); + } + public function testItCanGetBans() { $this->mockRequest('ban.json', 'bans/1287455'); diff --git a/tests/Unit/Models/fixtures/player.achievement.json b/tests/Unit/Models/fixtures/player.achievement.json new file mode 100644 index 0000000..cc0fa12 --- /dev/null +++ b/tests/Unit/Models/fixtures/player.achievement.json @@ -0,0 +1,7 @@ +{ + "id": 1, + "title": "Operation HQ", + "description": "Let's build a home for the TruckersMP community!", + "image_url": "https://static.truckersmp.com/images/achievements/achievement.png", + "achieved_at": "2022-02-14T12:42:24.000000Z" +} diff --git a/tests/Unit/Models/fixtures/player.award.json b/tests/Unit/Models/fixtures/player.award.json new file mode 100644 index 0000000..1bd934f --- /dev/null +++ b/tests/Unit/Models/fixtures/player.award.json @@ -0,0 +1,6 @@ +{ + "id": 2, + "name": "Easter Egg Hunt 2019", + "image_url": "https://static.truckersmp.com/images/awards/award.png", + "awarded_at": "2020-12-11T14:30:25.000000Z" +} diff --git a/tests/Unit/Models/fixtures/player.json b/tests/Unit/Models/fixtures/player.json index e3072a5..719e878 100644 --- a/tests/Unit/Models/fixtures/player.json +++ b/tests/Unit/Models/fixtures/player.json @@ -45,5 +45,29 @@ "joinDate": "2018-04-25 20:24:03", "leftDate": "2019-07-13 15:39:58" } + ], + "achievements": [ + { + "id": 1, + "title": "Operation HQ", + "description": "Let's build a home for the TruckersMP community!", + "image_url": "https://static.truckersmp.com/images/achievements/achievement.png", + "achieved_at": "2022-02-14T12:42:24.000000Z" + }, + { + "id": 3, + "title": "Celebrate TMP9", + "description": "Join the celebrations of TruckersMP's ninth anniversary.", + "image_url": "https://static.truckersmp.com/images/achievements/achievement.png", + "achieved_at": "2023-04-28T16:33:42.000000Z" + } + ], + "awards": [ + { + "id": 2, + "name": "Easter Egg Hunt 2019", + "image_url": "https://static.truckersmp.com/images/awards/award.png", + "awarded_at": "2020-12-11T14:30:25.000000Z" + } ] } diff --git a/tests/Unit/Requests/fixtures/player.json b/tests/Unit/Requests/fixtures/player.json index dd3f0c9..cb5f690 100644 --- a/tests/Unit/Requests/fixtures/player.json +++ b/tests/Unit/Requests/fixtures/player.json @@ -48,6 +48,30 @@ "joinDate": "2018-04-25 20:24:03", "leftDate": "2019-07-13 15:39:58" } + ], + "achievements": [ + { + "id": 1, + "title": "Operation HQ", + "description": "Let's build a home for the TruckersMP community!", + "image_url": "https://static.truckersmp.com/images/achievements/achievement.png", + "achieved_at": "2022-02-14T12:42:24.000000Z" + }, + { + "id": 3, + "title": "Celebrate TMP9", + "description": "Join the celebrations of TruckersMP's ninth anniversary.", + "image_url": "https://static.truckersmp.com/images/achievements/achievement.png", + "achieved_at": "2023-04-28T16:33:42.000000Z" + } + ], + "awards": [ + { + "id": 2, + "name": "Easter Egg Hunt 2019", + "image_url": "https://static.truckersmp.com/images/awards/award.png", + "awarded_at": "2020-12-11T14:30:25.000000Z" + } ] } }