重新优化中奖权重计算方式

This commit is contained in:
2026-03-11 15:40:15 +08:00
parent bb166350fd
commit 2af7fedcce
13 changed files with 330 additions and 322 deletions

View File

@@ -136,7 +136,8 @@ class DiceRewardConfigController extends BaseController
}
/**
* T1-T5、BIGWIN 权重配比:批量更新权重(T1-T5 同档位权重和须为 100%BIGWIN 为豹子权重单独设定、无合计要求
* T1-T5、BIGWIN 权重配比:批量更新权重(单条 weight 1-10000各档位权重和不限制
* 保存后 Logic 会重新实例化奖励配置表缓存DiceRewardConfig::refreshCache
* @param Request $request
* @return Response
*/

View File

@@ -13,49 +13,61 @@ use app\dice\model\reward_config\DiceRewardConfig;
use support\Log;
/**
* 奖励配置逻辑层
* weight 仅 tier=BIGWIN 时可设定,保存时非 BIGWIN 强制 weight=0
* 奖励配置逻辑层DiceRewardConfig
* weight 1-10000各档位权重和不限制
*/
class DiceRewardConfigLogic extends BaseLogic
{
/**
* 构造函数
*/
/** weight 取值范围 */
private const WEIGHT_MIN = 1;
private const WEIGHT_MAX = 10000;
public function __construct()
{
$this->model = new DiceRewardConfig();
}
/**
* 新增前:非 BIGWIN 时强制 weight=0
* 新增weight 限制 1-10000保存后刷新缓存
*/
public function add(array $data): mixed
{
$data = $this->normalizeWeightByTier($data);
return parent::add($data);
$data = $this->normalizeWeight($data);
$result = parent::add($data);
DiceRewardConfig::refreshCache();
return $result;
}
/**
* 修改前:非 BIGWIN 时强制 weight=0
* 修改weight 限制 1-10000保存后刷新缓存
*/
public function edit($id, array $data): mixed
{
$data = $this->normalizeWeightByTier($data);
return parent::edit($id, $data);
$data = $this->normalizeWeight($data);
$result = parent::edit($id, $data);
DiceRewardConfig::refreshCache();
return $result;
}
/**
* 仅 tier=BIGWIN 时保留 weight且限制 0-100否则强制为 0
* 删除后刷新缓存
*/
private function normalizeWeightByTier(array $data): array
public function destroy($ids): bool
{
$tier = isset($data['tier']) ? (string) $data['tier'] : '';
if ($tier !== 'BIGWIN') {
$data['weight'] = 0;
return $data;
$result = parent::destroy($ids);
if ($result) {
DiceRewardConfig::refreshCache();
}
$w = isset($data['weight']) ? (float) $data['weight'] : 0;
$data['weight'] = max(0, min(100, $w));
return $result;
}
/**
* 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;
}
@@ -81,9 +93,9 @@ class DiceRewardConfigLogic extends BaseLogic
}
/**
* 批量更新权重:T1-T5 同档位权重之和必须等于 100BIGWIN 为豹子权重单独设定,不校验合计
* @param array<int, array{id: int, weight: float}> $items 元素为 [ id => 配置ID, weight => 0-100 ]
* @throws ApiException 当单条 weight 非法或 T1-T5 某档位权重和≠100
* 批量更新权重:单条 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
{
@@ -98,16 +110,15 @@ class DiceRewardConfigLogic extends BaseLogic
continue;
}
$id = isset($item['id']) ? (int) $item['id'] : 0;
$w = isset($item['weight']) ? (float) $item['weight'] : 0;
$w = isset($item['weight']) ? (int) $item['weight'] : self::WEIGHT_MIN;
if ($id < 0) {
throw new ApiException('存在无效的配置ID');
}
if ($w < 0 || $w > 100) {
throw new ApiException('权重必须在 0-100 之间');
if ($w < self::WEIGHT_MIN || $w > self::WEIGHT_MAX) {
throw new ApiException('权重必须在 ' . self::WEIGHT_MIN . '-' . self::WEIGHT_MAX . ' 之间');
}
$ids[] = $id;
$w = max(0, min(100, $w));
$weightById[$id] = isset($weightById[$id]) ? min(100, $weightById[$id] + $w) : $w;
$weightById[$id] = $w;
}
$list = $this->model->whereIn('id', array_unique($ids))->field('id,tier,grid_number')->select()->toArray();
$idToTier = [];
@@ -115,26 +126,11 @@ class DiceRewardConfigLogic extends BaseLogic
$id = isset($r['id']) ? (int) $r['id'] : 0;
$idToTier[$id] = isset($r['tier']) ? (string) $r['tier'] : '';
}
$sumByTier = [];
foreach ($weightById as $id => $w) {
$tier = $idToTier[$id] ?? '';
if ($tier === '') {
throw new ApiException('配置ID ' . $id . ' 不存在或档位为空');
}
if ($tier === 'BIGWIN') {
continue;
}
if (!isset($sumByTier[$tier])) {
$sumByTier[$tier] = 0;
}
$sumByTier[$tier] += $w;
}
foreach ($sumByTier as $tier => $sum) {
if (abs($sum - 100)) {
throw new ApiException('档位 ' . $tier . ' 的权重之和必须等于 100%,当前为 ' . round($sum, 2));
}
}
foreach ($weightById as $id => $w) {
DiceRewardConfig::where('id', $id)->update(['weight' => $w]);
}
DiceRewardConfig::refreshCache();

View File

@@ -107,6 +107,58 @@ class DicePlayRecord extends BaseModel
}
}
/**
* 读取 roll_array 时:若为空且 roll_number 在 530则按点数和生成默认 5 个色子数组,避免“点数和有值但五个点数空”的展示问题
* @param mixed $value 库中原始值JSON 字符串或 null
* @param array $data 当前记录数据
* @return array 5 个元素的数组,每项 16
*/
public function getRollArrayAttr($value, $data = []): array
{
$arr = [];
if (is_string($value) && $value !== '') {
$decoded = json_decode($value, true);
$arr = is_array($decoded) ? $decoded : [];
} elseif (is_array($value)) {
$arr = $value;
}
$sum = isset($data['roll_number']) ? (int) $data['roll_number'] : 0;
if (count($arr) === 5 && array_sum($arr) === $sum) {
$valid = true;
foreach ($arr as $v) {
if (!is_numeric($v) || (int) $v < 1 || (int) $v > 6) {
$valid = false;
break;
}
}
if ($valid) {
return array_map('intval', array_slice($arr, 0, 5));
}
}
if ($sum >= 5 && $sum <= 30) {
return self::defaultRollArrayForSum($sum);
}
return array_slice(array_map('intval', $arr), 0, 5);
}
/**
* 根据点数和生成默认 5 个色子数组(每项 16用于补全缺失的 roll_array
*/
public static function defaultRollArrayForSum(int $sum): array
{
$sum = max(5, min(30, $sum));
$base = (int) floor($sum / 5);
$rem = $sum - 5 * $base;
$arr = array_fill(0, 5, $base);
for ($i = 0; $i < $rem; $i++) {
$arr[$i]++;
}
$arr = array_map(function ($v) {
return max(1, min(6, (int) $v));
}, $arr);
return array_values($arr);
}
/** 抽奖类型 */
public function searchLotteryTypeAttr($query, $value)
{

View File

@@ -13,46 +13,36 @@ use support\think\Cache;
* 奖励配置模型
*
* dice_reward_config 奖励配置
* 奖励列表为全玩家通用,保存时刷新缓存,游戏时优先读缓存。
* 按档位 T1-T5 直接权重抽取 grid_numberweight 1-10000起始索引 s_start_index / n_start_index
*
* @property $id ID
* @property $grid_number 色子点数
* @property $ui_text 前端显示文本
* @property $real_ev 真实资金结算
* @property $tier 所属档位
* @property $weight 权重%(仅 tier=BIGWIN 时可设定0-100
* @property $s_end_index 时针结束索引
* @property $n_end_index 时针结束索引
* @property $weight 权重 1-10000档位内按权重比抽取
* @property $n_start_index 时针起始索引
* @property $s_start_index 时针起始索引
* @property $remark 备注
* @property $create_time 创建时间
* @property $update_time 修改时间
*/
class DiceRewardConfig extends BaseModel
{
/** 缓存键:彩金池奖励列表实例(含列表与索引) */
/** 缓存键:彩金池奖励列表实例 */
private const CACHE_KEY_INSTANCE = 'dice:reward_config:instance';
/** 缓存过期时间(秒),保存时会主动刷新故设较长 */
private const CACHE_TTL = 86400 * 30;
/** 当前请求内已加载的实例,避免同请求多次读缓存 */
private static ?array $instance = null;
/**
* 数据表主键
* @var string
*/
protected $pk = 'id';
/**
* 数据库表名称
* @var string
*/
protected $table = 'dice_reward_config';
/**
* 获取彩金池实例(含 list / 索引),无则从库加载并写入缓存;同请求内复用
* @return array{list: array, by_tier: array, by_tier_grid: array, by_s_end_index: array, by_n_end_index: array, min_real_ev: float}
* 获取彩金池实例(含 list / by_tier / by_tier_grid),无则从库加载并写入缓存
* @return array{list: array, by_tier: array, by_tier_grid: array, min_real_ev: float}
*/
public static function getCachedInstance(): array
{
@@ -70,10 +60,6 @@ class DiceRewardConfig extends BaseModel
return self::$instance;
}
/**
* 获取缓存的奖励列表(无则从库加载并写入缓存)
* @return array<int, array>
*/
public static function getCachedList(): array
{
$inst = self::getCachedInstance();
@@ -81,26 +67,20 @@ class DiceRewardConfig extends BaseModel
}
/**
* 重新从数据库加载并写入缓存(DiceRewardConfig 新增/修改/删除后调用),构建列表与索引
* 实例化结果含完整行(含 weight供 playStart 从缓存中查找 BIGWIN 的 weight 按概率抽奖
* 重新从数据库加载并写入缓存(按档位+权重抽 grid_number含 by_tier、by_tier_grid
*/
public static function refreshCache(): void
{
$list = (new self())->order('id', 'asc')->select()->toArray();
$byTier = [];
$byTierGrid = [];
$bySEndIndex = [];
$byNEndIndex = [];
foreach ($list as $row) {
$tier = isset($row['tier']) ? (string) $row['tier'] : '';
if ($tier !== '') {
// 过滤 tier=BIGWIN不参与档位抽奖仅豹子时通过 getCachedByTierAndGridNumber('BIGWIN', ...) 使用
if ($tier !== 'BIGWIN') {
if (!isset($byTier[$tier])) {
$byTier[$tier] = [];
}
$byTier[$tier][] = $row;
if (!isset($byTier[$tier])) {
$byTier[$tier] = [];
}
$byTier[$tier][] = $row;
$gridNum = isset($row['grid_number']) ? (int) $row['grid_number'] : 0;
if (!isset($byTierGrid[$tier])) {
$byTierGrid[$tier] = [];
@@ -109,48 +89,29 @@ class DiceRewardConfig extends BaseModel
$byTierGrid[$tier][$gridNum] = $row;
}
}
$sEnd = isset($row['s_end_index']) ? (int) $row['s_end_index'] : 0;
if (!isset($bySEndIndex[$sEnd])) {
$bySEndIndex[$sEnd] = [];
}
$bySEndIndex[$sEnd][] = $row;
$nEnd = isset($row['n_end_index']) ? (int) $row['n_end_index'] : 0;
if (!isset($byNEndIndex[$nEnd])) {
$byNEndIndex[$nEnd] = [];
}
$byNEndIndex[$nEnd][] = $row;
}
$minRealEv = empty($list) ? 0.0 : (float) min(array_column($list, 'real_ev'));
self::$instance = [
'list' => $list,
'by_tier' => $byTier,
'by_tier_grid' => $byTierGrid,
'by_s_end_index' => $bySEndIndex,
'by_n_end_index' => $byNEndIndex,
'min_real_ev' => $minRealEv,
'list' => $list,
'by_tier' => $byTier,
'by_tier_grid' => $byTierGrid,
'min_real_ev' => $minRealEv,
];
Cache::set(self::CACHE_KEY_INSTANCE, self::$instance, self::CACHE_TTL);
}
/** 空实例结构 */
private static function buildEmptyInstance(): array
{
return [
'list' => [],
'by_tier' => [],
'by_tier_grid' => [],
'by_s_end_index' => [],
'by_n_end_index' => [],
'min_real_ev' => 0.0,
'list' => [],
'by_tier' => [],
'by_tier_grid' => [],
'min_real_ev' => 0.0,
];
}
/**
* 从缓存实例按档位 + 色子点数取一条奖励配置(用于超级大奖 tier=BIGWIN + grid_number=roll_number
* 返回行含 weight0-100playStart 据此概率抽奖weight=100 表示摇到该 roll_number 时 100% 中超级大奖
* @param string $tier 档位,如 BIGWIN
* @param int $gridNumber 色子点数(摇出总和 roll_number
* @return array|null 配置行(含 weight、real_ev 等)或 null
* 按档位+色子点数取一条(用于 BIGWIN
*/
public static function getCachedByTierAndGridNumber(string $tier, int $gridNumber): ?array
{
@@ -161,9 +122,6 @@ class DiceRewardConfig extends BaseModel
return is_array($row) ? $row : null;
}
/**
* 从缓存取最小 real_ev
*/
public static function getCachedMinRealEv(): float
{
$inst = self::getCachedInstance();
@@ -171,8 +129,7 @@ class DiceRewardConfig extends BaseModel
}
/**
* 从缓存按档位取奖励列表
* @return array<int, array>
* 从缓存按档位取奖励列表(含 weight用于按权重抽 grid_number
*/
public static function getCachedByTier(string $tier): array
{
@@ -181,55 +138,26 @@ class DiceRewardConfig extends BaseModel
return $byTier[$tier] ?? [];
}
/**
* 从缓存按顺时针结束索引取列表s_end_index = id 的配置)
* @return array<int, array>
*/
public static function getCachedBySEndIndex(int $id): array
{
$inst = self::getCachedInstance();
$by = $inst['by_s_end_index'] ?? [];
return $by[$id] ?? [];
}
/**
* 从缓存按逆时针结束索引取列表n_end_index = id 的配置)
* @return array<int, array>
*/
public static function getCachedByNEndIndex(int $id): array
{
$inst = self::getCachedInstance();
$by = $inst['by_n_end_index'] ?? [];
return $by[$id] ?? [];
}
/**
* 清除当前请求内实例(如测试或需强制下次读缓存时调用)
*/
public static function clearRequestInstance(): void
{
self::$instance = null;
}
/** 保存后刷新缓存 */
public static function onAfterInsert($model): void
{
self::refreshCache();
}
/** 更新后刷新缓存 */
public static function onAfterUpdate($model): void
{
self::refreshCache();
}
/** 删除后刷新缓存 */
public static function onAfterDelete($model): void
{
self::refreshCache();
}
/** 色子点数下限 */
public function searchGridNumberMinAttr($query, $value)
{
if ($value !== '' && $value !== null) {
@@ -237,7 +165,6 @@ class DiceRewardConfig extends BaseModel
}
}
/** 色子点数上限 */
public function searchGridNumberMaxAttr($query, $value)
{
if ($value !== '' && $value !== null) {
@@ -245,7 +172,6 @@ class DiceRewardConfig extends BaseModel
}
}
/** 前端显示文本模糊 */
public function searchUiTextAttr($query, $value)
{
if ($value !== '' && $value !== null) {
@@ -253,7 +179,6 @@ class DiceRewardConfig extends BaseModel
}
}
/** 真实资金结算下限 */
public function searchRealEvMinAttr($query, $value)
{
if ($value !== '' && $value !== null) {
@@ -261,7 +186,6 @@ class DiceRewardConfig extends BaseModel
}
}
/** 真实资金结算上限 */
public function searchRealEvMaxAttr($query, $value)
{
if ($value !== '' && $value !== null) {
@@ -269,7 +193,6 @@ class DiceRewardConfig extends BaseModel
}
}
/** 所属档位 */
public function searchTierAttr($query, $value)
{
if ($value !== '' && $value !== null) {
@@ -277,7 +200,6 @@ class DiceRewardConfig extends BaseModel
}
}
/** 权重下限(仅 tier=BIGWIN 时有意义) */
public function searchWeightMinAttr($query, $value)
{
if ($value !== '' && $value !== null) {
@@ -285,7 +207,6 @@ class DiceRewardConfig extends BaseModel
}
}
/** 权重上限 */
public function searchWeightMaxAttr($query, $value)
{
if ($value !== '' && $value !== null) {

View File

@@ -9,55 +9,49 @@ namespace app\dice\validate\reward_config;
use plugin\saiadmin\basic\BaseValidate;
/**
* 奖励配置验证器
* weight 仅当 tier=BIGWIN 时可设定,且严格限制 0-100%
* 奖励配置验证器DiceRewardConfig
* weight 1-10000各档位权重和不限制
*/
class DiceRewardConfigValidate extends BaseValidate
{
/**
* 定义验证规则
*/
private const WEIGHT_MIN = 1;
private const WEIGHT_MAX = 10000;
protected $rule = [
'grid_number' => 'require',
'ui_text' => 'require',
'real_ev' => 'require',
'tier' => 'require',
'weight' => 'checkWeight',
'grid_number' => 'require',
'ui_text' => 'require',
'real_ev' => 'require',
'tier' => 'require',
'weight' => 'checkWeight',
'n_start_index' => 'number',
's_start_index' => 'number',
];
/**
* 定义错误信息
*/
protected $message = [
'grid_number' => '色子点数必须填写',
'ui_text' => '前端显示文本必须填写',
'real_ev' => '真实资金结算必须填写',
'tier' => '所属档位必须填写',
'weight' => '权重仅 tier=BIGWIN 时可设定,且必须为 0-100',
'grid_number' => '色子点数必须填写',
'ui_text' => '前端显示文本必须填写',
'real_ev' => '真实资金结算必须填写',
'tier' => '所属档位必须填写',
'weight' => '权重必须为 1-10000',
'n_start_index' => '逆时针起始索引须为数字',
's_start_index' => '顺时针起始索引须为数字',
];
/**
* 定义场景
*/
protected $scene = [
'save' => ['grid_number', 'ui_text', 'real_ev', 'tier', 'weight'],
'update' => ['grid_number', 'ui_text', 'real_ev', 'tier', 'weight'],
'save' => ['grid_number', 'ui_text', 'real_ev', 'tier', 'weight', 'n_start_index', 's_start_index'],
'update' => ['grid_number', 'ui_text', 'real_ev', 'tier', 'weight', 'n_start_index', 's_start_index'],
];
/**
* weight仅 tier=BIGWIN 时可设定,严格限制 0-100%
* weight1-10000
*/
protected function checkWeight($value, $rule = '', $data = []): bool
{
$tier = isset($data['tier']) ? (string) $data['tier'] : '';
if ($tier !== 'BIGWIN') {
return true;
}
$num = is_numeric($value) ? (float) $value : null;
$num = is_numeric($value) ? (int) $value : null;
if ($num === null) {
return false;
}
if ($num < 0 || $num > 100) {
if ($num < self::WEIGHT_MIN || $num > self::WEIGHT_MAX) {
return false;
}
return true;