优化彩金池安全线触发方式

This commit is contained in:
2026-03-16 16:33:53 +08:00
parent 1213f8e58a
commit 0b2f4a026e
7 changed files with 127 additions and 34 deletions

View File

@@ -31,7 +31,7 @@ class DiceLotteryPoolConfigLogic extends BaseLogic
}
/**
* 获取当前彩金池:从 Redis 读取实例profit_amount 每次从 DB 实时读取以保证与抽奖累加一致
* 获取当前彩金池:从 Redis 读取实例profit_amount 每次从 DB 实时读取(表示玩家在该池子的累计盈利)
*
* @return array{id:int,name:string,safety_line:int,t1_weight:int,t2_weight:int,t3_weight:int,t4_weight:int,t5_weight:int,profit_amount:float}
*/
@@ -109,4 +109,16 @@ class DiceLotteryPoolConfigLogic extends BaseLogic
: (float) ($pool['profit_amount'] ?? 0);
Cache::set(self::REDIS_KEY_CURRENT_POOL, json_encode($pool), self::EXPIRE);
}
/**
* 重置当前彩金池的玩家累计盈利:将 profit_amount 置为 0并刷新 Redis 缓存
*/
public function resetProfitAmount(): void
{
$pool = $this->getCurrentPool();
$id = (int) $pool['id'];
DiceLotteryPoolConfig::where('id', $id)->update(['profit_amount' => 0]);
$pool['profit_amount'] = 0.0;
Cache::set(self::REDIS_KEY_CURRENT_POOL, json_encode($pool), self::EXPIRE);
}
}

View File

