41 lines
1.2 KiB
PHP
41 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware\Game;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class CheckCooldown
|
|
{
|
|
/**
|
|
* Handle an incoming request.
|
|
*
|
|
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
|
*/
|
|
public function handle(Request $request, Closure $next, string $actionType): Response
|
|
{
|
|
// Check if user is authenticated
|
|
if (!$request->user()) {
|
|
return redirect()->route('login');
|
|
}
|
|
|
|
// Check if user has a character
|
|
$character = $request->user()->character;
|
|
|
|
if (!$character) {
|
|
return redirect()->route('game.character.create');
|
|
}
|
|
|
|
$cooldownService = app(\App\Services\Game\CooldownService::class);
|
|
|
|
if ($cooldownService->isOnCooldown($character, $actionType)) {
|
|
$remaining = $cooldownService->getRemainingCooldown($character, $actionType);
|
|
return redirect()->back()
|
|
->with('error', "You must wait {$remaining} seconds before performing this action again.");
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
}
|