Files
pk10/App/Controllers/Web/PeriodController.php
T

147 lines
5.3 KiB
PHP
Executable File

<?php
namespace App\Controllers\Web;
use App\Core\WebBaseController;
use App\Core\GameFactory;
use Db\Database;
class PeriodController extends WebBaseController {
public function getCurrent() {
header('Content-Type: application/json');
$db = new Database();
$gameId = (int)($_GET['game_id'] ?? 0);
if (!$gameId) {
$gameId = (int)($db->get('games', 'id', ['code' => 'pk10']) ?: 0);
}
if (!$gameId) {
echo json_encode(['success' => false, 'message' => 'Game not found']);
return;
}
// 获取游戏类型和算法
$game = $db->get('games', ['id', 'type', 'period_duration', 'lock_before_end'], ['id' => $gameId]);
$gameType = $game['type'] ?? 'pk10';
$algoClass = GameFactory::getAlgorithm($gameType);
$resultTable = $algoClass::getResultTable();
$period = $db->get('periods', '*', [
'game_id' => $gameId,
'ORDER' => ['id' => 'DESC']
]);
$now = time();
$countdown = 0;
$lockCountdown = 0;
if ($period && in_array($period['status'], ['pending', 'locked'])) {
$start = strtotime($period['start_time'] ?? $period['created_at']);
$countdown = max(0, !empty($period['end_time']) ? strtotime($period['end_time']) - $now : 300 - ($now - $start));
$periodDuration = (int)($game['period_duration'] ?? 300);
$lockBefore = (int)($game['lock_before_end'] ?? 30);
$lockCountdown = max(0, ($periodDuration - $lockBefore) - ($now - $start));
}
// 获取结果的通用方法
$getResult = function($periodRow) use ($db, $resultTable, $algoClass) {
if (!$periodRow) return null;
if ($resultTable) {
return $db->get($resultTable, '*', ['period_id' => $periodRow['id']]);
}
// dice/xocdia: 结果在 periods 表中
return $periodRow;
};
// ====== 最近开奖结果 ======
$lastResult = null;
$lastResultStatus = null;
$lastDrawn = $db->get('periods', '*', [
'game_id' => $gameId,
'status' => ['drawn', 'settled'],
'ORDER' => ['id' => 'DESC']
]);
if ($lastDrawn) {
$pk10 = $getResult($lastDrawn);
if ($pk10) {
$lastResult = $pk10;
$lastResult['period_number'] = $lastDrawn['period_number'];
$lastResultStatus = $lastDrawn['status'];
}
}
// ====== locked 状态预开奖结果 ======
$raceResult = null;
if ($period && $period['status'] === 'locked' && !empty($period['result'])) {
$pk10Current = $getResult($period);
if ($pk10Current) {
$raceResult = $pk10Current;
$raceResult['period_number'] = $period['period_number'];
}
}
// history
$history = [];
$recentPeriods = $db->select('periods', ['id','period_number','result','status'], [
'game_id' => $gameId,
'status' => ['drawn', 'settled'],
'ORDER' => ['id' => 'DESC'],
'LIMIT' => 10
]);
foreach ($recentPeriods as $s) {
$pk = $getResult($s);
if ($pk) { $pk['period_number'] = $s['period_number']; $history[] = $pk; }
}
// user bets + balance
$myBets = [];
$balance = null;
$userId = $this->getWebUserId();
if ($userId && $period) {
$betsRaw = $db->select('bets', ['id', 'bet_type', 'bet_value', 'amount', 'odds', 'status', 'win_amount'], [
'user_id' => $userId,
'period_id' => $period['id'],
'ORDER' => ['id' => 'DESC']
]);
$myBets = $betsRaw ?: [];
$userRow = $db->get('users', ['balance'], ['id' => $userId]);
$balance = $userRow ? (float)$userRow['balance'] : null;
}
$lastMyBets = [];
if ($userId && $lastDrawn && $lastDrawn['status'] === 'settled') {
$lastBetsRaw = $db->select('bets', ['id', 'bet_type', 'bet_value', 'amount', 'odds', 'status', 'win_amount'], [
'user_id' => $userId,
'period_id' => $lastDrawn['id'],
'ORDER' => ['id' => 'DESC']
]);
$lastMyBets = $lastBetsRaw ?: [];
}
echo json_encode([
'success' => true,
'data' => [
'id' => $period['id'] ?? 0,
'period_number' => $period['period_number'] ?? '',
'status' => $period['status'] ?? 'none',
'remaining_seconds' => $countdown,
'lock_countdown' => $lockCountdown,
'auto_generated' => (int)($period['auto_generated'] ?? 0),
'server_time' => date('Y-m-d H:i:s'),
],
'last_result' => $lastResult,
'last_result_status' => $lastResultStatus,
'race_result' => $raceResult,
'history' => $history,
'my_bets' => $myBets,
'last_my_bets' => $lastMyBets,
'balance' => $balance,
]);
}
private function getWebUserId(): int {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
return (int)($_SESSION['web_user_id'] ?? 0);
}
}