2021-12-29 14:46:56 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
|
|
const greenworks = require("./greenworks");
|
2022-01-05 20:17:54 +01:00
|
|
|
const log = require("electron-log");
|
2021-12-29 14:46:56 +01:00
|
|
|
|
2022-01-12 19:34:17 +01:00
|
|
|
async function enableAchievementsInterval(window) {
|
2022-01-14 12:06:59 +01:00
|
|
|
// If the Steam API could not be initialized on game start, we'll abort this.
|
|
|
|
if (global.greenworksError) return;
|
|
|
|
|
2021-12-29 14:46:56 +01:00
|
|
|
// 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();
|
2022-01-13 00:59:38 +01:00
|
|
|
log.debug(`All Steam achievements ${JSON.stringify(steamAchievements)}`);
|
2022-01-12 19:57:45 +01:00
|
|
|
const playerAchieved = (await Promise.all(steamAchievements.map(checkSteamAchievement))).filter(name => !!name);
|
2022-01-13 00:59:38 +01:00
|
|
|
log.debug(`Player has Steam achievements ${JSON.stringify(playerAchieved)}`);
|
2021-12-29 14:46:56 +01:00
|
|
|
const intervalID = setInterval(async () => {
|
|
|
|
try {
|
|
|
|
const playerAchievements = await window.webContents.executeJavaScript("document.achievements");
|
|
|
|
for (const ach of playerAchievements) {
|
2022-01-12 19:34:17 +01:00
|
|
|
if (!steamAchievements.includes(ach)) continue; // Don't try activating achievements that don't exist Steam-side
|
|
|
|
if (playerAchieved.includes(ach)) continue; // Don't spam achievements that have already been recorded
|
|
|
|
log.info(`Granting Steam achievement ${ach}`);
|
2021-12-29 14:46:56 +01:00
|
|
|
greenworks.activateAchievement(ach, () => undefined);
|
2022-01-12 19:34:17 +01:00
|
|
|
playerAchieved.push(ach);
|
2021-12-29 14:46:56 +01:00
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2022-01-12 19:57:45 +01:00
|
|
|
function checkSteamAchievement(name) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
greenworks.getAchievement(name, playerHas => resolve(playerHas ? name : ""), err => {
|
|
|
|
log.warn(`Failed to get Steam achievement ${name} status: ${err}`);
|
|
|
|
resolve("");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-29 14:46:56 +01:00
|
|
|
function disableAchievementsInterval(window) {
|
|
|
|
if (window.achievementsIntervalID) {
|
|
|
|
clearInterval(window.achievementsIntervalID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
enableAchievementsInterval, disableAchievementsInterval
|
|
|
|
}
|