Skip to content

Commit

Permalink
Fixed player rank and pp update after score upload
Browse files Browse the repository at this point in the history
  • Loading branch information
NSGolova committed Jan 5, 2025
1 parent 827419d commit 9324c25
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
6 changes: 3 additions & 3 deletions include/Utils/ReplayManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ enum struct ReplayUploadStatus {
class ReplayManager
{
public:
static void ProcessReplay(Replay const &replay, PlayEndData status, bool skipUpload, function<void(ReplayUploadStatus, string, float,
static void ProcessReplay(Replay const &replay, PlayEndData status, bool skipUpload, function<void(ReplayUploadStatus, std::optional<string>, string, float,
int)> const &finished);
static void RetryPosting(function<void(ReplayUploadStatus, string, float, int)> const& finished);
static void RetryPosting(function<void(ReplayUploadStatus, std::optional<string>, string, float, int)> const& finished);

static void TryPostReplay(string name, PlayEndData status, int tryIndex, function<void(ReplayUploadStatus, string, float,
static void TryPostReplay(string name, PlayEndData status, int tryIndex, function<void(ReplayUploadStatus, std::optional<string>, string, float,
int)> const &finished);
static string lastReplayFilename;
static PlayEndData lastReplayStatus;
Expand Down
1 change: 1 addition & 0 deletions shared/Models/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ class Player

Player(rapidjson::Value const& userModInterface);
void SetHistory(rapidjson::Value const& history);
void SetFromScore(rapidjson::Value const& score);
Player() = default;
};
25 changes: 25 additions & 0 deletions src/Models/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,29 @@ ProfileSettings::ProfileSettings(rapidjson::Value const& document) {
effectName = document["effectName"].IsNull() ? "" : document["effectName"].GetString();
hue = document["hue"].GetInt();
saturation = document["saturation"].GetFloat();
}

void Player::SetFromScore(rapidjson::Value const& score) {

auto const& userModInterface = score["player"];
int currentContext = getModConfig().Context.GetValue();
std::optional<rapidjson::GenericArray<true, rapidjson::Value>> contextExtensions =
userModInterface.HasMember("contextExtensions") && !userModInterface["contextExtensions"].IsNull()
? userModInterface["contextExtensions"].GetArray()
: std::optional<rapidjson::GenericArray<true, rapidjson::Value>>();
// If we are Standard Context or we have no contexts or our selected context is not in contextextensions we use the normal rank. Else we use the correct context extension rank
rapidjson::Value const& contextRank =
currentContext == 0 ||
!contextExtensions ? userModInterface : [&]() -> const rapidjson::Value& {
for (auto& ext : contextExtensions.value()) {
if (ext["context"].GetInt() == currentContext) {
return ext;
}
}
return userModInterface;
}();

rank = contextRank["rank"].GetInt();
countryRank = contextRank["countryRank"].GetInt();
pp = contextRank["pp"].GetFloat();
}
20 changes: 10 additions & 10 deletions src/Utils/ReplayManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
string ReplayManager::lastReplayFilename = "";
PlayEndData ReplayManager::lastReplayStatus = PlayEndData(LevelEndType::Fail, 0);

void ReplayManager::ProcessReplay(Replay const &replay, PlayEndData status, bool skipUpload, function<void(ReplayUploadStatus, string, float,
void ReplayManager::ProcessReplay(Replay const &replay, PlayEndData status, bool skipUpload, function<void(ReplayUploadStatus, std::optional<string>, string, float,
int)> const &finished) {
if (status.GetEndType() == LevelEndType::Unknown) {
return;
Expand All @@ -26,20 +26,20 @@ void ReplayManager::ProcessReplay(Replay const &replay, PlayEndData status, bool
BeatLeaderLogger.info("{}",("Replay saved " + filename).c_str());

if(!UploadEnabled()) {
finished(ReplayUploadStatus::finished, "<color=#BB2020ff>Upload disabled. But replay was saved.</color>", 0, -1);
finished(ReplayUploadStatus::finished, std::nullopt, "<color=#BB2020ff>Upload disabled. But replay was saved.</color>", 0, -1);
return;
}

if (replay.info.failTime > 0.001 || replay.info.speed > 0.001) {
finished(ReplayUploadStatus::finished, "<color=#BB2020ff>Failed attempt was saved!</color>", 0, -1);
finished(ReplayUploadStatus::finished, std::nullopt, "<color=#BB2020ff>Failed attempt was saved!</color>", 0, -1);
}
if(skipUpload)
return;
if(PlayerController::currentPlayer != std::nullopt)
TryPostReplay(filename, status, 0, finished);
}

void ReplayManager::TryPostReplay(string name, PlayEndData status, int tryIndex, function<void(ReplayUploadStatus, string, float,
void ReplayManager::TryPostReplay(string name, PlayEndData status, int tryIndex, function<void(ReplayUploadStatus, std::optional<string>, string, float,
int)> const &finished) {
struct stat file_info;
stat(name.data(), &file_info);
Expand All @@ -49,7 +49,7 @@ void ReplayManager::TryPostReplay(string name, PlayEndData status, int tryIndex,
if (tryIndex == 0) {
BeatLeaderLogger.info("{}",("Started posting " + to_string(file_info.st_size)).c_str());
if (runCallback) {
finished(ReplayUploadStatus::inProgress, "<color=#b103fcff>Posting replay...", 0, 0);
finished(ReplayUploadStatus::inProgress, std::nullopt, "<color=#b103fcff>Posting replay...", 0, 0);
}
}
FILE *replayFile = fopen(name.data(), "rb");
Expand All @@ -63,14 +63,14 @@ void ReplayManager::TryPostReplay(string name, PlayEndData status, int tryIndex,
result = "Timed out";
}
if (runCallback) {
finished(ReplayUploadStatus::inProgress, "<color=#ffff00ff>Retrying posting replay after " + to_string(statusCode) + " try #" + to_string(tryIndex) + " " + std::string(result) + "</color>", 0, statusCode);
finished(ReplayUploadStatus::inProgress, std::nullopt, "<color=#ffff00ff>Retrying posting replay after " + to_string(statusCode) + " try #" + to_string(tryIndex) + " " + std::string(result) + "</color>", 0, statusCode);
}
TryPostReplay(name, status, tryIndex + 1, finished);
} else if (statusCode == 200) {
auto duration = chrono::duration_cast<std::chrono::milliseconds>(chrono::steady_clock::now() - replayPostStart).count();
BeatLeaderLogger.info("{}", ("Replay was posted! It took: " + to_string((int)duration) + "msec. \n").c_str());
if (runCallback) {
finished(ReplayUploadStatus::finished, "<color=#20BB20ff>Replay was posted!</color>", 100, statusCode);
finished(ReplayUploadStatus::finished, result, "<color=#20BB20ff>Replay was posted!</color>", 100, statusCode);
}
if (!getModConfig().SaveLocalReplays.GetValue()) {
remove(name.data());
Expand All @@ -82,17 +82,17 @@ void ReplayManager::TryPostReplay(string name, PlayEndData status, int tryIndex,
}
BeatLeaderLogger.error("{}", ("Replay was not posted! " + to_string(statusCode) + result).c_str());
if (runCallback) {
finished(ReplayUploadStatus::error, std::string("<color=#BB2020ff>Replay was not posted. " + result), 0, statusCode);
finished(ReplayUploadStatus::error, std::nullopt, std::string("<color=#BB2020ff>Replay was not posted. " + result), 0, statusCode);
}
}
}, [finished, runCallback](float percent) {
if (runCallback) {
finished(ReplayUploadStatus::inProgress, "<color=#b103fcff>Posting replay: " + to_string_wprecision(percent, 2) + "%", percent, 0);
finished(ReplayUploadStatus::inProgress, std::nullopt, "<color=#b103fcff>Posting replay: " + to_string_wprecision(percent, 2) + "%", percent, 0);
}
});
}

void ReplayManager::RetryPosting(const std::function<void(ReplayUploadStatus, std::string, float, int)>& finished) {
void ReplayManager::RetryPosting(const std::function<void(ReplayUploadStatus, std::optional<string>, string, float, int)>& finished) {
TryPostReplay(lastReplayFilename, lastReplayStatus, 0, finished);
};

Expand Down
27 changes: 13 additions & 14 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "config-utils/shared/config-utils.hpp"
#include "custom-types/shared/register.hpp"
#include "beatsaber-hook/shared/config/rapidjson-utils.hpp"

#include "GlobalNamespace/MenuTransitionsHelper.hpp"
#include "GlobalNamespace/AppInit.hpp"
Expand Down Expand Up @@ -82,21 +83,17 @@ MAKE_HOOK_MATCH(MenuTransitionsHelperRestartGame, &MenuTransitionsHelper::Restar
resetUI();
}

void replayPostCallback(ReplayUploadStatus status, const string& description, float progress, int code) {
void replayPostCallback(ReplayUploadStatus status, std::optional<string> score, const string& description, float progress, int code) {
if (!ReplayRecorder::recording) {
BSML::MainThreadScheduler::Schedule([status, description, progress, code] {
BSML::MainThreadScheduler::Schedule([status, score, description, progress, code] {
LeaderboardUI::updateStatus(status, description, progress, code > 450 || code < 200);
if (status == ReplayUploadStatus::finished) {
std::thread t ([] {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
PlayerController::Refresh(0, [](auto player, auto str){
BSML::MainThreadScheduler::Schedule([]{
LeaderboardUI::updatePlayerRank();
});
});
}
);
t.detach();
if (score != std::nullopt) {
rapidjson::Document document;
document.Parse(score->data());
PlayerController::currentPlayer->SetFromScore(document.GetObject());
}
LeaderboardUI::updatePlayerRank();
}
});
}
Expand Down Expand Up @@ -169,13 +166,15 @@ MAKE_HOOK_MATCH(MainFlowCoordinator_DidActivate, &GlobalNamespace::MainFlowCoord
GlobalNamespace::MainFlowCoordinator* self, bool firstActivation, bool addedToHierarchy, bool screenSystemEnabling) {
MainFlowCoordinator_DidActivate(self, firstActivation, addedToHierarchy, screenSystemEnabling);

if (!addedToHierarchy || !getModConfig().NoticeboardEnabled.GetValue()) return;
if (!addedToHierarchy) return;

if (!newsViewController) {
newsViewController = BSML::Helpers::CreateViewController<BeatLeader::BeatLeaderNewsViewController*>();
}

self->_providedRightScreenViewController = newsViewController;
if (getModConfig().NoticeboardEnabled.GetValue()) {
self->_providedRightScreenViewController = newsViewController;
}
}

MAKE_HOOK_MATCH(MainFlowCoordinator_TopViewControllerWillChange, &GlobalNamespace::MainFlowCoordinator::TopViewControllerWillChange, void,
Expand Down

0 comments on commit 9324c25

Please sign in to comment.