Merge branch 'discord'

This commit is contained in:
2023-06-08 12:21:54 +02:00
57 changed files with 17185 additions and 4 deletions

View File

@@ -14,6 +14,7 @@
#include "window/Display.h"
#include "updater/Updater.h"
#include "misc/Backward.hpp"
#include "misc/DiscordRPC.h"
#ifdef __ANDROID__
extern "C"
@@ -25,6 +26,9 @@ int main(int argc, const char* args[]) {
backward::SignalHandling sh;
#endif
// start discord rpc
td::utils::InitDiscordRPC();
// remove the outdated binary
td::utils::Updater::RemoveOldFile();
@@ -33,7 +37,10 @@ int main(int argc, const char* args[]) {
Display::PollEvents();
Display::Render();
Display::Update();
td::utils::UpdateDiscordRPC();
}
Display::Destroy();
td::utils::ShutdownDiscordRPC();
return 0;
}

View File

@@ -1,6 +1,7 @@
#include "game/client/ClientGame.h"
#include "protocol/PacketDispatcher.h"
#include "game/client/Client.h"
#include "misc/DiscordRPC.h"
namespace td {
namespace client {
@@ -97,7 +98,7 @@ void ClientGame::HandlePacket(const protocol::UpdateExpPacket* packet) {
}
void ClientGame::HandlePacket(const protocol::DisconnectPacket* packet) {
m_GameState = game::GameState::Disconnected;
SetGameState(game::GameState::Disconnected);
m_Renderer->SetBackgroundColor({ 0, 0, 0 });
}
@@ -114,5 +115,30 @@ void ClientGame::RenderWorld() {
}
}
void ClientGame::SetGameState(game::GameState newState) {
game::Game::SetGameState(newState);
// Update Discord presence
switch (newState) {
case game::GameState::Lobby:
utils::UpdateDiscordPresence("In Lobby", "Normal Mode", true);
break;
case game::GameState::Game:
utils::UpdateDiscordPresence("In Game", "Normal Mode", true);
break;
case game::GameState::Closed:
case game::GameState::Disconnected:
utils::UpdateDiscordPresence("In Main Menu", "Normal Mode", true);
break;
case game::GameState::EndGame:
utils::UpdateDiscordPresence("In End Game", "Normal Mode", true);
break;
}
}
} // namespace client
} // namespace td

72
src/misc/DiscordRPC.cpp Normal file
View File

@@ -0,0 +1,72 @@
#include "misc/DiscordRPC.h"
#include "misc/Time.h"
#include <cstring>
#include <discordrpc/discord_rpc.h>
namespace td {
namespace utils {
static const std::string APPLICATION_ID = "1114135294283874334";
static const std::string TD_ICON = "td_icon2";
static void handleDiscordReady(const DiscordUser* connectedUser);
static void handleDiscordDisconnected(int errcode, const char* message);
static void handleDiscordError(int errcode, const char* message);
void InitDiscordRPC() {
DiscordEventHandlers handlers {};
handlers.ready = handleDiscordReady;
handlers.disconnected = handleDiscordDisconnected;
handlers.errored = handleDiscordError;
Discord_Initialize(APPLICATION_ID.c_str(), &handlers, true, nullptr);
// Set default presence
UpdateDiscordPresence("In Main Menu", "", true);
}
void UpdateDiscordRPC() {
Discord_RunCallbacks();
}
void ShutdownDiscordRPC() {
Discord_Shutdown();
}
void UpdateDiscordPresence(const std::string& state, const std::string& details, bool resetTimer) {
static std::int64_t currentTimeStamp = static_cast<std::int64_t>(GetTime());
if (resetTimer) currentTimeStamp = static_cast<std::int64_t>(GetTime());
DiscordRichPresence discordPresence {};
discordPresence.state = state.c_str();
discordPresence.details = details.c_str();
discordPresence.startTimestamp = currentTimeStamp;
discordPresence.largeImageKey = TD_ICON.c_str();
discordPresence.largeImageText = "Tower Defense";
Discord_UpdatePresence(&discordPresence);
}
static void handleDiscordReady(const DiscordUser* connectedUser)
{
printf("\nDiscord: connected to user %s#%s - %s\n",
connectedUser->username,
connectedUser->discriminator,
connectedUser->userId);
}
static void handleDiscordDisconnected(int errcode, const char* message)
{
printf("\nDiscord: disconnected (%d: %s)\n", errcode, message);
}
static void handleDiscordError(int errcode, const char* message)
{
printf("\nDiscord: error (%d: %s)\n", errcode, message);
}
} // namespace utils
} // namespace td