用户询问:物品消耗,允许冻结,要优先冻结,目前是这么设计的么?
经过代码分析发现,当前物品消耗逻辑没有优先消耗冻结物品。
在 app/Module/GameItems/Logics/Item.php 的 consumeNormalItem 方法中:
// 第345行:只按过期时间排序
$userItems = $query->orderBy('expire_at')->get();
问题:
include_frozen=true 时,冻结和未冻结物品混合在一起expire_at 排序,没有优先消耗冻结物品include_frozen=true),应该优先消耗冻结物品修改 app/Module/GameItems/Logics/Item.php 第344-345行:
// 修改前
$userItems = $query->orderBy('expire_at')->get();
// 修改后
if ($includeFrozen) {
// 当包含冻结物品时,优先消耗冻结物品,再消耗未冻结物品
$userItems = $query->orderBy('is_frozen', 'desc') // 冻结物品优先(true > false)
->orderBy('expire_at') // 然后按过期时间排序
->get();
} else {
// 只消耗未冻结物品时,按过期时间排序
$userItems = $query->orderBy('expire_at')->get();
}
orderBy('is_frozen', 'desc'):冻结物品优先(true > false)orderBy('expire_at'):在同一冻结状态内,按过期时间排序include_frozen=false 时,保持原有逻辑不变创建了 tests/manual_test_consume_frozen_priority.php 测试脚本:
初始状态:
测试1:消耗50个物品
测试2:再消耗80个物品
=== 物品消耗优先冻结物品测试 ===
4. 测试优先消耗冻结物品(消耗50个)...
消耗50个物品: 成功
6. 验证结果(第一次消耗)...
✅ 测试通过:优先消耗了冻结物品
7. 测试消耗超过冻结数量(消耗80个)...
消耗80个物品: 成功
9. 验证最终结果...
✅ 测试通过:正确消耗了混合物品
include_frozen 默认为 false,只消耗未冻结物品include_frozen=true 时,优先消耗冻结物品is_frozen 字段的排序app/Module/GameItems/Logics/Item.php
consumeNormalItem 方法的排序逻辑tests/manual_test_consume_frozen_priority.php:手动测试脚本tests/Unit/GameItems/ItemConsumeFrozenTest.php:增加优先消耗测试方法通过优化物品消耗逻辑的排序机制,成功实现了优先消耗冻结物品的功能:
这个优化不仅解决了用户提出的问题,还为Mex模块等需要消耗冻结物品的业务场景提供了更好的支持。