盲盒转盘bug修改

This commit is contained in:
2025-09-16 18:08:15 +08:00
parent da92bf2134
commit 8b0597423f
2 changed files with 72 additions and 50 deletions

View File

@@ -538,29 +538,42 @@ class BlindBoxTurntableGift extends Model
// 生成缓存键 // 生成缓存键
$available_cache_key = 'blindbox_available_gifts_' . $gift_bag_id . '_' . $room_id; $available_cache_key = 'blindbox_available_gifts_' . $gift_bag_id . '_' . $room_id;
$last_remaining_cache_key = 'blindbox_last_remaining_' . $gift_bag_id . '_' . $room_id; $last_remaining_cache_key = 'blindbox_last_remaining_' . $gift_bag_id . '_' . $room_id;
// 保证有可用礼物
if (empty($available_gifts)) { if (empty($available_gifts)) {
return false; return false;
} }
$last_remaining_all = array_sum(array_column($last_periods_remaining, 'remaining_number')); $last_remaining_all = array_sum(array_column($last_periods_remaining, 'remaining_number'));
if ($last_remaining_all == 0) { if ($last_remaining_all == 0) {
$last_periods_remaining = []; $last_periods_remaining = [];
cache($last_remaining_cache_key, null); cache($last_remaining_cache_key, null);
} }
$last_periods_remaining_flag = 0; $last_periods_remaining_flag = 0;
if (!empty($last_periods_remaining)) { if (!empty($last_periods_remaining)) {
$available_gifts = $last_periods_remaining; $available_gifts = $last_periods_remaining;
//操作上期奖池标识
$last_periods_remaining_flag = 1; $last_periods_remaining_flag = 1;
} }
// 循环尝试直到抽中有效礼物
$max_attempts = 5; // 最大尝试次数,防止无限循环
$attempt = 0;
$selected_gift = null;
while ($attempt < $max_attempts && !$selected_gift) {
// 实现加权随机算法:剩余数量越多,被抽中的概率越大 // 实现加权随机算法:剩余数量越多,被抽中的概率越大
$remaining = 0; $remaining = 0;
foreach ($available_gifts as $gift) { foreach ($available_gifts as $gift) {
$remaining += $gift['remaining_number']; $remaining += $gift['remaining_number'];
} }
if ($remaining <= 0) {
break; // 如果没有剩余数量,跳出循环
}
$rand_value = mt_rand(1, $remaining); $rand_value = mt_rand(1, $remaining);
$current_sum = 0; $current_sum = 0;
$selected_gift = null;
foreach ($available_gifts as $gift) { foreach ($available_gifts as $gift) {
if ($gift['remaining_number'] <= 0) { if ($gift['remaining_number'] <= 0) {
@@ -572,9 +585,12 @@ class BlindBoxTurntableGift extends Model
break; break;
} }
} }
$attempt++;
}
if (!$selected_gift) { if (!$selected_gift) {
return false; return false;
} }
// 获取开出礼物的信息 // 获取开出礼物的信息
$gift = db::name("vs_gift") $gift = db::name("vs_gift")
->where(['gid' => $selected_gift['foreign_id']]) ->where(['gid' => $selected_gift['foreign_id']])
@@ -583,6 +599,7 @@ class BlindBoxTurntableGift extends Model
if (!$gift) { if (!$gift) {
return false; return false;
} }
// 操作缓存,减去缓存中对应数据数量 // 操作缓存,减去缓存中对应数据数量
foreach ($available_gifts as &$available_gifts_gift) { foreach ($available_gifts as &$available_gifts_gift) {
if ($selected_gift['id'] == $available_gifts_gift['id']) { if ($selected_gift['id'] == $available_gifts_gift['id']) {
@@ -631,6 +648,7 @@ class BlindBoxTurntableGift extends Model
$play_image[] = $draw_gift['play_image']; $play_image[] = $draw_gift['play_image'];
$text_message = $user_nickname . '在' . $room_name . '房间送给了' . $ToUserInfo['nickname'] . $draw_gift['gift_name'] . 'X' . $value['count']."\n"; $text_message = $user_nickname . '在' . $room_name . '房间送给了' . $ToUserInfo['nickname'] . $draw_gift['gift_name'] . 'X' . $value['count']."\n";
if($draw_gift['is_public_server'] == 1) {
$text_list_new[] = [ $text_list_new[] = [
'text' => $text_message, 'text' => $text_message,
'gift_picture' => $draw_gift['base_image'], 'gift_picture' => $draw_gift['base_image'],
@@ -641,6 +659,7 @@ class BlindBoxTurntableGift extends Model
'roomId' => $room_id, 'roomId' => $room_id,
'number' => $value['count'], 'number' => $value['count'],
]; ];
}
} }
@@ -666,7 +685,7 @@ class BlindBoxTurntableGift extends Model
//聊天室推送系统消息 //聊天室推送系统消息
model('Chat')->sendMsg(1028,$room_id,$text1); model('Chat')->sendMsg(1028,$room_id,$text1);
}else{ }else{
if($draw_gift['is_public_server'] == 1){ if(!empty($text_list_new)){
//推送礼物横幅 //推送礼物横幅
$push = new Push($blind_box_turntable['user_id'], $room_id); $push = new Push($blind_box_turntable['user_id'], $room_id);
$push->giftBanner($text_list_new); $push->giftBanner($text_list_new);

View File

@@ -35,7 +35,7 @@ class RoomPan
} }
echo "开始发放".count($blind_box_turntable)." \n"; echo "开始发放".count($blind_box_turntable)." \n";
foreach ($blind_box_turntable as $k => $v) { foreach ($blind_box_turntable as $k => $v) {
$blind_box_turntable_results_log = db('vs_blind_box_turntable_results_log')->where('tid',$v['id'])->find(); $blind_box_turntable_results_log = db('vs_blind_box_turntable_results_log')->where('tid',$v['id'])->select();
if(empty($blind_box_turntable_results_log)){ if(empty($blind_box_turntable_results_log)){
echo $v['id']." 没有需要发放的礼物 \n"; echo $v['id']." 没有需要发放的礼物 \n";
continue; continue;
@@ -53,6 +53,7 @@ class RoomPan
$play_image[] = $draw_gift['play_image']; $play_image[] = $draw_gift['play_image'];
$text_message = $text_message . '在' . $room_name . '房间送给了' . $ToUserInfo['nickname'] . $draw_gift['gift_name'] . 'X' . $value['count']."\n"; $text_message = $text_message . '在' . $room_name . '房间送给了' . $ToUserInfo['nickname'] . $draw_gift['gift_name'] . 'X' . $value['count']."\n";
if($draw_gift['is_public_server'] == 1) {
$text_list_new[] = [ $text_list_new[] = [
'text' => $text_message, 'text' => $text_message,
'gift_picture' => $draw_gift['base_image'], 'gift_picture' => $draw_gift['base_image'],
@@ -64,7 +65,9 @@ class RoomPan
'number' => $value['count'], 'number' => $value['count'],
]; ];
} }
}
$text = [ $text = [
'FromUserInfo' => $FromUserInfo,
'GiftInfo' => [ 'GiftInfo' => [
'play_image' => implode(',',$play_image), 'play_image' => implode(',',$play_image),
], ],
@@ -85,7 +88,7 @@ class RoomPan
//聊天室推送系统消息 //聊天室推送系统消息
model('Chat')->sendMsg(1028,$room_id,$text1); model('Chat')->sendMsg(1028,$room_id,$text1);
}else{ }else{
if($draw_gift['is_public_server'] == 1){ if(!empty($text_list_new)){
//推送礼物横幅 //推送礼物横幅
$push = new Push($v['user_id'], $room_id); $push = new Push($v['user_id'], $room_id);
$push->giftBanner($text_list_new); $push->giftBanner($text_list_new);