@@ -10,6 +10,7 @@ use app\dice\model\reward_config_record\DiceRewardConfigRecord;
use app\dice\model\reward\DiceReward;
use app\dice\model\reward_config\DiceRewardConfig;
use support\Log;
use support\think\Db;
/**
* 一键测试权重:单进程后台执行模拟摇色子,写入 dice_play_record_test 并更新 dice_reward_config_record 进度
@@ -67,27 +68,31 @@ class WeightTestRunner
$this->markFailed($recordId, '免费奖池配置不存在');
return;
}
$paidTierWeights = null;
$freeTierWeights = null;
if ($paidConfig === null) {
$paidTierWeights = $record->paid_tier_weights;
if (!is_array($paidTierWeights) || $paidTierWeights === []) {
$this->markFailed($recordId, '付费未选奖池时需提供 paid_tier_weights');
return;
}
$paidTierWeights = (is_array($record->paid_tier_weights ?? null) && $record->paid_tier_weights !== [])
? $record->paid_tier_weights
: null;
$freeTierWeights = (is_array($record->free_tier_weights ?? null) && $record->free_tier_weights !== [])
? $record->free_tier_weights
: null;
if ($paidConfig === null && $paidTierWeights === null) {
$this->markFailed($recordId, '付费未选奖池时需提供 paid_tier_weights');
return;
}
if ($freeConfig === null) {
$freeTierWeights = $record->free_tier_weights;
if (!is_array($freeTierWeights) || $freeTierWeights === []) {
$this->markFailed($recordId, '免费未选奖池时需提供 free_tier_weights');
return;
}
if ($freeConfig === null && $freeTierWeights === null) {
$this->markFailed($recordId, '免费未选奖池时需提供 free_tier_weights');
return;
}
// 每次测试开始前清空进程内静态缓存,强制从共享缓存读取最新 BIGWIN/奖励配置,与数据库一致
DiceRewardConfig::clearRequestInstance();
DiceReward::clearRequestInstance();
// 测试时按“单个虚拟玩家”累计中奖金额来判断是否触发杀分:达到 safety_line 前用自定义档位(玩家权重),达到后用奖池权重
$paidSafetyLine = $paidConfig !== null ? (int) ($paidConfig->safety_line ?? 0) : 0;
$freeSafetyLine = $freeConfig !== null ? (int) ($freeConfig->safety_line ?? 0) : 0;
$paidPlayerWinTotal = 0.0;
$freePlayerWinTotal = 0.0;
$playLogic = new PlayStartLogic();
$resultCounts = [];
$tierCounts = [];
@@ -96,28 +101,40 @@ class WeightTestRunner
try {
for ($i = 0; $i < $paidS; $i++) {
$row = $playLogic->simulateOnePlay($paidConfig, 0, 0, $paidTierWeights);
$usePoolWeights = $paidConfig !== null && $paidPlayerWinTotal >= $paidSafetyLine && $paidSafetyLine > 0;
$customWeights = $usePoolWeights ? null : $paidTierWeights;
$row = $playLogic->simulateOnePlay($paidConfig, 0, 0, $customWeights);
$this->accumulatePlayerWin($row, $paidPlayerWinTotal);
$this->aggregate($row, $resultCounts, $tierCounts);
$buffer[] = $this->rowForInsert($row, $recordId);
$done++;
$this->flushIfNeeded($buffer, $recordId, $done, $total, $resultCounts, $tierCounts);
}
for ($i = 0; $i < $paidN; $i++) {
$row = $playLogic->simulateOnePlay($paidConfig, 1, 0, $paidTierWeights);
$usePoolWeights = $paidConfig !== null && $paidPlayerWinTotal >= $paidSafetyLine && $paidSafetyLine > 0;
$customWeights = $usePoolWeights ? null : $paidTierWeights;
$row = $playLogic->simulateOnePlay($paidConfig, 1, 0, $customWeights);
$this->accumulatePlayerWin($row, $paidPlayerWinTotal);
$this->aggregate($row, $resultCounts, $tierCounts);
$buffer[] = $this->rowForInsert($row, $recordId);
$done++;
$this->flushIfNeeded($buffer, $recordId, $done, $total, $resultCounts, $tierCounts);
}
for ($i = 0; $i < $freeS; $i++) {
$row = $playLogic->simulateOnePlay($freeConfig, 0, 1, $freeTierWeights);
$usePoolWeights = $freeConfig !== null && $freePlayerWinTotal >= $freeSafetyLine && $freeSafetyLine > 0;
$customWeights = $usePoolWeights ? null : $freeTierWeights;
$row = $playLogic->simulateOnePlay($freeConfig, 0, 1, $customWeights);
$this->accumulatePlayerWin($row, $freePlayerWinTotal);
$this->aggregate($row, $resultCounts, $tierCounts);
$buffer[] = $this->rowForInsert($row, $recordId);
$done++;
$this->flushIfNeeded($buffer, $recordId, $done, $total, $resultCounts, $tierCounts);
}
for ($i = 0; $i < $freeN; $i++) {
$row = $playLogic->simulateOnePlay($freeConfig, 1, 1, $freeTierWeights);
$usePoolWeights = $freeConfig !== null && $freePlayerWinTotal >= $freeSafetyLine && $freeSafetyLine > 0;
$customWeights = $usePoolWeights ? null : $freeTierWeights;
$row = $playLogic->simulateOnePlay($freeConfig, 1, 1, $customWeights);
$this->accumulatePlayerWin($row, $freePlayerWinTotal);
$this->aggregate($row, $resultCounts, $tierCounts);
$buffer[] = $this->rowForInsert($row, $recordId);
$done++;
@@ -135,6 +152,15 @@ class WeightTestRunner
}
}
/** 累加单个虚拟玩家在测试过程中的中奖金额win_coin */
private function accumulatePlayerWin(array $row, float &$runningWinTotal): void
{
if (!isset($row['win_coin'])) {
return;
}
$runningWinTotal += (float) $row['win_coin'];
}
private function aggregate(array $row, array &$resultCounts, array &$tierCounts): void
{
$grid = (int) ($row['roll_number_for_count'] ?? $row['roll_number'] ?? 0);