修复赛马线上卡在跑马中状态
This commit is contained in:
@@ -28,6 +28,12 @@ use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* 类功能:赛马竞猜前台控制器
|
||||
*
|
||||
* 负责聊天室赛马玩法的当前场次查询、下注提交、历史记录读取,
|
||||
* 并在发现线上遗留的超时 running 场次时执行最小范围的状态自愈。
|
||||
*/
|
||||
class HorseRaceController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
@@ -44,7 +50,7 @@ class HorseRaceController extends Controller
|
||||
return response()->json(['message' => '未登录', 'status' => 'error'], 401);
|
||||
}
|
||||
|
||||
$race = HorseRace::currentRace();
|
||||
$race = $this->resolveCurrentRaceState(HorseRace::currentRace());
|
||||
|
||||
if (! $race) {
|
||||
return response()->json([
|
||||
@@ -264,6 +270,119 @@ class HorseRaceController extends Controller
|
||||
return response()->json(['history' => $history]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自愈当前场次状态,避免线上遗漏结算时长期卡在 running。
|
||||
*/
|
||||
private function resolveCurrentRaceState(?HorseRace $race): ?HorseRace
|
||||
{
|
||||
if (! $race || $race->status !== 'running') {
|
||||
return $race;
|
||||
}
|
||||
|
||||
if (! $this->shouldRecoverStaleRunningRace($race)) {
|
||||
return $race;
|
||||
}
|
||||
|
||||
$race = $this->prepareRunningRaceForSettlement($race);
|
||||
if (! $race || $race->status !== 'running' || ! $race->winner_horse_id) {
|
||||
return $race;
|
||||
}
|
||||
|
||||
// 线上若漏消费 CloseHorseRaceJob,这里同步补做一次结算,避免界面一直显示“跑马中”。
|
||||
app()->call([new \App\Jobs\CloseHorseRaceJob($race), 'handle']);
|
||||
|
||||
return HorseRace::currentRace();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断 running 场次是否已经超过合理比赛时长,需要请求侧补偿收尾。
|
||||
*/
|
||||
private function shouldRecoverStaleRunningRace(HorseRace $race): bool
|
||||
{
|
||||
if (! $race->race_starts_at) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$config = GameConfig::forGame('horse_racing')?->params ?? [];
|
||||
$raceDuration = max(1, (int) ($config['race_duration'] ?? 30));
|
||||
$recoveryGraceSeconds = 5;
|
||||
|
||||
return $race->race_starts_at->lte(now()->subSeconds($raceDuration + $recoveryGraceSeconds));
|
||||
}
|
||||
|
||||
/**
|
||||
* 为超时 running 场次补齐缺失赛果字段,确保后续结算任务可以安全执行。
|
||||
*/
|
||||
private function prepareRunningRaceForSettlement(HorseRace $race): ?HorseRace
|
||||
{
|
||||
if ($race->winner_horse_id && $race->race_ends_at) {
|
||||
return $race->fresh();
|
||||
}
|
||||
|
||||
$horses = $this->normalizeRaceHorses($race->horses);
|
||||
$winnerHorseId = $race->winner_horse_id ?: $this->resolveStaleRunningWinnerId($race, $horses);
|
||||
if (! $winnerHorseId) {
|
||||
return $race;
|
||||
}
|
||||
|
||||
$config = GameConfig::forGame('horse_racing')?->params ?? [];
|
||||
$seedPool = (int) ($config['seed_pool'] ?? 0);
|
||||
|
||||
// 线上补偿场景下以当前下注快照补齐统计,确保本次请求内的结算口径与正常流程一致。
|
||||
$totalBets = HorseBet::query()->where('race_id', $race->id)->count();
|
||||
$totalPool = $seedPool + (int) HorseBet::query()->where('race_id', $race->id)->sum('amount');
|
||||
|
||||
HorseRace::query()
|
||||
->where('id', $race->id)
|
||||
->where('status', 'running')
|
||||
->update([
|
||||
'winner_horse_id' => $winnerHorseId,
|
||||
'race_ends_at' => $race->race_ends_at ?? now(),
|
||||
'total_bets' => $totalBets,
|
||||
'total_pool' => $totalPool,
|
||||
]);
|
||||
|
||||
return $race->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* 为异常滞留的 running 场次推导一个稳定冠军,避免多次请求得到不同结算结果。
|
||||
*
|
||||
* @param array<int, array{id:int,name:string,emoji:string}> $horses
|
||||
*/
|
||||
private function resolveStaleRunningWinnerId(HorseRace $race, array $horses): ?int
|
||||
{
|
||||
if ($horses === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$horsePools = HorseBet::query()
|
||||
->where('race_id', $race->id)
|
||||
->groupBy('horse_id')
|
||||
->selectRaw('horse_id, SUM(amount) as pool')
|
||||
->pluck('pool', 'horse_id')
|
||||
->map(fn ($pool) => (int) $pool)
|
||||
->toArray();
|
||||
|
||||
$candidateIds = array_map(
|
||||
fn (array $horse): int => (int) $horse['id'],
|
||||
$horses,
|
||||
);
|
||||
|
||||
usort($candidateIds, function (int $leftId, int $rightId) use ($horsePools): int {
|
||||
$leftPool = (int) ($horsePools[$leftId] ?? 0);
|
||||
$rightPool = (int) ($horsePools[$rightId] ?? 0);
|
||||
|
||||
if ($leftPool === $rightPool) {
|
||||
return $leftId <=> $rightId;
|
||||
}
|
||||
|
||||
return $rightPool <=> $leftPool;
|
||||
});
|
||||
|
||||
return $candidateIds[0] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容旧赛马数据结构,统一清洗为前端可消费的马匹数组。
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user