Retry Discord lobby creation on failure 5 times before giving up

This commit is contained in:
MysterD 2021-08-09 22:27:52 -07:00
parent 6f9b447468
commit 024ae36003
3 changed files with 37 additions and 0 deletions

View File

@ -131,6 +131,7 @@ static void register_launch_command(void) {
static void ns_discord_update(void) {
if (!gDiscordInitialized) { return; }
discord_lobby_update();
DISCORD_REQUIRE(app.core->run_callbacks(app.core));
discord_network_flush();
}
@ -155,6 +156,11 @@ static bool ns_discord_initialize(enum NetworkType networkType) {
params.activity_events = discord_activity_initialize();
params.lobby_events = discord_lobby_initialize();
gCurLobbyId = 0;
gLobbyCreateRetry = false;
gLobbyCreateAttempts = 0;
gLobbyCreateAttemptElapsed = 0;
int rc = DiscordCreate(DISCORD_VERSION, &params, &app.core);
if (app.core != NULL) {
app.core->set_log_hook(app.core, DiscordLogLevel_Debug, NULL, discord_sdk_log_callback);

View File

@ -2,12 +2,38 @@
#include "activity.h"
#include "discord_network.h"
#include "pc/logfile.h"
#include "pc/utils/misc.h"
#define MAX_LOBBY_RETRY 5
#define MAX_LOBBY_RETRY_WAIT_TIME 6
static bool isHosting = false;
DiscordLobbyId gCurLobbyId = 0;
bool gLobbyCreateRetry = false;
u8 gLobbyCreateAttempts = 0;
f32 gLobbyCreateAttemptElapsed = 0;
void discord_lobby_update(void) {
if (gCurLobbyId != 0) { return; }
if (!gLobbyCreateRetry) { return; }
f32 timeUntilRetry = (clock_elapsed() - gLobbyCreateAttemptElapsed);
if (timeUntilRetry < MAX_LOBBY_RETRY_WAIT_TIME) { return; }
gLobbyCreateRetry = false;
discord_lobby_create();
}
static void on_lobby_create_callback(UNUSED void* data, enum EDiscordResult result, struct DiscordLobby* lobby) {
LOGFILE_INFO(LFT_DISCORD, "> on_lobby_create returned %d", (int)result);
if (result != DiscordResult_Ok && gLobbyCreateAttempts < MAX_LOBBY_RETRY) {
LOGFILE_INFO(LFT_DISCORD, "rescheduling lobby creation");
gLobbyCreateRetry = true;
gLobbyCreateAttempts++;
gLobbyCreateAttemptElapsed = clock_elapsed();
return;
}
DISCORD_REQUIRE(result);
LOGFILE_INFO(LFT_DISCORD, "Lobby id: %lld", lobby->id);
LOGFILE_INFO(LFT_DISCORD, "Lobby type: %u", lobby->type);

View File

@ -4,6 +4,11 @@
extern DiscordLobbyId gCurLobbyId;
extern bool gLobbyCreateRetry;
extern u8 gLobbyCreateAttempts;
extern f32 gLobbyCreateAttemptElapsed;
void discord_lobby_update(void);
void discord_lobby_create(void);
void discord_lobby_leave(void);
struct IDiscordLobbyEvents* discord_lobby_initialize(void);