Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/Models/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down
112 changes: 112 additions & 0 deletions src/Models/PlayerAchievement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace TruckersMP\APIClient\Models;

use Carbon\Carbon;
use TruckersMP\APIClient\Client;

class PlayerAchievement extends Model
{
/**
* The ID of the achievement.
*
* @var int
*/
protected int $id;

/**
* The title of the achievement.
*
* @var string
*/
protected string $title;

/**
* The description of the achievement.
*
* @var string
*/
protected string $description;

/**
* The link to the achievement image.
*
* @var string
*/
protected string $imageUrl;

/**
* The date and time the user was given the achievement (UTC).
*
* @var Carbon
*/
protected Carbon $achievedAt;

/**
* Create a new PlayerAchievement instance.
*
* @param Client $client
* @param array $achievement
* @return void
*/
public function __construct(Client $client, array $achievement)
{
parent::__construct($client, $achievement);

$this->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;
}
}
94 changes: 94 additions & 0 deletions src/Models/PlayerAward.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace TruckersMP\APIClient\Models;

use Carbon\Carbon;
use TruckersMP\APIClient\Client;

class PlayerAward extends Model
{
/**
* The ID of the award.
*
* @var int
*/
protected int $id;

/**
* The name of the award.
*
* @var string
*/
protected string $name;

/**
* The link to the award image.
*
* @var string
*/
protected string $imageUrl;

/**
* The date and time the user was given the award (UTC).
*
* @var Carbon
*/
protected Carbon $awardedAt;

/**
* Create a new PlayerAward instance.
*
* @param Client $client
* @param array $award
* @return void
*/
public function __construct(Client $client, array $award)
{
parent::__construct($client, $award);

$this->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;
}
}
62 changes: 62 additions & 0 deletions tests/Unit/Models/PlayerAchievementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Tests\Unit\Models;

use Tests\TestCase;
use Tests\Unit\MockModelData;
use TruckersMP\APIClient\Models\PlayerAchievement;

class PlayerAchievementTest extends TestCase
{
use MockModelData;

/**
* A PlayerAchievement model instance filled with mocked data.
*
* @var PlayerAchievement
*/
private PlayerAchievement $achievement;

/**
* This method is called before each test.
*
* @return void
*/
protected function setUp(): void
{
$data = $this->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());
}
}
54 changes: 54 additions & 0 deletions tests/Unit/Models/PlayerAwardTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Tests\Unit\Models;

use Tests\TestCase;
use Tests\Unit\MockModelData;
use TruckersMP\APIClient\Models\PlayerAward;

class PlayerAwardTest extends TestCase
{
use MockModelData;

/**
* A PlayerAward model instance filled with mocked data.
*
* @var PlayerAward
*/
private PlayerAward $award;

/**
* This method is called before each test.
*
* @return void
*/
protected function setUp(): void
{
$data = $this->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());
}
}
Loading
Loading