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

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

@@ -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,
];
}
}

View File

@@ -0,0 +1,150 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\controller\play_record_test;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\play_record_test\DicePlayRecordTestLogic;
use app\dice\validate\play_record_test\DicePlayRecordTestValidate;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;
use support\think\Db;
/**
* 玩家抽奖记录(测试数据)控制器
*/
class DicePlayRecordTestController extends BaseController
{
/**
* 构造函数
*/
public function __construct()
{
$this->logic = new DicePlayRecordTestLogic();
$this->validate = new DicePlayRecordTestValidate;
parent::__construct();
}
/**
* 数据列表,并在结果中附带当前筛选条件下所有测试数据的玩家总收益 total_win_coinDicePlayRecordTest.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());
}
}
}

View File

@@ -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/5000n_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 }, ...]

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),
]);
}
}

View File

@@ -0,0 +1,122 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\model\play_record_test;
use plugin\saiadmin\basic\think\BaseModel;
use app\dice\model\reward_config\DiceRewardConfig;
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
use think\model\relation\BelongsTo;
/**
* 玩家抽奖记录(测试数据)模型
*
* dice_play_record_test 玩家抽奖记录(测试数据)
*
* @property $id ID
* @property $lottery_config_id 彩金池配置id
* @property $lottery_type 抽奖类型:0=付费,1=赠送
* @property $is_win 中大奖:0=无,1=中奖
* @property $win_coin 赢取平台币
* @property $direction 方向:0=顺时针,1=逆时针
* @property $reward_config_id 奖励配置id
* @property $create_time 创建时间
* @property $update_time 修改时间
* @property $start_index 起始索引
* @property $target_index 结束索引
* @property $roll_number 摇取点数和
* @property $roll_array 摇取点数:[1,2,3,4,5,6]
* @property $status 状态:0=失败,1=成功
* @property $super_win_coin 中大奖平台币
* @property $reward_win_coin 摇色子中奖平台币
* @property $admin_id 所属管理员
*/
class DicePlayRecordTest extends BaseModel
{
/**
* 数据表主键
* @var string
*/
protected $pk = 'id';
/**
* 数据库表名称
* @var string
*/
protected $table = 'dice_play_record_test';
/**
* 彩金池配置
* 关联 lottery_config_id -> 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');
}
}
}

View File

@@ -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;
}

View File

@@ -0,0 +1,62 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\validate\play_record_test;
use plugin\saiadmin\basic\BaseValidate;
/**
* 玩家抽奖记录(测试数据)验证器
*/
class DicePlayRecordTestValidate extends BaseValidate
{
/**
* 定义验证规则
*/
protected $rule = [
'lottery_config_id' => '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',
],
];
}

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace app\process;
use app\dice\logic\reward_config_record\WeightTestRunner;
use app\dice\model\reward_config_record\DiceRewardConfigRecord;
use Workerman\Timer;
use Workerman\Worker;
/**
* 一键测试权重定时任务进程:每隔一定时间检查 status=0 的测试记录并执行一条,不占用 HTTP worker 资源
*/
class WeightTestProcess
{
/** 轮询间隔(秒) */
private const INTERVAL = 15;
public function onWorkerStart(Worker $worker): void
{
Timer::add(self::INTERVAL, function () {
$this->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
}
}
}