49 lines
1.2 KiB
PHP
49 lines
1.2 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Events\BalanceUpdateBroadcast;
|
||
|
||
/**
|
||
* 玩家私有频道实时广播。
|
||
*
|
||
* 对齐界面文档 §2.1:balance.update(频道 `player.{id}`)。
|
||
* 注意:玩家私有频道广播与大厅公共频道分开管理。
|
||
*/
|
||
final class PlayerRealtimeBroadcaster
|
||
{
|
||
/** `balance.update` —— 钱包余额变动 */
|
||
public function notifyBalanceUpdate(
|
||
int $playerId,
|
||
string $currencyCode,
|
||
int $balanceMinor,
|
||
int $changeMinor,
|
||
string $reason,
|
||
): void {
|
||
if (! $this->driverSupportsRealtime()) {
|
||
return;
|
||
}
|
||
|
||
broadcast(new BalanceUpdateBroadcast(
|
||
$playerId,
|
||
$currencyCode,
|
||
$balanceMinor,
|
||
$changeMinor,
|
||
$reason,
|
||
(int) floor(microtime(true) * 1000),
|
||
));
|
||
}
|
||
|
||
private function driverSupportsRealtime(): bool
|
||
{
|
||
$default = config('broadcasting.default');
|
||
if ($default === null || $default === 'null') {
|
||
return false;
|
||
}
|
||
|
||
$driver = config("broadcasting.connections.{$default}.driver") ?? $default;
|
||
|
||
return ! in_array($driver, ['null', 'log'], true);
|
||
}
|
||
}
|