Files
sbnews/server/app/common/model/ai/AiUnlock.php
T
2026-06-11 12:15:29 +08:00

88 lines
2.4 KiB
PHP

<?php
namespace app\common\model\ai;
use app\common\model\BaseModel;
use app\common\model\user\User;
class AiUnlock extends BaseModel
{
/**
* 检查用户是否已解锁某场比赛
*/
public static function isUnlocked(int $userId, int $matchId): bool
{
return self::where('user_id', $userId)->where('match_id', $matchId)->count() > 0;
}
/**
* 解锁比赛(扣次数 + 写记录)
* @return array ['ok' => bool, 'msg' => string]
*/
public static function unlock(int $userId, int $matchId): array
{
// 已解锁则直接返回
if (self::isUnlocked($userId, $matchId)) {
return ['ok' => true, 'msg' => '已解锁'];
}
// 检查今日剩余次数
$user = User::findOrEmpty($userId);
if ($user->isEmpty()) {
return ['ok' => false, 'msg' => '用户不存在'];
}
// VIP用户不限次数
if ($user->is_vip && $user->vip_expire_time > time()) {
self::create([
'user_id' => $userId,
'match_id' => $matchId,
]);
return ['ok' => true, 'msg' => 'VIP解锁'];
}
// 重置每日次数(跨天时)
$today = date('Y-m-d');
if ($user->ai_free_date !== $today) {
$user->ai_free_count = 3;
$user->ai_free_date = $today;
$user->save();
}
if ($user->ai_free_count <= 0) {
return ['ok' => false, 'msg' => '今日免费次数已用完,升级VIP可无限使用'];
}
// 扣次数 + 写解锁记录
$user->ai_free_count = $user->ai_free_count - 1;
$user->save();
self::create([
'user_id' => $userId,
'match_id' => $matchId,
]);
return ['ok' => true, 'msg' => '解锁成功', 'remain' => $user->ai_free_count];
}
/**
* 获取用户今日剩余次数
*/
public static function getRemainCount(int $userId): int
{
$user = User::findOrEmpty($userId);
if ($user->isEmpty()) {
return 0;
}
// VIP不限
if ($user->is_vip && $user->vip_expire_time > time()) {
return 999;
}
$today = date('Y-m-d');
if ($user->ai_free_date !== $today) {
return 3;
}
return max(0, (int)$user->ai_free_count);
}
}