Skip to content

Commit

Permalink
Merge branch 'master' into track-theme-scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland authored Sep 5, 2024
2 parents f66a2d6 + 7588b5d commit 75d83d1
Show file tree
Hide file tree
Showing 56 changed files with 1,316 additions and 400 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ rules:
react/no-unescaped-entities: off
react/prop-types: off
react/react-in-jsx-scope: off
react/jsx-no-target-blank: off # we don't support the old browsers this rule tries to protect

# disable some of the more aggressive unicorn rules
unicorn/filename-case: off
Expand Down
26 changes: 26 additions & 0 deletions app/Data/UserPermissionsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Data;

use App\Models\User;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Lazy;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;

#[TypeScript('UserPermissions')]
class UserPermissionsData extends Data
{
public function __construct(
public Lazy|bool $manageGameHashes,
) {
}

public static function fromUser(?User $user): self
{
return new self(
manageGameHashes: Lazy::create(fn () => $user ? $user->can('manage', \App\Models\GameHash::class) : false),
);
}
}
3 changes: 2 additions & 1 deletion app/Helpers/database/user-email-verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ function validateEmailVerificationToken(string $emailCookie, ?string &$user): bo
$response = SetAccountPermissionsJSON('Server', Permissions::Moderator, $user->username, Permissions::Registered);
if ($response['Success']) {
static_addnewregistereduser($user->username);
generateAPIKey($user->username);

$user->email_verified_at = Carbon::now();
$user->save();

generateAPIKey($user->username);

// SUCCESS: validated email address for $user
return true;
}
Expand Down
13 changes: 13 additions & 0 deletions app/Platform/Controllers/GameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Http\Controller;
use App\Models\Game;
use App\Models\System;
use App\Platform\Requests\GameRequest;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
Expand Down Expand Up @@ -106,4 +107,16 @@ public function destroy(Game $game): void
{
$this->authorize('delete', $game);
}

public function random(): RedirectResponse
{
$this->authorize('viewAny', Game::class);

$randomGameWithAchievements = Game::whereNotIn('ConsoleID', System::getNonGameSystems())
->where('achievements_published', '>=', 6)
->inRandomOrder()
->firstOrFail();

return redirect(route('game.show', ['game' => $randomGameWithAchievements]));
}
}
24 changes: 19 additions & 5 deletions app/Platform/Controllers/GameHashController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
namespace App\Platform\Controllers;

use App\Community\Enums\ArticleType;
use App\Data\UserPermissionsData;
use App\Http\Controller;
use App\Models\Game;
use App\Models\GameHash;
use App\Models\User;
use Illuminate\Contracts\View\View;
use App\Platform\Data\GameData;
use App\Platform\Data\GameHashData;
use App\Platform\Data\GameHashesPagePropsData;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;
use Inertia\Response as InertiaResponse;

class GameHashController extends Controller
{
Expand All @@ -20,12 +26,17 @@ protected function resourceName(): string
return 'game-hash';
}

public function index(Request $request): View
public function index(Request $request, Game $game): InertiaResponse
{
$this->authorize('viewAny', $this->resourceClass());

return view('resource.index')
->with('resource', $this->resourceName());
$gameData = GameData::fromGame($game)->include('badgeUrl', 'forumTopicId', 'system');
$hashes = GameHashData::fromCollection($game->hashes);
$can = UserPermissionsData::fromUser($request->user())->include('manageGameHashes');

$props = new GameHashesPagePropsData($gameData, $hashes, $can);

return Inertia::render('game/[game]/hashes', $props);
}

public function show(GameHash $gameHash): void
Expand Down Expand Up @@ -70,7 +81,10 @@ public function update(Request $request, GameHash $gameHash): JsonResponse
}

$gameHash->update($updatedAttributes);
$this->logGameHashUpdate($gameHash, $changedAttributes, Auth::user());

/** @var User $user */
$user = Auth::user();
$this->logGameHashUpdate($gameHash, $changedAttributes, $user);

return response()->json(['message' => __('legacy.success.update')]);
}
Expand Down
34 changes: 34 additions & 0 deletions app/Platform/Data/GameData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Platform\Data;

use App\Models\Game;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Lazy;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;

#[TypeScript('Game')]
class GameData extends Data
{
public function __construct(
public int $id,
public string $title,
public Lazy|string $badgeUrl,
public Lazy|int $forumTopicId,
public Lazy|SystemData $system,
) {
}

public static function fromGame(Game $game): self
{
return new self(
id: $game->id,
title: $game->title,
badgeUrl: Lazy::create(fn () => $game->badge_url),
forumTopicId: Lazy::create(fn () => $game->ForumTopicID),
system: Lazy::create(fn () => SystemData::fromSystem($game->system))
);
}
}
48 changes: 48 additions & 0 deletions app/Platform/Data/GameHashData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace App\Platform\Data;

use App\Models\GameHash;
use Illuminate\Database\Eloquent\Collection;
use Spatie\LaravelData\Data;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;
use Spatie\TypeScriptTransformer\Attributes\TypeScriptType;

