Skip to content
Permalink
Browse files

0.71.0 Move gbx from database to cache

  • Loading branch information...
brakerb committed Jul 23, 2019
1 parent 5b55b79 commit 24d2444102c683f33f4e1ed0ea6fcd2fd1416562
@@ -0,0 +1,42 @@
<?php
namespace esc\Migrations;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class UpdateMapsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(Builder $schemaBuilder)
{
$schemaBuilder->table('maps', function (Blueprint $table) {
$table->dropColumn('gbx');
$table->string('name')->nullable();
$table->string('environment')->nullable();
$table->string('title_id')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(Builder $schemaBuilder)
{
$schemaBuilder->table('maps', function (Blueprint $table) {
$table->text('gbx')->nullable();
$table->dropColumn('name');
$table->dropColumn('environment');
$table->dropColumn('title_id');
});
}
}
@@ -48,6 +48,10 @@ class MapController implements ControllerInterface
*/
public static function init()
{
if (!File::dirExists(cacheDir('gbx'))) {
File::makeDir(cacheDir('gbx'));
}
self::$mapsPath = Server::getMapsDirectory();
self::loadMaps();
@@ -310,7 +314,7 @@ public static function getGbxInformation($filename, bool $asString = true)
$gbx->PlayerModel = 'Unassigned';
$gbx->MapUid = $data->uid;
$gbx->Comment = $data->comment;
$gbx->TitleId = 'TMStadium'; //TODO: maybe support canyon
$gbx->TitleId = Server::getVersion()->titleId;
$gbx->AuthorLogin = $data->authorLogin;
$gbx->AuthorNick = $data->authorNick;
$gbx->Name = $data->name;
@@ -396,19 +400,31 @@ public static function loadMaps()
}
}
if (!$map->name) {
$map->update([
'name' => $gbx->Name
]);
}
if (!$map->environment) {
$map->update([
'environment' => $gbx->Environment
]);
}
if (!$map->title_id) {
$map->update([
'title_id' => $gbx->TitleId
]);
}
if (isVerbose()) {
printf("Loaded: %60s -> %s\n", $mapInfo->fileName, stripAll($map->gbx->Name));
printf("Loaded: %60s -> %s\n", $mapInfo->fileName, stripAll($map->name));
} else {
echo ".";
}
}
if (!$map->gbx) {
$map->update([
'gbx' => json_encode($gbx)
]);
}
echo "\n";
//get array with the uids
@@ -427,6 +443,7 @@ public static function loadMaps()
* Create map and retrieve object
*
* @param string $filename
* @param string $uid
* @param stdClass $gbx
* @return Map|null
* @throws \Throwable
@@ -457,7 +474,9 @@ private static function createMap(string $filename, string $uid, stdClass $gbx):
$map->author = $authorId;
$map->filename = $filename;
$map->enabled = false;
$map->gbx = json_encode($gbx);
$map->name = $gbx->Name;
$map->environment = $gbx->Environment;
$map->title_id = $gbx->TitleId;
$map->saveOrFail();
return $map;
@@ -218,7 +218,7 @@ public static function preCacheNextMap()
if (Server::getNextMapInfo()->uId != $firstMapInQueue->uid) {
Log::write('QueueController',
sprintf('Pre-caching map %s [%s]', $firstMapInQueue->gbx->Name, $firstMapInQueue->uid));
sprintf('Pre-caching map %s [%s]', $firstMapInQueue->name, $firstMapInQueue->uid));
Server::chooseNextMap($firstMapInQueue->filename);
}
} else {
@@ -3,27 +3,33 @@
namespace esc\Models;
use esc\Classes\Cache;
use esc\Classes\File;
use esc\Classes\Log;
use esc\Controllers\MapController;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use stdClass;
/**
* Class Map
*
* @package esc\Models
*
* @property string $uid
* @property string $filename
* @property string $uid
* @property string $filename
* @property boolean $plays
* @property string $author
* @property string $author
* @property boolean $enabled
* @property string $last_played
* @property string $mx_details
* @property string $mx_world_record
* @property string $cooldown
* @property string $last_played
* @property string $mx_details
* @property string $mx_world_record
* @property string $cooldown
* @property string $name
* @property string $environment
* @property string $title_id
*
*/
class Map extends Model
@@ -45,8 +51,10 @@ class Map extends Model
'enabled',
'mx_details',
'mx_world_record',
'gbx',
'cooldown'
'cooldown',
'name',
'environment',
'title_id',
];
/**
@@ -152,13 +160,20 @@ public function getMxWorldRecordAttribute($jsonMxWorldRecordDetails)
}
/**
* @param $gbxJson
*
* @return mixed
* @return stdClass
*/
public function getGbxAttribute($gbxJson)
public function getGbxAttribute()
{
return json_decode($gbxJson);
$cacheId = 'gbx/'.$this->uid;
if (Cache::has($cacheId)) {
return Cache::get($cacheId);
}
$gbx = MapController::getGbxInformation($this->filename, false);
Cache::put($cacheId, $gbx);
return $gbx;
}
/**
@@ -183,7 +198,7 @@ public function __toString()
$gbx = $this->gbx;
if (!$gbx) {
Log::write('Loading missing GBX for ' . $this->filename);
Log::write('Loading missing GBX for '.$this->filename);
$gbx = MapController::getGbxInformation($this->filename);
$this->gbx = $gbx;
$this->save();
@@ -195,25 +210,23 @@ public function __toString()
}
/**
* @param string $mapUid
* @param string $mapUid
*
* @return Map|null
*/
public static function getByUid(string $mapUid): ?Map
{
if (config('database.type') == 'mysql') {
return Map::where('gbx->MapUid', $mapUid)
->get()
->first();
} else {
return Map::all()->filter(function (Map $map) use ($mapUid) {
return $map->gbx->MapUid == $mapUid;
})->first();
foreach (Map::all() as $map) {
if ($map->gbx->MapUid == $mapUid) {
return $map;
}
}
return null;
}
/**
* @param string $mxId
* @param string $mxId
*
* @return Map|null
*/
@@ -162,9 +162,9 @@ static function getChallengeRecords(Map $map): ?Collection
self::paramAddStruct($params->addChild('param'), [
'UId' => $map->gbx->MapUid,
'Name' => str_replace('&', '', $map->gbx->Name),
'Name' => str_replace('&', '', $map->name),
'Environment' => $map->gbx->Environment,
'Author' => $map->gbx->AuthorLogin,
'Author' => $map->author->Login,
'NbCheckpoints' => $map->gbx->CheckpointsPerLaps,
'NbLaps' => $map->gbx->NbLaps,
]);
@@ -308,9 +308,9 @@ static function setChallengeTimes(Map $map)
//MapInfo: struct {'uid': string, 'Name': string, 'Environment': string, 'Author': string, 'NbCheckpoints': int, 'NbLaps': int} from GetCurrentChallengeInfo
self::paramAddStruct($params->addChild('param'), [
'UId' => $map->gbx->MapUid,
'Name' => $map->gbx->Name,
'Name' => $map->name,
'Environment' => $map->gbx->Environment,
'Author' => $map->gbx->AuthorLogin,
'Author' => $map->author->Login,
'NbCheckpoints' => $map->gbx->CheckpointsPerLaps,
'NbLaps' => $map->gbx->NbLaps,
]);
@@ -19,10 +19,10 @@ class MapList
{
public function __construct()
{
ManiaLinkEvent::add('maplist.disable', [MapList::class, 'disableMapEvent'], 'map_disable');
ManiaLinkEvent::add('maplist.delete', [MapList::class, 'deleteMapPermEvent'], 'map_delete');
ManiaLinkEvent::add('map.fav.add', [MapList::class, 'favAdd']);
ManiaLinkEvent::add('map.fav.remove', [MapList::class, 'favRemove']);
ManiaLinkEvent::add('maplist.disable', [self::class, 'disableMapEvent'], 'map_disable');
ManiaLinkEvent::add('maplist.delete', [self::class, 'deleteMapPermEvent'], 'map_delete');
ManiaLinkEvent::add('map.fav.add', [self::class, 'favAdd']);
ManiaLinkEvent::add('map.fav.remove', [self::class, 'favRemove']);
Hook::add('MapPoolUpdated', [self::class, 'sendUpdatedMaplist']);
Hook::add('MapQueueUpdated', [self::class, 'mapQueueUpdated']);
@@ -148,7 +148,7 @@ private static function getMapList(): Collection
return [
'id' => (string)$map->id,
'name' => $map->gbx->Name,
'name' => $map->name,
'a' => $map->author->id,
'r' => sprintf('%.1f', $map->average_rating),
'uid' => $map->gbx->MapUid,
@@ -72,13 +72,13 @@ public static function loadMxDetails(Map $map)
Log::write('Received: ' . $data, isVeryVerbose());
if ($data == '[]') {
Log::write('No MX information available for: ' . $map->gbx->Name);
Log::write('No MX information available for: ' . $map->name);
return;
}
$map->update(['mx_details' => $data]);
Log::write('Updated MX details for track: ' . $map->gbx->Name);
Log::write('Updated MX details for track: ' . $map->name);
self::loadMxWordlRecord($map);
}
@@ -95,6 +95,6 @@ public static function loadMxWordlRecord(Map $map)
$map->update(['mx_world_record' => $result->getBody()->getContents()]);
Log::write('Updated MX world record for track: ' . $map->gbx->Name);
Log::write('Updated MX world record for track: ' . $map->name);
}
}
@@ -13,7 +13,7 @@
<label textfont="{config('colors.ui.font')|noescape}" pos="+1 0" size="20 5" textsize="1" text="$l[https://tm.mania-exchange.com/tracks/{$map->mx_details->TrackID}]{$map->mx_details->TrackID} " valign="center" />

<label textfont="{config('colors.ui.font')|noescape}" pos="-1 -5" size="20 5" textprefix="$o$n" textsize="0.6" text="Name" halign="right" valign="center" />
<label textfont="{config('colors.ui.font')|noescape}" pos="+1 -5" size="20 5" textsize="1" text="{$map->gbx->Name}" valign="center" />
<label textfont="{config('colors.ui.font')|noescape}" pos="+1 -5" size="20 5" textsize="1" text="{$map->name}" valign="center" />

<label textfont="{config('colors.ui.font')|noescape}" pos="-1 -10" size="20 5" textprefix="$o$n" textsize="0.6" text="Author-Name" halign="right" valign="center" />
<label textfont="{config('colors.ui.font')|noescape}" pos="+1 -10" size="20 5" textsize="1" text="{$map->author->NickName}" valign="center" />
@@ -33,7 +33,7 @@

<frame pos="54 -10" scale="0.95">
<label textfont="{config('colors.ui.font')|noescape}" pos="-1 0" size="20 5" textprefix="$o$n" textsize="0.6" text="TitlePack" halign="right" valign="center" />
<label textfont="{config('colors.ui.font')|noescape}" pos="+1 0" size="20 5" textsize="1" text="{$map->gbx->TitleId}" valign="center" />
<label textfont="{config('colors.ui.font')|noescape}" pos="+1 0" size="20 5" textsize="1" text="{$map->title_id}" valign="center" />

<label textfont="{config('colors.ui.font')|noescape}" pos="-1 -5" size="20 5" textprefix="$o$n" textsize="0.6" text="Style" halign="right" valign="center" />
<label textfont="{config('colors.ui.font')|noescape}" pos="+1 -5" size="20 5" textsize="1" text="{$map->mx_details->StyleName}" valign="center" />
@@ -96,7 +96,7 @@ public static function addMap(Player $player, $mxId)
}
$map->uid = $mxMap->uid;
$map->author = self::getAuthorId($mxMap->gbx->AuthorLogin);
$map->author = self::getAuthorId($mxMap->author->Login);
$map->gbx = $mxMap->gbxString;
$map->mx_details = json_encode($mxMap->mxDetails);
$map->filename = $mxMap->getFilename();
@@ -290,8 +290,8 @@ public static function call(int $method, Map $map = null, array $votes = null):
'gamemode' => self::getGameMode(),
'titleid' => Server::getVersion()->titleId,
'mapuid' => $map->uid,
'mapname' => $map->gbx->Name,
'mapauthor' => $map->gbx->AuthorLogin,
'mapname' => $map->name,
'mapauthor' => $map->author->Login,
'isimport' => 'false',
'maptime' => CountdownController::getOriginalTimeLimit(),
'votes' => $votes,
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<manialink name="ESC:next-map-widget" id="next-map-widget" version="3">
<frame id="widget" pos="{config('map-list.pos.visible')}" data-pos-visible="{config('map-list.pos.visible')}" data-pos-hidden="{config('map-list.pos.hidden')}" scale="0.8" size="50 10" z-index="5000">
<label pos="38 -2" z-index="1" size="36 3" text="{$map->gbx->Name}" halign="right" textcolor="eeee" textsize="1.4" textfont="{config('colors.ui.font')|noescape}"/>
<label pos="38 -2" z-index="1" size="36 3" text="{$map->name}" halign="right" textcolor="eeee" textsize="1.4" textfont="{config('colors.ui.font')|noescape}"/>
<label pos="38 -7" z-index="1" size="36 3" text="{$map->author->NickName}" halign="right" textcolor="dddd" valign="center" textsize="0.6" textfont="{config('colors.ui.font')|noescape}"/>

<label pos="45 -5" valign="center" halign="center" textsize="0.4" size="6 6" text="NEXT" textcolor="{config('colors.ui.widget.text.primary')}" textfont="{config('colors.ui.font')|noescape}"/>
@@ -49,7 +49,7 @@ function buildClassMap()
{
global $classes;
$dirs = ['Interfaces', 'Classes', 'Controllers', 'Models', 'Modules', '..' . DIRECTORY_SEPARATOR . 'Migrations'];
$dirs = ['Interfaces', 'Classes', 'Commands', 'Controllers', 'Models', 'Modules', '..' . DIRECTORY_SEPARATOR . 'Migrations'];
foreach ($dirs as $dir) {
getClassesInDirectory($classes, __DIR__ . DIRECTORY_SEPARATOR . $dir);

0 comments on commit 24d2444

Please sign in to comment.
You can’t perform that action at this time.