上线代码修改

This commit is contained in:
2025-10-25 18:21:56 +08:00
parent ca1fc28af8
commit e4546acd31
2 changed files with 20 additions and 23 deletions

View File

@@ -345,14 +345,14 @@ class BlindBoxTurntableGiftDrawWorld extends Model
'draw_times' => $totalDrawTimes,
'periods' => $periods,
];
$precomputedResultss[] = [
'gift_user_id' => $giftUserId,
'gift_bag_detail' => $selectedGift,
'gift' => $gift,
'draw_times' => $totalDrawTimes,
'periods' => $periods,
];
$totalDrawTimes++;
// $precomputedResultss[] = [
// 'gift_user_id' => $giftUserId,
// 'gift_bag_detail' => $selectedGift,
// 'gift' => $gift,
// 'draw_times' => $totalDrawTimes,
// 'periods' => $periods,
// ];
// $totalDrawTimes++;
$currentXlhPeriodsNum++;
$addcurrentXlhPeriodsNum++;
@@ -361,6 +361,7 @@ class BlindBoxTurntableGiftDrawWorld extends Model
}
}
}
$totalDrawTimes = 0;
}
// 再从新奖池中分配剩余所需礼物
@@ -581,6 +582,16 @@ class BlindBoxTurntableGiftDrawWorld extends Model
// 批量更新
foreach ($inventoryUpdates as $giftId => $count) {
$giftBagDetail = Db::name("vs_gift_bag_detail")
->where('id', $giftId)
->find();
if (!$giftBagDetail) {
throw new \Exception("礼物详情不存在ID: " . $giftId);
}
// 检查库存是否足够
if ($giftBagDetail['remaining_number'] < $count) {
throw new \Exception("礼物库存不足ID: " . $giftId);
}
$ret = db::name("vs_gift_bag_detail")->where('id',$giftId)
->setDec('remaining_number', $count);
if (!$ret) {

View File

@@ -1411,29 +1411,15 @@ class GiveGift extends Model
if (empty($gift_bag_details)) {
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;
$weight_reduction = 0; // 可以根据需求调整权重降低幅度,例如设置为上次礼物剩余数量的一半
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'];
}
}
$rand_value = mt_rand(1, $total_remaining);
$current_sum = 0;
$gift_bag_detail = null;
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){
continue;
}