Files
2026-06-27 14:38:18 +03:00

46 lines
1.3 KiB
PHP

<?php
namespace App\Http\Middleware\Game;
use App\Services\Game\EnergyService;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class CheckEnergy
{
protected EnergyService $energyService;
public function __construct(EnergyService $energyService)
{
$this->energyService = $energyService;
}
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next, string $action = 'default'): Response
{
$character = $request->user()->character;
// Check if character can act
if (!$character->canAct()) {
return redirect()->route('game.dashboard')
->with('error', 'You cannot perform actions right now.');
}
// Get energy cost for this action type
$cost = $this->energyService->getCostFor($action);
// Check if character has enough energy
if (!$character->hasEnergy($cost)) {
return redirect()->route('game.dashboard')
->with('error', "You don't have enough energy ({$cost} required) for this action.");
}
return $next($request);
}
}