[色子游戏]玩家抽奖记录测试数据

This commit is contained in:
2026-03-12 19:21:10 +08:00
parent 7e4ba86afa
commit cc7e2d9a1a
21 changed files with 1712 additions and 4 deletions

View File

@@ -0,0 +1,27 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\logic\play_record_test;
use plugin\saiadmin\basic\think\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\dice\model\play_record_test\DicePlayRecordTest;
/**
* 玩家抽奖记录(测试数据)逻辑层
*/
class DicePlayRecordTestLogic extends BaseLogic
{
/**
* 构造函数
*/
public function __construct()
{
$this->model = new DicePlayRecordTest();
}
}

View File

@@ -141,4 +141,68 @@ class DiceRewardConfigRecordLogic extends BaseLogic
DiceRewardConfig::refreshCache();
DiceRewardConfig::clearRequestInstance();
}
/**
* 创建一键测试权重记录并返回 ID供后台执行器写入 dice_play_record_test 并更新进度
* @param int $lotteryConfigId 奖池配置 IDDiceLotteryPoolConfig
* @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;
}
}

View File

@@ -0,0 +1,156 @@
<?php
declare(strict_types=1);
namespace app\dice\logic\reward_config_record;
use app\api\logic\PlayStartLogic;
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
use app\dice\model\play_record_test\DicePlayRecordTest;
use app\dice\model\reward_config_record\DiceRewardConfigRecord;
use support\Log;
/**
* 一键测试权重:单进程后台执行模拟摇色子,写入 dice_play_record_test 并更新 dice_reward_config_record 进度
*/
class WeightTestRunner
{
private const BATCH_SIZE = 10;
/**
* 执行指定测试记录:按 s_count(顺时针)+n_count(逆时针) 模拟,每 10 条写入一次测试表并更新进度
* @param int $recordId dice_reward_config_record.id
*/
public function run(int $recordId): void
{
$record = DiceRewardConfigRecord::find($recordId);
if (!$record) {
Log::error("WeightTestRunner: 记录不存在 record_id={$recordId}");
return;
}
$sCount = (int) ($record->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),
]);
}
}