fix(batch-mining): 修正阶段划分逻辑

preMineDays 表示该批次比最后批次提前多少天开始挖矿
阶段天数 = 当前批次的preMineDays - 下一批次的preMineDays(差值)

例如:批次1(3天)、批次2(2天)、批次3(1天)、批次4(0天)
- 阶段1:只有批次1,持续 3-2=1 天
- 阶段2:批次1+2,持续 2-1=1 天
- 阶段3:批次1+2+3,持续 1-0=1 天
- 最后阶段:所有批次一起挖剩余天数

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-21 23:33:58 -08:00
parent 2358b3ea17
commit 9e9c791283
1 changed files with 38 additions and 23 deletions

View File

@ -350,18 +350,18 @@ export class BatchMiningService {
/**
*
*
* "提前天数"
* - preMineDays
* - 1 preMineDays=3 12313
* - 2 preMineDays=2 2321+22
* - 3 preMineDays=1 3411+2+31
* - 4 preMineDays=0 4
* preMineDays
* - 1 preMineDays=3 13
* - 2 preMineDays=2 22
* - 3 preMineDays=1 31
* - 4 preMineDays=0 4
*
*
* - 阶段1: 只有批次11 preMineDays 2
* - 阶段2: 批次1+22 preMineDays 3
* - 阶段3: 批次1+2+33 preMineDays 4
* - 最后阶段: 所有批次一起挖 (totalMiningDays - )
* preMineDays
* - preMineDays 排序后为: 批次1(3), 2(2), 3(1), 4(0)
* - 阶段1: 只有批次1 3-2=1
* - 阶段2: 批次1+2 2-1=1
* - 阶段3: 批次1+2+3 1-0=1
* - 最后阶段: 所有批次一起挖剩余天数
*/
private buildMiningPhases(
items: BatchMiningItem[],
@ -395,22 +395,37 @@ export class BatchMiningService {
return phases;
}
// 按 preMineDays 从大到小排序批次(提前天数多的批次先开始挖)
const batchesByPreMineDays = [...sortedBatches].sort((a, b) => {
const preA = batchPreMineDays.get(a) || 0;
const preB = batchPreMineDays.get(b) || 0;
return preB - preA; // 降序
});
this.logger.log(`[buildMiningPhases] 按提前天数排序后的批次: ${batchesByPreMineDays.join(', ')}`);
let currentPhase = 1;
let participatingBatches: number[] = [];
let usedDays = 0; // 已分配的天数
let usedDays = 0;
// 按批次顺序添加阶段
// 关键:先用当前批次的 preMineDays 创建阶段,然后下一批次加入
for (let i = 0; i < sortedBatches.length; i++) {
const currentBatch = sortedBatches[i];
// 按 preMineDays 从大到小处理批次
for (let i = 0; i < batchesByPreMineDays.length; i++) {
const currentBatch = batchesByPreMineDays[i];
const currentPreMineDays = batchPreMineDays.get(currentBatch) || 0;
// 先把当前批次加入参与列表
// 计算当前阶段的天数 = 当前批次的 preMineDays - 下一批次的 preMineDays
let nextPreMineDays = 0;
if (i + 1 < batchesByPreMineDays.length) {
const nextBatch = batchesByPreMineDays[i + 1];
nextPreMineDays = batchPreMineDays.get(nextBatch) || 0;
}
const phaseDays = currentPreMineDays - nextPreMineDays;
// 当前批次加入参与列表
participatingBatches.push(currentBatch);
// 该阶段持续天数 = 当前批次的 preMineDays当前批次比下一批次提前的天数
// 如果 preMineDays > 0说明当前参与批次要在下一批次加入前独挖这些天
if (currentPreMineDays > 0) {
if (phaseDays > 0) {
// 计算该阶段参与用户的总算力
let participatingContribution = new Decimal(0);
for (const batch of participatingBatches) {
@ -421,14 +436,14 @@ export class BatchMiningService {
phaseNumber: currentPhase,
startDate: new Date(),
endDate: new Date(),
daysInPhase: currentPreMineDays,
daysInPhase: phaseDays,
participatingContribution,
participatingBatches: [...participatingBatches],
});
this.logger.log(`[buildMiningPhases] 阶段${currentPhase}: ${currentPreMineDays}天, 批次[${participatingBatches.join(',')}], 参与算力=${participatingContribution.toFixed(2)}`);
this.logger.log(`[buildMiningPhases] 阶段${currentPhase}: ${phaseDays}天, 批次[${participatingBatches.join(',')}], 参与算力=${participatingContribution.toFixed(2)}`);
currentPhase++;
usedDays += currentPreMineDays;
usedDays += phaseDays;
}
}