1
0
forked from metin2/web

Localized ranking and modified controllers to read data from cache tables.

This commit is contained in:
2024-12-31 20:24:15 +02:00
parent 744e55e385
commit 103d21b5ef
17 changed files with 753 additions and 261 deletions

View File

@ -0,0 +1,86 @@
<?php
namespace App\Http\Controllers\Highscore;
use App\Http\Controllers\Controller;
use App\Models\Enums\EmpireEnum;
use App\Models\Game\Highscore\GuildHighscoreCache;
use Illuminate\Http\Request;
use Illuminate\View\View;
class GuildHighscoreController extends Controller
{
private const RESULTS_PER_PAGE = 10;
public function show(Request $request, ?int $empireChoice = null, ?int $page = null): View
{
$where = [];
// If "empireChoice" is a valid empire, add it to the query; otherwise set empireChoice to -1
if (in_array($empireChoice, array_column(EmpireEnum::cases(), 'value'), true))
$where[] = ['empire', $empireChoice];
else
$empireChoice = -1;
// Check whether "guildChoice" or "guildLeaderChoice" were requested
$validated = $request->validate([
'guild-choice' => 'nullable',
'guild-leader-choice' => 'nullable',
]);
$guildChoice = $validated['guild-choice'] ?? null;
$guildLeaderChoice = $validated['guild-leader-choice'] ?? null;
// If "guild-choice" was specified, find the guild with that name
if (!empty($guildChoice))
$where[] = ['name', $guildChoice];
// If "guild-leader-choice" was specified, find the guild master with that name
if (!empty($guildLeaderChoice))
$where[] = ['master', $guildLeaderChoice];
$highscore = GuildHighscoreCache::where($where)->paginate(self::RESULTS_PER_PAGE, page: $page);
return view('main/guildhighscore', [
'highscore' => $highscore,
'empireChoice' => $empireChoice,
'guildChoice' => $guildChoice,
'guildLeaderChoice' => $guildLeaderChoice,
]);
}
public function search(Request $request): View
{
$where = [];
$validated = $request->validate([
'empire-choice' => 'required|int',
'guild-choice' => 'nullable',
'guild-leader-choice' => 'nullable',
]);
$empireChoice = (int) $validated['empire-choice'];
$guildChoice = $validated['guild-choice'];
$guildLeaderChoice = $validated['guild-leader-choice'];
// If "empire-choice" is a valid empire, add it to the query
if (in_array($empireChoice, array_column(EmpireEnum::cases(), 'value'), true))
$where[] = ['empire', $empireChoice];
// If "guild-choice" was specified, find the guild with that name
if (!empty($guildChoice))
$where[] = ['name', $guildChoice];
// If "guild-leader-choice" was specified, find the guild master with that name
if (!empty($guildLeaderChoice))
$where[] = ['master', $guildLeaderChoice];
$highscore = GuildHighscoreCache::where($where)->paginate(self::RESULTS_PER_PAGE);
return view('main/guildhighscore', [
'highscore' => $highscore,
'empireChoice' => $empireChoice,
'guildChoice' => $guildChoice,
'guildLeaderChoice' => $guildLeaderChoice,
]);
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace App\Http\Controllers\Highscore;
use App\Http\Controllers\Controller;
use App\Models\Enums\CharacterClassEnum;
use App\Models\Enums\EmpireEnum;
use App\Models\Game\Highscore\HighscoreCache;
use Illuminate\Http\Request;
use Illuminate\View\View;
class HighscoreController extends Controller
{
private const RESULTS_PER_PAGE = 10;
public function show(Request $request, ?int $empireChoice = null, ?int $classChoice = null, ?int $page = null): View
{
$where = [];
// If "empireChoice" is a valid empire, add it to the query; otherwise set empireChoice to -1
if (in_array($empireChoice, array_column(EmpireEnum::cases(), 'value'), true))
$where[] = ['empire', $empireChoice];
else
$empireChoice = -1;
// If "classChoice" is a valid empire, add it to the query; otherwise set empireChoice to -1
if (in_array($classChoice, array_column(CharacterClassEnum::cases(), 'value'), true))
$where[] = ['job', $classChoice];
else
$classChoice = -1;
// Check whether "characterChoice" was requested
$validated = $request->validate(['character-choice' => 'nullable']);
$characterChoice = $validated['character-choice'] ?? null;
// If "character-choice" was specified, find the character with that name
if (!empty($characterChoice))
$where[] = ['name', $characterChoice];
$highscore = HighscoreCache::where($where)->paginate(self::RESULTS_PER_PAGE, page: $page);
return view('main/highscore', [
'highscore' => $highscore,
'empireChoice' => $empireChoice,
'classChoice' => $classChoice,
'characterChoice' => $characterChoice,
]);
}
public function search(Request $request): View
{
$where = [];
$validated = $request->validate([
'empire-choice' => 'required|int',
'class-choice' => 'required|int',
'character-choice' => 'nullable',
]);
$empireChoice = (int) $validated['empire-choice'];
$classChoice = (int) $validated['class-choice'];
$characterChoice = $validated['character-choice'];
// If "empire-choice" is a valid empire, add it to the query
if (in_array($empireChoice, array_column(EmpireEnum::cases(), 'value'), true))
$where[] = ['empire', $empireChoice];
// If "class-choice" is a valid character class, add it to the query
if (in_array($classChoice, array_column(CharacterClassEnum::cases(), 'value'), true))
$where[] = ['job', $classChoice];
// If "character-choice" was specified, find the character with that name
if (!empty($characterChoice))
$where[] = ['name', $characterChoice];
$highscore = HighscoreCache::where($where)->paginate(self::RESULTS_PER_PAGE);
return view('main/highscore', [
'highscore' => $highscore,
'empireChoice' => $empireChoice,
'classChoice' => $classChoice,
'characterChoice' => $characterChoice,
]);
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Models\Enums;
enum CharacterClassEnum: int
{
case WARRIOR = 0;
case NINJA = 1;
case SURA = 2;
case SHAMAN = 3;
public function name(): string
{
return match($this) {
self::WARRIOR => __('app/names.classes.warrior'),
self::NINJA => __('app/names.classes.ninja'),
self::SURA => __('app/names.classes.sura'),
self::SHAMAN => __('app/names.classes.shaman'),
};
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Models\Enums;
enum CharacterJobEnum: int
{
case WARRIOR_M = 0;
case NINJA_F = 1;
case SURA_M = 2;
case SHAMAN_F = 3;
case WARRIOR_F = 4;
case NINJA_M = 5;
case SURA_F = 6;
case SHAMAN_M = 7;
public function name(): string
{
return match($this) {
self::WARRIOR_M, self::WARRIOR_F => __('app/names.classes.warrior'),
self::NINJA_M, self::NINJA_F => __('app/names.classes.ninja'),
self::SURA_M, self::SURA_F => __('app/names.classes.sura'),
self::SHAMAN_M, self::SHAMAN_F => __('app/names.classes.shaman'),
};
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Models\Enums;
enum EmpireEnum: int
{
case SHINSOO = 1;
case CHUNJO = 2;
case JINNO = 3;
public function name(): string
{
return match($this) {
self::SHINSOO => __('app/names.empires.shinsoo'),
self::CHUNJO => __('app/names.empires.chunjo'),
self::JINNO => __('app/names.empires.jinno'),
};
}
public function longName(): string
{
return match($this) {
self::SHINSOO => __('app/names.empires.shinsoo.long'),
self::CHUNJO => __('app/names.empires.chunjo.long'),
self::JINNO => __('app/names.empires.jinno.long'),
};
}
}

View File

@ -0,0 +1,65 @@
<?php
namespace App\Models\Game\Highscore;
use App\Models\Enums\EmpireEnum;
use Illuminate\Database\Eloquent\Model;
class GuildHighscoreCache extends Model
{
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'website';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'guild_highscore_cache';
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'empire' => EmpireEnum::class,
'date' => 'datetime'
];
}

View File

@ -0,0 +1,67 @@
<?php
namespace App\Models\Game\Highscore;
use App\Models\Enums\CharacterClassEnum;
use App\Models\Enums\EmpireEnum;
use Illuminate\Database\Eloquent\Model;
class HighscoreCache extends Model
{
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'website';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'highscore_cache';
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'job' => CharacterClassEnum::class,
'empire' => EmpireEnum::class,
'date' => 'datetime'
];
}

View File

@ -2,6 +2,7 @@
namespace App\Providers;
use App\View\Composers\HighscoreComposer;
use App\View\Composers\MallComposer;
use App\View\Composers\ThemeComposer;
use Illuminate\Support\Facades;
@ -24,5 +25,6 @@ class ViewServiceProvider extends ServiceProvider
{
Facades\View::composer('layouts.app', ThemeComposer::class);
Facades\View::composer('layouts.mall', MallComposer::class);
Facades\View::composer('layouts.app', HighscoreComposer::class);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\View\Composers;
use App\Models\Game\Highscore\GuildHighscoreCache;
use App\Models\Game\Highscore\HighscoreCache;
use Illuminate\View\View;
class HighscoreComposer
{
/**
* Bind data to the view.
*/
public function compose(View $view): void
{
// Fetch the top highscore
$topHighscore = HighscoreCache::orderBy('id')->take(10)->get();
$view->with('topHighscore', $topHighscore);
// Fetch the top guild highscore
$topGuildHighscore = GuildHighscoreCache::orderBy('id')->take(10)->get();
$view->with('topGuildHighscore', $topGuildHighscore);
}
}