盲盒转盘优化-重构-调试
This commit is contained in:
@@ -353,248 +353,6 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
||||
return $gift_detail_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 触发奖池重新生成
|
||||
*/
|
||||
// private function triggerPoolRegeneration($gift_bag_id)
|
||||
// {
|
||||
// // 检查是否已经在重新生成奖池
|
||||
// $regenLockKey = "blind_box_pool_regen_lock:{$gift_bag_id}";
|
||||
// $lockAcquired = $this->redis->setnx($regenLockKey, 1);
|
||||
//
|
||||
// if ($lockAcquired) {
|
||||
// // 设置锁的过期时间
|
||||
// $this->redis->expire($regenLockKey, 300); // 5分钟过期
|
||||
//
|
||||
// // 触发异步奖池生成
|
||||
// // 这里可以使用队列系统来处理
|
||||
// $this->regenerateResultPool($gift_bag_id);
|
||||
// }
|
||||
// }
|
||||
/**
|
||||
* 生成奖池并存入Redis
|
||||
*/
|
||||
// public function generateGiftPool($gift_bag_id)
|
||||
// {
|
||||
// try {
|
||||
// // 获取奖池基本信息
|
||||
// $poolKey = "blind_box_pool:{$gift_bag_id}";
|
||||
// $poolInfoKey = "blind_box_pool_info:{$gift_bag_id}";
|
||||
// // 检查Redis连接
|
||||
// if (!$this->redis) {
|
||||
// throw new \Exception('Redis连接不可用');
|
||||
// }
|
||||
// // 获取数据库中的期数信息
|
||||
// $dbPeriods = db::name("vs_gift_bag")
|
||||
// ->where(['id' => $gift_bag_id])
|
||||
// ->value('periods');
|
||||
// // 获取奖池详细信息
|
||||
// $gift_bag_detail_data = $this->getCachedGiftBagDetail($gift_bag_id);
|
||||
//
|
||||
// // 获取可用礼物列表
|
||||
// $availableGifts = [];
|
||||
// foreach ($gift_bag_detail_data as $gift) {
|
||||
// if ($gift['remaining_number'] > 0) {
|
||||
// // 根据权重添加礼物到可选列表
|
||||
// for ($i = 0; $i < $gift['weight']; $i++) {
|
||||
// $availableGifts[] = $gift;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if (empty($availableGifts)) {
|
||||
// throw new \Exception('没有可用的礼物');
|
||||
// }
|
||||
// // 生成大量抽奖结果(例如1000个)
|
||||
// $results = [];
|
||||
// for ($i = 0; $i < 1000; $i++) {
|
||||
// $randomIndex = array_rand($availableGifts);
|
||||
// $selectedGift = $availableGifts[$randomIndex];
|
||||
//
|
||||
// // 获取礼物详细信息
|
||||
// $giftInfo = db::name("vs_gift")
|
||||
// ->where(['gid' => $selectedGift['foreign_id']])
|
||||
// ->find();
|
||||
//
|
||||
// $results[] = [
|
||||
// 'gift_bag_detail' => $selectedGift,
|
||||
// 'gift' => $giftInfo ?? []
|
||||
// ];
|
||||
// }
|
||||
//
|
||||
// // 使用事务确保原子操作
|
||||
// $this->redis->watch($poolKey, $poolInfoKey);
|
||||
// $this->redis->multi();
|
||||
//
|
||||
// // 清空现有奖池
|
||||
// $this->redis->del($poolKey);
|
||||
//
|
||||
// // 添加新结果到奖池
|
||||
// foreach ($results as $result) {
|
||||
// $this->redis->rPush($poolKey, json_encode($result));
|
||||
// }
|
||||
//
|
||||
// // 更新奖池信息
|
||||
// $this->redis->hMSet($poolInfoKey, [
|
||||
// 'periods' => $dbPeriods,
|
||||
// 'draw_times' => 0,
|
||||
// 'total_count' => count($results),
|
||||
// 'last_update' => time()
|
||||
// ]);
|
||||
//
|
||||
// $this->redis->exec();
|
||||
//
|
||||
// Log::record("奖池生成成功,gift_bag_id: {$gift_bag_id}", 'info');
|
||||
// return true;
|
||||
// } catch (\Exception $e) {
|
||||
// Log::record('生成奖池失败: ' . $e->getMessage(), 'error');
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 从奖池中获取一个礼物
|
||||
*/
|
||||
// public function getGiftFromPool($gift_bag_id)
|
||||
// {
|
||||
// try {
|
||||
// $poolKey = "blind_box_pool:{$gift_bag_id}";
|
||||
//
|
||||
// // 从Redis奖池中取出一个结果
|
||||
// $result = $this->redis->lPop($poolKey);
|
||||
//
|
||||
// if (!$result) {
|
||||
// // 奖池为空,重新生成
|
||||
// $this->generateGiftPool($gift_bag_id);
|
||||
// $result = $this->redis->lPop($poolKey);
|
||||
// }
|
||||
// return $result ? json_decode($result, true) : null;
|
||||
// } catch (\Exception $e) {
|
||||
// Log::record('从奖池获取礼物失败: ' . $e->getMessage(), 'error');
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 重置奖池
|
||||
*/
|
||||
// private function resetGiftPool($gift_bag_id)
|
||||
// {
|
||||
// try {
|
||||
// // 更新数据库中各礼物的剩余数量为初始数量
|
||||
// Db::name("vs_gift_bag_detail")
|
||||
// ->where(['gift_bag_id' => $gift_bag_id])
|
||||
// ->update(['remaining_number' => Db::raw('quantity')]);
|
||||
//
|
||||
// // 清除相关缓存
|
||||
// Cache::rm("pan_gift_bag_detail".$gift_bag_id);
|
||||
//
|
||||
// // 删除Redis中的奖池数据
|
||||
// $poolKey = "blind_box_pool:{$gift_bag_id}";
|
||||
// $poolInfoKey = "blind_box_pool_info:{$gift_bag_id}";
|
||||
// $this->redis->del($poolKey);
|
||||
// $this->redis->del($poolInfoKey);
|
||||
// // 重新生成奖池
|
||||
// $this->regenerateResultPool($gift_bag_id);
|
||||
// return true;
|
||||
// } catch (\Exception $e) {
|
||||
// Log::record('重置奖池失败: ' . $e->getMessage(), 'error');
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
/**
|
||||
* 重新生成结果奖池
|
||||
*/
|
||||
// private function regenerateResultPool($gift_bag_id)
|
||||
// {
|
||||
// try {
|
||||
// // 计算奖池信息
|
||||
// $poolInfo = $this->calculatePoolInfo($gift_bag_id);
|
||||
// if ($poolInfo['code'] !== 1) {
|
||||
// throw new \Exception('计算奖池信息失败');
|
||||
// }
|
||||
//
|
||||
// $totalDrawTimes = $poolInfo['data']['total_draw_times'];
|
||||
// $periods = $poolInfo['data']['periods'];
|
||||
//
|
||||
// // 获取可用礼物
|
||||
// $availableGifts = $this->getAvailableGifts($gift_bag_id);
|
||||
// if (empty($availableGifts)) {
|
||||
// $availableGifts = $this->resetPoolAndReload($gift_bag_id);
|
||||
// if (empty($availableGifts)) {
|
||||
// throw new \Exception('重置奖池后仍无可用礼物');
|
||||
// }
|
||||
// $totalDrawTimes = 0;
|
||||
// }
|
||||
//
|
||||
// // 预加载礼物信息
|
||||
// $giftInfoMap = $this->preloadGiftInfo($availableGifts);
|
||||
//
|
||||
// // 构建Alias表(别名表算法用于权重随机)
|
||||
// $aliasTable = $this->buildAliasTable($availableGifts);
|
||||
// if (!$aliasTable) {
|
||||
// throw new \Exception('构建Alias表失败');
|
||||
// }
|
||||
//
|
||||
// // 生成大量抽奖结果(例如1000个)
|
||||
// $results = [];
|
||||
// $drawTimes = $totalDrawTimes;
|
||||
//
|
||||
// for ($i = 0; $i < 1000; $i++) {
|
||||
// $selectedGift = $this->selectGiftWithAliasMethod($aliasTable, $giftInfoMap);
|
||||
// if ($selectedGift) {
|
||||
// $results[] = [
|
||||
// 'gift_bag_detail' => $selectedGift,
|
||||
// 'draw_times' => $drawTimes,
|
||||
// 'gift' => $giftInfoMap[$selectedGift['foreign_id']] ?? []
|
||||
// ];
|
||||
//
|
||||
// // 更新剩余数量
|
||||
// $this->updateGiftRemainingNumber($selectedGift['id']);
|
||||
// $drawTimes++;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 将结果存储到Redis奖池中
|
||||
// $poolKey = "blind_box_pool:{$gift_bag_id}";
|
||||
// $poolInfoKey = "blind_box_pool_info:{$gift_bag_id}";
|
||||
//
|
||||
// // 使用事务确保原子操作
|
||||
// $this->redis->watch($poolKey, $poolInfoKey);
|
||||
// $this->redis->multi();
|
||||
//
|
||||
// // 清空现有奖池
|
||||
// $this->redis->del($poolKey);
|
||||
//
|
||||
// // 添加新结果到奖池
|
||||
// foreach ($results as $result) {
|
||||
// $this->redis->rPush($poolKey, json_encode($result));
|
||||
// }
|
||||
//
|
||||
// // 更新奖池信息
|
||||
// $this->redis->hMSet($poolInfoKey, [
|
||||
// 'periods' => $periods,
|
||||
// 'draw_times' => $drawTimes,
|
||||
// 'total_count' => count($results),
|
||||
// 'last_update' => time()
|
||||
// ]);
|
||||
//
|
||||
// $this->redis->exec();
|
||||
//
|
||||
// // 释放重新生成锁
|
||||
// $regenLockKey = "blind_box_pool_regen_lock:{$gift_bag_id}";
|
||||
// $this->redis->del($regenLockKey);
|
||||
//
|
||||
// Log::record("奖池重新生成成功,gift_bag_id: {$gift_bag_id}", 'info');
|
||||
//
|
||||
// } catch (\Exception $e) {
|
||||
// Log::record('重新生成奖池失败: ' . $e->getMessage(), 'error');
|
||||
//
|
||||
// // 释放重新生成锁
|
||||
// $regenLockKey = "blind_box_pool_regen_lock:{$gift_bag_id}";
|
||||
// $this->redis->del($regenLockKey);
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 重置奖池并重新加载
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user