#[TypeScript('GameHash')]
class GameHashData extends Data
{
public function __construct(
public int $id,
public string $md5,
public ?string $name,
#[TypeScriptType('App\\Platform\\Data\\GameHashLabelData[]')]
public array $labels,
public ?string $patchUrl,
) {
}

public static function fromGameHash(GameHash $gameHash): self
{
return new self(
id: $gameHash->id,
md5: $gameHash->md5,
name: $gameHash->name,
labels: GameHashLabelData::fromLabelsString($gameHash->labels),
patchUrl: $gameHash->patch_url,
);
}

/**
* @param Collection<int, GameHash> $gameHashes
* @return GameHashData[]
*/
public static function fromCollection(Collection $gameHashes): array
{
return array_map(
fn ($gameHash) => self::fromGameHash($gameHash),
$gameHashes->all()
);
}
}
39 changes: 39 additions & 0 deletions app/Platform/Data/GameHashLabelData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Platform\Data;

use Illuminate\Support\Facades\File;
use Spatie\LaravelData\Data;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;

#[TypeScript('GameHashLabel')]
class GameHashLabelData extends Data
{
public function __construct(
public string $label,
public ?string $imgSrc
) {
}

/**
* @return GameHashLabelData[]
*/
public static function fromLabelsString(string $labels): array
{
$asArray = array_filter(explode(',', $labels));

return array_map(function (string $label) {
$imagePath = "/assets/images/labels/" . $label . '.png';
$publicPath = public_path($imagePath);

$imgSrc = File::exists($publicPath) ? asset($imagePath) : null;

return new self(
label: $label,
imgSrc: $imgSrc
);
}, $asArray);
}
}
23 changes: 23 additions & 0 deletions app/Platform/Data/GameHashesPagePropsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\Platform\Data;

use App\Data\UserPermissionsData;
use Spatie\LaravelData\Data;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;

#[TypeScript('GameHashesPageProps')]
class GameHashesPagePropsData extends Data
{
/**
* @param GameHashData[] $hashes
*/
public function __construct(
public GameData $game,
public array $hashes,
public UserPermissionsData $can,
) {
}
}
32 changes: 32 additions & 0 deletions app/Platform/Data/SystemData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Platform\Data;

use App\Models\System;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Lazy;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;

#[TypeScript('System')]
class SystemData extends Data
{
public function __construct(
public int $id,
public string $name,
public Lazy|string $nameFull,
public Lazy|string $nameShort,
) {
}

public static function fromSystem(System $system): self
{
return new self(
id: $system->id,
name: $system->name,
nameFull: Lazy::create(fn () => $system->name_full),
nameShort: Lazy::create(fn () => $system->name_short),
);
}
}
6 changes: 6 additions & 0 deletions app/Platform/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\GameHash;
use App\Platform\Controllers\AchievementController;
use App\Platform\Controllers\GameController;
use App\Platform\Controllers\GameHashController;
use App\Platform\Controllers\PlayerAchievementController;
use App\Platform\Controllers\PlayerGameController;
Expand Down Expand Up @@ -41,6 +42,10 @@ public function map(): void
protected function mapWebRoutes(): void
{
Route::middleware(['web', 'csp'])->group(function () {
Route::middleware(['inertia'])->group(function () {
Route::get('game/{game}/hashes', [GameHashController::class, 'index'])->name('game.hashes.index');
});

// Route::get('achievement/{achievement}{slug?}', [AchievementController::class, 'show'])->name('achievement.show');
// Route::resource('achievements', AchievementController::class)->only('index')->names(['index' => 'achievement.index']);
// Route::get(
Expand All @@ -63,6 +68,7 @@ protected function mapWebRoutes(): void
// Route::get('game/{game}/badges', [GameBadgeController::class, 'index'])->name('game.badge.index');
// Route::get('game/{game}/assets', [GameAssetsController::class, 'index'])->name('game.asset.index');
// Route::get('game/{game}/players', [GamePlayerController::class, 'index'])->name('game.player.index');
Route::get('game/random', [GameController::class, 'random'])->name('game.random');

// Route::get('create', CreateController::class)->name('create');
// Route::resource('developers', DeveloperController::class)->only('index');
Expand Down
9 changes: 4 additions & 5 deletions app/Policies/GameHashPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Policies;

use App\Enums\Permissions;
use App\Models\GameHash;
use App\Models\Role;
use App\Models\User;
Expand All @@ -20,8 +19,7 @@ public function manage(User $user): bool
Role::GAME_HASH_MANAGER,
Role::DEVELOPER_STAFF,
Role::DEVELOPER,
])
|| $user->getAttribute('Permissions') >= Permissions::Developer;
]);
}

public function viewAny(?User $user): bool
Expand All @@ -47,8 +45,9 @@ public function update(User $user): bool
{
return $user->hasAnyRole([
Role::GAME_HASH_MANAGER,
])
|| $user->getAttribute('Permissions') >= Permissions::Developer;
Role::DEVELOPER_STAFF,
Role::DEVELOPER,
]);
}

public function delete(User $user, GameHash $gameHash): bool
Expand Down
Loading

0 comments on commit 75d83d1

Please sign in to comment.