Skip to content

Commit

Permalink
自动刷新祈愿记录
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Jan 13, 2024
1 parent 854a13c commit d629233
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/Constants/ErrorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ enum ErrorCode: int implements ErrorCodeInterface
*/
case CONTENT_CANNOT_SAVE_CAUSED_BY_SHARE = 1201;

/**
* @Message("原神信息不存在")
*/
case YS_USER_NOT_EXIST = 1300;

public function getMessage(array $translate = null): string
{
$arguments = [];
Expand Down
45 changes: 45 additions & 0 deletions app/Constants/GachaType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace App\Constants;

enum GachaType: int
{
/**
* 新手池.
*/
case NEW_USER = 100;

/**
* 常驻池.
*/
case NORMAL = 200;

/**
* UP 角色池.
*/
case ROLE = 301;

/**
* 武器池.
*/
case WEAPON = 302;

public static function enums(): array
{
return [
self::NORMAL,
self::ROLE,
self::WEAPON,
];
}
}
5 changes: 5 additions & 0 deletions app/Model/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public function getContent()
$secret = UserAuth::instance()->build()->getSecret();
return di()->get(Encrypter::class)->decrypt($this->content, $secret);
}

public function isYuanShen(): bool
{
return $this->type === ContentType::YUAN_SHEN;
}
}
2 changes: 2 additions & 0 deletions app/Model/YsGachaLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
class YsGachaLog extends Model
{
public bool $incrementing = false;

/**
* The table associated with the model.
*/
Expand Down
4 changes: 4 additions & 0 deletions app/Service/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public function save(
throw $throwable;
}

if ($user) {
di()->get(YsGachaLogService::class)->load($user->id);
}

return true;
}
}
12 changes: 12 additions & 0 deletions app/Service/Dao/YsUserDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@

namespace App\Service\Dao;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\YsUser;
use Han\Utils\Service;

class YsUserDao extends Service
{
public function first(int $id, bool $throw = false): ?YsUser
{
$model = YsUser::findFromCache($id);
if (! $model && $throw) {
throw new BusinessException(ErrorCode::YS_USER_NOT_EXIST);
}

return $model;
}

public function firstByContentId(int $contentId): ?YsUser
{
return YsUser::query()->where('content_id', $contentId)->first();
Expand Down
68 changes: 68 additions & 0 deletions app/Service/YsGachaLogService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace App\Service;

use App\Constants\GachaType;
use App\Model\YsGachaLog;
use App\Service\Dao\YsUserDao;
use App\Service\SubService\Mihoyo;
use Han\Utils\Service;
use Hyperf\AsyncQueue\Annotation\AsyncQueueMessage;
use Hyperf\Redis\Redis;
use Throwable;

class YsGachaLogService extends Service
{
#[AsyncQueueMessage]
public function load(int $userId): void
{
$key = 'load:gacha:' . $userId;
if (di()->get(Redis::class)->set($key, '1', ['EX' => 3600, 'NX'])) {
try {
$user = di()->get(YsUserDao::class)->first($userId);
if (! $user) {
return;
}

foreach (GachaType::enums() as $type) {
$id = 0;
while (true) {
$result = di()->get(Mihoyo::class)->getGachaLog($user->auth_key, $type, $id);
if (empty($result['list'])) {
break;
}
foreach ($result['list'] as $re) {
$id = $re['id'];

$model = new YsGachaLog();
$model->id = $re['id'];
$model->uid = $re['uid'];
$model->gacha_type = $re['gacha_type'];
$model->time = $re['time'];
$model->name = $re['name'];
$model->item_type = $re['item_type'];
$model->rank_type = $re['rank_type'];
try {
$model->save();
} catch (Throwable) {
break 2;
}
}
}
}
} finally {
di()->get(Redis::class)->del($key);
}
}
}
}

0 comments on commit d629233

Please sign in to comment.