aboutsummaryrefslogtreecommitdiff
path: root/index.js
diff options
context:
space:
mode:
authorChristopher Gwynn <[email protected]>2021-02-06 05:45:23 -0500
committerChristopher Gwynn <[email protected]>2021-02-06 05:45:23 -0500
commit9d642618010bf8d0d5ccffb18eaf36f9df45c786 (patch)
tree089eb1c43342509f19c263e5dc3e18605e5f5792 /index.js
parentadd discord rpc capability (diff)
downloadwizard101-rpc-9d642618010bf8d0d5ccffb18eaf36f9df45c786.tar.xz
wizard101-rpc-9d642618010bf8d0d5ccffb18eaf36f9df45c786.zip
auto reconnect
Diffstat (limited to 'index.js')
-rw-r--r--index.js51
1 files changed, 37 insertions, 14 deletions
diff --git a/index.js b/index.js
index dd3e69e..9af8e0e 100644
--- a/index.js
+++ b/index.js
@@ -2,9 +2,9 @@ const fs = require("fs");
const path = require("path");
const readLastLines = require("read-last-lines");
const DiscordRPC = require("discord-rpc");
-const rpc = new DiscordRPC.Client({ transport: "ipc" });
const logPath = "C:\\ProgramData\\KingsIsle Entertainment\\Wizard101\\Bin\\WizardClient.log";
const zones = require(path.join(__dirname, "./zones.json"));
+let rpc;
console.log(" _ _ __ ___ __ \n (_) | /_ |/ _ \\/_ | \n __ ___ ______ _ _ __ __| || | | | || |______ _ __ _ __ ___ \n \\ \\ /\\ / / |_ / _` | \'__/ _` || | | | || |______| \'__| \'_ \\ / __|\n \\ V V /| |/ / (_| | | | (_| || | |_| || | | | | |_) | (__ \n \\_/\\_/ |_/___\\__,_|_| \\__,_||_|\\___/ |_| |_| | .__/ \\___|\n | | \n ");
@@ -15,25 +15,31 @@ fs.watchFile(logPath, { interval: 100 }, async (_curr, _prev) => {
let currentLineCount = await countLines();
readLastLines.read(logPath, currentLineCount - oldLineCount).then(async (logChunk) => {
// Get zone
- try {
- const zoneRegex = new RegExp(/zone = (.+),|CHARACTER LIST/gm);
- let tempZone = logChunk.match(zoneRegex);
- if(tempZone) {
- tempZone = zoneRegex.exec(tempZone[tempZone.length - 1]);
- zone = tempZone[1] || tempZone[0];
+ const zoneRegex = new RegExp(/zone = (.+),|CHARACTER LIST/gm);
+ let tempZone = logChunk.match(zoneRegex);
- world = /= (.+?)\/(.+)/.exec(tempZone);
- world = world && zones.zoneNames[world[1]] ? world[1] : "WizardCity";
- }
- } catch(err) { console.log(err); }
+ if(tempZone) {
+ tempZone = zoneRegex.exec(tempZone[tempZone.length - 1]);
+ zone = tempZone[1] || tempZone[0];
+
+ world = /= (.+?)\/(.+)/.exec(tempZone);
+ world = world && zones.zoneNames[world[1]] ? world[1] : "WizardCity";
+ }
// Get health
const healthRegex = new RegExp(/WizClientGameEf (.+): Updating health globe \(new health: (\d+), new health max: (\d+)\)/gm);
let tempHealth = logChunk.match(healthRegex);
+
if(tempHealth) {
health = healthRegex.exec(tempHealth[tempHealth.length - 1]);
health = health[3] < health[2] ? [health[2], health[2]] : [health[2], health[3]];
}
+
+ // Check if the game has been quit
+ if(logChunk.match(/GameClient::HandleQuit\(\)|Logout due to Away From Keyboard/)) {
+ rpc.destroy();
+ process.exit();
+ }
}).catch((err) => { console.log(err); });
oldLineCount = currentLineCount;
@@ -49,7 +55,6 @@ fs.watchFile(logPath, { interval: 100 }, async (_curr, _prev) => {
instance: false
};
rpc.setActivity(activity);
- console.log("Activity set!");
}
});
@@ -69,6 +74,24 @@ function countLines() {
});
}
-rpc.on("ready", () => { console.log("Successfully connected to Discord!"); });
+let reconnect;
+const connectDiscord = () => {
+ console.log("Connecting to your Discord app...");
-rpc.login({ clientId: "799404226081849345" }).catch(console.error);
+ rpc = new DiscordRPC.Client({ transport: "ipc" });
+
+ rpc.once("ready", () => {
+ console.log("Successfully connected to Discord!");
+ });
+
+ rpc.login({ clientId: "799404226081849345" }).then(() => {
+ clearInterval(reconnect);
+ }).catch((err) => {
+ console.log(`${err}\nMake sure Discord is running. You probably need to restart Discord (Ctrl+R on Discord).\n`);
+ if(!reconnect) {
+ reconnect = setInterval(connectDiscord, 10050);
+ connectDiscord();
+ }
+ });
+};
+connectDiscord();