Skip to content

Commit

Permalink
fixes achievements and adds idle mode (closes #16, fixes #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechStudent10 committed Mar 27, 2024
1 parent b1697ee commit 6bad7fe
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
3 changes: 2 additions & 1 deletion include/CustomPresense.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace gdrpc {
std::string smallImageText = "",
bool useTime = false,
bool shouldResetTime = false,
std::string largeImage = ""
std::string largeImage = "",
int timeOffset = 0
);
void registerMod(std::string modID) {
mods.insert(modID);
Expand Down
6 changes: 6 additions & 0 deletions mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
"description": "Displays the amount of time you've spent working on the level in total across all sessions",
"default": true,
"type": "bool"
},
"idling": {
"name": "Show idle status",
"description": "Will change your status to idle when GD is unfocused",
"type": "bool",
"default": true
}
},
"repository": "https://github.com/TechStudent10/DiscordRPC",
Expand Down
52 changes: 37 additions & 15 deletions src/CustomPresense.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../include/CustomPresense.hpp"
#include "Geode/loader/Log.hpp"
#include "Geode/modify/AppDelegate.hpp"
#include <Geode/Geode.hpp>
// #include <string>

Expand All @@ -8,6 +8,23 @@ using namespace geode::prelude;
static const char* APPLICATION_ID = "1178492879627366472";
time_t currentTime = time(0);

bool isIdling = false;

class $modify(AppDelegate) {
void applicationDidEnterBackground() {
AppDelegate::applicationDidEnterBackground();
isIdling = true;
log::info("idle time");
gdrpc::GDRPC::getSharedInstance()->updateDiscordRP("techstudent10.discord_rich_presence", "idling");
}

void applicationWillEnterForeground() {
AppDelegate::applicationWillEnterForeground();
isIdling = false;
}
};


static void handleDiscordReady(const DiscordUser* user) {
log::info("Connected to Discord RPC");
log::info("Username: {}", user->username);
Expand Down Expand Up @@ -44,7 +61,8 @@ void gdrpc::GDRPC::updateDiscordRP(
std::string smallImageText,
bool useTime,
bool shouldResetTime,
std::string largeImage
std::string largeImage,
int timeOffset
) {
if (!(mods.contains(modID))) {
log::error("Mod with ID \"{}\" is not registered. Please register your mod with DiscordRPC before utilizing it (gdrpc::GDRPC::getSharedInstance()->registerMod(\"{}\")).", modID, modID);
Expand All @@ -54,25 +72,29 @@ void gdrpc::GDRPC::updateDiscordRP(
auto shouldShowSensitive = Mod::get()->getSettingValue<bool>("private-info");
auto shouldShowTime = Mod::get()->getSettingValue<bool>("show-time");
DiscordRichPresence discordPresence{};
discordPresence.details = details.c_str();
discordPresence.state = state.c_str();
if (largeImage == "") {
discordPresence.largeImageKey = "gd_large";
} else {
discordPresence.largeImageKey = largeImage.c_str();
}
if (useTime && shouldShowTime) {
if (shouldResetTime) currentTime = time(0);
discordPresence.startTimestamp = currentTime;
}
if (shouldShowSensitive) {
discordPresence.largeImageText = fmt::format("{} (playing on {})", gm->m_playerName, GEODE_PLATFORM_NAME).c_str();
if (isIdling) {
discordPresence.details = "Idling";
if (shouldShowSensitive) {
discordPresence.largeImageText = fmt::format("{} (playing on {})", gm->m_playerName, GEODE_PLATFORM_NAME).c_str();
} else {
discordPresence.largeImageText = fmt::format("Playing Geometry Dash on {}", GEODE_PLATFORM_NAME).c_str();
}
} else {
discordPresence.largeImageText = fmt::format("Playing Geometry Dash on {}", GEODE_PLATFORM_NAME).c_str();
}
if (smallImageKey != "") {
discordPresence.smallImageKey = smallImageKey.c_str();
discordPresence.smallImageText = smallImageText.c_str();
discordPresence.details = details.c_str();
discordPresence.state = state.c_str();
if (useTime && shouldShowTime) {
if (shouldResetTime) currentTime = time(0);
discordPresence.startTimestamp = currentTime - timeOffset;
}
if (smallImageKey != "") {
discordPresence.smallImageKey = smallImageKey.c_str();
discordPresence.smallImageText = smallImageText.c_str();
}
}
discordPresence.instance = 0;
Discord_UpdatePresence(&discordPresence);
Expand Down
13 changes: 12 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class $modify(MenuLayer) {
void onAchievements(CCObject* sender) {
auto shouldBeFunny = Mod::get()->getSettingValue<bool>("funny-mode");
rpc->updateDiscordRP(MODID, "Browsing Menus", shouldBeFunny ? "Wishing for the Creator Points UFO" : "Achievements");
MenuLayer::onAchievements(sender); // NetheriteMiner didn't test this one :()
}
};

Expand Down Expand Up @@ -425,7 +426,17 @@ class $modify(MyLevelEditorLayer, LevelEditorLayer) {
if (showTotalTime) {
totalTime = " (worked on for " + workingTime(m_level->m_workingTime) + ")";
}
rpc->updateDiscordRP(MODID, details, std::to_string(objectCount) + fmt::format(" objects{}", totalTime), "editor", "Editing a level", true);
rpc->updateDiscordRP(
MODID,
details,
std::to_string(objectCount) + " objects{}",
"editor",
"Editing a level",
true,
false,
"",
m_level->m_workingTime
);
}
};

Expand Down

0 comments on commit 6bad7fe

Please sign in to comment.