123 lines
3.7 KiB
PHP
123 lines
3.7 KiB
PHP
<?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(EconomyService $economyService)
|
|
{
|
|
$this->economyService = $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.');
|
|
}
|
|
}
|