add: Item data service.
parent
2217592ebd
commit
9586c24f56
|
|
@ -37,7 +37,7 @@ class WikiController extends Controller
|
||||||
return ItemProtoSearchResource::collection(
|
return ItemProtoSearchResource::collection(
|
||||||
ItemProto::query()
|
ItemProto::query()
|
||||||
->select('locale_name', 'vnum')
|
->select('locale_name', 'vnum')
|
||||||
->when($request->query('query'), fn ($q, $term) => $q->where('locale_name', 'like', "%$term%"))
|
->when($request->query('query'), fn ($q, $term) => $q->where('locale_name', 'like', "%$term%")->orWhere('vnum', '=', $term))
|
||||||
->take(10)
|
->take(10)
|
||||||
->get()
|
->get()
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Http\Resources\Models;
|
namespace App\Http\Resources\Models;
|
||||||
|
|
||||||
|
use App\Services\ItemDataService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
|
@ -14,9 +15,13 @@ class ItemProtoSearchResource extends JsonResource
|
||||||
*/
|
*/
|
||||||
public function toArray(Request $request): array
|
public function toArray(Request $request): array
|
||||||
{
|
{
|
||||||
|
$staticData = app(ItemDataService::class);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'vnum' => $this->vnum,
|
'vnum' => $this->vnum,
|
||||||
'name' => $this->locale_name,
|
'name' => $this->locale_name,
|
||||||
|
'icon' => $staticData->getIcon($this->vnum),
|
||||||
|
'description' => $staticData->getDescription($this->vnum),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use App\Services\ItemDataService;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
|
|
@ -12,7 +13,7 @@ class AppServiceProvider extends ServiceProvider
|
||||||
*/
|
*/
|
||||||
public function register(): void
|
public function register(): void
|
||||||
{
|
{
|
||||||
//
|
$this->app->singleton(ItemDataService::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
|
class ItemDataService
|
||||||
|
{
|
||||||
|
protected static bool $loaded = false;
|
||||||
|
|
||||||
|
protected static ?array $icons = null;
|
||||||
|
|
||||||
|
protected static ?array $descriptions = null;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->loadData();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function loadData()
|
||||||
|
{
|
||||||
|
if (self::$loaded) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Storage::has('item-data/item_list.txt')) {
|
||||||
|
self::$icons = collect(explode("\n", Storage::get('item-data/item_list.txt')))
|
||||||
|
->map(fn ($line) => str_getcsv($line, "\t"))
|
||||||
|
->filter(fn ($item) => isset($item[0], $item[2]))
|
||||||
|
->mapWithKeys(fn ($item) => [intval($item[0]) => str($item[2])->basename()->replaceMatches('/tga|dds/i', 'png')])
|
||||||
|
->toArray();
|
||||||
|
} else {
|
||||||
|
self::$icons = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Storage::has('item-data/itemdesc.txt')) {
|
||||||
|
self::$descriptions = collect(explode("\n", Storage::get('item-data/itemdesc.txt')))
|
||||||
|
->map(fn ($line) => str_getcsv($line, "\t"))
|
||||||
|
->filter(fn ($item) => isset($item[0], $item[2]))
|
||||||
|
->mapWithKeys(fn ($item) => [intval($item[0]) => $item[2]])
|
||||||
|
->toArray();
|
||||||
|
} else {
|
||||||
|
self::$descriptions = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$loaded = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getIcon(int $vnum): ?string
|
||||||
|
{
|
||||||
|
$this->loadData();
|
||||||
|
|
||||||
|
return self::$icons[$vnum] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDescription(int $vnum): ?string
|
||||||
|
{
|
||||||
|
$this->loadData();
|
||||||
|
|
||||||
|
return self::$descriptions[$vnum] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function flush(): void
|
||||||
|
{
|
||||||
|
self::$loaded = false;
|
||||||
|
self::$icons = [];
|
||||||
|
self::$descriptions = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Services\ItemDataService;
|
||||||
use Laravel\Octane\Contracts\OperationTerminated;
|
use Laravel\Octane\Contracts\OperationTerminated;
|
||||||
use Laravel\Octane\Events\RequestHandled;
|
use Laravel\Octane\Events\RequestHandled;
|
||||||
use Laravel\Octane\Events\RequestReceived;
|
use Laravel\Octane\Events\RequestReceived;
|
||||||
|
|
@ -132,6 +133,7 @@ return [
|
||||||
|
|
||||||
'warm' => [
|
'warm' => [
|
||||||
...Octane::defaultServicesToWarm(),
|
...Octane::defaultServicesToWarm(),
|
||||||
|
ItemDataService::class,
|
||||||
],
|
],
|
||||||
|
|
||||||
'flush' => [
|
'flush' => [
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue