Untitled

mail@pastecode.io avatar
unknown
php
7 months ago
1.8 kB
3
Indexable
Never
<?php

namespace App\Http\Controllers;

use App\Http\Resources\PlayerResource;
use App\Http\Resources\Titles\TitlesResourceCollection;
use App\Http\Services\PlayerTitleService;
use App\Models\Player;
use App\Models\Title;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PlayerController extends Controller
{
    public function grantTitle(Request $request)
    {
        // Find player
        $player = Player::find(Auth::id());

        $titleService = new PlayerTitleService();


        // Validate the request parameters
        $validatedData = $request->validate([
            'title' => 'required|string|min:2',
        ]);


        // Grant the title using the service
        $titleService->grantTitle($validatedData['title']);

        $responseMessage = "Added {$validatedData['title']} title to {$player->username}";

        return response()->json(['success' => true, 'message' => $responseMessage]);
    }


    public function setActiveTitle(Request $request)
    {
        $titleService = new PlayerTitleService();

        return response()->json($titleService->setActiveTitle($request->title));
    }

    public function getProfile()
    {
        $player = Player::with(['titles', 'activeTitle'])->find(Auth::id());

        return response()->json(new PlayerResource($player));
    }

    public function getUnlockedTitles()
    {
        $player = Player::find(Auth::id());

        $unlockedTitles = new TitlesResourceCollection($player->unlockedTitles);

        return response()->json(['unlockedTitles' => $unlockedTitles]);
    }

    public function getActiveTitle()
    {
        $player = Player::find(Auth::id());

        $titleService = new PlayerTitleService();
        return response()->json($titleService->getActiveTitle($player));
    }
}
Leave a Comment