Files
lotteryLaravel/app/Services/Draw/DrawTickService.php
kang e27a00f260 feat: 更新玩法配置管理,简化字段并增强功能
- 将玩法相关的显示名称字段统一为 `display_name`,移除多语言字段。
- 在 `PlayTypePatchController` 中新增即时切换玩法开关的功能,并推送大厅更新。
- 优化多个控制器和服务中的权限检查与数据处理逻辑,提升代码可读性与维护性。
2026-05-25 14:34:24 +08:00

177 lines
6.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Services\Draw;
use Carbon\Carbon;
use App\Models\Draw;
use App\Lottery\DrawStatus;
use App\Services\LotterySettings;
use App\Services\Settlement\SettlementOrchestrator;
use App\Services\Settlement\SettlementTickFinalizer;
/**
* 每分钟调度:期号状态推进 → RNG若到期号→ 冷静期结束时进入结算态 → 补齐未来缓冲。
*
* @see 《04-领域字典》draw_status
*/
final class DrawTickService
{
public function __construct(
private readonly DrawPlannerService $planner,
private readonly DrawRngRunner $rng,
private readonly DrawHallSnapshotBuilder $hallSnapshot,
private readonly LotteryHallRealtimeBroadcaster $hallRealtime,
private readonly SettlementOrchestrator $settlementOrchestrator,
private readonly SettlementTickFinalizer $settlementFinalizer,
) {}
/**
* @return array{
* status_updates: array<string, int>,
* settling_settled: int,
* settlement_finalized: array{approved: int, paid: int},
* rng_rung: int,
* rng_errors: array<int, string>,
* planned: array<string, int>
* }
*/
public function tick(?Carbon $now = null): array
{
$nowUtc = ($now ?? Carbon::now())->utc();
$hallFpBefore = $this->hallSnapshot->hallTargetFingerprint($nowUtc);
$statusUpdates = [
'pending_to_open_or_later' => $this->promoteStalePendingRows($nowUtc),
'open_to_closing_or_closed' => $this->openToClosingOrClosed($nowUtc),
'closing_to_closed' => $this->closingToClosed($nowUtc),
'cooldown_to_settling' => $this->cooldownToSettling($nowUtc),
];
$settlingSettled = $this->settleSettlingDraws();
$settlementFinalized = $this->settlementFinalizer->finalizePendingBatches();
$rngOutcome = $this->rng->runDue($nowUtc);
$planned = $this->planner->ensureBuffer($nowUtc);
$report = [
'status_updates' => $statusUpdates,
'settling_settled' => $settlingSettled,
'settlement_finalized' => $settlementFinalized,
'rng_rung' => $rngOutcome['rung'],
'rng_errors' => $rngOutcome['errors'],
'planned' => $planned,
];
$snapshotAfter = $this->hallSnapshot->build($nowUtc);
$hallFpAfter = $this->hallSnapshot->hallTargetFingerprint($nowUtc);
$this->hallRealtime->notifyStatusChangeIfHallDbChanged($hallFpBefore, $hallFpAfter, $snapshotAfter);
return $report;
}
/** 补偿迟到的调度pending 可依当前时刻落到 open / closing / closed。 */
private function promoteStalePendingRows(Carbon $nowUtc): int
{
$toClosed = Draw::query()
->where('status', DrawStatus::Pending->value)
->whereNotNull('draw_time')
->where('draw_time', '<=', $nowUtc)
->update(['status' => DrawStatus::Closed->value]);
$toClosing = Draw::query()
->where('status', DrawStatus::Pending->value)
->whereNotNull('close_time')
->whereNotNull('draw_time')
->where('close_time', '<=', $nowUtc)
->where('draw_time', '>', $nowUtc)
->update(['status' => DrawStatus::Closing->value]);
$toOpen = Draw::query()
->where('status', DrawStatus::Pending->value)
->whereNotNull('start_time')
->where('start_time', '<=', $nowUtc)
->where(function ($q) use ($nowUtc): void {
$q->whereNull('close_time')
->orWhere('close_time', '>', $nowUtc);
})
->update(['status' => DrawStatus::Open->value]);
return (int) $toClosed + (int) $toClosing + (int) $toOpen;
}
/** 先处理「已封盘且已越过开奖时刻」直达 closed再走正常封盘中。 */
private function openToClosingOrClosed(Carbon $nowUtc): int
{
$toClosed = Draw::query()
->where('status', DrawStatus::Open->value)
->whereNotNull('close_time')
->where('close_time', '<=', $nowUtc)
->whereNotNull('draw_time')
->where('draw_time', '<=', $nowUtc)
->update(['status' => DrawStatus::Closed->value]);
$toClosing = Draw::query()
->where('status', DrawStatus::Open->value)
->whereNotNull('close_time')
->where('close_time', '<=', $nowUtc)
->where(function ($q) use ($nowUtc): void {
$q->whereNull('draw_time')
->orWhere('draw_time', '>', $nowUtc);
})
->update(['status' => DrawStatus::Closing->value]);
return (int) $toClosed + (int) $toClosing;
}
private function closingToClosed(Carbon $nowUtc): int
{
return Draw::query()
->where('status', DrawStatus::Closing->value)
->whereNotNull('draw_time')
->where('draw_time', '<=', $nowUtc)
->update(['status' => DrawStatus::Closed->value]);
}
/** 冷静期结束 → settling结算/派彩由后续阶段补齐)。 */
private function cooldownToSettling(Carbon $nowUtc): int
{
return Draw::query()
->where('status', DrawStatus::Cooldown->value)
->whereNotNull('cooling_end_time')
->where('cooling_end_time', '<=', $nowUtc)
->update(['status' => DrawStatus::Settling->value]);
}
/**
* 冷静期结束后已进入 `settling` 的期号:执行阶段 6 结算(可经 lottery_settings 关闭自动跑批)。
*
* @return int 成功跑完结算的期号数量
*/
private function settleSettlingDraws(): int
{
if (! (bool) LotterySettings::get('settlement.auto_run_on_tick', true)) {
return 0;
}
$n = 0;
$ids = Draw::query()->where('status', DrawStatus::Settling->value)->pluck('id');
foreach ($ids as $drawId) {
$draw = Draw::query()->find($drawId);
if ($draw === null) {
continue;
}
try {
if ($this->settlementOrchestrator->trySettleDraw($draw)) {
$n++;
}
} catch (\Throwable $e) {
report($e);
}
}
return $n;
}
}