diff --git a/application/api/model/BlindBoxTurntableGift.php b/application/api/model/BlindBoxTurntableGift.php index 5ac9666..affefd5 100644 --- a/application/api/model/BlindBoxTurntableGift.php +++ b/application/api/model/BlindBoxTurntableGift.php @@ -534,70 +534,87 @@ class BlindBoxTurntableGift extends Model /* * 预计算单次抽奖结果 */ - private function precompute_single_draw($gift_bag_id,$room_id,$available_gifts,$last_periods_remaining=[]) { + private function precompute_single_draw($gift_bag_id, $room_id, $available_gifts, $last_periods_remaining = []) { // 生成缓存键 $available_cache_key = 'blindbox_available_gifts_' . $gift_bag_id . '_' . $room_id; $last_remaining_cache_key = 'blindbox_last_remaining_' . $gift_bag_id . '_' . $room_id; + + // 保证有可用礼物 if (empty($available_gifts)) { return false; } + $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 = []; cache($last_remaining_cache_key, null); } + $last_periods_remaining_flag = 0; - if(!empty($last_periods_remaining)){ + if (!empty($last_periods_remaining)) { $available_gifts = $last_periods_remaining; - //操作上期奖池标识 $last_periods_remaining_flag = 1; } - // 实现加权随机算法:剩余数量越多,被抽中的概率越大 - $remaining = 0; - foreach ($available_gifts as $gift) { - $remaining += $gift['remaining_number']; - } - $rand_value = mt_rand(1, $remaining); - $current_sum = 0; + // 循环尝试直到抽中有效礼物 + $max_attempts = 5; // 最大尝试次数,防止无限循环 + $attempt = 0; $selected_gift = null; - foreach ($available_gifts as $gift) { - if($gift['remaining_number'] <= 0){ - continue; + while ($attempt < $max_attempts && !$selected_gift) { + // 实现加权随机算法:剩余数量越多,被抽中的概率越大 + $remaining = 0; + foreach ($available_gifts as $gift) { + $remaining += $gift['remaining_number']; } - $current_sum += $gift['remaining_number']; - if ($rand_value <= $current_sum) { - $selected_gift = $gift; - break; + + if ($remaining <= 0) { + break; // 如果没有剩余数量,跳出循环 } + + $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) { + $selected_gift = $gift; + break; + } + } + $attempt++; } if (!$selected_gift) { return false; } + // 获取开出礼物的信息 $gift = db::name("vs_gift") - ->where(['gid'=>$selected_gift['foreign_id']]) + ->where(['gid' => $selected_gift['foreign_id']]) ->find(); if (!$gift) { return false; } - //操作缓存,减去缓存中对应数据数量 + + // 操作缓存,减去缓存中对应数据数量 foreach ($available_gifts as &$available_gifts_gift) { - if($selected_gift['id'] == $available_gifts_gift['id']){ + if ($selected_gift['id'] == $available_gifts_gift['id']) { $available_gifts_gift['remaining_number'] -= 1; } } - if($available_gifts_gift['remaining_number'] == 0){ + if ($available_gifts_gift['remaining_number'] == 0) { unset($available_gifts_gift); } - if($last_periods_remaining_flag == 1){ - //操作上一轮奖池 - cache($last_remaining_cache_key,$available_gifts); - }else{ - cache($available_cache_key,$available_gifts); + if ($last_periods_remaining_flag == 1) { + // 操作上一轮奖池 + cache($last_remaining_cache_key, $available_gifts); + } else { + cache($available_cache_key, $available_gifts); } return [ @@ -631,16 +648,18 @@ class BlindBoxTurntableGift extends Model $play_image[] = $draw_gift['play_image']; $text_message = $user_nickname . '在' . $room_name . '房间送给了' . $ToUserInfo['nickname'] . $draw_gift['gift_name'] . 'X' . $value['count']."\n"; - $text_list_new[] = [ - 'text' => $text_message, - 'gift_picture' => $draw_gift['base_image'], - 'room_id' => $room_id, - 'fromUserName' => $FromUserInfo['nickname'], - 'toUserName' => $ToUserInfo['nickname'], - 'giftName' => $draw_gift['gift_name'], - 'roomId' => $room_id, - 'number' => $value['count'], - ]; + if($draw_gift['is_public_server'] == 1) { + $text_list_new[] = [ + 'text' => $text_message, + 'gift_picture' => $draw_gift['base_image'], + 'room_id' => $room_id, + 'fromUserName' => $FromUserInfo['nickname'], + 'toUserName' => $ToUserInfo['nickname'], + 'giftName' => $draw_gift['gift_name'], + 'roomId' => $room_id, + 'number' => $value['count'], + ]; + } } @@ -666,7 +685,7 @@ class BlindBoxTurntableGift extends Model //聊天室推送系统消息 model('Chat')->sendMsg(1028,$room_id,$text1); }else{ - if($draw_gift['is_public_server'] == 1){ + if(!empty($text_list_new)){ //推送礼物横幅 $push = new Push($blind_box_turntable['user_id'], $room_id); $push->giftBanner($text_list_new); diff --git a/application/cron/controller/RoomPan.php b/application/cron/controller/RoomPan.php index aa29f12..759669d 100644 --- a/application/cron/controller/RoomPan.php +++ b/application/cron/controller/RoomPan.php @@ -35,7 +35,7 @@ class RoomPan } echo "开始发放".count($blind_box_turntable)." \n"; 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)){ echo $v['id']." 没有需要发放的礼物 \n"; continue; @@ -53,18 +53,21 @@ class RoomPan $play_image[] = $draw_gift['play_image']; $text_message = $text_message . '在' . $room_name . '房间送给了' . $ToUserInfo['nickname'] . $draw_gift['gift_name'] . 'X' . $value['count']."\n"; - $text_list_new[] = [ - 'text' => $text_message, - 'gift_picture' => $draw_gift['base_image'], - 'room_id' => $room_id, - 'fromUserName' => $FromUserInfo['nickname'], - 'toUserName' => $ToUserInfo['nickname'], - 'giftName' => $draw_gift['gift_name'], - 'roomId' => $room_id, - 'number' => $value['count'], - ]; + if($draw_gift['is_public_server'] == 1) { + $text_list_new[] = [ + 'text' => $text_message, + 'gift_picture' => $draw_gift['base_image'], + 'room_id' => $room_id, + 'fromUserName' => $FromUserInfo['nickname'], + 'toUserName' => $ToUserInfo['nickname'], + 'giftName' => $draw_gift['gift_name'], + 'roomId' => $room_id, + 'number' => $value['count'], + ]; + } } $text = [ + 'FromUserInfo' => $FromUserInfo, 'GiftInfo' => [ 'play_image' => implode(',',$play_image), ], @@ -85,7 +88,7 @@ class RoomPan //聊天室推送系统消息 model('Chat')->sendMsg(1028,$room_id,$text1); }else{ - if($draw_gift['is_public_server'] == 1){ + if(!empty($text_list_new)){ //推送礼物横幅 $push = new Push($v['user_id'], $room_id); $push->giftBanner($text_list_new);