bitburner-src/electron/achievements.js
Martin Fournier 8192ab09ef Handle greenworks.init() errors gracefully
Since we are only using it to track achievements, we can still launch
the game if it fails to initialize, we just have to not run the
achievements interval.

Adds a dialog that tells the user to fix the issue & restart the game to
enable achievements.
2022-01-14 06:06:59 -05:00

40 lines
1.3 KiB
JavaScript

/* eslint-disable @typescript-eslint/no-var-requires */
const greenworks = require("./greenworks");
const log = require("electron-log");
function enableAchievementsInterval(window) {
// If the Steam API could not be initialized on game start, we'll abort this.
if (global.greenworksError) return;
// This is backward but the game fills in an array called `document.achievements` and we retrieve it from
// here. Hey if it works it works.
const steamAchievements = greenworks.getAchievementNames();
const intervalID = setInterval(async () => {
try {
const playerAchievements = await window.webContents.executeJavaScript("document.achievements");
for (const ach of playerAchievements) {
if (!steamAchievements.includes(ach)) continue;
greenworks.activateAchievement(ach, () => undefined);
}
} catch (error) {
log.error(error);
// The interval probably did not get cleared after a window kill
log.warn('Clearing achievements timer');
clearInterval(intervalID);
return;
}
}, 1000);
window.achievementsIntervalID = intervalID;
}
function disableAchievementsInterval(window) {
if (window.achievementsIntervalID) {
clearInterval(window.achievementsIntervalID);
}
}
module.exports = {
enableAchievementsInterval, disableAchievementsInterval
}