mirror of
https://github.com/tiennm99/WPlace-AutoBOT.git
synced 2026-10-13 10:15:29 +00:00
Fix runtime and add check every 30s
This commit is contained in:
1 parent
197a10205c
commit
fa8e87f955
1 file changed
+82
-63
+82
-63
@@ -1120,80 +1120,99 @@
|
||||
state.paintedMap = Array(height).fill().map(() => Array(width).fill(false));
|
||||
}
|
||||
|
||||
outerLoop:
|
||||
for (let y = startRow; y < height; y++) {
|
||||
for (let x = (y === startRow ? startCol : 0); x < width; x++) {
|
||||
// If stop is requested, save the last position and exit
|
||||
if (state.stopFlag) {
|
||||
state.lastPosition = { x, y };
|
||||
updateUI('paintingPaused', 'warning', { x, y });
|
||||
break outerLoop;
|
||||
}
|
||||
|
||||
// Skip pixel if already painted (for resume)
|
||||
if (state.paintedMap[y][x]) continue;
|
||||
|
||||
const idx = (y * width + x) * 4;
|
||||
const r = pixels[idx];
|
||||
const g = pixels[idx + 1];
|
||||
const b = pixels[idx + 2];
|
||||
const alpha = pixels[idx + 3];
|
||||
|
||||
// Skip transparent or white pixels
|
||||
if (alpha < CONFIG.TRANSPARENCY_THRESHOLD) continue;
|
||||
if (Utils.isWhitePixel(r, g, b)) continue;
|
||||
|
||||
const rgb = [r, g, b];
|
||||
const colorId = findClosestColor(rgb, state.availableColors);
|
||||
|
||||
if (state.currentCharges < 1) {
|
||||
updateUI('noCharges', 'warning', { time: Utils.formatTime(state.cooldown) });
|
||||
await Utils.sleep(state.cooldown);
|
||||
|
||||
const chargeUpdate = await WPlaceService.getCharges();
|
||||
// Setup interval to refresh getCharges every 30s while painting
|
||||
if (window._chargesInterval) clearInterval(window._chargesInterval);
|
||||
window._chargesInterval = setInterval(() => {
|
||||
// Don't use await in setInterval directly, use then()
|
||||
if (state.running && !state.stopFlag) {
|
||||
WPlaceService.getCharges().then(chargeUpdate => {
|
||||
state.currentCharges = chargeUpdate.charges;
|
||||
state.cooldown = chargeUpdate.cooldown;
|
||||
}
|
||||
updateStats();
|
||||
});
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
const pixelX = startX + x;
|
||||
const pixelY = startY + y;
|
||||
|
||||
const success = await WPlaceService.paintPixelInRegion(
|
||||
regionX,
|
||||
regionY,
|
||||
pixelX,
|
||||
pixelY,
|
||||
colorId
|
||||
);
|
||||
|
||||
// If CAPTCHA/token error, stop and notify
|
||||
if (success === 'token_error') {
|
||||
state.stopFlag = true;
|
||||
updateUI('captchaNeeded', 'error');
|
||||
Utils.showAlert(Utils.t('captchaNeeded'), 'error');
|
||||
try {
|
||||
outerLoop:
|
||||
for (let y = startRow; y < height; y++) {
|
||||
for (let x = (y === startRow ? startCol : 0); x < width; x++) {
|
||||
// If stop is requested, save the last position and exit
|
||||
if (state.stopFlag) {
|
||||
state.lastPosition = { x, y };
|
||||
updateUI('paintingPaused', 'warning', { x, y });
|
||||
break outerLoop;
|
||||
}
|
||||
}
|
||||
|
||||
if (success) {
|
||||
state.paintedPixels++;
|
||||
state.currentCharges--;
|
||||
state.paintedMap[y][x] = true; // Mark pixel as painted
|
||||
// Skip pixel if already painted (for resume)
|
||||
if (state.paintedMap[y][x]) continue;
|
||||
|
||||
state.estimatedTime = Utils.calculateEstimatedTime(
|
||||
state.totalPixels - state.paintedPixels,
|
||||
state.currentCharges,
|
||||
state.cooldown
|
||||
const idx = (y * width + x) * 4;
|
||||
const r = pixels[idx];
|
||||
const g = pixels[idx + 1];
|
||||
const b = pixels[idx + 2];
|
||||
const alpha = pixels[idx + 3];
|
||||
|
||||
// Skip transparent or white pixels
|
||||
if (alpha < CONFIG.TRANSPARENCY_THRESHOLD) continue;
|
||||
if (Utils.isWhitePixel(r, g, b)) continue;
|
||||
|
||||
const rgb = [r, g, b];
|
||||
const colorId = findClosestColor(rgb, state.availableColors);
|
||||
|
||||
if (state.currentCharges < 1) {
|
||||
updateUI('noCharges', 'warning', { time: Utils.formatTime(state.cooldown) });
|
||||
await Utils.sleep(state.cooldown);
|
||||
|
||||
const chargeUpdate = await WPlaceService.getCharges();
|
||||
state.currentCharges = chargeUpdate.charges;
|
||||
state.cooldown = chargeUpdate.cooldown;
|
||||
}
|
||||
|
||||
const pixelX = startX + x;
|
||||
const pixelY = startY + y;
|
||||
|
||||
const success = await WPlaceService.paintPixelInRegion(
|
||||
regionX,
|
||||
regionY,
|
||||
pixelX,
|
||||
pixelY,
|
||||
colorId
|
||||
);
|
||||
|
||||
if (state.paintedPixels % CONFIG.LOG_INTERVAL === 0) {
|
||||
updateStats();
|
||||
updateUI('paintingProgress', 'default', {
|
||||
painted: state.paintedPixels,
|
||||
total: state.totalPixels
|
||||
});
|
||||
// If CAPTCHA/token error, stop and notify
|
||||
if (success === 'token_error') {
|
||||
state.stopFlag = true;
|
||||
updateUI('captchaNeeded', 'error');
|
||||
Utils.showAlert(Utils.t('captchaNeeded'), 'error');
|
||||
break outerLoop;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
state.paintedPixels++;
|
||||
state.currentCharges--;
|
||||
state.paintedMap[y][x] = true; // Mark pixel as painted
|
||||
|
||||
state.estimatedTime = Utils.calculateEstimatedTime(
|
||||
state.totalPixels - state.paintedPixels,
|
||||
state.currentCharges,
|
||||
state.cooldown
|
||||
);
|
||||
|
||||
if (state.paintedPixels % CONFIG.LOG_INTERVAL === 0) {
|
||||
updateStats();
|
||||
updateUI('paintingProgress', 'default', {
|
||||
painted: state.paintedPixels,
|
||||
total: state.totalPixels
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
// Always clear the interval when painting stops or finishes
|
||||
if (window._chargesInterval) clearInterval(window._chargesInterval);
|
||||
window._chargesInterval = null;
|
||||
}
|
||||
|
||||
// If stopped, keep last position for resume; if finished, reset lastPosition and paintedMap
|
||||
|
||||
Reference in new issue
Block a user