95 lines
3.1 KiB
PHP
95 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Game;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Crime;
|
|
use App\Models\CrimeAttempt;
|
|
use App\Services\Game\CrimeService;
|
|
use App\Services\Game\EnergyService;
|
|
use App\Services\Game\NotificationService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class CrimeController extends Controller
|
|
{
|
|
protected CrimeService $crimeService;
|
|
protected EnergyService $energyService;
|
|
protected NotificationService $notificationService;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->crimeService = new CrimeService();
|
|
$this->energyService = new EnergyService();
|
|
$this->notificationService = new NotificationService();
|
|
}
|
|
|
|
/**
|
|
* Display a listing of available crimes
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
$character = $request->user()->character;
|
|
|
|
if (!$character) {
|
|
return redirect()->route('game.character.create');
|
|
}
|
|
|
|
$availableCrimes = $this->crimeService->getAvailableCrimes($character);
|
|
$recentAttempts = $this->crimeService->getRecentAttempts($character, 5);
|
|
|
|
return view('game.crimes.index', compact('character', 'availableCrimes', 'recentAttempts'));
|
|
}
|
|
|
|
/**
|
|
* Attempt to commit a crime
|
|
*/
|
|
public function attempt(Request $request, Crime $crime): RedirectResponse
|
|
{
|
|
$character = $request->user()->character;
|
|
|
|
if (!$character) {
|
|
return redirect()->route('game.character.create');
|
|
}
|
|
|
|
try {
|
|
$attempt = $this->crimeService->attemptCrime($character, $crime);
|
|
|
|
if ($attempt->isSuccessful()) {
|
|
$this->notificationService->sendCrimeSuccess($request->user(), $crime, $attempt->reward);
|
|
return redirect()->route('game.crimes.index')
|
|
->with('success', 'Crime successful! You earned $'.$attempt->reward);
|
|
} else {
|
|
$this->notificationService->sendCrimeFailure($request->user(), $crime);
|
|
return redirect()->route('game.crimes.index')
|
|
->with('error', 'Crime failed! You took damage.');
|
|
}
|
|
} catch (\Exception $e) {
|
|
return redirect()->route('game.crimes.index')
|
|
->with('error', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show crime details
|
|
*/
|
|
public function show(Request $request, Crime $crime): View
|
|
{
|
|
$character = $request->user()->character;
|
|
|
|
if (!$character) {
|
|
return redirect()->route('game.character.create');
|
|
}
|
|
|
|
// Check if crime is on cooldown
|
|
$onCooldown = $this->crimeService->isOnCooldown($character, $crime);
|
|
$cooldownRemaining = $onCooldown ? $this->crimeService->getCooldownRemaining($character, $crime) : 0;
|
|
|
|
// Calculate success rate for this character
|
|
$successRate = $crime->calculateSuccessRate($character);
|
|
|
|
return view('game.crimes.show', compact('character', 'crime', 'onCooldown', 'cooldownRemaining', 'successRate'));
|
|
}
|
|
}
|