(account, productName, productNum, exchangeNum)
| 100 | }); |
| 101 | |
| 102 | async function ExchangeForAccount(account, productName, productNum, exchangeNum) { |
| 103 | // 先只查询商品信息(不查询积分) |
| 104 | const productList = await ListProduct(account); |
| 105 | |
| 106 | // 查找目标商品 |
| 107 | const product = productList.find(t => t.title == productName); |
| 108 | if (!product) { |
| 109 | return { success: false, insufficient: false, message: `查询商品失败: 未找到"${productName}"` }; |
| 110 | } |
| 111 | |
| 112 | // 先检查商品库存,如果库存为0则直接返回,不查询积分 |
| 113 | if (!product.remain_amount) { |
| 114 | $.info(`查询商品: ${productName}, 库存: 0`); |
| 115 | return { success: false, insufficient: false, message: `抢购终止: 商品库存为0` }; |
| 116 | } |
| 117 | |
| 118 | // 库存充足时才查询积分 |
| 119 | const userPoint = await GetUserPoint(account); |
| 120 | $.info(`查询商品: ${productName}, 库存: ${product.remain_amount}, 当前积分: ${userPoint}`); |
| 121 | |
| 122 | // 检查积分是否足够 |
| 123 | if (userPoint < product.real_cost) { |
| 124 | return { |
| 125 | success: false, |
| 126 | insufficient: true, |
| 127 | message: `抢购终止: 积分不足 (需要${product.real_cost}, 当前${userPoint})` |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | // 计算兑换数量 |
| 132 | const num = productNum > 0 ? Math.min(productNum, Math.floor(userPoint / product.real_cost)) : Math.floor(userPoint / product.real_cost); |
| 133 | |
| 134 | if (num <= 0) { |
| 135 | return { |
| 136 | success: false, |
| 137 | insufficient: true, |
| 138 | message: `抢购终止: 积分不足以兑换` |
| 139 | }; |
| 140 | } |
| 141 | |
| 142 | // 开始抢购 |
| 143 | for (let i = 0; i < exchangeNum; i++) { |
| 144 | const result = await StartExchange(account, product, num, i); |
| 145 | if (result.success) { |
| 146 | return { |
| 147 | success: true, |
| 148 | insufficient: false, |
| 149 | message: `抢购成功: 第${i + 1}次, 数量: ${num}, 消耗积分: ${num * product.real_cost}` |
| 150 | }; |
| 151 | } |
| 152 | if (i === exchangeNum - 1) { |
| 153 | return { |
| 154 | success: false, |
| 155 | insufficient: false, |
| 156 | message: `抢购失败: 已尝试${exchangeNum}次 (${result.message})` |
| 157 | }; |
| 158 | } |
| 159 | } |
no test coverage detected