diff options
| author | Christopher Gwynn <[email protected]> | 2021-02-03 05:25:26 -0500 |
|---|---|---|
| committer | Christopher Gwynn <[email protected]> | 2021-02-03 05:25:26 -0500 |
| commit | 47ba6a53999e6db83f66b9baa45ae13a0d6fa15e (patch) | |
| tree | b8b6e21128fe91c6511d3e9b84304d93884c929c | |
| parent | read and print relevant info from logs (diff) | |
| download | wizard101-rpc-47ba6a53999e6db83f66b9baa45ae13a0d6fa15e.tar.xz wizard101-rpc-47ba6a53999e6db83f66b9baa45ae13a0d6fa15e.zip | |
add discord rpc capability
| -rw-r--r-- | index.js | 52 | ||||
| -rw-r--r-- | package.json | 2 | ||||
| -rw-r--r-- | yarn.lock | 8 | ||||
| -rw-r--r-- | zones.json | 27 |
4 files changed, 73 insertions, 16 deletions
@@ -1,24 +1,56 @@ const fs = require("fs"); const path = require("path"); -const readLastLine = require("read-last-line"); +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")); console.log(" _ _ __ ___ __ \n (_) | /_ |/ _ \\/_ | \n __ ___ ______ _ _ __ __| || | | | || |______ _ __ _ __ ___ \n \\ \\ /\\ / / |_ / _` | \'__/ _` || | | | || |______| \'__| \'_ \\ / __|\n \\ V V /| |/ / (_| | | | (_| || | |_| || | | | | |_) | (__ \n \\_/\\_/ |_/___\\__,_|_| \\__,_||_|\\___/ |_| |_| | .__/ \\___|\n | | \n "); -let oldLineCount; +let oldLineCount, zone, world, health, activity, currentTime; (async () => { oldLineCount = await countLines(); })(); + fs.watchFile(logPath, { interval: 100 }, async (_curr, _prev) => { let currentLineCount = await countLines(); - await readLastLine.read(logPath, (currentLineCount - oldLineCount) * 2).then(async (logChunk) => { + readLastLines.read(logPath, currentLineCount - oldLineCount).then(async (logChunk) => { + // Get zone try { - console.log(zones[/zone = (.+),/.exec(logChunk)[1]]); - } catch(_err) { /* No zone information found in this chunk */ } + 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"; + } + } catch(err) { console.log(err); } - let health = /WizClientGameEf (.+): Updating health globe \(new health: (\d+), new health max: (\d+)\)/gm.exec(logChunk); - console.log(`new health: ${health[2]}, new max health: ${health[3]}`); - }).catch(() => { /* No health information found in this chunk */ }); + // 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]]; + } + }).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); + console.log("Activity set!"); + } }); function countLines() { @@ -36,3 +68,7 @@ function countLines() { }).on("error", reject); }); } + +rpc.on("ready", () => { console.log("Successfully connected to Discord!"); }); + +rpc.login({ clientId: "799404226081849345" }).catch(console.error); diff --git a/package.json b/package.json index dcc54b1..5418f5c 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,6 @@ "license": "MIT", "dependencies": { "discord-rpc": "^3.1.4", - "read-last-line": "^1.1.0" + "read-last-lines": "^1.8.0" } } @@ -34,10 +34,10 @@ object-assign@^4.0.1: resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM= -read-last-line@^1.1.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/read-last-line/-/read-last-line-1.1.0.tgz#aa10d85568b28eddd4ff632f68fb5ef09bdce58a" - integrity sha512-XpM0ZE7uGidTV9YjZddIwow+jeKMFKdRosb0G/PZUFVpWMxTvvADhqKTDBpZ1FULInoXZWPRf8l9w/+2hfnZ5A== +read-last-lines@^1.8.0: + version "1.8.0" + resolved "https://registry.yarnpkg.com/read-last-lines/-/read-last-lines-1.8.0.tgz#4f94d4345ece7b8083ebb71c5fcdf60bd7afb9cc" + integrity sha512-oPL0cnZkhsO2xF7DBrdzVhXSNajPP5TzzCim/2IAjeGb17ArLLTRriI/ceV6Rook3L27mvbrOvLlf9xYYnaftQ== dependencies: mz "^2.7.0" @@ -1226,9 +1226,9 @@ "Housing_SP/SP_Interior_Preview": "Sultan's Palace", "Housing_SP_TomeFallArena/Interior": "Autumn Monstrodome", "Housing_SP_TomeSummerArena/Interior": "Summer Monstrodome", - "Housing_SkyCastle/Housing_SkyCastle_Ext/": "Heavenly Palace", + "Housing_SkyCastle/Housing_SkyCastle_Ext": "Heavenly Palace", "Housing_SkyCastle/Housing_SkyCastle_Ext_Preview": "Heavenly Palace", - "Housing_SkyCastle/Housing_SkyCastle_Int/": "Heavenly Palace", + "Housing_SkyCastle/Housing_SkyCastle_Int": "Heavenly Palace", "Housing_SkyCastle/Housing_SkyCastle_Int_Preview": "Heavenly Palace", "Housing_SunPal/Exterior": "The Sun Palace", "Housing_SunPal/Exterior_Preview": "The Sun Palace", @@ -2645,5 +2645,26 @@ "Zafaria/ZF_Z10_Elephant_Graveyard": "Elephant Graveyard", "Zafaria/ZF_Z11_Mirror_Lake": "Mirror Lake", "Zafaria/ZF_Z12_Shining_Mountain": "Mirror Lake", - "Zafaria/ZF_Z13_Mirror_Lake_Drained": "Mirror Lake" + "Zafaria/ZF_Z13_Mirror_Lake_Drained": "Mirror Lake", + "zoneNames": { + "Aquila": "Aquila", + "Arcanum": "Arcanum", + "Avalon": "Avalon", + "Azteca": "Azteca", + "Celestia": "Celestia", + "DragonSpire": "Dragonspyre", + "Empyrea": "Empyrea", + "Grizzleheim": "Grizzleheim", + "Karamelle": "Karamelle", + "Khrysalis": "Khrysalis", + "Krokotopia": "Krokotopia", + "Marleybone": "Marleybone", + "Mirage": "Mirage", + "MooShu": "MooShu", + "Polaris": "Polaris", + "WizardCity": "Wizard City", + "Wysteria": "Wysteria", + "Zafaria": "Zafaria" + }, + "CHARACTER LIST": "Character Selection" }
\ No newline at end of file |