1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
const fs = require("fs");
const path = require("path");
const readLastLines = require("read-last-lines");
const DiscordRPC = require("discord-rpc");
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 ");
let oldLineCount, zone, world, health, activity, currentTime;
(async () => { oldLineCount = await countLines(); })();
fs.watchFile(logPath, { interval: 100 }, async (_curr, _prev) => {
let currentLineCount = await countLines();
readLastLines.read(logPath, currentLineCount - oldLineCount).then(async (logChunk) => {
// Get zone
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];
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;
if((!activity && zone) || activity && (activity.details !== zones[zone] || health && activity.state !== `Health: ${health[0]}/${health[1]}`)) {
if(!activity || activity.details !== zones[zone]) currentTime = new Date();
if(zone === "CHARACTER LIST") health = null;
activity = {
details: zones[zone],
state: zone === "CHARACTER LIST" ? undefined : `Health: ${health ? `${health[0]}/${health[1]}` : "unkown"}`,
startTimestamp: currentTime,
largeImageKey: world.toLowerCase(),
largeImageText: zones.zoneNames[world],
instance: false
};
rpc.setActivity(activity);
}
});
function countLines() {
return new Promise((resolve, reject) => {
let lineCount = 0;
fs.createReadStream(logPath).on("data", (buffer) => {
let index = -1;
lineCount--;
do {
index = buffer.indexOf(10, index + 1);
lineCount++;
} while(index !== -1);
}).on("end", () => {
resolve(lineCount);
}).on("error", reject);
});
}
let reconnect;
const connectDiscord = () => {
console.log("Connecting to your Discord app...");
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();
|