盲盒转盘 优化-后台重置加清除缓存 提交

This commit is contained in:
2025-10-29 17:57:30 +08:00
parent 78a35941da
commit 0b4abbe3d6

View File

@@ -171,11 +171,10 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
$newGiftsNeeded = max(0, $total_num - $remainingGiftCount); // 计算还需要多少礼物从新奖池中抽取 $newGiftsNeeded = max(0, $total_num - $remainingGiftCount); // 计算还需要多少礼物从新奖池中抽取
// 先从上期剩余礼物中分配 // 先从上期剩余礼物中分配
if (!empty($remaining_available_gifts)) { if (!empty($remaining_available_gifts)) {
$aliasTableForRemaining = $this->buildAliasTable($remaining_available_gifts);
// 计算上期剩余礼物总数 // 计算上期剩余礼物总数
foreach ($gift_user_ids as $giftUserId) { foreach ($gift_user_ids as $giftUserId) {
// 为每个用户先分配上期剩余礼物 // 为每个用户先分配上期剩余礼物
$userRemainingAllocation = floor($remainingGiftCount / $total_num); $userRemainingAllocation = floor($remainingGiftCount / count($gift_user_ids));
if (count($gift_user_ids) > 0) { // 防止除零错误 if (count($gift_user_ids) > 0) { // 防止除零错误
$extraGifts = $remainingGiftCount % count($gift_user_ids); $extraGifts = $remainingGiftCount % count($gift_user_ids);
if (array_search($giftUserId, $gift_user_ids) < $extraGifts) { if (array_search($giftUserId, $gift_user_ids) < $extraGifts) {
@@ -183,19 +182,26 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
} }
} }
for ($i = 0; $i < $userRemainingAllocation; $i++) { for ($i = 0; $i < $userRemainingAllocation; $i++) {
$selectedGift = $this->selectGiftWithAliasMethod($aliasTableForRemaining); if(!empty($remaining_available_gifts)){
if ($selectedGift) { $randomKey = array_rand($remaining_available_gifts);
$gift = $giftInfoMap[$selectedGift['foreign_id']]; $selectedGift = $remaining_available_gifts[$randomKey];
$precomputedResults[] = [ --$remaining_available_gifts[$randomKey]['remaining_number'];
'gift_user_id' => $giftUserId, if($remaining_available_gifts[$randomKey]['remaining_number'] <=0){
'gift_bag_detail' => $selectedGift, unset($remaining_available_gifts[$randomKey]);
'gift' => $gift, }
'draw_times' => $this->getCachedPanDrawTimes($gift_bag_id), if ($selectedGift) {
'periods' => $periods, $gift = $giftInfoMap[$selectedGift['foreign_id']];
]; $precomputedResults[] = [
$this->getCachedXlhPeriodsNum("set");//添加寻乐会条件次数 'gift_user_id' => $giftUserId,
$pan_total_draw_times = $this->getCachedPanDrawTimes($gift_bag_id,"set"); // 总抽奖次数 '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");//总抽奖次数重置 $pan_total_draw_times = $this->getCachedPanDrawTimes($gift_bag_id,"clear");//总抽奖次数重置
@@ -214,7 +220,22 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
} }
for ($i = 0; $i < $userNewAllocation; $i++) { for ($i = 0; $i < $userNewAllocation; $i++) {
$selectedGift = $this->selectGiftWithAliasMethod($aliasTableForNew); $selectedGift = $this->selectGiftWithAliasMethod($aliasTableForNew);
if(!$selectedGift){
//重置奖池
$availableGifts = $this->resetPoolAndReload($gift_bag_id);
if (empty($availableGifts)) {
throw new \Exception('重置奖池后仍无可用礼物');
}
$this->getCachedPanDrawTimes($gift_bag_id,"clear");//总抽奖次数重置
$aliasTableForNew = $this->buildAliasTable($availableGifts);
$selectedGift = $this->selectGiftWithAliasMethod($aliasTableForNew);
}
if ($selectedGift) { if ($selectedGift) {
$giftBagDetailCached = $this->getCachedGiftBagDetailItem($gift_bag_id, $selectedGift['id']);
if($giftBagDetailCached['remaining_number']<=0){
$aliasTableForNew = $this->buildAliasTable($availableGifts);
$selectedGift = $this->selectGiftWithAliasMethod($aliasTableForNew);
}
$gift = $giftInfoMap[$selectedGift['foreign_id']]??[]; $gift = $giftInfoMap[$selectedGift['foreign_id']]??[];
$precomputedResults[] = [ $precomputedResults[] = [
'gift_user_id' => $giftUserId, 'gift_user_id' => $giftUserId,
@@ -400,6 +421,12 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
} }
$attempt++; $attempt++;
} }
// 兜底方案:遍历查找第一个有库存的礼物
foreach ($aliasTable['index_map'] as $gift) {
if ($gift['remaining_number'] > 0) {
return $gift;
}
}
// 如果重试次数用完仍未抽中有效礼物返回null // 如果重试次数用完仍未抽中有效礼物返回null
return null; return null;
} }
@@ -477,7 +504,9 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
if ($gift_bag_detail_data) { if ($gift_bag_detail_data) {
foreach ($gift_bag_detail_data as &$item) { foreach ($gift_bag_detail_data as &$item) {
if ($item['id'] == $detail_id) { if ($item['id'] == $detail_id) {
$item['remaining_number'] -= $decrement; if( $item['remaining_number'] >= $decrement){
$item['remaining_number'] -= $decrement;
}
break; break;
} }
} }
@@ -715,6 +744,9 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
}else{ }else{
$upRemainingNumber = $giftBagDetailCached['remaining_number']; $upRemainingNumber = $giftBagDetailCached['remaining_number'];
} }
if($upRemainingNumber < 0){
throw new \Exception('礼物数量不足');
}
$ret = db::name("vs_gift_bag_detail")->where('id',$giftId)->update([ $ret = db::name("vs_gift_bag_detail")->where('id',$giftId)->update([
'remaining_number' => $upRemainingNumber 'remaining_number' => $upRemainingNumber
]); ]);