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

Commit

Permalink
Merge pull request #18 from bschultz/fix-auto-clear-quests
Browse files Browse the repository at this point in the history
Fix auto clear quests
  • Loading branch information
versx authored Sep 14, 2020
2 parents aa91fee + aeee6f9 commit 4348bd2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"start": "node src/index.js",
"create-locales": "node src/createLocales.js",
"test": "npx eslint src/**/*.js",
"test-fix": "npx eslint src/**/*.js --fix"
"test-fix": "npx eslint src/**/*.js --fix",
"update": "npm install && npm update pogo-translations && npm run create-locales"
},
"repository": {
"type": "git",
Expand Down
63 changes: 33 additions & 30 deletions src/controllers/instances/auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const Account = require('../../models/account.js');
const Cell = require('../../models/cell.js');
const Pokestop = require('../../models/pokestop.js');

const ClearQuestsInterval = 60 * 1000; // every minute

const AutoType = {
Quest: 'quest'
};
Expand Down Expand Up @@ -91,9 +89,22 @@ class AutoInstanceController {
this.bootstrap()
.then(x => x)
.catch(err => {
console.error('[AutoInstanceController] Failed to bootstrap instance:', this.name, err);
console.error('[AutoInstanceController] Failed to bootstrap instance:', this.name, err);
});
this.clearQuestsTimer = setInterval(() => this.clearQuests(), ClearQuestsInterval);
this.setClearQuestsTimer();
}

setClearQuestsTimer() {
let date = new Date();
// TODO: timeZone
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
let timeLeftSeconds = (23 - hour) * 3600 + (59 - minute) * 60 + (60 - second);
let at = new Date(date.getTime() + (timeLeftSeconds * 1000));
console.debug(`[AutoInstanceController] [${this.name}] Clearing Quests in ${timeLeftSeconds.toLocaleString()}s at ${at.toLocaleString()} (Currently: ${date.toLocaleString()})`);

this.clearQuestsTimer = setInterval(() => this.clearQuests(), timeLeftSeconds * 1000);
}

async bootstrap() {
Expand Down Expand Up @@ -455,37 +466,29 @@ class AutoInstanceController {
}

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

if (timeLeft <= 0) {
if (this.shouldExit) {
return;
}
if (!this.allStops || this.allStops.length === 0) {
console.warn(`[AutoInstanceController] [${this.name}] Tried clearing quests but no pokestops.`);
return;
}
console.debug(`[AutoInstanceController] [${this.name}] Getting pokestop ids`);
let ids = this.allStops.map(x => x.id);
console.debug(`[AutoInstanceController] [${this.name}] Clearing Quests for ids: ${ids}.`);
try {
await Pokestop.clearQuests(ids);
} catch (err) {
console.error(`[AutoInstanceController] [${this.name} Failed to clear quests:`, err);
if (this.shouldExit) {
return;
}
if (!this.allStops || this.allStops.length === 0) {
console.warn(`[AutoInstanceController] [${this.name}] Tried clearing quests but no pokestops.`);
return;
}
console.debug(`[AutoInstanceController] [${this.name}] Getting pokestop ids`);
let ids = this.allStops.map(x => x.id);
console.debug(`[AutoInstanceController] [${this.name}] Clearing Quests for ids: ${ids}.`);
try {
await Pokestop.clearQuests(ids);
} catch (err) {
console.error(`[AutoInstanceController] [${this.name} Failed to clear quests:`, err);
if (this.shouldExit) {
return;
}
}
this.update();
}
await this.update();
}
}

module.exports = { AutoType, CooldownTimes, AutoInstanceController };
module.exports = { AutoType, CooldownTimes, AutoInstanceController };

0 comments on commit 4348bd2

Please sign in to comment.