盲盒加上次抽中礼物加权

This commit is contained in:
2025-10-25 16:05:11 +08:00
parent 113923f8ad
commit 53d4e4c380
2 changed files with 26 additions and 10 deletions

View File

@@ -125,28 +125,28 @@ class Level extends adminApi
if($id == ''){ if($id == ''){
return V(0,"参数错误"); return V(0,"参数错误");
} }
if($level){ if($level!=''){
$level_data['level'] = $level; $level_data['level'] = $level;
} }
if($name){ if($name!=''){
$level_data['name'] = $name; $level_data['name'] = $name;
} }
if($image){ if($image!=''){
$level_data['image'] = $image; $level_data['image'] = $image;
} }
if($status){ if($status!=''){
$level_data['status'] = $status; $level_data['status'] = $status;
} }
if($change_value){ if($change_value!=''){
$level_data['change_value'] = $change_value; $level_data['change_value'] = $change_value;
} }
if($coins){ if($coins!=''){
$level_data['coins'] = $coins; $level_data['coins'] = $coins;
} }
if($bg_image){ if($bg_image!=''){
$level_data['bg_image'] = $bg_image; $level_data['bg_image'] = $bg_image;
} }
if($color){ if($color!=''){
$level_data['color'] = $color; $level_data['color'] = $color;
} }
$level_data['updatetime'] = time(); $level_data['updatetime'] = time();

View File

@@ -140,7 +140,9 @@ class GiveGift extends Model
$gift_box1 = []; $gift_box1 = [];
$gift_box = []; $gift_box = [];
//查询礼物是否是盲盒 //查询礼物是否是盲盒
if($gift_info['label'] == 2){//趣味礼物(盲盒) $gift_info_label = Db::name('vs_gift')->where(['gid'=>$gid])
->field('label')->find();
if($gift_info_label['label'] == 2){//趣味礼物(盲盒)
$gift_price_box = 0; $gift_price_box = 0;
if(isset($ext['is_draw_gift']) && $ext['is_draw_gift'] == 1){ if(isset($ext['is_draw_gift']) && $ext['is_draw_gift'] == 1){
//收礼记录行为日志 //收礼记录行为日志
@@ -1409,15 +1411,29 @@ class GiveGift extends Model
if (empty($gift_bag_details)) { if (empty($gift_bag_details)) {
return ['code' => 0, 'msg' => '当前盲盒无可用礼物', 'data' => []]; return ['code' => 0, 'msg' => '当前盲盒无可用礼物', 'data' => []];
} }
//上一次抽取的礼物
$last_receive_log = db::name("vs_gift_bag_receive_log")->where(['gift_bag_id'=>$gift_bag_id,'user_id'=>$user_id])->order('id desc')->find();
$last_receive_gift_id = $last_receive_log ? $last_receive_log['gift_id'] : 0;
// 实现加权随机算法:剩余数量越多,被抽中的概率越大 // 实现加权随机算法:剩余数量越多,被抽中的概率越大
$total_remaining = 0; $total_remaining = 0;
$weight_reduction = 0; // 可以根据需求调整权重降低幅度,例如设置为上次礼物剩余数量的一半
foreach ($gift_bag_details as $gift) { foreach ($gift_bag_details as $gift) {
if ($gift['foreign_id'] == $last_receive_gift_id) {
// 对上次抽中的礼物降低权重,例如减少其剩余数量的一半作为惩罚因子
$reduced_remaining = max(1, floor($gift['remaining_number'] / 2));
$total_remaining += $reduced_remaining;
} else {
$total_remaining += $gift['remaining_number']; $total_remaining += $gift['remaining_number'];
} }
}
$rand_value = mt_rand(1, $total_remaining); $rand_value = mt_rand(1, $total_remaining);
$current_sum = 0; $current_sum = 0;
$gift_bag_detail = null; $gift_bag_detail = null;
foreach ($gift_bag_details as $gift) { foreach ($gift_bag_details as $gift) {
// 如果是上次抽中的礼物,则使用调整后的剩余数量
if ($gift['foreign_id'] == $last_receive_gift_id) {
$gift['remaining_number'] = max(1, floor($gift['remaining_number'] / 2));
}
if($gift['remaining_number'] <= 0){ if($gift['remaining_number'] <= 0){
continue; continue;
} }