优化当前彩金池-安全线

This commit is contained in:
2026-03-17 14:22:11 +08:00
parent 216d3ac8fe
commit f6b4fb99f0
7 changed files with 136 additions and 152 deletions

View File

@@ -31,83 +31,49 @@ class DiceLotteryPoolConfigLogic extends BaseLogic
}
/**
* 获取当前彩金池:从 Redis 读取实例profit_amount 每次从 DB 实时读取(表示玩家在该池子的累计盈利)
* 获取当前彩金池type=0+ 杀分权重为 type=1 的只读展示
* profit_amount 每次从 DB 实时读取t1_weightt5_weight 来自 type=1杀分权重不可在弹窗内修改
*
* @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}
* @return array{id:int,name:string,safety_line:int,t1_weight:int,...,t5_weight:int,profit_amount:float}
*/
public function getCurrentPool(): array
{
$cached = Cache::get(self::REDIS_KEY_CURRENT_POOL);
if ($cached && is_string($cached)) {
$data = json_decode($cached, true);
if (is_array($data)) {
$config = DiceLotteryPoolConfig::find($data['id'] ?? 0);
$profit = 0.0;
if ($config) {
$profit = isset($config->profit_amount) ? (float) $config->profit_amount : (isset($config->ev) ? (float) $config->ev : 0.0);
} else {
$profit = (float) ($data['profit_amount'] ?? 0);
}
$data['profit_amount'] = $profit;
return $data;
}
}
$config = DiceLotteryPoolConfig::where('type', 0)->find();
if (!$config) {
$configType0 = DiceLotteryPoolConfig::where('type', 0)->find();
if (!$configType0) {
throw new ApiException('未找到 type=0 的奖池配置,请先创建');
}
$row = $config->toArray();
$profitAmount = isset($row['profit_amount']) ? (float) $row['profit_amount'] : (isset($row['ev']) ? (float) $row['ev'] : 0.0);
$configType1 = DiceLotteryPoolConfig::where('type', 1)->find();
$row0 = $configType0->toArray();
$profitAmount = isset($row0['profit_amount']) ? (float) $row0['profit_amount'] : (isset($row0['ev']) ? (float) $row0['ev'] : 0.0);
$pool = [
'id' => (int) $row['id'],
'name' => (string) ($row['name'] ?? ''),
'safety_line' => (int) ($row['safety_line'] ?? 0),
't1_weight' => (int) ($row['t1_weight'] ?? 0),
't2_weight' => (int) ($row['t2_weight'] ?? 0),
't3_weight' => (int) ($row['t3_weight'] ?? 0),
't4_weight' => (int) ($row['t4_weight'] ?? 0),
't5_weight' => (int) ($row['t5_weight'] ?? 0),
'id' => (int) $row0['id'],
'name' => (string) ($row0['name'] ?? ''),
'safety_line' => (int) ($row0['safety_line'] ?? 0),
'profit_amount' => $profitAmount,
];
Cache::set(self::REDIS_KEY_CURRENT_POOL, json_encode($pool), self::EXPIRE);
$row1 = $configType1 ? $configType1->toArray() : [];
$pool['t1_weight'] = (int) ($row1['t1_weight'] ?? 0);
$pool['t2_weight'] = (int) ($row1['t2_weight'] ?? 0);
$pool['t3_weight'] = (int) ($row1['t3_weight'] ?? 0);
$pool['t4_weight'] = (int) ($row1['t4_weight'] ?? 0);
$pool['t5_weight'] = (int) ($row1['t5_weight'] ?? 0);
return $pool;
}
/**
* 更新当前彩金池:仅允许修改 safety_line、t1_weightt5_weight不修改 profit_amount
* 同时更新 Redis 与 DB 中 type=0 的记录
* 更新当前彩金池:仅允许修改 type=0 的 safety_line(杀分权重来自 type=1不可在此接口修改
*
* @param array{safety_line?:int,t1_weight?:int,t2_weight?:int,t3_weight?:int,t4_weight?:int,t5_weight?:int} $data
* @param array{safety_line?:int} $data
*/
public function updateCurrentPool(array $data): void
{
$pool = $this->getCurrentPool();
$id = (int) $pool['id'];
$config = DiceLotteryPoolConfig::find($id);
if (!$config) {
throw new ApiException('奖池配置不存在');
}
$allow = ['safety_line', 't1_weight', 't2_weight', 't3_weight', 't4_weight', 't5_weight'];
$update = [];
foreach ($allow as $k) {
if (array_key_exists($k, $data)) {
if ($k === 'safety_line') {
$update[$k] = (int) $data[$k];
} else {
$update[$k] = max(0, min(100, (int) $data[$k]));
}
}
}
if (empty($update)) {
if (!array_key_exists('safety_line', $data)) {
return;
}
DiceLotteryPoolConfig::where('id', $id)->update($update);
$pool = array_merge($pool, $update);
$refreshed = DiceLotteryPoolConfig::find($id);
$pool['profit_amount'] = $refreshed && (isset($refreshed->profit_amount) || isset($refreshed->ev))
? (float) ($refreshed->profit_amount ?? $refreshed->ev)
: (float) ($pool['profit_amount'] ?? 0);
Cache::set(self::REDIS_KEY_CURRENT_POOL, json_encode($pool), self::EXPIRE);
$safetyLine = (int) $data['safety_line'];
DiceLotteryPoolConfig::where('id', $id)->update(['safety_line' => $safetyLine]);
}
/**