Merge pull request 'add dynamic beatmap images to presence' (#12) from dev into master
Reviewed-on: #12
This commit is contained in:
commit
c3f0882951
15
electron/imageUtil.js
Normal file
15
electron/imageUtil.js
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
async function checkImageExists(url) {
|
||||||
|
try {
|
||||||
|
const response = await fetch(url, { method: "HEAD" });
|
||||||
|
if (!response.ok) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const contentType = response.headers.get("content-type");
|
||||||
|
if (!contentType) return false;
|
||||||
|
return contentType.startsWith("image/");
|
||||||
|
} catch (error) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { checkImageExists };
|
|
@ -2,7 +2,10 @@ const DiscordRPC = require("discord-auto-rpc");
|
||||||
const { appName, appVersion } = require("./appInfo.js");
|
const { appName, appVersion } = require("./appInfo.js");
|
||||||
|
|
||||||
const clientId = "1032772293220384808";
|
const clientId = "1032772293220384808";
|
||||||
|
|
||||||
|
/** @type {DiscordRPC.AutoClient} */
|
||||||
let richPresence;
|
let richPresence;
|
||||||
|
|
||||||
let intervalId;
|
let intervalId;
|
||||||
|
|
||||||
let currentStatus = {
|
let currentStatus = {
|
||||||
|
@ -32,6 +35,7 @@ module.exports = {
|
||||||
richPresence = new DiscordRPC.AutoClient({ transport: "ipc" });
|
richPresence = new DiscordRPC.AutoClient({ transport: "ipc" });
|
||||||
richPresence.endlessLogin({ clientId });
|
richPresence.endlessLogin({ clientId });
|
||||||
richPresence.once("ready", () => {
|
richPresence.once("ready", () => {
|
||||||
|
console.log("connected presence with user " + richPresence.user.username);
|
||||||
richPresence.setActivity(currentStatus);
|
richPresence.setActivity(currentStatus);
|
||||||
intervalId = setInterval(() => {
|
intervalId = setInterval(() => {
|
||||||
richPresence.setActivity(currentStatus);
|
richPresence.setActivity(currentStatus);
|
||||||
|
@ -47,16 +51,17 @@ module.exports = {
|
||||||
richPresence = null;
|
richPresence = null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
updateStatus: ({ state, details }) => {
|
updateStatus: ({ state, details, largeImageKey }) => {
|
||||||
currentStatus.state = state ?? " ";
|
currentStatus.state = state ?? " ";
|
||||||
currentStatus.details = details ?? " ";
|
currentStatus.details = details ?? " ";
|
||||||
|
currentStatus.largeImageKey = largeImageKey ?? "ezppfarm";
|
||||||
},
|
},
|
||||||
updateVersion: (osuVersion) => {
|
updateUser: ({ username, id }) => {
|
||||||
currentStatus.smallImageKey = osuVersion ? "osu" : " ";
|
currentStatus.smallImageKey = id ? `https://a.ez-pp.farm/${id}` : " ";
|
||||||
currentStatus.smallImageText = osuVersion ? `osu! ${osuVersion}` : " ";
|
currentStatus.smallImageText = username ?? " ";
|
||||||
},
|
},
|
||||||
update: () => {
|
update: () => {
|
||||||
if (richPresence) {
|
if (richPresence && richPresence.user) {
|
||||||
richPresence.setActivity(currentStatus);
|
richPresence.setActivity(currentStatus);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
58
main.js
58
main.js
|
@ -46,6 +46,7 @@ const { getHwId } = require("./electron/hwidUtil");
|
||||||
const { appName, appVersion } = require("./electron/appInfo");
|
const { appName, appVersion } = require("./electron/appInfo");
|
||||||
const { updateAvailable, releasesUrl } = require("./electron/updateCheck");
|
const { updateAvailable, releasesUrl } = require("./electron/updateCheck");
|
||||||
const fkill = require("fkill");
|
const fkill = require("fkill");
|
||||||
|
const { checkImageExists } = require("./electron/imageUtil");
|
||||||
|
|
||||||
// Keep a global reference of the window object, if you don't, the window will
|
// Keep a global reference of the window object, if you don't, the window will
|
||||||
// be closed automatically when the JavaScript object is garbage collected.
|
// be closed automatically when the JavaScript object is garbage collected.
|
||||||
|
@ -72,6 +73,25 @@ function startOsuStatus() {
|
||||||
if (firstInstance) {
|
if (firstInstance) {
|
||||||
if (!osuLoaded) {
|
if (!osuLoaded) {
|
||||||
osuLoaded = true;
|
osuLoaded = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const currentUserInfo = await fetch(`https://api.ez-pp.farm/get_player_info?name=${currentUser.username}&scope=info`);
|
||||||
|
const currentUserInfoJson = await currentUserInfo.json();
|
||||||
|
if ("player" in currentUserInfoJson && currentUserInfoJson.player != null) {
|
||||||
|
if ("info" in currentUserInfoJson.player && currentUserInfoJson.player.info != null) {
|
||||||
|
const id = currentUserInfoJson.player.info.id;
|
||||||
|
const username = currentUserInfoJson.player.info.name;
|
||||||
|
richPresence.updateUser({
|
||||||
|
id,
|
||||||
|
username,
|
||||||
|
});
|
||||||
|
richPresence.update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (patch) {
|
if (patch) {
|
||||||
const patcherExecuteable = path.join(
|
const patcherExecuteable = path.join(
|
||||||
|
@ -96,15 +116,32 @@ function startOsuStatus() {
|
||||||
if (!("player_status" in currentStatus)) return;
|
if (!("player_status" in currentStatus)) return;
|
||||||
if (!("status" in currentStatus.player_status)) return;
|
if (!("status" in currentStatus.player_status)) return;
|
||||||
|
|
||||||
|
let largeImageKey = "ezppfarm";
|
||||||
let details = "Idle...";
|
let details = "Idle...";
|
||||||
let infoText = currentStatus.player_status.status.info_text.length > 0
|
let infoText = currentStatus.player_status.status.info_text.length > 0
|
||||||
? currentStatus.player_status.status.info_text
|
? currentStatus.player_status.status.info_text
|
||||||
: " ";
|
: " ";
|
||||||
|
if (
|
||||||
|
"beatmap" in currentStatus.player_status.status &&
|
||||||
|
currentStatus.player_status.status.beatmap !== null
|
||||||
|
) {
|
||||||
|
const setId = currentStatus.player_status.status.beatmap.set_id;
|
||||||
|
if (setId) {
|
||||||
|
const coverImage =
|
||||||
|
`https://assets.ppy.sh/beatmaps/${setId}/covers/list@2x.jpg`;
|
||||||
|
if (
|
||||||
|
checkImageExists(coverImage)
|
||||||
|
) {
|
||||||
|
largeImageKey = coverImage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch (currentStatus.player_status.status.action) {
|
switch (currentStatus.player_status.status.action) {
|
||||||
case 1:
|
case 1:
|
||||||
details = "AFK...";
|
details = "AFK...";
|
||||||
infoText = " ";
|
infoText = " ";
|
||||||
|
largeImageKey = "ezppfarm";
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
details = "Playing...";
|
details = "Playing...";
|
||||||
|
@ -118,6 +155,7 @@ function startOsuStatus() {
|
||||||
case 5:
|
case 5:
|
||||||
details = "Multiplayer: Selecting a Beatmap...";
|
details = "Multiplayer: Selecting a Beatmap...";
|
||||||
infoText = " ";
|
infoText = " ";
|
||||||
|
largeImageKey = "ezppfarm";
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
details = "Watching...";
|
details = "Watching...";
|
||||||
|
@ -127,10 +165,12 @@ function startOsuStatus() {
|
||||||
break;
|
break;
|
||||||
case 9:
|
case 9:
|
||||||
details = "Submitting...";
|
details = "Submitting...";
|
||||||
|
largeImageKey = "ezppfarm";
|
||||||
break;
|
break;
|
||||||
case 11:
|
case 11:
|
||||||
details = "Multiplayer: Idle...";
|
details = "Multiplayer: Idle...";
|
||||||
infoText = " ";
|
infoText = " ";
|
||||||
|
largeImageKey = "ezppfarm";
|
||||||
break;
|
break;
|
||||||
case 12:
|
case 12:
|
||||||
details = "Multiplayer: Playing...";
|
details = "Multiplayer: Playing...";
|
||||||
|
@ -138,12 +178,14 @@ function startOsuStatus() {
|
||||||
case 13:
|
case 13:
|
||||||
details = "Browsing osu!direct...";
|
details = "Browsing osu!direct...";
|
||||||
infoText = " ";
|
infoText = " ";
|
||||||
|
largeImageKey = "ezppfarm";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
richPresence.updateStatus({
|
richPresence.updateStatus({
|
||||||
details,
|
details,
|
||||||
state: infoText,
|
state: infoText,
|
||||||
|
largeImageKey,
|
||||||
});
|
});
|
||||||
|
|
||||||
richPresence.update();
|
richPresence.update();
|
||||||
|
@ -386,8 +428,7 @@ function registerIPCPipes() {
|
||||||
progress: Math.ceil(data.progress),
|
progress: Math.ceil(data.progress),
|
||||||
});
|
});
|
||||||
mainWindow.webContents.send("ezpplauncher:launchstatus", {
|
mainWindow.webContents.send("ezpplauncher:launchstatus", {
|
||||||
status: `Downloading ${data.fileName}(${formatBytes(data.loaded)}/${
|
status: `Downloading ${data.fileName}(${formatBytes(data.loaded)}/${formatBytes(data.total)
|
||||||
formatBytes(data.total)
|
|
||||||
})...`,
|
})...`,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -417,8 +458,7 @@ function registerIPCPipes() {
|
||||||
progress: Math.ceil(data.progress),
|
progress: Math.ceil(data.progress),
|
||||||
});
|
});
|
||||||
mainWindow.webContents.send("ezpplauncher:launchstatus", {
|
mainWindow.webContents.send("ezpplauncher:launchstatus", {
|
||||||
status: `Downloading ${data.fileName}(${formatBytes(data.loaded)}/${
|
status: `Downloading ${data.fileName}(${formatBytes(data.loaded)}/${formatBytes(data.total)
|
||||||
formatBytes(data.total)
|
|
||||||
})...`,
|
})...`,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -464,8 +504,7 @@ function registerIPCPipes() {
|
||||||
progress: Math.ceil(data.progress),
|
progress: Math.ceil(data.progress),
|
||||||
});
|
});
|
||||||
mainWindow.webContents.send("ezpplauncher:launchstatus", {
|
mainWindow.webContents.send("ezpplauncher:launchstatus", {
|
||||||
status: `Downloading ${data.fileName}(${formatBytes(data.loaded)}/${
|
status: `Downloading ${data.fileName}(${formatBytes(data.loaded)}/${formatBytes(data.total)
|
||||||
formatBytes(data.total)
|
|
||||||
})...`,
|
})...`,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -541,8 +580,6 @@ function registerIPCPipes() {
|
||||||
} catch { }
|
} catch { }
|
||||||
|
|
||||||
const userConfig = getUserConfig(osuPath);
|
const userConfig = getUserConfig(osuPath);
|
||||||
richPresence.updateVersion(await userConfig.get("LastVersion"));
|
|
||||||
richPresence.update();
|
|
||||||
if (richPresence.hasPresence) {
|
if (richPresence.hasPresence) {
|
||||||
await userConfig.set("DiscordRichPresence", "0");
|
await userConfig.set("DiscordRichPresence", "0");
|
||||||
}
|
}
|
||||||
|
@ -563,7 +600,10 @@ function registerIPCPipes() {
|
||||||
mainWindow.show();
|
mainWindow.show();
|
||||||
mainWindow.focus();
|
mainWindow.focus();
|
||||||
stopOsuStatus();
|
stopOsuStatus();
|
||||||
richPresence.updateVersion();
|
richPresence.updateUser({
|
||||||
|
username: " ",
|
||||||
|
id: undefined
|
||||||
|
});
|
||||||
richPresence.updateStatus({
|
richPresence.updateStatus({
|
||||||
state: "Idle in Launcher...",
|
state: "Idle in Launcher...",
|
||||||
details: undefined,
|
details: undefined,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user