优化中奖权重计算方式
This commit is contained in:
@@ -8,7 +8,7 @@ namespace app\dice\model\play_record;
|
||||
|
||||
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
use app\dice\model\player\DicePlayer;
|
||||
use app\dice\model\reward_config\DiceRewardConfig;
|
||||
use app\dice\model\reward\DiceRewardConfig;
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
|
||||
139
server/app/dice/model/reward/DiceReward.php
Normal file
139
server/app/dice/model/reward/DiceReward.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\reward;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use support\think\Cache;
|
||||
|
||||
/**
|
||||
* 奖励对照模型
|
||||
*
|
||||
* dice_reward 奖励对照表(主键 id 自增)
|
||||
* 唯一约束 (direction, grid_number),保证每个点数、每个方向各一条;end_index 关联 DiceRewardConfig.id
|
||||
*
|
||||
* @property $id 主键
|
||||
* @property $tier 档位 T1-T5/BIGWIN
|
||||
* @property $direction 方向:0=顺时针,1=逆时针
|
||||
* @property $end_index 结束索引(DiceRewardConfig.id)
|
||||
* @property $weight 权重 1-10000,档位内按权重比抽取
|
||||
* @property $grid_number 色子点数(摇取值)
|
||||
* @property $start_index 起始索引(DiceRewardConfig.id)
|
||||
* @property $ui_text 显示文本(来自config)
|
||||
* @property $real_ev 实际中奖金额(来自config)
|
||||
* @property $remark 备注(来自config)
|
||||
* @property $type 奖励类型(来自config)
|
||||
*/
|
||||
class DiceReward extends BaseModel
|
||||
{
|
||||
/** 方向:顺时针 */
|
||||
public const DIRECTION_CLOCKWISE = 0;
|
||||
/** 方向:逆时针 */
|
||||
public const DIRECTION_COUNTERCLOCKWISE = 1;
|
||||
|
||||
/** 缓存键:奖励对照实例 */
|
||||
private const CACHE_KEY_INSTANCE = 'dice:reward:instance';
|
||||
|
||||
private const CACHE_TTL = 86400 * 30;
|
||||
|
||||
private static ?array $instance = null;
|
||||
|
||||
protected $table = 'dice_reward';
|
||||
|
||||
/** 主键 id 自增,唯一约束 (direction, grid_number) */
|
||||
protected $pk = 'id';
|
||||
|
||||
/**
|
||||
* 获取奖励对照实例(按档位+方向索引,用于抽奖与权重配比)
|
||||
* @return array{list: array, by_tier_direction: array}
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按档位+方向取权重列表(用于抽奖:该档位该方向下 end_index => weight)
|
||||
* @return array<int, int> end_index => weight
|
||||
*/
|
||||
public static function getCachedByTierAndDirection(string $tier, int $direction): array
|
||||
{
|
||||
$inst = self::getCachedInstance();
|
||||
$byTierDirection = $inst['by_tier_direction'] ?? [];
|
||||
$list = $byTierDirection[$tier][$direction] ?? [];
|
||||
$result = [];
|
||||
foreach ($list as $row) {
|
||||
$endIndex = isset($row['end_index']) ? (int) $row['end_index'] : 0;
|
||||
$weight = isset($row['weight']) ? (int) $row['weight'] : 1;
|
||||
$result[$endIndex] = $weight;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新从数据库加载并写入缓存;修改/新增/删除后需调用以实例化
|
||||
*/
|
||||
public static function refreshCache(): void
|
||||
{
|
||||
$list = (new self())->order('tier')->order('direction')->order('end_index')->select()->toArray();
|
||||
$byTierDirection = [];
|
||||
foreach ($list as $row) {
|
||||
$tier = isset($row['tier']) ? (string) $row['tier'] : '';
|
||||
$direction = isset($row['direction']) ? (int) $row['direction'] : 0;
|
||||
if ($tier !== '') {
|
||||
if (!isset($byTierDirection[$tier])) {
|
||||
$byTierDirection[$tier] = [0 => [], 1 => []];
|
||||
}
|
||||
if (!isset($byTierDirection[$tier][$direction])) {
|
||||
$byTierDirection[$tier][$direction] = [];
|
||||
}
|
||||
$byTierDirection[$tier][$direction][] = $row;
|
||||
}
|
||||
}
|
||||
self::$instance = [
|
||||
'list' => $list,
|
||||
'by_tier_direction' => $byTierDirection,
|
||||
];
|
||||
Cache::set(self::CACHE_KEY_INSTANCE, self::$instance, self::CACHE_TTL);
|
||||
}
|
||||
|
||||
private static function buildEmptyInstance(): array
|
||||
{
|
||||
return [
|
||||
'list' => [],
|
||||
'by_tier_direction' => [],
|
||||
];
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
9
server/app/dice/model/reward/DiceRewardConfig.php
Normal file
9
server/app/dice/model/reward/DiceRewardConfig.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 别名:reward 命名空间下引用 reward_config\DiceRewardConfig
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\reward;
|
||||
|
||||
class DiceRewardConfig extends \app\dice\model\reward_config\DiceRewardConfig
|
||||
{
|
||||
}
|
||||
@@ -6,24 +6,23 @@
|
||||
// +----------------------------------------------------------------------
|
||||
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 奖励配置
|
||||
* 按档位 T1-T5 直接权重抽取 grid_number,weight 1-10000;起始索引 s_start_index / n_start_index
|
||||
* dice_reward_config 奖励配置;BIGWIN 档位使用本表 weight(0-10000,10000=100% 中大奖)
|
||||
*
|
||||
* @property $id ID
|
||||
* @property $grid_number 色子点数
|
||||
* @property $ui_text 前端显示文本
|
||||
* @property $real_ev 真实资金结算
|
||||
* @property $tier 所属档位
|
||||
* @property $weight 权重 1-10000,档位内按权重比抽取
|
||||
* @property $n_start_index 逆时针起始索引
|
||||
* @property $s_start_index 顺时针起始索引
|
||||
* @property $weight 权重(仅 BIGWIN 使用,0-10000)
|
||||
* @property $remark 备注
|
||||
* @property $type 奖励类型 -2=唯一惩罚,-1=抽水,0=回本,1=再来一次,2=小赚,3=大奖格
|
||||
* @property $create_time 创建时间
|
||||
* @property $update_time 修改时间
|
||||
*/
|
||||
@@ -129,7 +128,7 @@ class DiceRewardConfig extends BaseModel
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓存按档位取奖励列表(含 weight,用于按权重抽 grid_number)
|
||||
* 从缓存按档位取奖励列表(不含权重,仅配置)
|
||||
*/
|
||||
public static function getCachedByTier(string $tier): array
|
||||
{
|
||||
@@ -138,6 +137,22 @@ class DiceRewardConfig extends BaseModel
|
||||
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;
|
||||
@@ -199,18 +214,4 @@ class DiceRewardConfig extends BaseModel
|
||||
$query->where('tier', '=', $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function searchWeightMinAttr($query, $value)
|
||||
{
|
||||
if ($value !== '' && $value !== null) {
|
||||
$query->where('weight', '>=', $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function searchWeightMaxAttr($query, $value)
|
||||
{
|
||||
if ($value !== '' && $value !== null) {
|
||||
$query->where('weight', '<=', $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user