从新设计抽奖逻辑

This commit is contained in:
2026-03-06 16:02:17 +08:00
parent 931af70c36
commit 27f95a303a
4 changed files with 198 additions and 61 deletions

View File

@@ -20,18 +20,23 @@ use support\think\Cache;
* @property $ui_text 前端显示文本
* @property $real_ev 真实资金结算
* @property $tier 所属档位
* @property $s_end_index 顺时针结束索引
* @property $n_end_index 逆时针结束索引
* @property $remark 备注
* @property $create_time 创建时间
* @property $update_time 修改时间
*/
class DiceRewardConfig extends BaseModel
{
/** 缓存键:全玩家通用的奖励配置列表 */
private const CACHE_KEY_LIST = 'dice:reward_config:list';
/** 缓存键:彩金池奖励列表实例(含列表与索引) */
private const CACHE_KEY_INSTANCE = 'dice:reward_config:instance';
/** 缓存过期时间(秒),保存时会主动刷新故设较长 */
private const CACHE_TTL = 86400 * 30;
/** 当前请求内已加载的实例,避免同请求多次读缓存 */
private static ?array $instance = null;
/**
* 数据表主键
* @var string
@@ -44,28 +49,89 @@ class DiceRewardConfig extends BaseModel
*/
protected $table = 'dice_reward_config';
/**
* 获取彩金池实例(含 list / 索引),无则从库加载并写入缓存;同请求内复用
* @return array{list: array, by_tier: array, by_s_end_index: array, by_n_end_index: array, min_real_ev: float}
*/
public static function getCachedInstance(): array
{
if (self::$instance !== null) {
return self::$instance;
}
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
if ($instance !== null && is_array($instance)) {
self::$instance = $instance;
return $instance;
}
self::refreshCache();
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
self::$instance = is_array($instance) ? $instance : self::buildEmptyInstance();
return self::$instance;
}
/**
* 获取缓存的奖励列表(无则从库加载并写入缓存)
* @return array<int, array>
*/
public static function getCachedList(): array
{
$list = Cache::get(self::CACHE_KEY_LIST);
if ($list !== null && is_array($list)) {
return $list;
}
self::refreshCache();
$list = Cache::get(self::CACHE_KEY_LIST);
return is_array($list) ? $list : [];
$inst = self::getCachedInstance();
return $inst['list'] ?? [];
}
/**
* 重新从数据库加载并写入缓存(保存时调用)
* 重新从数据库加载并写入缓存(保存时调用),构建列表与索引
*/
public static function refreshCache(): void
{
$list = (new self())->order('id', 'asc')->select()->toArray();
Cache::set(self::CACHE_KEY_LIST, $list, self::CACHE_TTL);
$byTier = [];
$bySEndIndex = [];
$byNEndIndex = [];
foreach ($list as $row) {
$tier = isset($row['tier']) ? (string) $row['tier'] : '';
if ($tier !== '') {
if (!isset($byTier[$tier])) {
$byTier[$tier] = [];
}
$byTier[$tier][] = $row;
}
$sEnd = isset($row['s_end_index']) ? (int) $row['s_end_index'] : 0;
if ($sEnd !== 0) {
if (!isset($bySEndIndex[$sEnd])) {
$bySEndIndex[$sEnd] = [];
}
$bySEndIndex[$sEnd][] = $row;
}
$nEnd = isset($row['n_end_index']) ? (int) $row['n_end_index'] : 0;
if ($nEnd !== 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_s_end_index' => $bySEndIndex,
'by_n_end_index' => $byNEndIndex,
'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_s_end_index' => [],
'by_n_end_index' => [],
'min_real_ev' => 0.0,
];
}
/**
@@ -73,13 +139,8 @@ class DiceRewardConfig extends BaseModel
*/
public static function getCachedMinRealEv(): float
{
$list = self::getCachedList();
if (empty($list)) {
return 0.0;
}
$vals = array_column($list, 'real_ev');
$min = min($vals);
return (float) $min;
$inst = self::getCachedInstance();
return (float) ($inst['min_real_ev'] ?? 0.0);
}
/**
@@ -88,14 +149,39 @@ class DiceRewardConfig extends BaseModel
*/
public static function getCachedByTier(string $tier): array
{
$list = self::getCachedList();
$rows = [];
foreach ($list as $row) {
if (isset($row['tier']) && (string) $row['tier'] === $tier) {
$rows[] = $row;
}
}
return $rows;
$inst = self::getCachedInstance();
$byTier = $inst['by_tier'] ?? [];
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;
}
/** 保存后刷新缓存 */