[游戏管理]游戏实时对局

This commit is contained in:
2026-04-16 15:10:12 +08:00
parent 15fdd3ba57
commit c7149e7058
29 changed files with 1158 additions and 157 deletions

View File

@@ -0,0 +1,58 @@
<?php
namespace app\admin\controller\game;
use app\common\controller\Backend;
use app\common\service\GameLiveService;
use support\Response;
use Webman\Http\Request as WebmanRequest;
/**
* 游戏实时对局
*/
class Live extends Backend
{
protected ?object $model = null;
protected function initController(WebmanRequest $request): ?Response
{
return null;
}
protected function _index(): Response
{
$recordIdRaw = $this->request ? $this->request->get('record_id') : null;
$recordId = is_numeric((string) $recordIdRaw) ? (int) $recordIdRaw : null;
return $this->success('', GameLiveService::buildSnapshot($recordId));
}
public function snapshot(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
$recordIdRaw = $request->get('record_id');
$recordId = is_numeric((string) $recordIdRaw) ? (int) $recordIdRaw : null;
return $this->success('', GameLiveService::buildSnapshot($recordId));
}
public function pushConfig(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
$ws = (string) config('plugin.webman.push.app.websocket');
$ws = str_replace('websocket://', 'ws://', $ws);
$ws = str_replace('0.0.0.0', '127.0.0.1', $ws);
return $this->success('', [
'url' => $ws,
'app_key' => (string) config('plugin.webman.push.app.app_key'),
'channel' => 'game-live',
'event' => 'bet-updated',
]);
}
}

View File

@@ -3,7 +3,7 @@
namespace app\admin\controller\game;
use app\common\controller\Backend;
use app\common\service\GamePeriodService;
use app\common\service\GameRecordService;
use support\Response;
use Throwable;
use Webman\Http\Request as WebmanRequest;
@@ -29,7 +29,7 @@ class Period extends Backend
protected function initController(WebmanRequest $request): ?Response
{
$this->model = new \app\common\model\GamePeriod();
$this->model = new \app\common\model\GameRecord();
return null;
}
@@ -44,7 +44,7 @@ class Period extends Backend
}
$method = $request->method();
if ($method === 'GET') {
return $this->success('', GamePeriodService::getPeriodSettings());
return $this->success('', GameRecordService::getRecordSettings());
}
if ($method === 'POST') {
$data = $request->post();
@@ -52,7 +52,7 @@ class Period extends Backend
return $this->error(__('Parameter %s can not be empty', ['']));
}
try {
GamePeriodService::savePeriodSettings($data);
GameRecordService::saveRecordSettings($data);
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
@@ -73,7 +73,7 @@ class Period extends Backend
if ($request->method() !== 'POST') {
return $this->error(__('Parameter error'));
}
$result = GamePeriodService::createNextPeriodForManual();
$result = GameRecordService::createNextRecordForManual();
if ($result['ok']) {
return $this->success($result['msg'], ['period_no' => $result['period_no'] ?? '']);
}

View File

@@ -0,0 +1,117 @@
<?php
namespace app\admin\controller\game;
use app\common\controller\Backend;
use app\common\service\GameRecordService;
use support\Response;
use Throwable;
use Webman\Http\Request as WebmanRequest;
/**
* 游戏对局记录
*/
class Record extends Backend
{
protected ?object $model = null;
protected string|array $preExcludeFields = ['id', 'create_time', 'update_time'];
protected string|array $quickSearchField = ['id', 'period_no'];
protected string|array $defaultSortField = ['id' => 'desc'];
protected string|array $orderGuarantee = ['id' => 'desc'];
protected bool $modelValidate = true;
protected bool $modelSceneValidate = true;
protected function initController(WebmanRequest $request): ?Response
{
$this->model = new \app\common\model\GameRecord();
return null;
}
public function recordSettings(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if ($request->method() === 'GET') {
return $this->success('', GameRecordService::getRecordSettings());
}
if ($request->method() === 'POST') {
$data = $request->post();
if (!is_array($data)) {
return $this->error(__('Parameter %s can not be empty', ['']));
}
try {
GameRecordService::saveRecordSettings($data);
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
return $this->success(__('Saved successfully'));
}
return $this->error(__('Parameter error'));
}
public function createNextManual(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if ($request->method() !== 'POST') {
return $this->error(__('Parameter error'));
}
$result = GameRecordService::createNextRecordForManual();
if ($result['ok']) {
return $this->success($result['msg'], ['period_no' => $result['period_no'] ?? '']);
}
return $this->error($result['msg']);
}
protected function _add(): Response
{
if ($this->request && $this->request->method() === 'POST') {
$data = $this->request->post();
if (!is_array($data)) {
return $this->error(__('Parameter %s can not be empty', ['']));
}
$data = $this->applyInputFilter($data);
$data = $this->excludeFields($data);
if (!isset($data['period_start_at']) || $data['period_start_at'] === '' || $data['period_start_at'] === null) {
$data['period_start_at'] = time();
}
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$data[$this->dataLimitField] = $this->auth->id;
}
$result = false;
$this->model->startTrans();
try {
if ($this->modelValidate) {
$validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
if (class_exists($validate)) {
$validate = new $validate();
if ($this->modelSceneValidate) {
$validate->scene('add');
}
$validate->check($data);
}
}
$result = $this->model->save($data);
$this->model->commit();
} catch (Throwable $e) {
$this->model->rollback();
return $this->error($e->getMessage());
}
if ($result !== false) {
return $this->success(__('Added successfully'));
}
return $this->error(__('No rows were added'));
}
return $this->error(__('Parameter error'));
}
}

View File

@@ -22,7 +22,7 @@ class BetOrder extends Backend
protected string|array $orderGuarantee = ['id' => 'desc'];
protected array $withJoinTable = ['user', 'channel', 'gamePeriod'];
protected array $withJoinTable = ['user', 'channel', 'gameRecord'];
protected function initController(WebmanRequest $request): ?Response
{
@@ -89,7 +89,7 @@ class BetOrder extends Backend
->visible([
'user' => ['username', 'phone'],
'channel' => ['name'],
'gamePeriod' => ['period_no', 'status'],
'gameRecord' => ['period_no', 'status'],
])
->alias($alias)
->where($where)