Files
dafuweng-saiadmin6.x/server/app/dice/model/reward_config/DiceRewardConfig.php

218 lines
6.7 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\model\reward_config;
use app\dice\model\reward\DiceReward;
use plugin\saiadmin\basic\think\BaseModel;
use support\think\Cache;
/**
* 奖励配置模型
*
* dice_reward_config 奖励配置BIGWIN 档位使用本表 weight0-1000010000=100% 中大奖)
*
* @property $id ID
* @property $grid_number 色子点数
* @property $ui_text 前端显示文本
* @property $real_ev 真实资金结算
* @property $tier 所属档位
* @property $weight 权重(仅 BIGWIN 使用0-10000
* @property $remark 备注
* @property $type 奖励类型 -2=唯一惩罚,-1=抽水,0=回本,1=再来一次,2=小赚,3=大奖格
* @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;
protected $pk = 'id';
protected $table = 'dice_reward_config';
/**
* 获取彩金池实例(含 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
{
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;
}
public static function getCachedList(): array
{
$inst = self::getCachedInstance();
return $inst['list'] ?? [];
}
/**
* 重新从数据库加载并写入缓存(按档位+权重抽 grid_number含 by_tier、by_tier_grid
*/
public static function refreshCache(): void
{
$list = (new self())->order('id', 'asc')->select()->toArray();
$byTier = [];
$byTierGrid = [];
foreach ($list as $row) {
$tier = isset($row['tier']) ? (string) $row['tier'] : '';
if ($tier !== '') {
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] = [];
}
if (!isset($byTierGrid[$tier][$gridNum])) {
$byTierGrid[$tier][$gridNum] = $row;
}
}
}
$minRealEv = empty($list) ? 0.0 : (float) min(array_column($list, 'real_ev'));
self::$instance = [
'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' => [],
'min_real_ev' => 0.0,
];
}
/**
* 按档位+色子点数取一条(用于 BIGWIN
*/
public static function getCachedByTierAndGridNumber(string $tier, int $gridNumber): ?array
{
$inst = self::getCachedInstance();
$byTierGrid = $inst['by_tier_grid'] ?? [];
$tierData = $byTierGrid[$tier] ?? [];
$row = $tierData[$gridNumber] ?? null;
return is_array($row) ? $row : null;
}
public static function getCachedMinRealEv(): float
{
$inst = self::getCachedInstance();
return (float) ($inst['min_real_ev'] ?? 0.0);
}
/**
* 从缓存按档位取奖励列表(不含权重,仅配置)
*/
public static function getCachedByTier(string $tier): array
{
$inst = self::getCachedInstance();
$byTier = $inst['by_tier'] ?? [];
return $byTier[$tier] ?? [];
}
/**
* 按档位+方向取奖励列表(合并 dice_reward 权重,用于抽奖)
* @param int $direction 0=顺时针, 1=逆时针
* @return array 每行含 id, grid_number, real_ev, tier, weight 等
*/
public static function getCachedByTierForDirection(string $tier, int $direction): array
{
$list = self::getCachedByTier($tier);
$weightMap = DiceReward::getCachedByTierAndDirection($tier, $direction);
foreach ($list as $i => $row) {
$id = isset($row['id']) ? (int) $row['id'] : 0;
$list[$i]['weight'] = $weightMap[$id] ?? 1;
}
return $list;
}
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) {
$query->where('grid_number', '>=', $value);
}
}
public function searchGridNumberMaxAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('grid_number', '<=', $value);
}
}
public function searchUiTextAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('ui_text', 'like', '%' . $value . '%');
}
}
public function searchRealEvMinAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('real_ev', '>=', $value);
}
}
public function searchRealEvMaxAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('real_ev', '<=', $value);
}
}
public function searchTierAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('tier', '=', $value);
}
}
}