add file cache for non-existent players
This commit is contained in:
parent
9f02cf26c1
commit
fffb8fd441
34
app/Events/CheckPlayerExists.php
Normal file
34
app/Events/CheckPlayerExists.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Events\Event;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class CheckPlayerExists extends Event
|
||||
{
|
||||
use SerializesModels;
|
||||
|
||||
public $player_name;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($player_name)
|
||||
{
|
||||
$this->player_name = $player_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should be broadcast on.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function broadcastOn()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
35
app/Events/PlayerWasAdded.php
Normal file
35
app/Events/PlayerWasAdded.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\PlayerModel;
|
||||
use App\Events\Event;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class PlayerWasAdded extends Event
|
||||
{
|
||||
use SerializesModels;
|
||||
|
||||
public $player;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(PlayerModel $player)
|
||||
{
|
||||
$this->player = $player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should be broadcast on.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function broadcastOn()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
@ -3,12 +3,14 @@
|
|||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
use App\Events\PlayerWasAdded;
|
||||
use App\Models\User;
|
||||
use App\Models\Player;
|
||||
use App\Models\PlayerModel;
|
||||
use App\Models\Texture;
|
||||
use App\Exceptions\PrettyPageException;
|
||||
use Validate;
|
||||
use Event;
|
||||
use Utils;
|
||||
use Option;
|
||||
use View;
|
||||
|
|
@ -61,6 +63,8 @@ class PlayerController extends BaseController
|
|||
$player->last_modified = Utils::getTimeFormatted();
|
||||
$player->save();
|
||||
|
||||
Event::fire(new PlayerWasAdded($player));
|
||||
|
||||
$this->user->setScore(Option::get('score_per_player'), 'minus');
|
||||
|
||||
View::json('成功添加了角色 '.$player_name.'', 0);
|
||||
|
|
@ -98,12 +102,10 @@ class PlayerController extends BaseController
|
|||
if (!PlayerModel::where('player_name', $new_player_name)->get()->isEmpty())
|
||||
View::json('此角色名已被他人使用,换一个吧~', 6);
|
||||
|
||||
$old_player_name = $this->player->model->player_name;
|
||||
$this->player->model->player_name = $new_player_name;
|
||||
$this->player->model->last_modified = Utils::getTimeFormatted();
|
||||
$this->player->model->save();
|
||||
$old_player_name = $this->player_name;
|
||||
$this->player->rename($new_player_name);
|
||||
|
||||
View::json('角色 '.$old_player_name.' 已更名为 '.$_POST['new_player_name'], 0);
|
||||
View::json("角色 $old_player_name 已更名为 $new_player_name", 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\PlayerModel;
|
||||
use App\Events\CheckPlayerExists;
|
||||
use Event;
|
||||
|
||||
class CheckPlayerExistMiddleware
|
||||
{
|
||||
|
|
@ -16,6 +18,8 @@ class CheckPlayerExistMiddleware
|
|||
|
||||
$player_name = urldecode($matches[1]);
|
||||
|
||||
Event::fire(new CheckPlayerExists($player_name));
|
||||
|
||||
if (PlayerModel::where('player_name', $player_name)->get()->isEmpty()) {
|
||||
abort(404, '角色不存在');
|
||||
}
|
||||
|
|
|
|||
40
app/Listeners/CachePlayerExists.php
Normal file
40
app/Listeners/CachePlayerExists.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Models\PlayerModel;
|
||||
use App\Events\CheckPlayerExists;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class CachePlayerExists
|
||||
{
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param CheckPlayerExists $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(CheckPlayerExists $event)
|
||||
{
|
||||
$player_name = $event->player_name;
|
||||
|
||||
if (!\Storage::disk('cache')->has("notfound/$player_name")) {
|
||||
if (PlayerModel::where('player_name', $player_name)->get()->isEmpty()) {
|
||||
\Storage::disk('cache')->put("notfound/$player_name", '');
|
||||
}
|
||||
} else {
|
||||
abort(404, '角色不存在');
|
||||
}
|
||||
}
|
||||
}
|
||||
35
app/Listeners/FreshNotFoundCache.php
Normal file
35
app/Listeners/FreshNotFoundCache.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\PlayerWasAdded;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class FreshNotFoundCache
|
||||
{
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param PlayerWasAdded $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(PlayerWasAdded $event)
|
||||
{
|
||||
$player_name = $event->player->player_name;
|
||||
|
||||
if (\Storage::disk('cache')->has("notfound/$player_name")) {
|
||||
\Storage::disk('cache')->delete("notfound/$player_name", '');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -126,6 +126,18 @@ class Player
|
|||
return $this->model['preference'];
|
||||
}
|
||||
|
||||
public function rename($new_name)
|
||||
{
|
||||
$this->model->update([
|
||||
'player_name' => $new_name,
|
||||
'last_modified' => Utils::getTimeFormatted()
|
||||
]);
|
||||
|
||||
$this->player_name = $new_name;
|
||||
|
||||
return Event::fire(new PlayerProfileUpdated($this));
|
||||
}
|
||||
|
||||
public function setOwner($uid) {
|
||||
$this->model->update(['uid' => $uid]);
|
||||
|
||||
|
|
@ -193,7 +205,7 @@ class PlayerModel extends \Illuminate\Database\Eloquent\Model
|
|||
protected $table = 'players';
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = ['preference', 'last_modified'];
|
||||
protected $fillable = ['player_name', 'preference', 'last_modified'];
|
||||
|
||||
public function scopeLike($query, $field, $value)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -25,6 +25,12 @@ class EventServiceProvider extends ServiceProvider
|
|||
'App\Events\PlayerProfileUpdated' => [
|
||||
'App\Listeners\FreshPlayerJson',
|
||||
],
|
||||
'App\Events\CheckPlayerExists' => [
|
||||
'App\Listeners\CachePlayerExists',
|
||||
],
|
||||
'App\Events\PlayerWasAdded' => [
|
||||
'App\Listeners\FreshNotFoundCache',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
|||
3
storage/cache/notfound/.gitignore
vendored
Normal file
3
storage/cache/notfound/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
*
|
||||
!public/
|
||||
!.gitignore
|
||||
Loading…
Reference in New Issue
Block a user