1.优化后台测试推送功能页面
2.优化开奖和实时对局页面
This commit is contained in:
@@ -18,6 +18,9 @@ class GameRecord extends Model
|
||||
'draw_mode' => 'integer',
|
||||
'preset_number' => 'integer',
|
||||
'result_number' => 'integer',
|
||||
'ai_locked_number' => 'integer',
|
||||
'pending_draw_number' => 'integer',
|
||||
'payout_until' => 'integer',
|
||||
'platform_profit_amount' => 'string',
|
||||
'winner_user_count' => 'integer',
|
||||
];
|
||||
|
||||
@@ -33,6 +33,9 @@ final class GameBetSettleService
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
/** @var array<int, array{period_no: string, total_win: string, balance_after: string, orders: list<array{order_no: string, win_amount: string, hit: bool}>}> */
|
||||
$aggregateByUser = [];
|
||||
|
||||
foreach ($bets as $bet) {
|
||||
$betId = (int) ($bet['id'] ?? 0);
|
||||
if ($betId <= 0) {
|
||||
@@ -59,11 +62,62 @@ final class GameBetSettleService
|
||||
// 结算刚刚成功(status 1 → 2):把本单下注总额 1:1 累加到用户打码量
|
||||
self::creditUserBetFlow($bet, $now);
|
||||
|
||||
if (bccomp($win, '0', 4) <= 0) {
|
||||
$userId = (int) ($bet['user_id'] ?? 0);
|
||||
if ($userId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
self::creditUserPayout($bet, $betId, $win, $now);
|
||||
$balanceAfter = (string) (Db::name('user')->where('id', $userId)->value('coin') ?? '0');
|
||||
if (bccomp($win, '0', 4) > 0) {
|
||||
$paid = self::creditUserPayout($bet, $betId, $win, $now);
|
||||
if ($paid !== null) {
|
||||
$balanceAfter = $paid;
|
||||
}
|
||||
}
|
||||
|
||||
$periodNo = (string) ($bet['period_no'] ?? '');
|
||||
if (!isset($aggregateByUser[$userId])) {
|
||||
$aggregateByUser[$userId] = [
|
||||
'period_no' => $periodNo,
|
||||
'total_win' => '0.0000',
|
||||
'balance_after' => $balanceAfter,
|
||||
'orders' => [],
|
||||
];
|
||||
}
|
||||
$aggregateByUser[$userId]['total_win'] = bcadd($aggregateByUser[$userId]['total_win'], $win, 4);
|
||||
$aggregateByUser[$userId]['balance_after'] = $balanceAfter;
|
||||
$aggregateByUser[$userId]['orders'][] = [
|
||||
'order_no' => (string) $betId,
|
||||
'win_amount' => $win,
|
||||
'hit' => bccomp($win, '0', 4) > 0,
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($aggregateByUser as $userId => $agg) {
|
||||
$hitOrderCount = 0;
|
||||
foreach ($agg['orders'] as $o) {
|
||||
if (($o['hit'] ?? false) === true) {
|
||||
$hitOrderCount++;
|
||||
}
|
||||
}
|
||||
UserPushService::publish((int) $userId, UserPushService::EVT_BET_SETTLED, [
|
||||
'period_no' => $agg['period_no'],
|
||||
'result_number' => $resultNumber,
|
||||
'total_win_amount' => $agg['total_win'],
|
||||
'order_count' => count($agg['orders']),
|
||||
'hit_order_count' => $hitOrderCount,
|
||||
'balance_after' => $agg['balance_after'],
|
||||
]);
|
||||
|
||||
if (bccomp($agg['total_win'], '0', 4) > 0) {
|
||||
UserPushService::publish((int) $userId, UserPushService::EVT_WALLET_CHANGED, [
|
||||
'reason' => 'payout',
|
||||
'ref_type' => 'game_period',
|
||||
'ref_id' => (string) $recordId,
|
||||
'delta' => $agg['total_win'],
|
||||
'balance_after' => $agg['balance_after'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,21 +215,26 @@ final class GameBetSettleService
|
||||
]);
|
||||
}
|
||||
|
||||
private static function creditUserPayout(array $bet, int $betId, string $winAmount, int $now): void
|
||||
/**
|
||||
* @return string|null 派彩后余额;已幂等入账过时返回当前余额;失败或未执行派彩返回 null
|
||||
*/
|
||||
private static function creditUserPayout(array $bet, int $betId, string $winAmount, int $now): ?string
|
||||
{
|
||||
$userId = (int) ($bet['user_id'] ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$idem = 'payout_bet_' . $betId;
|
||||
if (Db::name('user_wallet_record')->where('idempotency_key', $idem)->value('id')) {
|
||||
return;
|
||||
$coin = Db::name('user')->where('id', $userId)->value('coin');
|
||||
|
||||
return (string) ($coin ?? '0');
|
||||
}
|
||||
|
||||
$user = Db::name('user')->where('id', $userId)->find();
|
||||
if (!$user) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$before = (string) ($user['coin'] ?? '0');
|
||||
@@ -201,5 +260,7 @@ final class GameBetSettleService
|
||||
'coin' => $after,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
|
||||
return $after;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ final class GameLiveService
|
||||
private const EVT_PERIOD_TICK = 'period.tick';
|
||||
private const EVT_PERIOD_LOCKED = 'period.locked';
|
||||
private const EVT_PERIOD_OPENED = 'period.opened';
|
||||
private const EVT_PERIOD_PAYOUT = 'period.payout';
|
||||
private const KEY_PERIOD_SECONDS = 'period_seconds';
|
||||
private const KEY_BET_SECONDS = 'bet_seconds';
|
||||
private const KEY_PICK_MAX_NUMBER_COUNT = 'pick_max_number_count';
|
||||
@@ -26,26 +27,21 @@ final class GameLiveService
|
||||
/** 开奖结果号码池:1 至此上限(与单注可选号码个数配置无关) */
|
||||
private const DRAW_NUMBER_MAX = 36;
|
||||
|
||||
/** 开奖后派彩展示宽限期(秒),之后再创建下一期 */
|
||||
private const PAYOUT_GRACE_SECONDS = 3;
|
||||
|
||||
public static function buildSnapshot(?int $recordId = null): array
|
||||
{
|
||||
$record = self::resolveRecord($recordId);
|
||||
if (!$record) {
|
||||
return [
|
||||
'record' => null,
|
||||
'bets' => [],
|
||||
'candidate_numbers' => [],
|
||||
'ai_default_number' => null,
|
||||
'calc_number' => null,
|
||||
'period_seconds' => self::getConfigInt(self::KEY_PERIOD_SECONDS, 30),
|
||||
'bet_seconds' => self::getConfigInt(self::KEY_BET_SECONDS, 20),
|
||||
'pick_max_number_count' => self::getPickMaxNumberCount(),
|
||||
'draw_number_max' => self::DRAW_NUMBER_MAX,
|
||||
'remaining_seconds' => 0,
|
||||
'bet_remaining_seconds' => 0,
|
||||
'can_calculate' => false,
|
||||
'can_draw' => false,
|
||||
'server_time' => time(),
|
||||
];
|
||||
return self::emptySnapshotPayload();
|
||||
}
|
||||
|
||||
$rid = (int) $record['id'];
|
||||
self::ensureAiLocked($rid);
|
||||
$record = self::reloadRecord($rid);
|
||||
if (!$record) {
|
||||
return self::emptySnapshotPayload();
|
||||
}
|
||||
|
||||
$periodSeconds = self::getConfigInt(self::KEY_PERIOD_SECONDS, 30);
|
||||
@@ -54,19 +50,22 @@ final class GameLiveService
|
||||
$elapsed = max(0, time() - (int) $record['period_start_at']);
|
||||
$remaining = max(0, $periodSeconds - $elapsed);
|
||||
$betRemaining = max(0, $betSeconds - $elapsed);
|
||||
$status = (int) $record['status'];
|
||||
|
||||
$payoutUntil = isset($record['payout_until']) ? (int) $record['payout_until'] : 0;
|
||||
$payoutRemaining = 0;
|
||||
if ($status === 3 && $payoutUntil > 0) {
|
||||
$payoutRemaining = max(0, $payoutUntil - time());
|
||||
}
|
||||
|
||||
$bets = Db::name('bet_order')
|
||||
->where('period_id', (int) $record['id'])
|
||||
->where('period_id', $rid)
|
||||
->order('id', 'desc')
|
||||
->limit(200)
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$candidates = [];
|
||||
$bestNumber = null;
|
||||
$bestLoss = null;
|
||||
$bestNumbers = [];
|
||||
$status = (int) $record['status'];
|
||||
$canCalculate = $elapsed >= $betSeconds && ($status === 0 || $status === 1);
|
||||
if ($canCalculate) {
|
||||
for ($n = 1; $n <= self::DRAW_NUMBER_MAX; $n++) {
|
||||
@@ -75,18 +74,28 @@ final class GameLiveService
|
||||
'number' => $n,
|
||||
'estimated_loss' => $loss,
|
||||
];
|
||||
if ($bestLoss === null || bccomp((string) $loss, (string) $bestLoss, 4) < 0) {
|
||||
$bestLoss = $loss;
|
||||
$bestNumbers = [$n];
|
||||
continue;
|
||||
}
|
||||
if (bccomp((string) $loss, (string) $bestLoss, 4) === 0) {
|
||||
$bestNumbers[] = $n;
|
||||
}
|
||||
}
|
||||
$bestNumber = self::pickRandomNumber($bestNumbers);
|
||||
}
|
||||
|
||||
$aiLocked = $record['ai_locked_number'] ?? null;
|
||||
$aiDisplay = null;
|
||||
if ($aiLocked !== null && $aiLocked !== '' && is_numeric((string) $aiLocked)) {
|
||||
$aiDisplay = (int) $aiLocked;
|
||||
}
|
||||
|
||||
$pendingRaw = $record['pending_draw_number'] ?? null;
|
||||
$pendingDraw = null;
|
||||
if ($pendingRaw !== null && $pendingRaw !== '' && is_numeric((string) $pendingRaw)) {
|
||||
$pd = (int) $pendingRaw;
|
||||
if ($pd >= 1 && $pd <= self::DRAW_NUMBER_MAX) {
|
||||
$pendingDraw = $pd;
|
||||
}
|
||||
}
|
||||
|
||||
$canScheduleDraw = ($status === 0 || $status === 1)
|
||||
&& $elapsed >= $betSeconds
|
||||
&& $elapsed < $periodSeconds;
|
||||
|
||||
return [
|
||||
'record' => $record,
|
||||
'bets' => array_map(static function (array $row): array {
|
||||
@@ -101,16 +110,47 @@ final class GameLiveService
|
||||
];
|
||||
}, $bets),
|
||||
'candidate_numbers' => $candidates,
|
||||
'ai_default_number' => $bestNumber,
|
||||
'calc_number' => $bestNumber,
|
||||
'ai_default_number' => $aiDisplay,
|
||||
'calc_number' => $aiDisplay,
|
||||
'pending_draw_number' => $pendingDraw,
|
||||
'period_seconds' => $periodSeconds,
|
||||
'bet_seconds' => $betSeconds,
|
||||
'pick_max_number_count' => $pickMax,
|
||||
'draw_number_max' => self::DRAW_NUMBER_MAX,
|
||||
'remaining_seconds' => $remaining,
|
||||
'bet_remaining_seconds' => $betRemaining,
|
||||
'payout_remaining_seconds' => $payoutRemaining,
|
||||
'is_payout_phase' => $status === 3,
|
||||
'can_calculate' => $canCalculate,
|
||||
'can_draw' => $canCalculate,
|
||||
'can_draw' => $canScheduleDraw,
|
||||
'can_schedule_draw' => $canScheduleDraw,
|
||||
'server_time' => time(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private static function emptySnapshotPayload(): array
|
||||
{
|
||||
return [
|
||||
'record' => null,
|
||||
'bets' => [],
|
||||
'candidate_numbers' => [],
|
||||
'ai_default_number' => null,
|
||||
'calc_number' => null,
|
||||
'pending_draw_number' => null,
|
||||
'period_seconds' => self::getConfigInt(self::KEY_PERIOD_SECONDS, 30),
|
||||
'bet_seconds' => self::getConfigInt(self::KEY_BET_SECONDS, 20),
|
||||
'pick_max_number_count' => self::getPickMaxNumberCount(),
|
||||
'draw_number_max' => self::DRAW_NUMBER_MAX,
|
||||
'remaining_seconds' => 0,
|
||||
'bet_remaining_seconds' => 0,
|
||||
'payout_remaining_seconds' => 0,
|
||||
'is_payout_phase' => false,
|
||||
'can_calculate' => false,
|
||||
'can_draw' => false,
|
||||
'can_schedule_draw' => false,
|
||||
'server_time' => time(),
|
||||
];
|
||||
}
|
||||
@@ -130,12 +170,11 @@ final class GameLiveService
|
||||
if ($elapsed < $betSeconds) {
|
||||
return ['ok' => false, 'msg' => '下注开放时长未结束,暂不可计算'];
|
||||
}
|
||||
if ((int) $record['status'] === 0) {
|
||||
Db::name('game_record')->where('id', (int) $record['id'])->update([
|
||||
'status' => 1,
|
||||
'update_time' => time(),
|
||||
]);
|
||||
$record['status'] = 1;
|
||||
|
||||
self::ensureAiLocked((int) $record['id']);
|
||||
$record = self::reloadRecord((int) $record['id']);
|
||||
if (!$record) {
|
||||
return ['ok' => false, 'msg' => '未找到进行中的对局'];
|
||||
}
|
||||
|
||||
$pickMax = self::getPickMaxNumberCount();
|
||||
@@ -162,6 +201,12 @@ final class GameLiveService
|
||||
}
|
||||
$bestNumber = self::pickRandomNumber($bestNumbers);
|
||||
|
||||
$aiLocked = $record['ai_locked_number'] ?? null;
|
||||
$aiDisplay = null;
|
||||
if ($aiLocked !== null && $aiLocked !== '' && is_numeric((string) $aiLocked)) {
|
||||
$aiDisplay = (int) $aiLocked;
|
||||
}
|
||||
|
||||
$finalNumber = $manualNumber ?? $bestNumber;
|
||||
$finalLoss = '0.0000';
|
||||
if ($finalNumber !== null) {
|
||||
@@ -177,31 +222,122 @@ final class GameLiveService
|
||||
'pick_max_number_count' => $pickMax,
|
||||
'draw_number_max' => self::DRAW_NUMBER_MAX,
|
||||
'candidate_numbers' => $candidates,
|
||||
'ai_default_number' => $bestNumber,
|
||||
'ai_default_number' => $aiDisplay,
|
||||
'final_number' => $finalNumber,
|
||||
'final_estimated_loss' => $finalLoss,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员预约本期开奖号码(倒计时结束后由 tick 自动开奖,不立即开奖)。
|
||||
*/
|
||||
public static function scheduleDraw(?int $recordId, int $manualNumber): array
|
||||
{
|
||||
if ($manualNumber < 1 || $manualNumber > self::DRAW_NUMBER_MAX) {
|
||||
return ['ok' => false, 'msg' => '开奖号码超出允许范围'];
|
||||
}
|
||||
$record = self::resolveRecord($recordId);
|
||||
if (!$record) {
|
||||
return ['ok' => false, 'msg' => '未找到进行中的对局'];
|
||||
}
|
||||
if (!in_array((int) $record['status'], [0, 1], true)) {
|
||||
return ['ok' => false, 'msg' => '当前对局状态不可预约开奖'];
|
||||
}
|
||||
$periodSeconds = self::getConfigInt(self::KEY_PERIOD_SECONDS, 30);
|
||||
$betSeconds = self::getConfigInt(self::KEY_BET_SECONDS, 20);
|
||||
$elapsed = max(0, time() - (int) $record['period_start_at']);
|
||||
if ($elapsed < $betSeconds) {
|
||||
return ['ok' => false, 'msg' => '下注尚未结束,无法预约开奖'];
|
||||
}
|
||||
if ($elapsed >= $periodSeconds) {
|
||||
return ['ok' => false, 'msg' => '本期倒计时已结束,请刷新页面'];
|
||||
}
|
||||
|
||||
self::ensureAiLocked((int) $record['id']);
|
||||
Db::name('game_record')->where('id', (int) $record['id'])->update([
|
||||
'pending_draw_number' => $manualNumber,
|
||||
'update_time' => time(),
|
||||
]);
|
||||
self::publishSnapshot(null);
|
||||
|
||||
return [
|
||||
'ok' => true,
|
||||
'msg' => '已预约本期开奖号码,倒计时结束后将使用该号码开奖',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 倒计时结束自动开奖(AI 或预约号码);派彩宽限期后由 finalizePayoutGrace 结单并开下一期。
|
||||
*/
|
||||
public static function drawResult(?int $recordId, ?int $manualNumber = null): array
|
||||
{
|
||||
$calc = self::calculateResult($recordId, $manualNumber);
|
||||
if (!($calc['ok'] ?? false)) {
|
||||
return $calc;
|
||||
$record = self::resolveRecord($recordId);
|
||||
if (!$record) {
|
||||
return ['ok' => false, 'msg' => '未找到进行中的对局'];
|
||||
}
|
||||
$record = $calc['record'];
|
||||
$finalNumber = (int) $calc['final_number'];
|
||||
if (!in_array((int) $record['status'], [0, 1], true)) {
|
||||
return ['ok' => false, 'msg' => '当前对局状态不可开奖'];
|
||||
}
|
||||
$periodSeconds = self::getConfigInt(self::KEY_PERIOD_SECONDS, 30);
|
||||
$betSeconds = self::getConfigInt(self::KEY_BET_SECONDS, 20);
|
||||
$elapsed = max(0, time() - (int) $record['period_start_at']);
|
||||
if ($elapsed < $betSeconds) {
|
||||
return ['ok' => false, 'msg' => '下注开放时长未结束,不可开奖'];
|
||||
}
|
||||
if ($elapsed < $periodSeconds) {
|
||||
return ['ok' => false, 'msg' => '本期倒计时未结束,无法开奖'];
|
||||
}
|
||||
|
||||
self::ensureAiLocked((int) $record['id']);
|
||||
$record = self::reloadRecord((int) $record['id']);
|
||||
if (!$record) {
|
||||
return ['ok' => false, 'msg' => '未找到进行中的对局'];
|
||||
}
|
||||
|
||||
$useManual = $manualNumber;
|
||||
if ($useManual === null) {
|
||||
$p = $record['pending_draw_number'] ?? null;
|
||||
if ($p !== null && $p !== '' && is_numeric((string) $p)) {
|
||||
$pn = (int) $p;
|
||||
if ($pn >= 1 && $pn <= self::DRAW_NUMBER_MAX) {
|
||||
$useManual = $pn;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$finalNumber = null;
|
||||
$drawMode = 0;
|
||||
if ($useManual !== null && $useManual >= 1 && $useManual <= self::DRAW_NUMBER_MAX) {
|
||||
$finalNumber = $useManual;
|
||||
$drawMode = 1;
|
||||
} else {
|
||||
$al = $record['ai_locked_number'] ?? null;
|
||||
if ($al !== null && $al !== '' && is_numeric((string) $al)) {
|
||||
$finalNumber = (int) $al;
|
||||
}
|
||||
}
|
||||
if ($finalNumber === null || $finalNumber < 1) {
|
||||
$bets = Db::name('bet_order')->where('period_id', (int) $record['id'])->select()->toArray();
|
||||
$finalNumber = self::computeBestNumberFromBets($bets) ?? 1;
|
||||
$drawMode = 0;
|
||||
}
|
||||
|
||||
$bets = Db::name('bet_order')->where('period_id', (int) $record['id'])->select()->toArray();
|
||||
$finalLoss = self::estimateLossForNumber($bets, $finalNumber);
|
||||
$now = time();
|
||||
$payoutUntil = $now + self::PAYOUT_GRACE_SECONDS;
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
Db::name('game_record')->where('id', (int) $record['id'])->update([
|
||||
'status' => 4,
|
||||
'status' => 3,
|
||||
'result_number' => $finalNumber,
|
||||
'draw_mode' => $manualNumber === null ? 0 : 1,
|
||||
'draw_mode' => $drawMode,
|
||||
'pending_draw_number' => null,
|
||||
'payout_until' => $payoutUntil,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
GameBetSettleService::settleBetsForDraw((int) $record['id'], $finalNumber);
|
||||
GameRecordService::createNextRecordAfterDraw();
|
||||
Db::commit();
|
||||
GameRecordStatService::refreshForRecordId((int) $record['id']);
|
||||
} catch (Throwable $e) {
|
||||
@@ -210,16 +346,50 @@ final class GameLiveService
|
||||
}
|
||||
|
||||
self::publishPublicPeriodOpened((string) $record['period_no'], $finalNumber, $now);
|
||||
|
||||
self::publishPublicPeriodPayout((string) $record['period_no'], $finalNumber, $payoutUntil);
|
||||
self::publishSnapshot(null);
|
||||
|
||||
return [
|
||||
'ok' => true,
|
||||
'msg' => '开奖完成',
|
||||
'msg' => '开奖完成,派彩中',
|
||||
'result_number' => $finalNumber,
|
||||
'estimated_loss' => $calc['final_estimated_loss'],
|
||||
'estimated_loss' => $finalLoss,
|
||||
'payout_until' => $payoutUntil,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 派彩宽限期结束:将本期置为已结束并创建下一期。
|
||||
*/
|
||||
public static function finalizePayoutGrace(): void
|
||||
{
|
||||
$row = Db::name('game_record')
|
||||
->where('status', 3)
|
||||
->where('payout_until', '>', 0)
|
||||
->where('payout_until', '<=', time())
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
if (!$row) {
|
||||
return;
|
||||
}
|
||||
$id = (int) $row['id'];
|
||||
Db::startTrans();
|
||||
try {
|
||||
Db::name('game_record')->where('id', $id)->update([
|
||||
'status' => 4,
|
||||
'payout_until' => null,
|
||||
'update_time' => time(),
|
||||
]);
|
||||
GameRecordService::createNextRecordAfterDraw();
|
||||
Db::commit();
|
||||
} catch (Throwable) {
|
||||
Db::rollback();
|
||||
return;
|
||||
}
|
||||
GameRecordStatService::refreshForRecordId($id);
|
||||
self::publishSnapshot(null);
|
||||
}
|
||||
|
||||
public static function tickAutoDraw(): void
|
||||
{
|
||||
$record = self::resolveRecord(null);
|
||||
@@ -229,14 +399,12 @@ final class GameLiveService
|
||||
$betSeconds = self::getConfigInt(self::KEY_BET_SECONDS, 20);
|
||||
$periodSeconds = self::getConfigInt(self::KEY_PERIOD_SECONDS, 30);
|
||||
$elapsed = max(0, time() - (int) $record['period_start_at']);
|
||||
if ($elapsed >= $betSeconds && (int) $record['status'] === 0) {
|
||||
Db::name('game_record')->where('id', (int) $record['id'])->update([
|
||||
'status' => 1,
|
||||
'update_time' => time(),
|
||||
]);
|
||||
$record['status'] = 1;
|
||||
self::publishPublicPeriodLocked($record);
|
||||
self::ensureAiLocked((int) $record['id']);
|
||||
$record = self::reloadRecord((int) $record['id']);
|
||||
if (!$record) {
|
||||
return;
|
||||
}
|
||||
$elapsed = max(0, time() - (int) $record['period_start_at']);
|
||||
if ($elapsed < $periodSeconds) {
|
||||
return;
|
||||
}
|
||||
@@ -272,6 +440,8 @@ final class GameLiveService
|
||||
$serverTime = (int) ($snapshot['server_time'] ?? time());
|
||||
$remaining = (int) ($snapshot['remaining_seconds'] ?? 0);
|
||||
$betCloseIn = (int) ($snapshot['bet_remaining_seconds'] ?? 0);
|
||||
$payoutRem = (int) ($snapshot['payout_remaining_seconds'] ?? 0);
|
||||
$isPayout = !empty($snapshot['is_payout_phase']);
|
||||
$periodNo = '';
|
||||
$dbStatus = 0;
|
||||
$resultNumber = null;
|
||||
@@ -292,6 +462,9 @@ final class GameLiveService
|
||||
'status' => $status,
|
||||
'countdown' => $remaining,
|
||||
'bet_close_in'=> $betCloseIn,
|
||||
'payout_remaining_seconds' => $payoutRem,
|
||||
'is_payout_phase' => $isPayout,
|
||||
'payout_message' => $isPayout ? '派彩中,请稍候' : '',
|
||||
];
|
||||
if ($periodNo !== '' && $record !== null) {
|
||||
$start = (int) ($record['period_start_at'] ?? 0);
|
||||
@@ -341,6 +514,24 @@ final class GameLiveService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 派彩阶段开始(开奖后宽限期内推送)
|
||||
*/
|
||||
private static function publishPublicPeriodPayout(string $periodNo, int $resultNumber, int $payoutUntil): void
|
||||
{
|
||||
try {
|
||||
$payload = [
|
||||
'period_no' => $periodNo,
|
||||
'result_number' => $resultNumber,
|
||||
'payout_until' => $payoutUntil,
|
||||
'message' => '派彩中,请稍候',
|
||||
];
|
||||
$api = self::createPushApi();
|
||||
$api->trigger(self::CHANNEL_PUBLIC_GAME_PERIOD, self::EVT_PERIOD_PAYOUT, $payload);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 与文档 3.1/4.1 中 status 字符串对齐:betting / locked / settling / finished
|
||||
*/
|
||||
@@ -355,7 +546,10 @@ final class GameLiveService
|
||||
if ($dbStatus === 4) {
|
||||
return 'finished';
|
||||
}
|
||||
if ($dbStatus === 2 || $dbStatus === 3) {
|
||||
if ($dbStatus === 3) {
|
||||
return 'payouting';
|
||||
}
|
||||
if ($dbStatus === 2) {
|
||||
return 'settling';
|
||||
}
|
||||
|
||||
@@ -373,6 +567,84 @@ final class GameLiveService
|
||||
return Db::name('game_record')->whereIn('status', [0, 1, 2, 3])->order('id', 'desc')->find();
|
||||
}
|
||||
|
||||
private static function reloadRecord(int $id): ?array
|
||||
{
|
||||
$row = Db::name('game_record')->where('id', $id)->find();
|
||||
return $row ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 封盘后计算并锁定 AI 号码(本期不变),并封盘(status 0→1)。
|
||||
*/
|
||||
private static function ensureAiLocked(int $recordId): void
|
||||
{
|
||||
$record = Db::name('game_record')->where('id', $recordId)->find();
|
||||
if (!$record) {
|
||||
return;
|
||||
}
|
||||
$betSeconds = self::getConfigInt(self::KEY_BET_SECONDS, 20);
|
||||
$elapsed = max(0, time() - (int) $record['period_start_at']);
|
||||
if ($elapsed < $betSeconds) {
|
||||
return;
|
||||
}
|
||||
$st = (int) $record['status'];
|
||||
if ($st !== 0 && $st !== 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$existing = $record['ai_locked_number'] ?? null;
|
||||
if ($existing !== null && $existing !== '' && is_numeric((string) $existing) && (int) $existing > 0) {
|
||||
if ($st === 0) {
|
||||
Db::name('game_record')->where('id', $recordId)->update([
|
||||
'status' => 1,
|
||||
'update_time' => time(),
|
||||
]);
|
||||
$record['status'] = 1;
|
||||
self::publishPublicPeriodLocked($record);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
$bets = Db::name('bet_order')->where('period_id', $recordId)->select()->toArray();
|
||||
$best = self::computeBestNumberFromBets($bets);
|
||||
if ($best === null || $best < 1) {
|
||||
$best = 1;
|
||||
}
|
||||
$update = [
|
||||
'ai_locked_number' => $best,
|
||||
'update_time' => time(),
|
||||
];
|
||||
if ($st === 0) {
|
||||
$update['status'] = 1;
|
||||
}
|
||||
Db::name('game_record')->where('id', $recordId)->update($update);
|
||||
$record = array_merge($record, $update);
|
||||
if ($st === 0) {
|
||||
self::publishPublicPeriodLocked($record);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $bets
|
||||
*/
|
||||
private static function computeBestNumberFromBets(array $bets): ?int
|
||||
{
|
||||
$bestLoss = null;
|
||||
$bestNumbers = [];
|
||||
for ($n = 1; $n <= self::DRAW_NUMBER_MAX; $n++) {
|
||||
$loss = self::estimateLossForNumber($bets, $n);
|
||||
if ($bestLoss === null || bccomp((string) $loss, (string) $bestLoss, 4) < 0) {
|
||||
$bestLoss = $loss;
|
||||
$bestNumbers = [$n];
|
||||
continue;
|
||||
}
|
||||
if (bccomp((string) $loss, (string) $bestLoss, 4) === 0) {
|
||||
$bestNumbers[] = $n;
|
||||
}
|
||||
}
|
||||
return self::pickRandomNumber($bestNumbers);
|
||||
}
|
||||
|
||||
private static function getConfigInt(string $key, int $default): int
|
||||
{
|
||||
$row = Db::name('game_config')->where('config_key', $key)->find();
|
||||
|
||||
61
app/common/service/UserPushService.php
Normal file
61
app/common/service/UserPushService.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use support\think\Db;
|
||||
use Throwable;
|
||||
use Webman\Push\Api;
|
||||
|
||||
/**
|
||||
* 用户私有频道推送:private-user-{uuid}(与移动端接口设计草案 7.1 一致)
|
||||
*/
|
||||
final class UserPushService
|
||||
{
|
||||
public const EVT_BET_ACCEPTED = 'bet.accepted';
|
||||
|
||||
/** 单注开奖结果(含未中奖 win_amount=0) */
|
||||
public const EVT_BET_SETTLED = 'bet.settled';
|
||||
|
||||
public const EVT_WALLET_CHANGED = 'wallet.changed';
|
||||
|
||||
private static function channelName(string $uuid): string
|
||||
{
|
||||
return 'private-user-' . $uuid;
|
||||
}
|
||||
|
||||
private static function createApi(): Api
|
||||
{
|
||||
return new Api(
|
||||
str_replace('0.0.0.0', '127.0.0.1', (string) config('plugin.webman.push.app.api')),
|
||||
(string) config('plugin.webman.push.app.app_key'),
|
||||
(string) config('plugin.webman.push.app.app_secret')
|
||||
);
|
||||
}
|
||||
|
||||
public static function uuidForUserId(int $userId): ?string
|
||||
{
|
||||
if ($userId <= 0) {
|
||||
return null;
|
||||
}
|
||||
$u = Db::name('user')->where('id', $userId)->value('uuid');
|
||||
|
||||
return is_string($u) && $u !== '' ? $u : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public static function publish(int $userId, string $event, array $data): void
|
||||
{
|
||||
$uuid = self::uuidForUserId($userId);
|
||||
if ($uuid === null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
self::createApi()->trigger(self::channelName($uuid), $event, $data);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user