Skip to content

Commit 3c422ad

Browse files
committed
Add player's awards and achievements.
1 parent f37af2d commit 3c422ad

File tree

10 files changed

+449
-0
lines changed

10 files changed

+449
-0
lines changed

src/Models/Player.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ class Player extends Model
183183
*/
184184
protected Collection $companyHistory;
185185

186+
/**
187+
* The player's achievements sorted from the oldest.
188+
*
189+
* @var Collection
190+
*/
191+
protected Collection $achievements;
192+
193+
/**
194+
* The player's awards sorted from the oldest.
195+
*
196+
* @var Collection
197+
*/
198+
protected Collection $awards;
199+
186200
/**
187201
* Create a new Player instance.
188202
*
@@ -216,6 +230,12 @@ public function __construct(Client $client, array $player)
216230
$history = new Collection($this->getValue('vtcHistory', []));
217231
$this->companyHistory = $history->map(fn (array $entry) => new PlayerCompanyHistory($client, $entry));
218232

233+
$achievements = new Collection($this->getValue('achievements', []));
234+
$this->achievements = $achievements->map(fn (array $entry) => new PlayerAchievement($client, $entry));
235+
236+
$awards = new Collection($this->getValue('awards', []));
237+
$this->awards = $awards->map(fn (array $entry) => new PlayerAward($client, $entry));
238+
219239
$this->isStaff = $this->getValue('permissions.isStaff', false);
220240
$this->isUpperStaff = $this->getValue('permissions.isUpperStaff', false);
221241
$this->inGameAdmin = $this->getValue('permissions.isGameAdmin', false);
@@ -480,6 +500,30 @@ public function getDiscordSnowflake(): ?string
480500
return $this->discordSnowflake;
481501
}
482502

503+
/**
504+
* Get the player's achievements sorted from the oldest.
505+
*
506+
* Returns an empty collection if they are private.
507+
*
508+
* @return Collection
509+
*/
510+
public function getAchievements(): Collection
511+
{
512+
return $this->achievements;
513+
}
514+
515+
/**
516+
* Get the player's awards sorted from the oldest.
517+
*
518+
* Returns an empty collection if they are private.
519+
*
520+
* @return Collection
521+
*/
522+
public function getAwards(): Collection
523+
{
524+
return $this->awards;
525+
}
526+
483527
/**
484528
* Get the player's bans.
485529
*

src/Models/PlayerAchievement.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace TruckersMP\APIClient\Models;
4+
5+
use Carbon\Carbon;
6+
use TruckersMP\APIClient\Client;
7+
8+
class PlayerAchievement extends Model
9+
{
10+
/**
11+
* The ID of the achievement.
12+
*
13+
* @var int
14+
*/
15+
protected int $id;
16+
17+
/**
18+
* The title of the achievement.
19+
*
20+
* @var string
21+
*/
22+
protected string $title;
23+
24+
/**
25+
* The description of the achievement.
26+
*
27+
* @var string
28+
*/
29+
protected string $description;
30+
31+
/**
32+
* The link to the achievement image.
33+
*
34+
* @var string
35+
*/
36+
protected string $imageUrl;
37+
38+
/**
39+
* The date and time the user was given the achievement (UTC).
40+
*
41+
* @var Carbon
42+
*/
43+
protected Carbon $achievedAt;
44+
45+
/**
46+
* Create a new PlayerAchievement instance.
47+
*
48+
* @param Client $client
49+
* @param array $achievement
50+
* @return void
51+
*/
52+
public function __construct(Client $client, array $achievement)
53+
{
54+
parent::__construct($client, $achievement);
55+
56+
$this->id = $this->getValue('id');
57+
$this->title = $this->getValue('title');
58+
$this->description = $this->getValue('description');
59+
$this->imageUrl = $this->getValue('image_url');
60+
$this->achievedAt = new Carbon($this->getValue('achieved_at'), 'UTC');
61+
}
62+
63+
/**
64+
* Get the ID of the achievement.
65+
*
66+
* @return int
67+
*/
68+
public function getId(): int
69+
{
70+
return $this->id;
71+
}
72+
73+
/**
74+
* Get the title of the achievement.
75+
*
76+
* @return string
77+
*/
78+
public function getTitle(): string
79+
{
80+
return $this->title;
81+
}
82+
83+
/**
84+
* Get the description of the achievement.
85+
*
86+
* @return string
87+
*/
88+
public function getDescription(): string
89+
{
90+
return $this->description;
91+
}
92+
93+
/**
94+
* Get the link to the achievement image.
95+
*
96+
* @return string
97+
*/
98+
public function getImageUrl(): string
99+
{
100+
return $this->imageUrl;
101+
}
102+
103+
/**
104+
* Get the date and time the user was given the achievement (UTC).
105+
*
106+
* @return Carbon
107+
*/
108+
public function getAchievedAt(): Carbon
109+
{
110+
return $this->achievedAt;
111+
}
112+
}

