巡乐会抽奖接口提交.-联调-抽奖逻辑调整
This commit is contained in:
@@ -196,13 +196,20 @@ class BlindBoxTurntableGift extends Model
|
||||
* 单人单次抽奖
|
||||
*/
|
||||
public function draw_gift_one($gift_bag_id, $user_id, $gift_user_id,$gift_price,$periods,$room_id=0,$box_turntable_log=0){
|
||||
//奖池总的抽奖次数
|
||||
$total_quantity = db::name("vs_gift_bag_detail")->where(['gift_bag_id' => $gift_bag_id])->sum('quantity');
|
||||
$total_remaining = db::name("vs_room_pan")->where(['room_id'=>$room_id,'gift_bag_id'=>$gift_bag_id])->value('remaining_number');
|
||||
//本期当前第多少次后抽奖 总的抽奖次数- 剩余数量
|
||||
$total_draw_times = $total_quantity - $total_remaining;
|
||||
//随机获取一个礼物
|
||||
$where = [
|
||||
'a.gift_bag_id' => $gift_bag_id,
|
||||
'a.quantity' => ['>',0],
|
||||
'b.remaining_number' => ['>',0],
|
||||
'b.room_id' => $room_id
|
||||
'b.room_id' => $room_id,
|
||||
];
|
||||
// 使用闭包条件来处理复杂的 weight 逻辑
|
||||
$where['a.weight'] = ['exp', Db::raw('= 0 OR a.weight < '.$total_draw_times)];
|
||||
// 优化:基于剩余数量的加权随机选择
|
||||
$gift_bag_details = db::name("vs_gift_bag_detail")
|
||||
->field('a.id,a.quantity,b.remaining_number,a.weight,a.foreign_id')
|
||||
@@ -213,26 +220,18 @@ class BlindBoxTurntableGift extends Model
|
||||
if (empty($gift_bag_details)) {
|
||||
return ['code' => 0, 'msg' => '当前盲盒无可用礼物', 'data' => []];
|
||||
}
|
||||
//奖池总的抽奖次数
|
||||
$total_quantity = db::name("vs_gift_bag_detail")->where(['gift_bag_id' => $gift_bag_id])->sum('quantity');
|
||||
// 实现加权随机算法:剩余数量越多,被抽中的概率越大
|
||||
$total_remaining = 0;
|
||||
$remaining = 0;
|
||||
foreach ($gift_bag_details as $gift) {
|
||||
$total_remaining += $gift['remaining_number'];
|
||||
$remaining += $gift['remaining_number'];
|
||||
}
|
||||
//本期当前第多少次后抽奖 总的抽奖次数- 剩余数量
|
||||
$total_draw_times = $total_quantity - $total_remaining;
|
||||
|
||||
$rand_value = mt_rand(1, $total_remaining);
|
||||
$rand_value = mt_rand(1, $remaining);
|
||||
$current_sum = 0;
|
||||
$gift_bag_detail = null;
|
||||
foreach ($gift_bag_details as $gift) {
|
||||
if($gift['remaining_number'] <= 0){
|
||||
continue;
|
||||
}
|
||||
if($total_draw_times < $gift['weight']){
|
||||
continue;
|
||||
}
|
||||
$current_sum += $gift['remaining_number'];
|
||||
if ($rand_value <= $current_sum) {
|
||||
$gift_bag_detail = $gift;
|
||||
@@ -494,16 +493,24 @@ class BlindBoxTurntableGift extends Model
|
||||
}
|
||||
$is_zhong_jiang = 0;
|
||||
$pan_xlh_num = $pan_xlh['num'];
|
||||
//奖池总的抽奖次数
|
||||
$total_quantity = db::name("vs_gift_bag_detail")->where(['gift_bag_id' => $gift_bag_id])->sum('quantity');
|
||||
|
||||
db::startTrans();
|
||||
try{
|
||||
for($i = 0; $i < $num; $i++){
|
||||
//本期当前第多少次后抽奖 总的抽奖次数- 剩余数量
|
||||
$total_remaining = db::name("vs_room_pan")->where(['room_id'=>$room_id,'gift_bag_id'=>$gift_bag_id])->value('remaining_number');
|
||||
$total_draw_times = $total_quantity - $total_remaining;
|
||||
//随机获取一个礼物
|
||||
$where = [
|
||||
'a.gift_bag_id' => $gift_bag_id,
|
||||
'a.quantity' => ['>',0],
|
||||
'b.remaining_number' => ['>',0],
|
||||
'b.room_id' => $room_id
|
||||
'b.room_id' => $room_id,
|
||||
];
|
||||
// 使用闭包条件来处理复杂的 weight 逻辑
|
||||
$where['a.weight'] = ['exp', Db::raw('= 0 OR a.weight < '.$total_draw_times)];
|
||||
// 优化:基于剩余数量的加权随机选择
|
||||
$gift_bag_details = db::name("vs_gift_bag_detail")
|
||||
->field('a.id,a.quantity,b.remaining_number,a.weight,a.foreign_id')
|
||||
@@ -511,29 +518,23 @@ class BlindBoxTurntableGift extends Model
|
||||
->join('vs_room_pan b','b.gift_bag_detail_id = a.id','left')
|
||||
->where($where)
|
||||
->select();
|
||||
print_r($gift_bag_details);die;
|
||||
if (empty($gift_bag_details)) {
|
||||
return ['code' => 0, 'msg' => '当前盲盒无可用礼物', 'data' => []];
|
||||
}
|
||||
//奖池总的抽奖次数
|
||||
$total_quantity = db::name("vs_gift_bag_detail")->where(['gift_bag_id' => $gift_bag_id])->sum('quantity');
|
||||
// 实现加权随机算法:剩余数量越多,被抽中的概率越大
|
||||
$total_remaining = 0;
|
||||
foreach ($gift_bag_details as $gift) {
|
||||
$total_remaining += $gift['remaining_number'];
|
||||
}
|
||||
//本期当前第多少次后抽奖 总的抽奖次数- 剩余数量
|
||||
$total_draw_times = $total_quantity - $total_remaining;
|
||||
|
||||
$rand_value = mt_rand(1, $total_remaining);
|
||||
// 实现加权随机算法:剩余数量越多,被抽中的概率越大
|
||||
$remaining = 0;
|
||||
foreach ($gift_bag_details as $gift) {
|
||||
$remaining += $gift['remaining_number'];
|
||||
}
|
||||
$rand_value = mt_rand(1, $remaining);
|
||||
$current_sum = 0;
|
||||
$gift_bag_detail = null;
|
||||
foreach ($gift_bag_details as $gift) {
|
||||
if($gift['remaining_number'] <= 0){
|
||||
continue;
|
||||
}
|
||||
if($total_draw_times < $gift['weight']){
|
||||
continue;
|
||||
}
|
||||
$current_sum += $gift['remaining_number'];
|
||||
if ($rand_value <= $current_sum) {
|
||||
$gift_bag_detail = $gift;
|
||||
|
||||
Reference in New Issue
Block a user