From cc7e2d9a1a525bc19f38a9e3c0bb914f047d901b Mon Sep 17 00:00:00 2001 From: zhenhui <1276357500@qq.com> Date: Thu, 12 Mar 2026 19:21:10 +0800 Subject: [PATCH] =?UTF-8?q?[=E8=89=B2=E5=AD=90=E6=B8=B8=E6=88=8F]=E7=8E=A9?= =?UTF-8?q?=E5=AE=B6=E6=8A=BD=E5=A5=96=E8=AE=B0=E5=BD=95=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- saiadmin-artd/src/utils/table/tableUtils.ts | 4 +- .../plugin/dice/api/play_record_test/index.ts | 74 +++++ .../src/views/plugin/dice/api/reward/index.ts | 27 ++ .../dice/play_record_test/index/index.vue | 263 +++++++++++++++++ .../index/modules/edit-dialog.vue | 270 ++++++++++++++++++ .../index/modules/table-search.vue | 129 +++++++++ .../views/plugin/dice/reward/index/index.vue | 10 + .../index/modules/weight-test-dialog.vue | 108 +++++++ .../dice/reward_config_record/index/index.vue | 21 +- server/app/api/logic/PlayStartLogic.php | 94 ++++++ .../DicePlayRecordTestController.php | 150 ++++++++++ .../reward/DiceRewardController.php | 66 +++++ .../DicePlayRecordTestLogic.php | 27 ++ .../DiceRewardConfigRecordLogic.php | 64 +++++ .../reward_config_record/WeightTestRunner.php | 156 ++++++++++ .../play_record_test/DicePlayRecordTest.php | 122 ++++++++ .../DiceRewardConfigRecord.php | 16 +- .../DicePlayRecordTestValidate.php | 62 ++++ server/app/process/WeightTestProcess.php | 44 +++ server/config/process.php | 5 + server/plugin/saiadmin/config/route.php | 4 + 21 files changed, 1712 insertions(+), 4 deletions(-) create mode 100644 saiadmin-artd/src/views/plugin/dice/api/play_record_test/index.ts create mode 100644 saiadmin-artd/src/views/plugin/dice/play_record_test/index/index.vue create mode 100644 saiadmin-artd/src/views/plugin/dice/play_record_test/index/modules/edit-dialog.vue create mode 100644 saiadmin-artd/src/views/plugin/dice/play_record_test/index/modules/table-search.vue create mode 100644 saiadmin-artd/src/views/plugin/dice/reward/index/modules/weight-test-dialog.vue create mode 100644 server/app/dice/controller/play_record_test/DicePlayRecordTestController.php create mode 100644 server/app/dice/logic/play_record_test/DicePlayRecordTestLogic.php create mode 100644 server/app/dice/logic/reward_config_record/WeightTestRunner.php create mode 100644 server/app/dice/model/play_record_test/DicePlayRecordTest.php create mode 100644 server/app/dice/validate/play_record_test/DicePlayRecordTestValidate.php create mode 100644 server/app/process/WeightTestProcess.php diff --git a/saiadmin-artd/src/utils/table/tableUtils.ts b/saiadmin-artd/src/utils/table/tableUtils.ts index 3ca9db1..1604968 100644 --- a/saiadmin-artd/src/utils/table/tableUtils.ts +++ b/saiadmin-artd/src/utils/table/tableUtils.ts @@ -143,10 +143,10 @@ export const defaultResponseAdapter = (response: unknown): ApiResponse => total = extractTotal(res, records, tableConfig.totalFields) pagination = extractPagination(res) - // 如果没有找到,检查嵌套data + // 如果没有找到,检查嵌套 data(如 ThinkPHP paginate: { data: { total, per_page, current_page, data: [] } }) if (records.length === 0 && 'data' in res && typeof res.data === 'object') { const data = res.data as Record - records = extractRecords(data, ['list', 'records', 'items']) + records = extractRecords(data, ['list', 'data', 'records', 'items']) total = extractTotal(data, records, tableConfig.totalFields) pagination = extractPagination(res, data) diff --git a/saiadmin-artd/src/views/plugin/dice/api/play_record_test/index.ts b/saiadmin-artd/src/views/plugin/dice/api/play_record_test/index.ts new file mode 100644 index 0000000..58a1bb7 --- /dev/null +++ b/saiadmin-artd/src/views/plugin/dice/api/play_record_test/index.ts @@ -0,0 +1,74 @@ +import request from '@/utils/http' + +/** + * 玩家抽奖记录(测试数据) API接口 + */ +export default { + /** + * 获取数据列表 + * @param params 搜索参数 + * @returns 数据列表 + */ + list(params: Record) { + return request.get({ + url: '/core/dice/play_record_test/DicePlayRecordTest/index', + params + }) + }, + + /** + * 读取数据 + * @param id 数据ID + * @returns 数据详情 + */ + read(id: number | string) { + return request.get({ + url: '/core/dice/play_record_test/DicePlayRecordTest/read?id=' + id + }) + }, + + /** + * 创建数据 + * @param params 数据参数 + * @returns 执行结果 + */ + save(params: Record) { + return request.post({ + url: '/core/dice/play_record_test/DicePlayRecordTest/save', + data: params + }) + }, + + /** + * 更新数据 + * @param params 数据参数 + * @returns 执行结果 + */ + update(params: Record) { + return request.put({ + url: '/core/dice/play_record_test/DicePlayRecordTest/update', + data: params + }) + }, + + /** + * 删除数据 + * @param id 数据ID + * @returns 执行结果 + */ + delete(params: Record) { + return request.del({ + url: '/core/dice/play_record_test/DicePlayRecordTest/destroy', + data: params + }) + }, + + /** + * 一键删除所有测试数据 + */ + clearAll() { + return request.post({ + url: '/core/dice/play_record_test/DicePlayRecordTest/clearAll' + }) + } +} diff --git a/saiadmin-artd/src/views/plugin/dice/api/reward/index.ts b/saiadmin-artd/src/views/plugin/dice/api/reward/index.ts index 9d832d6..3bc1d8c 100644 --- a/saiadmin-artd/src/views/plugin/dice/api/reward/index.ts +++ b/saiadmin-artd/src/views/plugin/dice/api/reward/index.ts @@ -57,5 +57,32 @@ export default { url: '/core/dice/reward/DiceReward/batchUpdateWeightsByDirection', data: { direction, items } }) + }, + + /** + * 一键测试权重:创建测试记录并启动后台执行,返回 record_id 用于轮询进度 + */ + startWeightTest(params: { lottery_config_id: number; s_count: number; n_count: number }) { + return request.post<{ record_id: number }>({ + url: '/core/dice/reward/DiceReward/startWeightTest', + data: params + }) + }, + + /** + * 查询一键测试进度 + */ + getTestProgress(recordId: number) { + return request.get<{ + total_play_count: number + over_play_count: number + status: number + remark: string | null + result_counts: Record | null + tier_counts: Record | null + }>({ + url: '/core/dice/reward/DiceReward/getTestProgress', + params: { record_id: recordId } + }) } } diff --git a/saiadmin-artd/src/views/plugin/dice/play_record_test/index/index.vue b/saiadmin-artd/src/views/plugin/dice/play_record_test/index/index.vue new file mode 100644 index 0000000..e22ccc1 --- /dev/null +++ b/saiadmin-artd/src/views/plugin/dice/play_record_test/index/index.vue @@ -0,0 +1,263 @@ + + + + + diff --git a/saiadmin-artd/src/views/plugin/dice/play_record_test/index/modules/edit-dialog.vue b/saiadmin-artd/src/views/plugin/dice/play_record_test/index/modules/edit-dialog.vue new file mode 100644 index 0000000..0382ac4 --- /dev/null +++ b/saiadmin-artd/src/views/plugin/dice/play_record_test/index/modules/edit-dialog.vue @@ -0,0 +1,270 @@ + + + diff --git a/saiadmin-artd/src/views/plugin/dice/play_record_test/index/modules/table-search.vue b/saiadmin-artd/src/views/plugin/dice/play_record_test/index/modules/table-search.vue new file mode 100644 index 0000000..cf87327 --- /dev/null +++ b/saiadmin-artd/src/views/plugin/dice/play_record_test/index/modules/table-search.vue @@ -0,0 +1,129 @@ + + + + + diff --git a/saiadmin-artd/src/views/plugin/dice/reward/index/index.vue b/saiadmin-artd/src/views/plugin/dice/reward/index/index.vue index 4d8048d..2e4098f 100644 --- a/saiadmin-artd/src/views/plugin/dice/reward/index/index.vue +++ b/saiadmin-artd/src/views/plugin/dice/reward/index/index.vue @@ -21,6 +21,13 @@ > 权重配比 + + 一键测试权重 + @@ -39,6 +46,7 @@ + @@ -47,9 +55,11 @@ import api from '../../api/reward/index' import TableSearch from './modules/table-search.vue' import WeightRatioDialog from './modules/weight-ratio-dialog.vue' + import WeightTestDialog from './modules/weight-test-dialog.vue' const currentDirection = ref<0 | 1>(0) const weightRatioVisible = ref(false) + const weightTestVisible = ref(false) const searchForm = ref>({ direction: 0, diff --git a/saiadmin-artd/src/views/plugin/dice/reward/index/modules/weight-test-dialog.vue b/saiadmin-artd/src/views/plugin/dice/reward/index/modules/weight-test-dialog.vue new file mode 100644 index 0000000..091691c --- /dev/null +++ b/saiadmin-artd/src/views/plugin/dice/reward/index/modules/weight-test-dialog.vue @@ -0,0 +1,108 @@ + + + diff --git a/saiadmin-artd/src/views/plugin/dice/reward_config_record/index/index.vue b/saiadmin-artd/src/views/plugin/dice/reward_config_record/index/index.vue index 0305f2c..41e53f3 100644 --- a/saiadmin-artd/src/views/plugin/dice/reward_config_record/index/index.vue +++ b/saiadmin-artd/src/views/plugin/dice/reward_config_record/index/index.vue @@ -168,7 +168,19 @@ columnsFactory: () => [ { type: 'selection' }, { prop: 'id', label: 'ID', width: 80, align: 'center' }, - { prop: 'test_count', label: '测试次数', width: 100, align: 'center' }, + { + prop: 'status', + label: '状态', + width: 90, + align: 'center', + formatter: (row: Record) => + row.status === 1 ? '完成' : row.status === -1 ? '失败' : '待完成' + }, + { prop: 's_count', label: '顺时针次数', width: 110, align: 'center' }, + { prop: 'n_count', label: '逆时针次数', width: 110, align: 'center' }, + { prop: 'test_count', label: '测试总次数', width: 110, align: 'center' }, + { prop: 'over_play_count', label: '完成次数', width: 110, align: 'center' }, + { prop: 'lottery_config_id', label: '奖池配置ID', width: 110, align: 'center' }, { prop: 'admin_name', label: '管理员', @@ -190,6 +202,13 @@ align: 'center', useSlot: true }, + { + prop: 'remark', + label: '备注', + minWidth: 140, + align: 'center', + showOverflowTooltip: true + }, { prop: 'create_time', label: '创建时间', width: 170, align: 'center' }, { prop: 'operation', diff --git a/server/app/api/logic/PlayStartLogic.php b/server/app/api/logic/PlayStartLogic.php index fc9ea2a..579871e 100644 --- a/server/app/api/logic/PlayStartLogic.php +++ b/server/app/api/logic/PlayStartLogic.php @@ -427,4 +427,98 @@ class PlayStartLogic } return $this->generateRollArrayFromSum($sum); } + + /** + * 模拟一局抽奖(不写库、不扣玩家),用于权重测试写入 dice_play_record_test + * @param \app\dice\model\lottery_pool_config\DiceLotteryPoolConfig $config 奖池配置 + * @param int $direction 0=顺时针 1=逆时针 + * @return array 可直接用于 DicePlayRecordTest::create 的字段 + tier(用于统计档位概率) + */ + public function simulateOnePlay($config, int $direction): array + { + $rewardInstance = DiceReward::getCachedInstance(); + $byTierDirection = $rewardInstance['by_tier_direction'] ?? []; + $maxTierRetry = 10; + $chosen = null; + $tier = null; + for ($tierAttempt = 0; $tierAttempt < $maxTierRetry; $tierAttempt++) { + $tier = LotteryService::drawTierByWeights($config); + $tierRewards = $byTierDirection[$tier][$direction] ?? []; + if (empty($tierRewards)) { + continue; + } + try { + $chosen = self::drawRewardByWeight($tierRewards); + } catch (\RuntimeException $e) { + if ($e->getMessage() === self::EXCEPTION_WEIGHT_ALL_ZERO) { + continue; + } + throw $e; + } + break; + } + if ($chosen === null) { + throw new \RuntimeException('模拟抽奖:无可用奖励配置'); + } + + $startIndex = (int) ($chosen['start_index'] ?? 0); + $targetIndex = (int) ($chosen['end_index'] ?? 0); + $rollNumber = (int) ($chosen['grid_number'] ?? 0); + $realEv = (float) ($chosen['real_ev'] ?? 0); + $isTierT5 = (string) ($chosen['tier'] ?? '') === 'T5'; + $rewardWinCoin = $isTierT5 ? $realEv : (100 + $realEv); + + $superWinCoin = 0; + $isWin = 0; + if (in_array($rollNumber, self::SUPER_WIN_GRID_NUMBERS, true)) { + $bigWinConfig = DiceRewardConfig::getCachedByTierAndGridNumber('BIGWIN', $rollNumber); + $alwaysSuperWin = in_array($rollNumber, self::SUPER_WIN_ALWAYS_GRID_NUMBERS, true); + $doSuperWin = $alwaysSuperWin; + if (!$doSuperWin) { + $bigWinWeight = 10000; + if ($bigWinConfig !== null && isset($bigWinConfig['weight'])) { + $bigWinWeight = min(10000, max(0, (int) $bigWinConfig['weight'])); + } + $roll = (random_int(0, PHP_INT_MAX - 1) / (float) PHP_INT_MAX); + $doSuperWin = $bigWinWeight > 0 && $roll < ($bigWinWeight / 10000.0); + } + if ($doSuperWin) { + $rollArray = $this->getSuperWinRollArray($rollNumber); + $isWin = 1; + $superWinCoin = ($bigWinConfig['real_ev'] ?? 0) > 0 ? (float) ($bigWinConfig['real_ev'] ?? 0) : self::SUPER_WIN_BONUS; + $rewardWinCoin = 0; + } else { + $rollArray = $this->generateNonSuperWinRollArrayWithSum($rollNumber); + } + } else { + $rollArray = $this->generateRollArrayFromSum($rollNumber); + } + + $winCoin = $superWinCoin + $rewardWinCoin; + $configId = (int) $config->id; + $rewardId = ($isWin === 1 && $superWinCoin > 0) ? 0 : $targetIndex; + $configName = (string) ($config->name ?? ''); + + return [ + 'player_id' => 0, + 'admin_id' => 0, + 'lottery_config_id' => $configId, + 'lottery_type' => self::LOTTERY_TYPE_PAID, + 'is_win' => $isWin, + 'win_coin' => $winCoin, + 'super_win_coin' => $superWinCoin, + 'reward_win_coin' => $rewardWinCoin, + 'use_coins' => 0, + 'direction' => $direction, + 'reward_config_id' => $rewardId, + 'start_index' => $startIndex, + 'target_index' => $targetIndex, + 'roll_array' => json_encode($rollArray), + 'roll_number' => array_sum($rollArray), + 'lottery_name' => $configName, + 'status' => self::RECORD_STATUS_SUCCESS, + 'tier' => $tier, + 'roll_number_for_count' => $rollNumber, + ]; + } } diff --git a/server/app/dice/controller/play_record_test/DicePlayRecordTestController.php b/server/app/dice/controller/play_record_test/DicePlayRecordTestController.php new file mode 100644 index 0000000..6b4da38 --- /dev/null +++ b/server/app/dice/controller/play_record_test/DicePlayRecordTestController.php @@ -0,0 +1,150 @@ +logic = new DicePlayRecordTestLogic(); + $this->validate = new DicePlayRecordTestValidate; + parent::__construct(); + } + + /** + * 数据列表,并在结果中附带当前筛选条件下所有测试数据的玩家总收益 total_win_coin(DicePlayRecordTest.win_coin 求和) + * @param Request $request + * @return Response + */ + #[Permission('玩家抽奖记录(测试数据)列表', 'dice:play_record_test:index:index')] + public function index(Request $request): Response + { + $where = $request->more([ + ['lottery_type', ''], + ['direction', ''], + ['is_win', ''], + ['win_coin_min', ''], + ['win_coin_max', ''], + ['reward_tier', ''], + ]); + $query = $this->logic->search($where); + $query->with(['diceLotteryPoolConfig', 'diceRewardConfig']); + + // 按当前筛选条件统计所有测试数据的总收益(游戏总亏损) + $sumQuery = clone $query; + $totalWinCoin = $sumQuery->sum('win_coin'); + + $data = $this->logic->getList($query); + $data['total_win_coin'] = $totalWinCoin; + return $this->success($data); + } + + /** + * 读取数据 + * @param Request $request + * @return Response + */ + #[Permission('玩家抽奖记录(测试数据)读取', 'dice:play_record_test:index:read')] + public function read(Request $request): Response + { + $id = $request->input('id', ''); + $model = $this->logic->read($id); + if ($model) { + $data = is_array($model) ? $model : $model->toArray(); + return $this->success($data); + } else { + return $this->fail('未查找到信息'); + } + } + + /** + * 保存数据 + * @param Request $request + * @return Response + */ + #[Permission('玩家抽奖记录(测试数据)添加', 'dice:play_record_test:index:save')] + public function save(Request $request): Response + { + $data = $request->post(); + $this->validate('save', $data); + $result = $this->logic->add($data); + if ($result) { + return $this->success('添加成功'); + } else { + return $this->fail('添加失败'); + } + } + + /** + * 更新数据 + * @param Request $request + * @return Response + */ + #[Permission('玩家抽奖记录(测试数据)修改', 'dice:play_record_test:index:update')] + public function update(Request $request): Response + { + $data = $request->post(); + $this->validate('update', $data); + $result = $this->logic->edit($data['id'], $data); + if ($result) { + return $this->success('修改成功'); + } else { + return $this->fail('修改失败'); + } + } + + /** + * 删除数据 + * @param Request $request + * @return Response + */ + #[Permission('玩家抽奖记录(测试数据)删除', 'dice:play_record_test:index:destroy')] + public function destroy(Request $request): Response + { + $ids = $request->post('ids', ''); + if (empty($ids)) { + return $this->fail('请选择要删除的数据'); + } + $result = $this->logic->destroy($ids); + if ($result) { + return $this->success('删除成功'); + } else { + return $this->fail('删除失败'); + } + } + + /** + * 一键删除所有测试数据:清空 dice_play_record_test 表 + * @param Request $request + * @return Response + */ + #[Permission('玩家抽奖记录(测试数据)删除', 'dice:play_record_test:index:destroy')] + public function clearAll(Request $request): Response + { + try { + $table = (new \app\dice\model\play_record_test\DicePlayRecordTest())->getTable(); + Db::execute('TRUNCATE TABLE `' . $table . '`'); + return $this->success('已清空所有测试数据'); + } catch (\Throwable $e) { + return $this->fail('清空失败:' . $e->getMessage()); + } + } +} diff --git a/server/app/dice/controller/reward/DiceRewardController.php b/server/app/dice/controller/reward/DiceRewardController.php index 3e2ba52..21dbe8a 100644 --- a/server/app/dice/controller/reward/DiceRewardController.php +++ b/server/app/dice/controller/reward/DiceRewardController.php @@ -5,8 +5,12 @@ namespace app\dice\controller\reward; use app\dice\logic\reward\DiceRewardLogic; +use app\dice\logic\reward_config_record\DiceRewardConfigRecordLogic; use app\dice\model\reward\DiceReward; +use app\dice\model\play_record_test\DicePlayRecordTest; +use app\dice\model\reward_config_record\DiceRewardConfigRecord; use plugin\saiadmin\basic\BaseController; +use support\think\Db; use plugin\saiadmin\service\Permission; use support\Request; use support\Response; @@ -74,6 +78,68 @@ class DiceRewardController extends BaseController return $this->success($data); } + /** + * 一键测试权重:创建测试记录并启动单进程后台执行,实时写入 dice_play_record_test,更新 dice_reward_config_record 进度 + * 参数:lottery_config_id 奖池配置,s_count 顺时针次数 100/500/1000/5000,n_count 逆时针次数 100/500/1000/5000 + */ + #[Permission('奖励对照列表', 'dice:reward:index:index')] + public function startWeightTest(Request $request): Response + { + $lotteryConfigId = (int) $request->post('lottery_config_id', 0); + $sCount = (int) $request->post('s_count', 100); + $nCount = (int) $request->post('n_count', 100); + $adminId = isset($this->adminInfo['id']) ? (int) $this->adminInfo['id'] : null; + try { + $logic = new DiceRewardConfigRecordLogic(); + $recordId = $logic->createWeightTestRecord($lotteryConfigId, $sCount, $nCount, $adminId); + // 由独立进程 WeightTestProcess 定时轮询 status=0 并执行,不占用 HTTP 资源 + return $this->success(['record_id' => $recordId]); + } catch (\plugin\saiadmin\exception\ApiException $e) { + return $this->fail($e->getMessage()); + } + } + + /** + * 查询一键测试进度:total_play_count、over_play_count、status、remark + */ + #[Permission('奖励对照列表', 'dice:reward:index:index')] + public function getTestProgress(Request $request): Response + { + $recordId = (int) $request->input('record_id', 0); + if ($recordId <= 0) { + return $this->fail('请传入 record_id'); + } + $record = DiceRewardConfigRecord::find($recordId); + if (!$record) { + return $this->fail('记录不存在'); + } + $arr = $record->toArray(); + $data = [ + 'total_play_count' => (int) ($arr['total_play_count'] ?? 0), + 'over_play_count' => (int) ($arr['over_play_count'] ?? 0), + 'status' => (int) ($arr['status'] ?? 0), + 'remark' => $arr['remark'] ?? null, + 'result_counts' => $arr['result_counts'] ?? null, + 'tier_counts' => $arr['tier_counts'] ?? null, + ]; + return $this->success($data); + } + + /** + * 一键清空测试数据:清空 dice_play_record_test 表 + */ + #[Permission('奖励对照列表', 'dice:reward:index:index')] + public function clearPlayRecordTest(Request $request): Response + { + try { + $table = (new DicePlayRecordTest())->getTable(); + Db::execute('TRUNCATE TABLE `' . $table . '`'); + return $this->success('已清空测试数据'); + } catch (\Throwable $e) { + return $this->fail('清空失败:' . $e->getMessage()); + } + } + /** * 权重编辑弹窗:按方向+点数批量更新权重(写入 dice_reward) * 参数:items: [{ grid_number, weight_clockwise, weight_counterclockwise }, ...] diff --git a/server/app/dice/logic/play_record_test/DicePlayRecordTestLogic.php b/server/app/dice/logic/play_record_test/DicePlayRecordTestLogic.php new file mode 100644 index 0000000..f8c7c9f --- /dev/null +++ b/server/app/dice/logic/play_record_test/DicePlayRecordTestLogic.php @@ -0,0 +1,27 @@ +model = new DicePlayRecordTest(); + } + +} diff --git a/server/app/dice/logic/reward_config_record/DiceRewardConfigRecordLogic.php b/server/app/dice/logic/reward_config_record/DiceRewardConfigRecordLogic.php index f073a7e..73b1094 100644 --- a/server/app/dice/logic/reward_config_record/DiceRewardConfigRecordLogic.php +++ b/server/app/dice/logic/reward_config_record/DiceRewardConfigRecordLogic.php @@ -141,4 +141,68 @@ class DiceRewardConfigRecordLogic extends BaseLogic DiceRewardConfig::refreshCache(); DiceRewardConfig::clearRequestInstance(); } + + /** + * 创建一键测试权重记录并返回 ID,供后台执行器写入 dice_play_record_test 并更新进度 + * @param int $lotteryConfigId 奖池配置 ID(DiceLotteryPoolConfig) + * @param int $sCount 顺时针模拟次数 100/500/1000/5000 + * @param int $nCount 逆时针模拟次数 100/500/1000/5000 + * @param int|null $adminId 执行人 + * @return int 记录 ID + * @throws ApiException + */ + public function createWeightTestRecord(int $lotteryConfigId, int $sCount, int $nCount, ?int $adminId = null): int + { + $allowed = [100, 500, 1000, 5000]; + if (!in_array($sCount, $allowed, true) || !in_array($nCount, $allowed, true)) { + throw new ApiException('顺时针/逆时针次数仅支持 100、500、1000、5000'); + } + $config = DiceLotteryPoolConfig::find($lotteryConfigId); + if (!$config) { + throw new ApiException('奖池配置不存在'); + } + + $instance = DiceReward::getCachedInstance(); + $byTierDirection = $instance['by_tier_direction'] ?? []; + $snapshot = []; + foreach ($byTierDirection as $tier => $byDir) { + foreach ($byDir as $dir => $rows) { + foreach ($rows as $row) { + $snapshot[] = [ + 'id' => (int) ($row['id'] ?? 0), + 'grid_number' => (int) ($row['grid_number'] ?? 0), + 'tier' => (string) ($tier ?? ''), + 'weight' => (int) ($row['weight'] ?? 0), + ]; + } + } + } + $tierWeightsSnapshot = [ + 'T1' => (int) ($config->t1_weight ?? 0), + 'T2' => (int) ($config->t2_weight ?? 0), + 'T3' => (int) ($config->t3_weight ?? 0), + 'T4' => (int) ($config->t4_weight ?? 0), + 'T5' => (int) ($config->t5_weight ?? 0), + ]; + $total = $sCount + $nCount; + + $record = new DiceRewardConfigRecord(); + $record->test_count = $total; + $record->weight_config_snapshot = $snapshot; + $record->tier_weights_snapshot = $tierWeightsSnapshot; + $record->lottery_config_id = $lotteryConfigId; + $record->total_play_count = $total; + $record->over_play_count = 0; + $record->status = DiceRewardConfigRecord::STATUS_RUNNING; + $record->remark = null; + $record->s_count = $sCount; + $record->n_count = $nCount; + $record->result_counts = []; + $record->tier_counts = null; + $record->admin_id = $adminId; + $record->create_time = date('Y-m-d H:i:s'); + $record->save(); + + return (int) $record->id; + } } diff --git a/server/app/dice/logic/reward_config_record/WeightTestRunner.php b/server/app/dice/logic/reward_config_record/WeightTestRunner.php new file mode 100644 index 0000000..b94a3cb --- /dev/null +++ b/server/app/dice/logic/reward_config_record/WeightTestRunner.php @@ -0,0 +1,156 @@ +s_count ?? 0); + $nCount = (int) ($record->n_count ?? 0); + $total = $sCount + $nCount; + if ($total <= 0) { + $this->markFailed($recordId, 's_count + n_count 必须大于 0'); + return; + } + + $configId = (int) ($record->lottery_config_id ?? 0); + $config = $configId > 0 ? DiceLotteryPoolConfig::find($configId) : DiceLotteryPoolConfig::where('type', 0)->find(); + if (!$config) { + $this->markFailed($recordId, '奖池配置不存在'); + return; + } + + $playLogic = new PlayStartLogic(); + $resultCounts = []; // grid_number => count + $tierCounts = []; // tier => count + $buffer = []; + $done = 0; + + try { + for ($i = 0; $i < $sCount; $i++) { + $row = $playLogic->simulateOnePlay($config, 0); + $this->aggregate($row, $resultCounts, $tierCounts); + $buffer[] = $this->rowForInsert($row); + $done++; + $this->flushIfNeeded($buffer, $recordId, $done, $total, $resultCounts, $tierCounts); + } + for ($i = 0; $i < $nCount; $i++) { + $row = $playLogic->simulateOnePlay($config, 1); + $this->aggregate($row, $resultCounts, $tierCounts); + $buffer[] = $this->rowForInsert($row); + $done++; + $this->flushIfNeeded($buffer, $recordId, $done, $total, $resultCounts, $tierCounts); + } + if (!empty($buffer)) { + $this->insertBuffer($buffer); + $this->updateProgress($recordId, $done, $resultCounts, $tierCounts); + } + $this->markSuccess($recordId, $resultCounts, $tierCounts); + } catch (\Throwable $e) { + Log::error('WeightTestRunner exception: ' . $e->getMessage(), ['record_id' => $recordId, 'trace' => $e->getTraceAsString()]); + $this->markFailed($recordId, $e->getMessage()); + } + } + + private function aggregate(array $row, array &$resultCounts, array &$tierCounts): void + { + $grid = (int) ($row['roll_number_for_count'] ?? $row['roll_number'] ?? 0); + if ($grid >= 5 && $grid <= 30) { + $resultCounts[$grid] = ($resultCounts[$grid] ?? 0) + 1; + } + $tier = (string) ($row['tier'] ?? ''); + if ($tier !== '') { + $tierCounts[$tier] = ($tierCounts[$tier] ?? 0) + 1; + } + } + + private function rowForInsert(array $row): array + { + $out = []; + $keys = [ + 'player_id', 'admin_id', 'lottery_config_id', 'lottery_type', 'is_win', 'win_coin', + 'super_win_coin', 'reward_win_coin', 'use_coins', 'direction', 'reward_config_id', + 'start_index', 'target_index', 'roll_array', 'roll_number', 'lottery_name', 'status', + ]; + foreach ($keys as $k) { + if (array_key_exists($k, $row)) { + $out[$k] = $row[$k]; + } + } + return $out; + } + + private function flushIfNeeded(array &$buffer, int $recordId, int $done, int $total, array $resultCounts, array $tierCounts): void + { + if (count($buffer) < self::BATCH_SIZE) { + return; + } + $this->insertBuffer($buffer); + $buffer = []; + $this->updateProgress($recordId, $done, $resultCounts, $tierCounts); + } + + private function insertBuffer(array $rows): void + { + if (empty($rows)) { + return; + } + foreach ($rows as $row) { + DicePlayRecordTest::create($row); + } + } + + private function updateProgress(int $recordId, int $overPlayCount, array $resultCounts, array $tierCounts): void + { + $record = DiceRewardConfigRecord::find($recordId); + if ($record) { + $record->over_play_count = $overPlayCount; + $record->result_counts = $resultCounts; + $record->tier_counts = $tierCounts; + $record->save(); + } + } + + private function markSuccess(int $recordId, array $resultCounts, array $tierCounts): void + { + $record = DiceRewardConfigRecord::find($recordId); + if ($record) { + $record->status = DiceRewardConfigRecord::STATUS_SUCCESS; + $record->result_counts = $resultCounts; + $record->tier_counts = $tierCounts; + $record->remark = null; + $record->save(); + } + } + + private function markFailed(int $recordId, string $message): void + { + DiceRewardConfigRecord::where('id', $recordId)->update([ + 'status' => DiceRewardConfigRecord::STATUS_FAIL, + 'remark' => mb_substr($message, 0, 500), + ]); + } +} diff --git a/server/app/dice/model/play_record_test/DicePlayRecordTest.php b/server/app/dice/model/play_record_test/DicePlayRecordTest.php new file mode 100644 index 0000000..99e10bc --- /dev/null +++ b/server/app/dice/model/play_record_test/DicePlayRecordTest.php @@ -0,0 +1,122 @@ + DiceLotteryPoolConfig.id + */ + public function diceLotteryPoolConfig(): BelongsTo + { + return $this->belongsTo(DiceLotteryPoolConfig::class, 'lottery_config_id', 'id'); + } + + /** + * 奖励配置(终点格 = target_index 对应 DiceRewardConfig.id,表中为 reward_config_id) + * 关联 reward_config_id -> DiceRewardConfig.id + */ + public function diceRewardConfig(): BelongsTo + { + return $this->belongsTo(DiceRewardConfig::class, 'reward_config_id', 'id'); + } + + /** 抽奖类型 0=付费 1=赠送 */ + public function searchLotteryTypeAttr($query, $value) + { + if ($value !== '' && $value !== null) { + $query->where('lottery_type', '=', $value); + } + } + + /** 方向 0=顺时针 1=逆时针 */ + public function searchDirectionAttr($query, $value) + { + if ($value !== '' && $value !== null) { + $query->where('direction', '=', $value); + } + } + + /** 是否中大奖 0=无 1=中大奖 */ + public function searchIsWinAttr($query, $value) + { + if ($value !== '' && $value !== null) { + $query->where('is_win', '=', $value); + } + } + + /** 赢取平台币下限 */ + public function searchWinCoinMinAttr($query, $value) + { + if ($value !== '' && $value !== null) { + $query->where('win_coin', '>=', $value); + } + } + + /** 赢取平台币上限 */ + public function searchWinCoinMaxAttr($query, $value) + { + if ($value !== '' && $value !== null) { + $query->where('win_coin', '<=', $value); + } + } + + /** 中奖档位(按 reward_config_id 对应 DiceRewardConfig.tier) */ + public function searchRewardTierAttr($query, $value) + { + if ($value === '' || $value === null) { + return; + } + $ids = DiceRewardConfig::where('tier', '=', $value)->column('id'); + if (!empty($ids)) { + $query->whereIn('reward_config_id', $ids); + } else { + $query->whereRaw('1=0'); + } + } +} diff --git a/server/app/dice/model/reward_config_record/DiceRewardConfigRecord.php b/server/app/dice/model/reward_config_record/DiceRewardConfigRecord.php index 50ad3ac..3d97cc3 100644 --- a/server/app/dice/model/reward_config_record/DiceRewardConfigRecord.php +++ b/server/app/dice/model/reward_config_record/DiceRewardConfigRecord.php @@ -18,17 +18,31 @@ use plugin\saiadmin\basic\think\BaseModel; * @property array $weight_config_snapshot 测试时权重配比快照:按档位 id,grid_number,tier,weight * @property array $tier_weights_snapshot 测试时 T1-T5 档位权重快照(来自奖池配置) * @property int|null $lottery_config_id 测试时使用的奖池配置 ID + * @property int $total_play_count 总模拟次数(s_count+n_count) + * @property int $over_play_count 已完成次数 + * @property int $status 状态 -1失败 0进行中 1成功 + * @property string|null $remark 失败时记录原因 + * @property int $s_count 顺时针模拟次数 + * @property int $n_count 逆时针模拟次数 * @property array $result_counts 落点统计 grid_number=>出现次数 + * @property array|null $tier_counts 档位出现次数 T1=>count * @property int|null $admin_id 执行测试的管理员ID * @property string|null $create_time 创建时间 */ class DiceRewardConfigRecord extends BaseModel { + /** 状态:失败 */ + public const STATUS_FAIL = -1; + /** 状态:进行中 */ + public const STATUS_RUNNING = 0; + /** 状态:成功 */ + public const STATUS_SUCCESS = 1; + protected $pk = 'id'; protected $table = 'dice_reward_config_record'; - protected $json = ['weight_config_snapshot', 'tier_weights_snapshot', 'result_counts']; + protected $json = ['weight_config_snapshot', 'tier_weights_snapshot', 'result_counts', 'tier_counts']; protected $jsonAssoc = true; } diff --git a/server/app/dice/validate/play_record_test/DicePlayRecordTestValidate.php b/server/app/dice/validate/play_record_test/DicePlayRecordTestValidate.php new file mode 100644 index 0000000..7426aba --- /dev/null +++ b/server/app/dice/validate/play_record_test/DicePlayRecordTestValidate.php @@ -0,0 +1,62 @@ + 'require', + 'lottery_type' => 'require', + 'is_win' => 'require', + 'direction' => 'require', + 'reward_config_id' => 'require', + 'status' => 'require', + ]; + + /** + * 定义错误信息 + */ + protected $message = [ + 'lottery_config_id' => '彩金池配置id必须填写', + 'lottery_type' => '抽奖类型:0=付费,1=赠送必须填写', + 'is_win' => '中大奖:0=无,1=中奖必须填写', + 'direction' => '方向:0=顺时针,1=逆时针必须填写', + 'reward_config_id' => '奖励配置id必须填写', + 'status' => '状态:0=失败,1=成功必须填写', + ]; + + /** + * 定义场景 + */ + protected $scene = [ + 'save' => [ + 'lottery_config_id', + 'lottery_type', + 'is_win', + 'direction', + 'reward_config_id', + 'status', + ], + 'update' => [ + 'lottery_config_id', + 'lottery_type', + 'is_win', + 'direction', + 'reward_config_id', + 'status', + ], + ]; + +} diff --git a/server/app/process/WeightTestProcess.php b/server/app/process/WeightTestProcess.php new file mode 100644 index 0000000..a92d2ef --- /dev/null +++ b/server/app/process/WeightTestProcess.php @@ -0,0 +1,44 @@ +runOnePending(); + }); + } + + /** + * 执行一条待完成的测试记录(status=0) + */ + private function runOnePending(): void + { + $record = DiceRewardConfigRecord::where('status', DiceRewardConfigRecord::STATUS_RUNNING) + ->order('id') + ->find(); + if (!$record) { + return; + } + $recordId = (int) $record->id; + try { + (new WeightTestRunner())->run($recordId); + } catch (\Throwable $e) { + // WeightTestRunner 内部会更新 status=-1 和 remark + } + } +} diff --git a/server/config/process.php b/server/config/process.php index 88ae963..24ad124 100644 --- a/server/config/process.php +++ b/server/config/process.php @@ -35,6 +35,11 @@ return [ 'publicPath' => public_path() ] ], + // 一键测试权重:定时轮询 status=0 的测试记录并执行,不占用 HTTP 资源 + 'weight_test' => [ + 'handler' => app\process\WeightTestProcess::class, + 'count' => 1, + ], // File update detection and automatic reload 'monitor' => [ 'handler' => app\process\Monitor::class, diff --git a/server/plugin/saiadmin/config/route.php b/server/plugin/saiadmin/config/route.php index cb97b97..c0ae026 100644 --- a/server/plugin/saiadmin/config/route.php +++ b/server/plugin/saiadmin/config/route.php @@ -107,6 +107,8 @@ Route::group('/core', function () { Route::get('/dice/reward/DiceReward/weightRatioListWithDirection', [\app\dice\controller\reward\DiceRewardController::class, 'weightRatioListWithDirection']); Route::post('/dice/reward/DiceReward/batchUpdateWeights', [\app\dice\controller\reward\DiceRewardController::class, 'batchUpdateWeights']); Route::post('/dice/reward/DiceReward/batchUpdateWeightsByDirection', [\app\dice\controller\reward\DiceRewardController::class, 'batchUpdateWeightsByDirection']); + Route::post('/dice/reward/DiceReward/startWeightTest', [\app\dice\controller\reward\DiceRewardController::class, 'startWeightTest']); + Route::get('/dice/reward/DiceReward/getTestProgress', [\app\dice\controller\reward\DiceRewardController::class, 'getTestProgress']); fastRoute('dice/reward_config/DiceRewardConfig', \app\dice\controller\reward_config\DiceRewardConfigController::class); Route::get('/dice/reward_config/DiceRewardConfig/weightRatioList', [\app\dice\controller\reward_config\DiceRewardConfigController::class, 'weightRatioList']); Route::post('/dice/reward_config/DiceRewardConfig/batchUpdateWeights', [\app\dice\controller\reward_config\DiceRewardConfigController::class, 'batchUpdateWeights']); @@ -118,6 +120,8 @@ Route::group('/core', function () { Route::post('/dice/lottery_pool_config/DiceLotteryPoolConfig/updateCurrentPool', [\app\dice\controller\lottery_pool_config\DiceLotteryPoolConfigController::class, 'updateCurrentPool']); fastRoute('dice/reward_config_record/DiceRewardConfigRecord', \app\dice\controller\reward_config_record\DiceRewardConfigRecordController::class); Route::post('/dice/reward_config_record/DiceRewardConfigRecord/importFromRecord', [\app\dice\controller\reward_config_record\DiceRewardConfigRecordController::class, 'importFromRecord']); + fastRoute('dice/play_record_test/DicePlayRecordTest', \app\dice\controller\play_record_test\DicePlayRecordTestController::class); + Route::post('/dice/play_record_test/DicePlayRecordTest/clearAll', [\app\dice\controller\play_record_test\DicePlayRecordTestController::class, 'clearAll']); // 数据表维护 Route::get("/database/index", [\plugin\saiadmin\app\controller\system\DataBaseController::class, 'index']);