mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Перенесена логика join операции для современных серверов.
Нужно признать, что перенесена она так себе, но в будущем я обязательно это перепишу.
This commit is contained in:
57
api/modules/session/models/SessionModel.php
Normal file
57
api/modules/session/models/SessionModel.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
namespace api\modules\session\models;
|
||||
|
||||
use Yii;
|
||||
|
||||
class SessionModel {
|
||||
|
||||
const KEY_TIME = 120; // 2 min
|
||||
|
||||
public $username;
|
||||
|
||||
public $serverId;
|
||||
|
||||
public function __construct(string $username, string $serverId) {
|
||||
$this->username = $username;
|
||||
$this->serverId = $serverId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $username
|
||||
* @param $serverId
|
||||
*
|
||||
* @return static|null
|
||||
*/
|
||||
public static function find($username, $serverId) {
|
||||
$key = static::buildKey($username, $serverId);
|
||||
$result = Yii::$app->redis->executeCommand('GET', [$key]);
|
||||
if (!$result) {
|
||||
/** @noinspection PhpIncompatibleReturnTypeInspection шторм что-то сума сходит, когда видит static */
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = json_decode($result, true);
|
||||
$model = new static($data['username'], $data['serverId']);
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function save() {
|
||||
$key = static::buildKey($this->username, $this->serverId);
|
||||
$data = json_encode([
|
||||
'username' => $this->username,
|
||||
'serverId' => $this->serverId,
|
||||
]);
|
||||
|
||||
return Yii::$app->redis->executeCommand('SETEX', [$key, self::KEY_TIME, $data]);
|
||||
}
|
||||
|
||||
public function delete() {
|
||||
return Yii::$app->redis->executeCommand('DEL', [static::buildKey($this->username, $this->serverId)]);
|
||||
}
|
||||
|
||||
protected static function buildKey($username, $serverId) {
|
||||
return md5('minecraft:join-server:' . mb_strtolower($username) . ':' . $serverId);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user