Files
thecrimesynticate/app/Http/Controllers/Game/CharacterController.php
T

205 lines
6.5 KiB
PHP
Raw Normal View History

2026-06-27 14:38:18 +03:00
<?php
namespace App\Http\Controllers\Game;
use App\Http\Controllers\Controller;
use App\Models\Character;
use App\Models\Location;
use App\Models\ActivityLog;
use App\Services\Game\EconomyService;
use App\Services\Game\EnergyService;
use App\Services\Game\HealthService;
use App\Services\Game\NotificationService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
class CharacterController extends Controller
{
protected EconomyService $economyService;
protected EnergyService $energyService;
protected HealthService $healthService;
protected NotificationService $notificationService;
public function __construct()
{
$this->economyService = new EconomyService();
$this->energyService = new EnergyService();
$this->healthService = new HealthService();
$this->notificationService = new NotificationService();
}
/**
* Show the character creation form
*/
public function create(Request $request): View
{
$genders = ['male', 'female', 'other'];
$locations = Location::orderBy('name')->get();
return view('game.character.create', compact('genders', 'locations'));
}
/**
* Store a newly created character
*/
public function store(Request $request): RedirectResponse
{
$validated = $request->validate([
'name' => ['required', 'string', 'max:50', 'unique:characters,name'],
'gender' => ['required', 'string', 'in:male,female,other'],
'location_id' => ['required', 'exists:locations,id'],
]);
$user = Auth::user();
// Check if user already has a character
if ($user->character) {
return redirect()->route('game.dashboard')
->with('error', 'You already have a character!');
}
// Create the character
$character = Character::create([
'user_id' => $user->id,
'name' => $validated['name'],
'gender' => $validated['gender'],
'location_id' => $validated['location_id'],
'level' => 1,
'experience' => 0,
'money' => $this->economyService->getStartingMoney(),
'bank' => $this->economyService->getStartingBank(),
'energy' => config('game.energy.start_amount', 100),
'max_energy' => config('game.energy.max', 100),
'health' => config('game.health.start_amount', 100),
'max_health' => config('game.health.max', 100),
'strength' => 5,
'defense' => 5,
'speed' => 5,
'dexterity' => 5,
'status' => 'active',
'last_action_at' => now(),
]);
// Log character creation
ActivityLog::logCharacterCreated($character);
return redirect()->route('game.dashboard')
->with('success', 'Character created successfully! Welcome to the game.');
}
/**
* Display the character profile
*/
public function show(Request $request): View
{
$character = $request->user()->character;
if (!$character) {
return redirect()->route('game.character.create');
}
return view('game.character.show', compact('character'));
}
/**
* Show the character edit form
*/
public function edit(Request $request): View
{
$character = $request->user()->character;
if (!$character) {
return redirect()->route('game.character.create');
}
return view('game.character.edit', compact('character'));
}
/**
* Update the character
*/
public function update(Request $request): RedirectResponse
{
$character = $request->user()->character;
if (!$character) {
return redirect()->route('game.character.create');
}
$validated = $request->validate([
'name' => ['sometimes', 'string', 'max:50', 'unique:characters,name,'.$character->id],
'gender' => ['sometimes', 'string', 'in:male,female,other'],
]);
$character->update($validated);
return redirect()->route('game.character.show')
->with('success', 'Character updated successfully!');
}
/**
* Deposit money to bank
*/
public function deposit(Request $request): RedirectResponse
{
$character = $request->user()->character;
$validated = $request->validate([
'amount' => ['required', 'integer', 'min:1'],
]);
if ($this->economyService->depositToBank($character, $validated['amount'])) {
return redirect()->back()
->with('success', 'Money deposited to bank successfully!');
}
return redirect()->back()
->with('error', 'Failed to deposit money. You may not have enough money or reached the bank limit.');
}
/**
* Withdraw money from bank
*/
public function withdraw(Request $request): RedirectResponse
{
$character = $request->user()->character;
$validated = $request->validate([
'amount' => ['required', 'integer', 'min:1'],
]);
if ($this->economyService->withdrawFromBank($character, $validated['amount'])) {
return redirect()->back()
->with('success', 'Money withdrawn from bank successfully!');
}
return redirect()->back()
->with('error', 'Failed to withdraw money. You may not have enough in your bank.');
}
/**
* Revive character from hospital
*/
public function revive(Request $request): RedirectResponse
{
$character = $request->user()->character;
if (!$character->isDead() && !$character->isInHospital()) {
return redirect()->route('game.dashboard')
->with('error', 'Your character is not in hospital.');
}
if ($this->healthService->revive($character)) {
$this->notificationService->sendCharacterRevived($request->user(), $character);
return redirect()->route('game.dashboard')
->with('success', 'Character revived successfully!');
}
return redirect()->route('game.dashboard')
->with('error', 'Failed to revive character. You may not have enough money.');
}
}