定版之后bug修改--寻乐会报错修复。

This commit is contained in:
2025-09-20 00:04:11 +08:00
parent d668ec5e74
commit a9e0411993
2 changed files with 42 additions and 18 deletions

View File

@@ -496,12 +496,13 @@ class BlindBoxTurntableGiftDraw extends Model
->where([
'room_id' => $room_id,
'gift_bag_detail_id' => $giftId,
'remaining_number' => ['>=', $count]
// 'remaining_number' => ['>=', $count]
])
->setDec('remaining_number', $count);
if (!$ret) {
throw new \Exception('更新礼物剩余数量失败');
Log::record('巡乐会更新礼物剩余数量: ' . $room_id."【数据】".var_export($precomputedResults, true),"info");
// throw new \Exception('更新礼物剩余数量失败');
}
}
}
@@ -911,6 +912,9 @@ class BlindBoxTurntableGiftDraw extends Model
* @param int $periods 期数
*/
private function reset_gift_pool($room_id, $gift_bag_id, $periods,$remaining_available_gifts=[]) {
$room_pan = db::name("vs_room_pan")
->where(['room_id'=>$room_id,'gift_bag_id'=>$gift_bag_id])
->select();
// 重置奖池中所有礼物数量
db::name("vs_room_pan")
->where(['room_id'=>$room_id,'gift_bag_id'=>$gift_bag_id])
@@ -918,6 +922,13 @@ class BlindBoxTurntableGiftDraw extends Model
'remaining_number' => db::raw('(SELECT quantity FROM fa_vs_gift_bag_detail WHERE id = fa_vs_room_pan.gift_bag_detail_id)'),
'periods' => $periods
]);
//防止并发上把如果件数小于0则加上
foreach ($room_pan as $pan) {
if($pan['remaining_number']<0){
db::name("vs_room_pan")->where('id', $pan['id'])->setInc('remaining_number', $pan['remaining_number']);
}
}
//补充上把礼物有剩余
if(!empty($remaining_available_gifts)){
foreach ($remaining_available_gifts as $gift) {
db::name("vs_room_pan")->where('id', $gift['id'])->setInc('remaining_number',$gift['remaining_number']);
@@ -1106,18 +1117,23 @@ class BlindBoxTurntableGiftDraw extends Model
$total_remaining = array_sum(array_column($room_pan_data, 'remaining_number'));
$total_draw_times = $total_quantity - $total_remaining;
if ($total_draw_times < 0) $total_draw_times = 0;
// 5. 获取可用礼物(提前过滤无效礼物)
$available_gifts = [];
foreach ($room_pan_data as $pan) {
if ($pan['remaining_number'] > 0 && $pan['weight'] < $total_draw_times) {
if ($pan['remaining_number'] > 0 && $pan['weight'] <= $total_draw_times) {
$available_gifts[] = $pan;
}
}
if (empty($available_gifts)) {
return ['code' => 0, 'msg' => '当前盲盒无可用礼物', 'data' => []];
// 移除已无剩余数量的礼物
$remaining_available_gifts = array_filter($room_pan_data, function($gift) {
return $gift['remaining_number'] > 0;
});
$this->reset_gift_pool($room_id, $gift_bag_id, $room_pan_data[0]['periods'] + 1,$remaining_available_gifts);
// return ['code' => 0, 'msg' => '当前盲盒无可用礼物', 'data' => []];
}
// 6. 预计算抽奖结果
@@ -1261,7 +1277,8 @@ class BlindBoxTurntableGiftDraw extends Model
$total_processed += $current_batch;
} catch (\Exception $e) {
db::rollback();
return ['code' => 0, 'msg' => $e->getMessage(), 'data' => null];
Log::record('巡乐会抽奖失败: ' . $e->getMessage(),"infos");
return ['code' => 0, 'msg' => "抽奖中,请稍等...", 'data' => null];
}
}
@@ -1338,21 +1355,28 @@ class BlindBoxTurntableGiftDraw extends Model
if ($remaining <= 0) {
return null;
}
// 循环尝试直到抽中有效礼物
$max_attempts = 5; // 最大尝试次数,防止无限循环
$attempt = 0;
$selected_gift = null;
while ($attempt < $max_attempts && !$selected_gift) {
$rand_value = mt_rand(1, $remaining);
$current_sum = 0;
$rand_value = mt_rand(1, $remaining);
$current_sum = 0;
foreach ($available_gifts as $gift) {
if ($gift['remaining_number'] <= 0) {
continue;
}
$current_sum += $gift['remaining_number'];
if ($rand_value <= $current_sum) {
return $gift;
foreach ($available_gifts as $gift) {
if ($gift['remaining_number'] <= 0) {
continue;
}
$current_sum += $gift['remaining_number'];
if ($rand_value <= $current_sum) {
$selected_gift = $gift;
break;
}
}
$attempt++;
}
return null;
return $selected_gift;
}
/**