Skip to content
This repository was archived by the owner on Jan 18, 2021. It is now read-only.

Commit 4348bd2

Browse files
authored
Merge pull request #18 from bschultz/fix-auto-clear-quests
Fix auto clear quests
2 parents aa91fee + aeee6f9 commit 4348bd2

3 files changed

Lines changed: 36 additions & 32 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"start": "node src/index.js",
88
"create-locales": "node src/createLocales.js",
99
"test": "npx eslint src/**/*.js",
10-
"test-fix": "npx eslint src/**/*.js --fix"
10+
"test-fix": "npx eslint src/**/*.js --fix",
11+
"update": "npm install && npm update pogo-translations && npm run create-locales"
1112
},
1213
"repository": {
1314
"type": "git",

src/controllers/instances/auto.js

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ const Account = require('../../models/account.js');
88
const Cell = require('../../models/cell.js');
99
const Pokestop = require('../../models/pokestop.js');
1010

11-
const ClearQuestsInterval = 60 * 1000; // every minute
12-
1311
const AutoType = {
1412
Quest: 'quest'
1513
};
@@ -91,9 +89,22 @@ class AutoInstanceController {
9189
this.bootstrap()
9290
.then(x => x)
9391
.catch(err => {
94-
console.error('[AutoInstanceController] Failed to bootstrap instance:', this.name, err);
92+
console.error('[AutoInstanceController] Failed to bootstrap instance:', this.name, err);
9593
});
96-
this.clearQuestsTimer = setInterval(() => this.clearQuests(), ClearQuestsInterval);
94+
this.setClearQuestsTimer();
95+
}
96+
97+
setClearQuestsTimer() {
98+
let date = new Date();
99+
// TODO: timeZone
100+
let hour = date.getHours();
101+
let minute = date.getMinutes();
102+
let second = date.getSeconds();
103+
let timeLeftSeconds = (23 - hour) * 3600 + (59 - minute) * 60 + (60 - second);
104+
let at = new Date(date.getTime() + (timeLeftSeconds * 1000));
105+
console.debug(`[AutoInstanceController] [${this.name}] Clearing Quests in ${timeLeftSeconds.toLocaleString()}s at ${at.toLocaleString()} (Currently: ${date.toLocaleString()})`);
106+
107+
this.clearQuestsTimer = setInterval(() => this.clearQuests(), timeLeftSeconds * 1000);
97108
}
98109

99110
async bootstrap() {
@@ -455,37 +466,29 @@ class AutoInstanceController {
455466
}
456467

457468
async clearQuests() {
458-
let date = new Date();
459-
// TODO: timeZone
460-
let hour = date.getHours();
461-
let minute = date.getMinutes();
462-
let second = date.getSeconds();
463-
let timeLeft = (23 - hour) * 3600 + (59 - minute) * 60 + (60 - second);
464-
let at = new Date(date.getTime() + (timeLeft * 1000));
465-
console.debug(`[AutoInstanceController] [${this.name}] Clearing Quests in ${timeLeft.toLocaleString()}s at ${at.toLocaleString()} (Currently: ${date.toLocaleString()})`);
469+
clearInterval(this.clearQuestsTimer);
470+
this.setClearQuestsTimer();
466471

467-
if (timeLeft <= 0) {
472+
if (this.shouldExit) {
473+
return;
474+
}
475+
if (!this.allStops || this.allStops.length === 0) {
476+
console.warn(`[AutoInstanceController] [${this.name}] Tried clearing quests but no pokestops.`);
477+
return;
478+
}
479+
console.debug(`[AutoInstanceController] [${this.name}] Getting pokestop ids`);
480+
let ids = this.allStops.map(x => x.id);
481+
console.debug(`[AutoInstanceController] [${this.name}] Clearing Quests for ids: ${ids}.`);
482+
try {
483+
await Pokestop.clearQuests(ids);
484+
} catch (err) {
485+
console.error(`[AutoInstanceController] [${this.name} Failed to clear quests:`, err);
468486
if (this.shouldExit) {
469487
return;
470488
}
471-
if (!this.allStops || this.allStops.length === 0) {
472-
console.warn(`[AutoInstanceController] [${this.name}] Tried clearing quests but no pokestops.`);
473-
return;
474-
}
475-
console.debug(`[AutoInstanceController] [${this.name}] Getting pokestop ids`);
476-
let ids = this.allStops.map(x => x.id);
477-
console.debug(`[AutoInstanceController] [${this.name}] Clearing Quests for ids: ${ids}.`);
478-
try {
479-
await Pokestop.clearQuests(ids);
480-
} catch (err) {
481-
console.error(`[AutoInstanceController] [${this.name} Failed to clear quests:`, err);
482-
if (this.shouldExit) {
483-
return;
484-
}
485-
}
486-
this.update();
487489
}
490+
await this.update();
488491
}
489492
}
490493

491-
module.exports = { AutoType, CooldownTimes, AutoInstanceController };
494+
module.exports = { AutoType, CooldownTimes, AutoInstanceController };

0 commit comments

Comments
 (0)