盲盒转盘优化-重构-调试-再加日志

This commit is contained in:
2025-10-29 17:47:12 +08:00
parent 3d4465a5c6
commit 5754f71023

View File

@@ -171,11 +171,10 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
$newGiftsNeeded = max(0, $total_num - $remainingGiftCount); // 计算还需要多少礼物从新奖池中抽取
// 先从上期剩余礼物中分配
if (!empty($remaining_available_gifts)) {
$aliasTableForRemaining = $this->buildAliasTable($remaining_available_gifts);
// 计算上期剩余礼物总数
foreach ($gift_user_ids as $giftUserId) {
// 为每个用户先分配上期剩余礼物
$userRemainingAllocation = floor($remainingGiftCount / $total_num);
$userRemainingAllocation = floor($remainingGiftCount / count($gift_user_ids));
if (count($gift_user_ids) > 0) { // 防止除零错误
$extraGifts = $remainingGiftCount % count($gift_user_ids);
if (array_search($giftUserId, $gift_user_ids) < $extraGifts) {
@@ -183,19 +182,26 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
}
}
for ($i = 0; $i < $userRemainingAllocation; $i++) {
$selectedGift = $this->selectGiftWithAliasMethod($aliasTableForRemaining);
if ($selectedGift) {
$gift = $giftInfoMap[$selectedGift['foreign_id']];
$precomputedResults[] = [
'gift_user_id' => $giftUserId,
'gift_bag_detail' => $selectedGift,
'gift' => $gift,
'draw_times' => $this->getCachedPanDrawTimes($gift_bag_id),
'periods' => $periods,
];
$this->getCachedXlhPeriodsNum("set");//添加寻乐会条件次数
$pan_total_draw_times = $this->getCachedPanDrawTimes($gift_bag_id,"set"); // 总抽奖次数
}
if(!empty($remaining_available_gifts)){
$randomKey = array_rand($remaining_available_gifts);
$selectedGift = $remaining_available_gifts[$randomKey];
--$remaining_available_gifts[$randomKey]['remaining_number'];
if($remaining_available_gifts[$randomKey]['remaining_number'] <=0){
unset($remaining_available_gifts[$randomKey]);
}
if ($selectedGift) {
$gift = $giftInfoMap[$selectedGift['foreign_id']];
$precomputedResults[] = [
'gift_user_id' => $giftUserId,
'gift_bag_detail' => $selectedGift,
'gift' => $gift,
'draw_times' => $this->getCachedPanDrawTimes($gift_bag_id),
'periods' => $periods,
];
$this->getCachedXlhPeriodsNum("set");//添加寻乐会条件次数
$pan_total_draw_times = $this->getCachedPanDrawTimes($gift_bag_id,"set"); // 总抽奖次数
}
}
}
}
$pan_total_draw_times = $this->getCachedPanDrawTimes($gift_bag_id,"clear");//总抽奖次数重置
@@ -258,20 +264,6 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
}
}
}
if (count($precomputedResults) != $total_num) {
// 记录错误到Redis
// 使用正确的Redis方法存储数据
$key = 'blind_box_draw_world_errors_' . date('Y-m-d-H-i-s');
$errorData = [
'total_num' => $total_num,
'chag_total_num' => count($precomputedResults),
'precomputedResults' => $precomputedResults,
'availableGifts' => $availableGifts,
'remaining_available_gifts' => $remaining_available_gifts,
];
$this->redis->setex($key, 86400 * 7, "超出数量".json_encode($errorData));
return ['code' => 0, 'msg' => '网络加载失败,请重试!', 'data' => null];
}
return ['precomputedResults' => $precomputedResults, 'precomputedResultss' => $precomputedResultss];
}