first commit

This commit is contained in:
Doar Mihai
2026-06-27 14:38:18 +03:00
commit 96d317480b
165 changed files with 20061 additions and 0 deletions
@@ -0,0 +1,204 @@
<?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.');
}
}
@@ -0,0 +1,94 @@
<?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'));
}
}
@@ -0,0 +1,38 @@
<?php
namespace App\Http\Controllers\Game;
use App\Http\Controllers\Controller;
use App\Models\ActivityLog;
use App\Models\Notification;
use Illuminate\Http\Request;
use Illuminate\View\View;
class DashboardController extends Controller
{
/**
* Display the game dashboard
*/
public function index(Request $request): View
{
$character = $request->user()->character;
if (!$character) {
return redirect()->route('game.character.create');
}
// Get recent activity logs
$recentActivity = ActivityLog::where('character_id', $character->id)
->latest()
->take(10)
->get();
// Get unread notifications
$unreadNotifications = $request->user()->notifications()
->whereNull('read_at')
->orderBy('created_at', 'desc')
->get();
return view('game.dashboard', compact('character', 'recentActivity', 'unreadNotifications'));
}
}
@@ -0,0 +1,122 @@
<?php
namespace App\Http\Controllers\Game;
use App\Http\Controllers\Controller;
use App\Models\Inventory;
use App\Models\Item;
use App\Services\Game\EconomyService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class InventoryController extends Controller
{
protected EconomyService $economyService;
public function __construct()
{
$this->economyService = new EconomyService();
}
/**
* Display the character's inventory
*/
public function index(Request $request): View
{
$character = $request->user()->character;
if (!$character) {
return redirect()->route('game.character.create');
}
$inventory = $character->inventory()->with('item')->get();
return view('game.inventory.index', compact('character', 'inventory'));
}
/**
* Equip an item
*/
public function equip(Request $request, Inventory $inventory): RedirectResponse
{
$character = $request->user()->character;
if (!$character || $inventory->character_id !== $character->id) {
return redirect()->route('game.inventory.index')
->with('error', 'Unauthorized action.');
}
if ($inventory->equip()) {
return redirect()->route('game.inventory.index')
->with('success', 'Item equipped successfully!');
}
return redirect()->route('game.inventory.index')
->with('error', 'Cannot equip this item.');
}
/**
* Unequip an item
*/
public function unequip(Request $request, Inventory $inventory): RedirectResponse
{
$character = $request->user()->character;
if (!$character || $inventory->character_id !== $character->id) {
return redirect()->route('game.inventory.index')
->with('error', 'Unauthorized action.');
}
$inventory->unequip();
return redirect()->route('game.inventory.index')
->with('success', 'Item unequipped successfully!');
}
/**
* Use an item
*/
public function use(Request $request, Inventory $inventory): RedirectResponse
{
$character = $request->user()->character;
if (!$character || $inventory->character_id !== $character->id) {
return redirect()->route('game.inventory.index')
->with('error', 'Unauthorized action.');
}
if ($inventory->use($character)) {
return redirect()->route('game.inventory.index')
->with('success', 'Item used successfully!');
}
return redirect()->route('game.inventory.index')
->with('error', 'Cannot use this item.');
}
/**
* Drop an item
*/
public function drop(Request $request, Inventory $inventory): RedirectResponse
{
$character = $request->user()->character;
if (!$character || $inventory->character_id !== $character->id) {
return redirect()->route('game.inventory.index')
->with('error', 'Unauthorized action.');
}
$validated = $request->validate([
'quantity' => ['required', 'integer', 'min:1', 'max:'.$inventory->quantity],
]);
if ($inventory->remove($validated['quantity'])) {
return redirect()->route('game.inventory.index')
->with('success', 'Item dropped successfully!');
}
return redirect()->route('game.inventory.index')
->with('error', 'Failed to drop item.');
}
}