1.重构实时消息WebSocket连接
2.MySQL备份
This commit is contained in:
@@ -10,7 +10,6 @@ use app\common\service\GameHotDataCoordinator;
|
||||
use app\common\service\GameHotDataLock;
|
||||
use support\think\Db;
|
||||
use Throwable;
|
||||
use Webman\Push\Api;
|
||||
|
||||
final class GameLiveService
|
||||
{
|
||||
@@ -384,8 +383,6 @@ final class GameLiveService
|
||||
GameRecordStatService::refreshForRecordId($rid);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
JackpotPushService::publishHits($settleOut['jackpot_hits'] ?? []);
|
||||
|
||||
self::publishPublicPeriodOpened((string) $record['period_no'], $finalNumber, $now);
|
||||
self::publishPublicPeriodPayout((string) $record['period_no'], $finalNumber, $payoutUntil);
|
||||
self::publishSnapshot(null);
|
||||
@@ -611,72 +608,60 @@ final class GameLiveService
|
||||
|
||||
public static function publishSnapshot(?int $recordId = null): void
|
||||
{
|
||||
try {
|
||||
$payload = self::buildSnapshot($recordId);
|
||||
$api = self::createPushApi();
|
||||
$api->trigger(self::CHANNEL, self::EVENT, $payload);
|
||||
self::publishPublicPeriodTick($payload, $api);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
}
|
||||
|
||||
private static function createPushApi(): 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')
|
||||
);
|
||||
$snapshot = self::buildSnapshot($recordId);
|
||||
self::publishPublicPeriodTick($snapshot);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动端公共频道:每秒心跳,含期号、倒计时、阶段(对齐 lobbyInit/periodCurrent 语义)
|
||||
*/
|
||||
private static function publishPublicPeriodTick(array $snapshot, Api $api): void
|
||||
private static function publishPublicPeriodTick(array $snapshot): void
|
||||
{
|
||||
$record = $snapshot['record'] ?? null;
|
||||
$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;
|
||||
$periodId = 0;
|
||||
$status = 'finished';
|
||||
$resultNumber = null;
|
||||
if (is_array($record)) {
|
||||
$periodNo = (string) ($record['period_no'] ?? '');
|
||||
$dbStatus = (int) ($record['status'] ?? 0);
|
||||
$rn = $record['result_number'] ?? null;
|
||||
$resultNumber = is_numeric((string) $rn) ? (int) $rn : null;
|
||||
}
|
||||
if ($record === null || $periodNo === '') {
|
||||
$status = 'idle';
|
||||
} else {
|
||||
$status = self::mapPublicPeriodStatus($dbStatus, $betCloseIn);
|
||||
}
|
||||
$payload = [
|
||||
'server_time' => $serverTime,
|
||||
'period_no' => $periodNo,
|
||||
'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);
|
||||
$betSeconds = (int) ($snapshot['bet_seconds'] ?? 20);
|
||||
$periodSeconds = (int) ($snapshot['period_seconds'] ?? 30);
|
||||
if ($start > 0) {
|
||||
$payload['lock_at'] = $start + $betSeconds;
|
||||
$payload['open_at'] = $start + $periodSeconds;
|
||||
$periodNoRaw = $record['period_no'] ?? '';
|
||||
if (is_string($periodNoRaw)) {
|
||||
$periodNo = $periodNoRaw;
|
||||
}
|
||||
$periodIdRaw = $record['id'] ?? 0;
|
||||
$periodIdParsed = filter_var($periodIdRaw, FILTER_VALIDATE_INT);
|
||||
if ($periodIdParsed !== false && $periodIdParsed > 0) {
|
||||
$periodId = $periodIdParsed;
|
||||
}
|
||||
$dbStatusRaw = $record['status'] ?? 4;
|
||||
$dbStatus = filter_var($dbStatusRaw, FILTER_VALIDATE_INT);
|
||||
if ($dbStatus === false) {
|
||||
$dbStatus = 4;
|
||||
}
|
||||
$betRemainingRaw = $snapshot['bet_remaining_seconds'] ?? 0;
|
||||
$betRemaining = filter_var($betRemainingRaw, FILTER_VALIDATE_INT);
|
||||
if ($betRemaining === false || $betRemaining < 0) {
|
||||
$betRemaining = 0;
|
||||
}
|
||||
$status = self::mapPublicPeriodStatus($dbStatus, $betRemaining);
|
||||
$resultRaw = $record['result_number'] ?? null;
|
||||
$resultParsed = filter_var($resultRaw, FILTER_VALIDATE_INT);
|
||||
if ($resultParsed !== false && $resultParsed > 0) {
|
||||
$resultNumber = $resultParsed;
|
||||
}
|
||||
}
|
||||
if ($resultNumber !== null) {
|
||||
$payload['result_number'] = $resultNumber;
|
||||
}
|
||||
$api->trigger(self::CHANNEL_PUBLIC_GAME_PERIOD, self::EVT_PERIOD_TICK, $payload);
|
||||
|
||||
$payload = [
|
||||
'period_id' => $periodId,
|
||||
'period_no' => $periodNo,
|
||||
'status' => $status,
|
||||
'countdown' => max(0, self::safeInt($snapshot['remaining_seconds'] ?? 0)),
|
||||
'bet_close_in' => max(0, self::safeInt($snapshot['bet_remaining_seconds'] ?? 0)),
|
||||
'result_number' => $resultNumber,
|
||||
'runtime_enabled' => !empty($snapshot['runtime_enabled']),
|
||||
'server_time' => time(),
|
||||
];
|
||||
|
||||
GameWebSocketEventBus::publish(self::EVT_PERIOD_TICK, $payload);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -684,32 +669,26 @@ final class GameLiveService
|
||||
*/
|
||||
private static function publishPublicPeriodLocked(array $record): void
|
||||
{
|
||||
try {
|
||||
$start = (int) ($record['period_start_at'] ?? 0);
|
||||
$betSeconds = self::getConfigInt(self::KEY_BET_SECONDS, 20);
|
||||
$periodNo = (string) ($record['period_no'] ?? '');
|
||||
$payload = [
|
||||
'period_no' => $periodNo,
|
||||
'lock_at' => $start > 0 ? $start + $betSeconds : time(),
|
||||
];
|
||||
$api = self::createPushApi();
|
||||
$api->trigger(self::CHANNEL_PUBLIC_GAME_PERIOD, self::EVT_PERIOD_LOCKED, $payload);
|
||||
} catch (Throwable) {
|
||||
$periodNo = is_string($record['period_no'] ?? null) ? $record['period_no'] : '';
|
||||
$periodId = filter_var($record['id'] ?? 0, FILTER_VALIDATE_INT);
|
||||
if ($periodId === false) {
|
||||
$periodId = 0;
|
||||
}
|
||||
GameWebSocketEventBus::publish(self::EVT_PERIOD_LOCKED, [
|
||||
'period_id' => $periodId,
|
||||
'period_no' => $periodNo,
|
||||
'status' => 'locked',
|
||||
'server_time' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
private static function publishPublicPeriodOpened(string $periodNo, int $resultNumber, int $openTime): void
|
||||
{
|
||||
try {
|
||||
$payload = [
|
||||
'period_no' => $periodNo,
|
||||
'result_number' => $resultNumber,
|
||||
'open_time' => $openTime,
|
||||
];
|
||||
$api = self::createPushApi();
|
||||
$api->trigger(self::CHANNEL_PUBLIC_GAME_PERIOD, self::EVT_PERIOD_OPENED, $payload);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
GameWebSocketEventBus::publish(self::EVT_PERIOD_OPENED, [
|
||||
'period_no' => $periodNo,
|
||||
'result_number' => $resultNumber,
|
||||
'open_time' => $openTime,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -717,17 +696,12 @@ 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) {
|
||||
}
|
||||
GameWebSocketEventBus::publish(self::EVT_PERIOD_PAYOUT, [
|
||||
'period_no' => $periodNo,
|
||||
'result_number' => $resultNumber,
|
||||
'payout_until' => $payoutUntil,
|
||||
'server_time' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -911,4 +885,13 @@ final class GameLiveService
|
||||
$index = random_int(0, count($numbers) - 1);
|
||||
return $numbers[$index];
|
||||
}
|
||||
|
||||
private static function safeInt($value): int
|
||||
{
|
||||
$parsed = filter_var($value, FILTER_VALIDATE_INT);
|
||||
if ($parsed === false) {
|
||||
return 0;
|
||||
}
|
||||
return $parsed;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user