Untitled

mail@pastecode.io avatar
unknown
javascript
7 months ago
982 B
0
Indexable
Never
class PlayerService
{
  public function grantTitle(Player $player, string $titleName)
  {
    $title = Title::firstOrCreate(['name' => $titleName]);
    $player->titles()->attach($title->id);

    $responseMessage = "Granted {$titleName} title to {$player->username}";
    return ['success' => true, 'message' => $responseMessage];
  }

  public function setActiveTitle(Player $player, string $titleName)
  {
    $title = $player->titles()->whereName($titleName)->firstOrFail();
    $player->active_title_id = $title->id;
    $player->save();

    return [
      'success' => true,
      'message' => "Set {$titleName} as active title"
    ];
  }

  public function getProfile(int $playerId)
  {
    return Player::with(['titles', 'activeTitle'])->findOrFail($playerId);
  }

  public function getActiveTitle(Player $player)
  {
    if (!$player->activeTitle) {
      return ['active_title' => null];
    }

    return [
      'active_title' => $player->activeTitle->name
    ];
  }
Leave a Comment