adjust health percent

This commit is contained in:
MaysWind 2016-05-25 23:20:42 +08:00
parent ad2a9e6c7f
commit bb88dd8be3

View file

@ -158,7 +158,7 @@
} }
}, },
estimateCompletedPercentFromBitField: function (bitfield) { estimateCompletedPercentFromBitField: function (bitfield) {
var totalLength = bitfield.length * 16; var totalLength = bitfield.length * 0xf;
var completedLength = 0; var completedLength = 0;
if (totalLength == 0) { if (totalLength == 0) {
@ -177,13 +177,21 @@
return task.completePercent; return task.completePercent;
} }
var bitfieldArr = new Array(task.bitfield.length); var bitfieldCompletedArr = new Array(task.bitfield.length);
var totalLength = task.bitfield.length * 16; var bitfieldPieceArr = new Array(task.bitfield.length);
var totalLength = task.bitfield.length * 0xf;
var healthBitCount = 0; var healthBitCount = 0;
for (var i = 0; i < task.bitfield.length; i++) { for (var i = 0; i < task.bitfield.length; i++) {
var num = parseInt(task.bitfield[i], 16); var num = parseInt(task.bitfield[i], 16);
bitfieldArr[i] = num; bitfieldCompletedArr[i] = 0;
bitfieldPieceArr[i] = 0;
if (num == 0xf) {
bitfieldCompletedArr[i] = num;
} else {
bitfieldPieceArr[i] = num;
}
} }
for (var i = 0; i < peers.length; i++) { for (var i = 0; i < peers.length; i++) {
@ -192,20 +200,29 @@
for (var j = 0; j < bitfield.length; j++) { for (var j = 0; j < bitfield.length; j++) {
var num = parseInt(bitfield[j], 16); var num = parseInt(bitfield[j], 16);
bitfieldArr[j] += num;
if (num == 0xf) {
bitfieldCompletedArr[j] += num;
} else {
bitfieldPieceArr[j] = Math.max(bitfieldPieceArr[j], num);
} }
} }
}
for (var i = 0; i < bitfieldCompletedArr.length; i++) {
bitfieldCompletedArr[i] += bitfieldPieceArr[i];
}
while (true) { while (true) {
var completed = true; var completed = true;
for (var i = 0; i < bitfieldArr.length; i++) { for (var i = 0; i < bitfieldCompletedArr.length; i++) {
if (bitfieldArr[i] > 16) { if (bitfieldCompletedArr[i] > 0xf) {
healthBitCount += 16; healthBitCount += 0xf;
bitfieldArr[i] -= 16; bitfieldCompletedArr[i] -= 0xf;
} else { } else {
healthBitCount += bitfieldArr[i]; healthBitCount += bitfieldCompletedArr[i];
bitfieldArr[i] = 0; bitfieldCompletedArr[i] = 0;
completed = false; completed = false;
} }
} }
@ -215,7 +232,13 @@
} }
} }
return healthBitCount / totalLength * 100; var healthPercent = healthBitCount / totalLength * 100;
if (healthPercent < task.completePercent) {
healthPercent = task.completePercent;
}
return healthPercent;
} }
}; };
}]); }]);