优化彩金池累加计算方式

This commit is contained in:
2026-03-11 09:54:49 +08:00
parent 84d499145d
commit bb166350fd
4 changed files with 72 additions and 42 deletions

View File

@@ -101,7 +101,7 @@ class PlayStartLogic
}
$maxRewardRetry = count($tierRewards);
for ($attempt = 0; $attempt < $maxRewardRetry; $attempt++) {
$chosen = $tierRewards[array_rand($tierRewards)];
$chosen = self::drawRewardByWeight($tierRewards);
$chosenId = (int) ($chosen['id'] ?? 0);
if ($direction === 0) {
$startCandidates = DiceRewardConfig::getCachedBySEndIndex($chosenId);
@@ -246,10 +246,10 @@ class PlayStartLogic
$p->save();
// 彩金池盈利累加:每局累加 (100 - 本局总 real_ev),其中本局总 real_ev = 普通档位 real_ev + BIGWIN.real_ev如触发)
// 彩金池盈利:每局扣除本局发放的真实成本(普通档位 real_ev + BIGWIN.real_ev 如触发),不额外加 100
// 需确保表有 profit_amount 字段(见 db/dice_lottery_config_add_profit_amount.sql
$totalRealEv = $realEv + $bigWinRealEv;
$addProfit = 100 - $totalRealEv;
$addProfit = -$totalRealEv;
try {
DiceLotteryPoolConfig::where('id', $configId)->update([
'profit_amount' => Db::raw('IFNULL(profit_amount,0) + ' . (float) $addProfit),
@@ -322,6 +322,36 @@ class PlayStartLogic
return $arr;
}
/**
* T1-T5 档位内按 DiceRewardConfig.weight 抽取一条配置;权重均为 0 或未设时等权随机
* @param array<int, array> $rewards 该档位配置列表(每条含 weight、id、real_ev 等)
* @return array 抽中的一条配置
*/
private static function drawRewardByWeight(array $rewards): array
{
if (empty($rewards)) {
throw new \InvalidArgumentException('rewards 不能为空');
}
$weights = [];
foreach ($rewards as $i => $row) {
$w = isset($row['weight']) ? max(0, (float) $row['weight']) : 0;
$weights[$i] = $w;
}
$total = array_sum($weights);
if ($total <= 0) {
return $rewards[array_rand($rewards)];
}
$r = mt_rand(1, (int) $total);
$acc = 0;
foreach ($weights as $i => $w) {
$acc += $w;
if ($r <= $acc) {
return $rewards[$i];
}
}
return $rewards[array_key_last($rewards)];
}
/**
* 根据摇取点数5-30生成 5 个色子数组,每个 1-6总和为 $sum
* @return int[] 如 [1,2,3,4,5]