Files
dafuweng-saiadmin6.x/server/app/dice/logic/lottery_pool_config/DiceLotteryPoolConfigLogic.php

91 lines
3.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\logic\lottery_pool_config;
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
use plugin\saiadmin\basic\think\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use support\think\Cache;
/**
* 色子奖池配置逻辑层
*/
class DiceLotteryPoolConfigLogic extends BaseLogic
{
/** Redis 当前彩金池type=0 实例key无则按 type=0 创建 */
private const REDIS_KEY_CURRENT_POOL = 'api:game:lottery_pool:default';
private const EXPIRE = 86400 * 7;
/**
* 构造函数
*/
public function __construct()
{
$this->model = new DiceLotteryPoolConfig();
}
/**
* 获取当前彩金池type=0+ 杀分权重为 type=1 的只读展示
* profit_amount 每次从 DB 实时读取t1_weightt5_weight 来自 type=1杀分权重不可在弹窗内修改
*
* @return array{id:int,name:string,safety_line:int,t1_weight:int,...,t5_weight:int,profit_amount:float}
*/
public function getCurrentPool(): array
{
$configType0 = DiceLotteryPoolConfig::where('type', 0)->find();
if (!$configType0) {
throw new ApiException('未找到 type=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) $row0['id'],
'name' => (string) ($row0['name'] ?? ''),
'safety_line' => (int) ($row0['safety_line'] ?? 0),
'profit_amount' => $profitAmount,
];
$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;
}
/**
* 更新当前彩金池:仅允许修改 type=0 的 safety_line杀分权重来自 type=1不可在此接口修改
*
* @param array{safety_line?:int} $data
*/
public function updateCurrentPool(array $data): void
{
$pool = $this->getCurrentPool();
$id = (int) $pool['id'];
if (!array_key_exists('safety_line', $data)) {
return;
}
$safetyLine = (int) $data['safety_line'];
DiceLotteryPoolConfig::where('id', $id)->update(['safety_line' => $safetyLine]);
}
/**
* 重置当前彩金池的玩家累计盈利:将 profit_amount 置为 0并刷新 Redis 缓存
*/
public function resetProfitAmount(): void
{
$pool = $this->getCurrentPool();
$id = (int) $pool['id'];
DiceLotteryPoolConfig::where('id', $id)->update(['profit_amount' => 0]);
$pool['profit_amount'] = 0.0;
Cache::set(self::REDIS_KEY_CURRENT_POOL, json_encode($pool), self::EXPIRE);
}
}