重新优化中奖权重计算方式
This commit is contained in:
@@ -36,9 +36,9 @@ class PlayStartLogic
|
||||
private const MIN_COIN_EXTRA = 100;
|
||||
/** 豹子号中大奖额外平台币(无 BIGWIN 配置时兜底) */
|
||||
private const SUPER_WIN_BONUS = 500;
|
||||
/** 可触发超级大奖的 grid_number(5=全1 10=全2 15=全3 20=全4 25=全5 30=全6);其中 5 和 30 固定 100% 出豹子 */
|
||||
/** 可触发超级大奖的 grid_number(5=全1 10=全2 15=全3 20=全4 25=全5 30=全6) */
|
||||
private const SUPER_WIN_GRID_NUMBERS = [5, 10, 15, 20, 25, 30];
|
||||
/** grid_number 为 5 或 30 时豹子概率固定 100%(DiceRewardConfig tier=BIGWIN 约定) */
|
||||
/** 5 和 30 抽到即豹子,不参与 BIGWIN 权重判定;10/15/20/25 按 BIGWIN weight 判定是否豹子 */
|
||||
private const SUPER_WIN_ALWAYS_GRID_NUMBERS = [5, 30];
|
||||
|
||||
/**
|
||||
@@ -85,10 +85,9 @@ class PlayStartLogic
|
||||
$safetyLine = (int) ($config->safety_line ?? 0);
|
||||
$usePoolWeights = $poolProfit >= $safetyLine;
|
||||
|
||||
// 按上述规则抽取档位;若该档位无奖励或该方向下均无可用路径则重新摇取档位
|
||||
// 按档位 T1-T5 抽取后,直接按该档位内 weight 抽取一条 DiceRewardConfig,得到 grid_number
|
||||
$maxTierRetry = 10;
|
||||
$chosen = null;
|
||||
$startCandidates = [];
|
||||
$tier = null;
|
||||
for ($tierAttempt = 0; $tierAttempt < $maxTierRetry; $tierAttempt++) {
|
||||
$tier = $usePoolWeights
|
||||
@@ -99,58 +98,55 @@ class PlayStartLogic
|
||||
Log::warning("档位 {$tier} 无任何奖励配置,重新摇取档位");
|
||||
continue;
|
||||
}
|
||||
$maxRewardRetry = count($tierRewards);
|
||||
for ($attempt = 0; $attempt < $maxRewardRetry; $attempt++) {
|
||||
try {
|
||||
$chosen = self::drawRewardByWeight($tierRewards);
|
||||
$chosenId = (int) ($chosen['id'] ?? 0);
|
||||
if ($direction === 0) {
|
||||
$startCandidates = DiceRewardConfig::getCachedBySEndIndex($chosenId);
|
||||
} else {
|
||||
$startCandidates = DiceRewardConfig::getCachedByNEndIndex($chosenId);
|
||||
} catch (\RuntimeException $e) {
|
||||
if ($e->getMessage() === self::EXCEPTION_WEIGHT_ALL_ZERO) {
|
||||
Log::warning("档位 {$tier} 下所有奖励权重均为 0,重新摇取档位");
|
||||
continue;
|
||||
}
|
||||
if (!empty($startCandidates)) {
|
||||
break 2;
|
||||
}
|
||||
Log::warning("方向 {$direction} 下无 s_end_index/n_end_index={$chosenId} 的配置,重新摇取");
|
||||
throw $e;
|
||||
}
|
||||
Log::warning("方向 {$direction} 下档位 {$tier} 所有奖励均无可用路径配置,重新摇取档位");
|
||||
break;
|
||||
}
|
||||
if (empty($startCandidates)) {
|
||||
Log::error("方向 {$direction} 下多次摇取档位后仍无可用路径配置");
|
||||
throw new ApiException('该方向下暂无可用路径配置');
|
||||
if ($chosen === null) {
|
||||
Log::error("多次摇取档位后仍无有效权重配置");
|
||||
throw new ApiException('暂无可用奖励配置');
|
||||
}
|
||||
$chosenId = (int) ($chosen['id'] ?? 0);
|
||||
$startRecord = $startCandidates[array_rand($startCandidates)];
|
||||
|
||||
$startIndex = (int) ($startRecord['id'] ?? 0);
|
||||
$targetIndex = $direction === 0
|
||||
? (int) ($startRecord['s_end_index'] ?? 0)
|
||||
: (int) ($startRecord['n_end_index'] ?? 0);
|
||||
$rollNumber = (int) ($startRecord['grid_number'] ?? 0);
|
||||
// 起始索引:根据 direction 取 s_start_index(顺时针)或 n_start_index(逆时针);结束索引:当前中奖配置 id
|
||||
$startIndex = $direction === 0
|
||||
? (int) ($chosen['s_start_index'] ?? 0)
|
||||
: (int) ($chosen['n_start_index'] ?? 0);
|
||||
$targetIndex = $chosenId;
|
||||
$rollNumber = (int) ($chosen['grid_number'] ?? 0);
|
||||
$realEv = (float) ($chosen['real_ev'] ?? 0);
|
||||
$rewardWinCoin = 100 + $realEv; // 摇色子中奖平台币 = 100 + DiceRewardConfig.real_ev
|
||||
$isTierT5 = (string) ($chosen['tier'] ?? '') === 'T5';
|
||||
// T5(再来一次):摇色子中奖平台币 = 配置的 real_ev;其他档位 = 100 + real_ev
|
||||
$rewardWinCoin = $isTierT5 ? $realEv : (100 + $realEv);
|
||||
|
||||
// 当抽到的 grid_number 为 5/10/15/20/25/30 时,可出豹子;其中 grid_number=5 与 30 固定 100% 豹子(BIGWIN 约定)
|
||||
// 流程:先抽档位 T1-T5,再按档位内权重抽色子点数;5 和 30 抽到即豹子,10/15/20/25 按 BIGWIN weight 判定是否中大奖(豹子如 [4,4,4,4,4])
|
||||
$superWinCoin = 0;
|
||||
$isWin = 0;
|
||||
$bigWinRealEv = 0.0; // BIGWIN 档位的真实资金结算,用于从彩金池盈利中一并扣除
|
||||
$bigWinRealEv = 0.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) {
|
||||
$weight = $bigWinConfig !== null
|
||||
? max(0.0, min(100.0, (float) ($bigWinConfig['weight'] ?? 0)))
|
||||
: 100.0;
|
||||
$roll = mt_rand(1, 10000) / 10000;
|
||||
$doSuperWin = $roll <= $weight / 100;
|
||||
? max(1, min(10000, (int) ($bigWinConfig['weight'] ?? 1)))
|
||||
: 10000;
|
||||
$roll = (random_int(0, PHP_INT_MAX - 1) / (float) PHP_INT_MAX);
|
||||
$doSuperWin = $roll < ($weight / 10000.0);
|
||||
}
|
||||
if ($doSuperWin) {
|
||||
$rollArray = $this->getSuperWinRollArray($rollNumber);
|
||||
$isWin = 1;
|
||||
if ($bigWinConfig !== null) {
|
||||
$bigWinRealEv = (float) ($bigWinConfig['real_ev'] ?? 0);
|
||||
$superWinCoin = 100 + $bigWinRealEv;
|
||||
$superWinCoin = $bigWinRealEv;
|
||||
} else {
|
||||
$superWinCoin = self::SUPER_WIN_BONUS;
|
||||
}
|
||||
@@ -174,7 +170,6 @@ class PlayStartLogic
|
||||
$configId = (int) $config->id;
|
||||
$rewardId = $chosenId;
|
||||
$configName = (string) ($config->name ?? '');
|
||||
$isTierT5 = (string) ($chosen['tier'] ?? '') === 'T5';
|
||||
$adminId = ($player->admin_id ?? null) ? (int) $player->admin_id : null;
|
||||
try {
|
||||
Db::transaction(function () use (
|
||||
@@ -322,34 +317,39 @@ class PlayStartLogic
|
||||
return $arr;
|
||||
}
|
||||
|
||||
/** 该组配置权重均为 0 时抛出,供调用方重试 */
|
||||
private const EXCEPTION_WEIGHT_ALL_ZERO = 'REWARD_WEIGHT_ALL_ZERO';
|
||||
|
||||
/**
|
||||
* T1-T5 档位内按 DiceRewardConfig.weight 抽取一条配置;权重均为 0 或未设时等权随机
|
||||
* @param array<int, array> $rewards 该档位配置列表(每条含 weight、id、real_ev 等)
|
||||
* @return array 抽中的一条配置
|
||||
* 按权重抽取一条配置:仅 weight>0 参与抽取(weight=0 不会被摇到)
|
||||
* 使用 [0, total) 浮点随机,支持最小权重 0.1%(如 weight=0.1),避免整数随机导致小权重失真
|
||||
* 全部 weight 为 0 时抛出 RuntimeException(EXCEPTION_WEIGHT_ALL_ZERO)
|
||||
*/
|
||||
private static function drawRewardByWeight(array $rewards): array
|
||||
{
|
||||
if (empty($rewards)) {
|
||||
throw new \InvalidArgumentException('rewards 不能为空');
|
||||
}
|
||||
$weights = [];
|
||||
$candidateWeights = [];
|
||||
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];
|
||||
$w = isset($row['weight']) ? (float) $row['weight'] : 0.0;
|
||||
if ($w > 0) {
|
||||
$candidateWeights[$i] = $w;
|
||||
}
|
||||
}
|
||||
return $rewards[array_key_last($rewards)];
|
||||
$total = (float) array_sum($candidateWeights);
|
||||
if ($total > 0) {
|
||||
$r = (random_int(0, PHP_INT_MAX - 1) / (float) PHP_INT_MAX) * $total;
|
||||
$acc = 0.0;
|
||||
foreach ($candidateWeights as $i => $w) {
|
||||
$acc += $w;
|
||||
if ($r < $acc) {
|
||||
return $rewards[$i];
|
||||
}
|
||||
}
|
||||
return $rewards[array_key_last($candidateWeights)];
|
||||
}
|
||||
throw new \RuntimeException(self::EXCEPTION_WEIGHT_ALL_ZERO);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -368,7 +368,7 @@ class PlayStartLogic
|
||||
if (empty($candidates)) {
|
||||
break;
|
||||
}
|
||||
$idx = $candidates[array_rand($candidates)];
|
||||
$idx = $candidates[random_int(0, count($candidates) - 1)];
|
||||
$arr[$idx]++;
|
||||
}
|
||||
shuffle($arr);
|
||||
@@ -403,8 +403,8 @@ class PlayStartLogic
|
||||
$arr = $super;
|
||||
$maxAttempts = 20;
|
||||
for ($a = 0; $a < $maxAttempts; $a++) {
|
||||
$idx = array_rand($arr);
|
||||
$j = array_rand($arr);
|
||||
$idx = random_int(0, count($arr) - 1);
|
||||
$j = random_int(0, count($arr) - 1);
|
||||
if ($idx === $j) {
|
||||
$j = ($j + 1) % 5;
|
||||
}
|
||||
|
||||
@@ -119,9 +119,9 @@ class LotteryService
|
||||
{
|
||||
$total = array_sum($weights);
|
||||
if ($total <= 0) {
|
||||
return $tiers[array_rand($tiers)];
|
||||
return $tiers[random_int(0, count($tiers) - 1)];
|
||||
}
|
||||
$r = mt_rand(1, $total);
|
||||
$r = random_int(1, $total);
|
||||
$acc = 0;
|
||||
foreach ($weights as $i => $w) {
|
||||
$acc += $w;
|
||||
@@ -145,7 +145,7 @@ class LotteryService
|
||||
return 0;
|
||||
}
|
||||
$total = $paid + $free;
|
||||
$r = mt_rand(1, $total);
|
||||
$r = random_int(1, $total);
|
||||
return $r <= $paid ? 0 : 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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 同档位权重之和必须等于 100;BIGWIN 为豹子权重单独设定,不校验合计
|
||||
* @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();
|
||||
|
||||
@@ -107,6 +107,58 @@ class DicePlayRecord extends BaseModel
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取 roll_array 时:若为空且 roll_number 在 5~30,则按点数和生成默认 5 个色子数组,避免“点数和有值但五个点数空”的展示问题
|
||||
* @param mixed $value 库中原始值(JSON 字符串或 null)
|
||||
* @param array $data 当前记录数据
|
||||
* @return array 5 个元素的数组,每项 1~6
|
||||
*/
|
||||
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 个色子数组(每项 1~6),用于补全缺失的 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)
|
||||
{
|
||||
|
||||
@@ -13,46 +13,36 @@ use support\think\Cache;
|
||||
* 奖励配置模型
|
||||
*
|
||||
* dice_reward_config 奖励配置
|
||||
* 奖励列表为全玩家通用,保存时刷新缓存,游戏时优先读缓存。
|
||||
* 按档位 T1-T5 直接权重抽取 grid_number,weight 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)
|
||||
* 返回行含 weight(0-100):playStart 据此概率抽奖,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) {
|
||||
|
||||
@@ -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(%)
|
||||
* weight:1-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;
|
||||
|
||||
Reference in New Issue
Block a user