src/Models/PlayerAward.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace TruckersMP\APIClient\Models;
4+
5+
use Carbon\Carbon;
6+
use TruckersMP\APIClient\Client;
7+
8+
class PlayerAward extends Model
9+
{
10+
/**
11+
* The ID of the award.
12+
*
13+
* @var int
14+
*/
15+
protected int $id;
16+
17+
/**
18+
* The name of the award.
19+
*
20+
* @var string
21+
*/
22+
protected string $name;
23+
24+
/**
25+
* The link to the award image.
26+
*
27+
* @var string
28+
*/
29+
protected string $imageUrl;
30+
31+
/**
32+
* The date and time the user was given the award (UTC).
33+
*
34+
* @var Carbon
35+
*/
36+
protected Carbon $awardedAt;
37+
38+
/**
39+
* Create a new PlayerAward instance.
40+
*
41+
* @param Client $client
42+
* @param array $award
43+
* @return void
44+
*/
45+
public function __construct(Client $client, array $award)
46+
{
47+
parent::__construct($client, $award);
48+
49+
$this->id = $this->getValue('id');
50+
$this->name = $this->getValue('name');
51+
$this->imageUrl = $this->getValue('image_url');
52+
$this->awardedAt = new Carbon($this->getValue('awarded_at'), 'UTC');
53+
}
54+
55+
/**
56+
* Get the ID of the award.
57+
*
58+
* @return int
59+
*/
60+
public function getId(): int
61+
{
62+
return $this->id;
63+
}
64+
65+
/**
66+
* Get the name of the award.
67+
*
68+
* @return string
69+
*/
70+
public function getName(): string
71+
{
72+
return $this->name;
73+
}
74+
75+
/**
76+
* Get the link to the award image.
77+
*
78+
* @return string
79+
*/
80+
public function getImageUrl(): string
81+
{
82+
return $this->imageUrl;
83+
}
84+
85+
/**
86+
* Get the date and time the user was given the award (UTC).
87+
*
88+
* @return Carbon
89+
*/
90+
public function getAwardedAt(): Carbon
91+
{
92+
return $this->awardedAt;
93+
}
94+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Tests\Unit\Models;
4+
5+
use Tests\TestCase;
6+
use Tests\Unit\MockModelData;
7+
use TruckersMP\APIClient\Models\PlayerAchievement;
8+
9+
class PlayerAchievementTest extends TestCase
10+
{
11+
use MockModelData;
12+
13+
/**
14+
* A PlayerAchievement model instance filled with mocked data.
15+
*
16+
* @var PlayerAchievement
17+
*/
18+
private PlayerAchievement $achievement;
19+
20+
/**
21+
* This method is called before each test.
22+
*
23+
* @return void
24+
*/
25+
protected function setUp(): void
26+
{
27+
$data = $this->getFixtureData('player.achievement.json');
28+
29+
$this->achievement = new PlayerAchievement($this->client, $data);
30+
}
31+
32+
public function testItHasAnId()
33+
{
34+
$this->assertSame(1, $this->achievement->getId());
35+
}
36+
37+
public function testItHasATitle()
38+
{
39+
$this->assertSame('Operation HQ', $this->achievement->getTitle());
40+
}
41+
42+
public function testItHasADescription()
43+
{
44+
$this->assertSame(
45+
'Let\'s build a home for the TruckersMP community!',
46+
$this->achievement->getDescription(),
47+
);
48+
}
49+
50+
public function testItHasAnImageUrl()
51+
{
52+
$this->assertSame(
53+
'https://static.truckersmp.com/images/achievements/achievement.png',
54+
$this->achievement->getImageUrl(),
55+
);
56+
}
57+
58+
public function testItHasAnAchievedDate()
59+
{
60+
$this->assertDate('2022-02-14 12:42:24', $this->achievement->getAchievedAt());
61+
}
62+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Tests\Unit\Models;
4+
5+
use Tests\TestCase;
6+
use Tests\Unit\MockModelData;
7+
use TruckersMP\APIClient\Models\PlayerAward;
8+
9+
class PlayerAwardTest extends TestCase
10+
{
11+
use MockModelData;
12+
13+
/**
14+
* A PlayerAward model instance filled with mocked data.
15+
*
16+
* @var PlayerAward
17+
*/
18+
private PlayerAward $award;
19+
20+
/**
21+
* This method is called before each test.
22+
*
23+
* @return void
24+
*/
25+
protected function setUp(): void
26+
{
27+
$data = $this->getFixtureData('player.award.json');
28+
29+
$this->award = new PlayerAward($this->client, $data);
30+
}
31+
32+
public function testItHasAnId()
33+
{
34+
$this->assertSame(2, $this->award->getId());
35+
}
36+
37+
public function testItHasAName()
38+
{
39+
$this->assertSame('Easter Egg Hunt 2019', $this->award->getName());
40+
}
41+
42+
public function testItHasAnImageUrl()
43+
{
44+
$this->assertSame(
45+
'https://static.truckersmp.com/images/awards/award.png',
46+
$this->award->getImageUrl(),
47+
);
48+
}
49+
50+
public function testItHasAnAwardedDate()
51+
{
52+
$this->assertDate('2020-12-11 14:30:25', $this->award->getAwardedAt());
53+
}
54+
}

0 commit comments

Comments
 (0)