Files
moonwell-web/app/Http/Controllers/CabinetController.php
T

140 lines
5.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\ChangePasswordRequest;
use App\Services\AzerothCoreAccountService;
use App\Services\BalanceService;
use App\Services\GameClientDownloadService;
use App\Services\StoreAvailabilityService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
use Throwable;
class CabinetController extends Controller
{
public function index(
Request $request,
AzerothCoreAccountService $accounts,
BalanceService $balances,
StoreAvailabilityService $storeAvailability,
): Response
{
$user = $request->user();
$balance = $balances->balance($user->id);
$characters = $accounts->charactersForAccount($user->id)->values();
return Inertia::render('Cabinet/Index', [
'site' => $this->siteData(),
'auth' => [
'authenticated' => true,
'username' => $user->username,
'access_label' => $user->accessLabel(),
'is_admin' => $user->canAccessAdminPanel(),
],
'urls' => $this->urls(),
'csrfToken' => csrf_token(),
'flash' => [
'status' => $request->session()->get('status'),
'error' => $request->session()->get('error'),
],
'account' => [
'username' => $user->username,
'email' => $user->email !== '' ? $user->email : '—',
'access_label' => $user->accessLabel(),
'joined_at' => $user->joinedAtLabel(),
'last_login_at' => $user->lastLoginLabel(),
'locked' => $user->locked,
'status_label' => $user->locked ? 'Заблокирован' : 'Активен',
],
'characters' => $characters->all(),
'client' => [
'filename' => config('moonwell.client.object_key'),
],
'wallet' => [
'deposit_enabled' => $storeAvailability->isEnabled(),
'amount' => $balance,
'formatted_amount' => number_format((float) $balance, 2, ',', ' '),
'minimum_deposit' => (float) config('services.robokassa.minimum_amount'),
'maximum_deposit' => (float) config('services.robokassa.maximum_amount'),
'formatted_maximum_deposit' => number_format((float) config('services.robokassa.maximum_amount'), 0, ',', ' '),
'tears_per_ruble' => (int) config('services.robokassa.tears_per_ruble'),
'deposit_amount' => $request->old('amount', 500),
'transactions' => $balances->recentTransactions($user->id)
->map(fn ($transaction): array => [
'id' => $transaction->id,
'type' => $transaction->type,
'label' => match ($transaction->type) {
'deposit' => 'Пополнение',
'admin_credit' => 'Начисление',
'admin_debit' => 'Списание',
default => 'Покупка в игре',
},
'amount' => (string) $transaction->amount,
'formatted_amount' => number_format((float) $transaction->amount, 2, ',', ' '),
'is_positive' => (float) $transaction->amount >= 0,
'created_at' => $transaction->created_at?->format('d.m.Y H:i'),
])
->values()
->all(),
],
]);
}
public function updatePassword(ChangePasswordRequest $request, AzerothCoreAccountService $accounts): RedirectResponse
{
$user = $request->user();
if (! $accounts->validatePassword($user, $request->string('current_password')->toString())) {
return back()->withErrors(['current_password' => 'Текущий пароль указан неверно.']);
}
$accounts->updatePassword($user, $request->string('password')->toString());
return back()->with('status', 'Пароль успешно обновлён.');
}
public function client(GameClientDownloadService $clientDownloadService): RedirectResponse
{
try {
return redirect()->away($clientDownloadService->temporaryUrl());
} catch (Throwable $exception) {
report($exception);
return back()->with('error', 'Не удалось получить ссылку на клиент. Попробуй ещё раз позже.');
}
}
/**
* @return array<string, string|null>
*/
private function siteData(): array
{
return [
'name' => config('moonwell.realm.server_name'),
'tagline' => config('moonwell.realm.tagline'),
];
}
/**
* @return array<string, string>
*/
private function urls(): array
{
return [
'home' => route('landing.index'),
'cabinet' => route('cabinet.index'),
'admin' => route('admin.accounts.index'),
'logout' => route('logout'),
'client' => route('cabinet.client'),
'password' => route('cabinet.password.update'),
'deposit' => route('cabinet.balance.deposit'),
'offer' => config('moonwell.legal.offer_published') ? route('legal.offer') : null,
'privacy' => config('moonwell.legal.privacy_published') ? route('legal.privacy') : null,
'rules' => config('moonwell.legal.rules_published') ? route('legal.rules') : null,
];
}
}