优化中奖权重计算方式

This commit is contained in:
2026-03-12 17:17:00 +08:00
parent 064ce06393
commit 7e4ba86afa
25 changed files with 2344 additions and 403 deletions

View File

@@ -6,9 +6,10 @@
// +----------------------------------------------------------------------
namespace app\dice\logic\reward_config;
use app\dice\logic\reward\DiceRewardLogic;
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
use app\dice\model\reward_config\DiceRewardConfig;
use app\dice\model\reward_config\DiceRewardConfigRecord;
use app\dice\model\reward\DiceRewardConfig;
use app\dice\model\reward_config_record\DiceRewardConfigRecord;
use plugin\saiadmin\basic\think\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
@@ -30,27 +31,43 @@ class DiceRewardConfigLogic extends BaseLogic
}
/**
* 新增:weight 限制 1-10000保存后刷新缓存
* 新增:保存后刷新缓存(权重已迁移至 dice_reward 表)
*/
public function add(array $data): mixed
{
$data = $this->normalizeWeight($data);
$result = parent::add($data);
DiceRewardConfig::refreshCache();
return $result;
}
/**
* 修改:weight 限制 1-10000保存后刷新缓存
* 修改:保存后刷新缓存BIGWIN 的 weight 直接写入 dice_reward_config 表,抽奖时从 Config 读取
*/
public function edit($id, array $data): mixed
{
$data = $this->normalizeWeight($data);
$result = parent::edit($id, $data);
DiceRewardConfig::refreshCache();
return $result;
}
/**
* 为列表/分页数据中的 BIGWIN 行附加 weight来自 DiceReward 缓存)
*/
public function enrichBigwinWeight(array $listResult): array
{
$key = isset($listResult['data']) ? 'data' : (isset($listResult['records']) ? 'records' : null);
if ($key === null || empty($listResult[$key])) {
return $listResult;
}
$rewardLogic = new DiceRewardLogic();
foreach ($listResult[$key] as $i => $row) {
if (isset($row['tier']) && $row['tier'] === 'BIGWIN' && isset($row['grid_number'])) {
$listResult[$key][$i]['weight'] = $rewardLogic->getBigwinWeightByGridNumber((int) $row['grid_number']);
}
}
return $listResult;
}
/**
* 删除后刷新缓存
*/
@@ -64,18 +81,7 @@ class DiceRewardConfigLogic extends BaseLogic
}
/**
* weight 限制 1-10000
*/
private function normalizeWeight(array $data): array
{
$w = isset($data['weight']) ? (int) $data['weight'] : self::WEIGHT_MIN;
$data['weight'] = max(self::WEIGHT_MIN, min(self::WEIGHT_MAX, $w));
return $data;
}
/**
* 按档位分组返回奖励配置列表(用于 T1-T5、BIGWIN 权重配比)
* @return array<string, array> 键为 T1|T2|T3|T4|T5|BIGWIN值为该档位下的配置行数组
* 按档位分组返回奖励配置列表(仅配置,权重在 dice_reward 表;权重配比请用 DiceRewardLogic::getListGroupedByTierWithDirection
*/
public function getListGroupedByTier(): array
{
@@ -94,50 +100,6 @@ class DiceRewardConfigLogic extends BaseLogic
return $grouped;
}
/**
* 批量更新权重:单条 weight 1-10000各档位权重和不限制
* @param array<int, array{id: int, weight: int}> $items 元素为 [ id => 配置ID, weight => 1-10000 ]
* @throws ApiException 当单条 weight 非法时
*/
public function batchUpdateWeights(array $items): void
{
if (empty($items)) {
return;
}
$items = array_values($items);
$ids = [];
$weightById = [];
foreach ($items as $item) {
if (!is_array($item)) {
continue;
}
$id = isset($item['id']) ? (int) $item['id'] : 0;
$w = isset($item['weight']) ? (int) $item['weight'] : self::WEIGHT_MIN;
if ($id < 0) {
throw new ApiException('存在无效的配置ID');
}
if ($w < self::WEIGHT_MIN || $w > self::WEIGHT_MAX) {
throw new ApiException('权重必须在 ' . self::WEIGHT_MIN . '-' . self::WEIGHT_MAX . ' 之间');
}
$ids[] = $id;
$weightById[$id] = $w;
}
$list = $this->model->whereIn('id', array_unique($ids))->field('id,tier,grid_number')->select()->toArray();
$idToTier = [];
foreach ($list as $r) {
$id = isset($r['id']) ? (int) $r['id'] : 0;
$idToTier[$id] = isset($r['tier']) ? (string) $r['tier'] : '';
}
foreach ($weightById as $id => $w) {
$tier = $idToTier[$id] ?? '';
if ($tier === '') {
throw new ApiException('配置ID ' . $id . ' 不存在或档位为空');
}
DiceRewardConfig::where('id', $id)->update(['weight' => $w]);
}
DiceRewardConfig::refreshCache();
}
/** 测试时档位权重均为 0 的异常标识 */
private const EXCEPTION_WEIGHT_ALL_ZERO = 'REWARD_WEIGHT_ALL_ZERO';
@@ -208,7 +170,10 @@ class DiceRewardConfigLogic extends BaseLogic
throw new ApiException('测试次数仅支持 100、500、1000、5000、10000');
}
$grouped = $this->getListGroupedByTier();
$grouped = [];
foreach (['T1', 'T2', 'T3', 'T4', 'T5', 'BIGWIN'] as $t) {
$grouped[$t] = $this->model::getCachedByTierForDirection($t, 0);
}
$tiers = ['T1', 'T2', 'T3', 'T4', 'T5'];
$tierWeights = [1, 1, 1, 1, 1];
$config = null;