Skip to content

Commit 6ff4a91

Browse files
committed
Chain score uploads
1 parent c20b6ad commit 6ff4a91

File tree

2 files changed

+55
-48
lines changed

2 files changed

+55
-48
lines changed

src/Etterna/Singletons/DownloadManager.cpp

+53-46
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ DownloadManager::UploadScore(HighScore* hs,
925925
LOG->Trace(
926926
"Attempted to upload score when not logged in (scorekey: \"%s\")",
927927
hs->GetScoreKey().c_str());
928+
callback();
928929
return;
929930
}
930931

@@ -979,8 +980,8 @@ DownloadManager::UploadScore(HighScore* hs,
979980
curlHandle, form, lastPtr, "replay_data", replayString);
980981
SetCURLPostToURL(curlHandle, url);
981982
curl_easy_setopt(curlHandle, CURLOPT_HTTPPOST, form);
982-
auto done = [this, hs, callback{ move(callback) }, load_from_disk](
983-
HTTPRequest& req, CURLMsg*) {
983+
auto done = [this, hs, callback, load_from_disk](HTTPRequest& req,
984+
CURLMsg*) {
984985
long response_code;
985986
curl_easy_getinfo(req.handle, CURLINFO_RESPONSE_CODE, &response_code);
986987
Document d;
@@ -1008,8 +1009,7 @@ DownloadManager::UploadScore(HighScore* hs,
10081009
DLMAN->StartSession(
10091010
DLMAN->sessionUser,
10101011
DLMAN->sessionPass,
1011-
[hs, callback{ move(callback) }, load_from_disk](
1012-
bool logged) {
1012+
[hs, callback, load_from_disk](bool logged) {
10131013
if (logged) {
10141014
DLMAN->UploadScore(hs, callback, load_from_disk);
10151015
}
@@ -1084,8 +1084,10 @@ DownloadManager::UploadScore(HighScore* hs,
10841084
}
10851085
callback();
10861086
};
1087-
// TODO: Do we want to call callback in the fail HTTPRequest handler?
1088-
HTTPRequest* req = new HTTPRequest(curlHandle, done);
1087+
HTTPRequest* req = new HTTPRequest(
1088+
curlHandle, done, nullptr, [callback](HTTPRequest& req, CURLMsg*) {
1089+
callback();
1090+
});
10891091
SetCURLResultsString(curlHandle, &(req->result));
10901092
curl_multi_add_handle(mHTTPHandle, req->handle);
10911093
HTTPRequests.push_back(req);
@@ -1119,21 +1121,19 @@ DownloadManager::UploadScoreWithReplayDataFromDisk(HighScore* hs,
11191121
// (So it is essentially kind of recursive, with the base case of an empty
11201122
// deque)
11211123
void
1122-
uploadSequentially(deque<HighScore*> toUpload, int workload)
1124+
uploadSequentially()
11231125
{
11241126
Message msg("UploadProgress");
1125-
msg.SetParam("percent",
1126-
1.f - (static_cast<float>(toUpload.size()) /
1127-
static_cast<float>(workload)));
1127+
msg.SetParam(
1128+
"percent",
1129+
1.f - (static_cast<float>(DLMAN->ScoreUploadSequentialQueue.size()) /
1130+
static_cast<float>(DLMAN->sequentialScoreUploadTotalWorkload)));
11281131
MESSAGEMAN->Broadcast(msg);
11291132

1130-
if (!toUpload.empty()) {
1131-
auto hs = toUpload.front();
1132-
toUpload.pop_front();
1133-
DLMAN->UploadScoreWithReplayDataFromDisk(
1134-
hs, [hs, toUpload{ move(toUpload) }, workload]() {
1135-
uploadSequentially(toUpload, workload);
1136-
});
1133+
if (!DLMAN->ScoreUploadSequentialQueue.empty()) {
1134+
auto hs = DLMAN->ScoreUploadSequentialQueue.front();
1135+
DLMAN->ScoreUploadSequentialQueue.pop_front();
1136+
DLMAN->UploadScoreWithReplayDataFromDisk(hs, uploadSequentially);
11371137
}
11381138
return;
11391139
}
@@ -1144,10 +1144,10 @@ DownloadManager::UploadScores()
11441144
if (!LoggedIn())
11451145
return false;
11461146

1147-
// First we accumulate top 2 scores that have not been uploaded and have
1148-
// replay data
1147+
// First we accumulate top 2 scores that have
1148+
// not been uploaded and have replay data
11491149
auto scores = SCOREMAN->GetAllPBPtrs();
1150-
deque<HighScore*> toUpload;
1150+
vector<HighScore*> toUpload;
11511151
for (auto& vec : scores) {
11521152
for (auto& scorePtr : vec) {
11531153
auto ts = scorePtr->GetTopScore();
@@ -1172,74 +1172,81 @@ DownloadManager::UploadScores()
11721172
if (!toUpload.empty())
11731173
LOG->Trace("Updating online scores. (Uploading %d scores)",
11741174
toUpload.size());
1175-
uploadSequentially(toUpload, toUpload.size());
1175+
1176+
bool was_not_uploading_already = this->ScoreUploadSequentialQueue.empty();
1177+
if (was_not_uploading_already)
1178+
this->sequentialScoreUploadTotalWorkload = toUpload.size();
1179+
else
1180+
this->sequentialScoreUploadTotalWorkload += toUpload.size();
1181+
this->ScoreUploadSequentialQueue.insert(
1182+
this->ScoreUploadSequentialQueue.end(), toUpload.begin(), toUpload.end());
1183+
if (was_not_uploading_already)
1184+
uploadSequentially();
11761185

11771186
return true;
11781187
}
1179-
void
1180-
DownloadManager::BeginSequentialUploadOfAccumulatedQueue()
1181-
{
1182-
// figure out how to make this not start if there are active uploads
1183-
uploadSequentially(ForceUploadScoreQueue, ForceUploadScoreQueue.size());
1184-
ForceUploadScoreQueue.clear();
1185-
ForceUploadScoreQueue.shrink_to_fit();
1186-
}
11871188

11881189
// manual upload function that will upload all scores for a chart
11891190
// that skips some of the constraints of the auto uploaders
11901191
void
11911192
DownloadManager::ForceUploadScoresForChart(const std::string& ck, bool startnow)
11921193
{
1193-
// This check is a bit redundant, we don't want to try to begin the upload
1194-
// when this is called from within a "bigger" forceupload, either of these
1195-
// checks should be enough I think
1196-
startnow = startnow && ForceUploadScoreQueue.empty();
1194+
startnow = startnow && this->ScoreUploadSequentialQueue.empty();
11971195
auto cs = SCOREMAN->GetScoresForChart(ck);
11981196
if (cs) { // ignoring topscore flags; upload worst->best
11991197
auto& test = cs->GetAllScores();
12001198
for (auto& s : test)
12011199
if (!s->forceuploadedthissession)
1202-
if (s->GetGrade() != Grade_Failed)
1203-
ForceUploadScoreQueue.push_back(s);
1200+
if (s->GetGrade() != Grade_Failed) {
1201+
this->ScoreUploadSequentialQueue.push_back(s);
1202+
this->sequentialScoreUploadTotalWorkload += 1;
1203+
}
12041204
}
12051205

12061206
if (startnow) {
1207+
this->sequentialScoreUploadTotalWorkload =
1208+
this->ScoreUploadSequentialQueue.size();
12071209
LOG->Trace("Starting sequential upload of %d scores",
1208-
ForceUploadScoreQueue.size());
1209-
BeginSequentialUploadOfAccumulatedQueue();
1210+
this->ScoreUploadSequentialQueue.size());
1211+
uploadSequentially();
12101212
}
12111213
}
12121214
// wrapper for packs
12131215
void
12141216
DownloadManager::ForceUploadScoresForPack(const std::string& pack,
12151217
bool startnow)
12161218
{
1217-
// This check is a bit redundant, we don't want to try to begin the
1218-
// upload when this is called from within a "bigger" forceupload, either
1219-
// of these checks should be enough I think
1220-
startnow = startnow && ForceUploadScoreQueue.empty();
1219+
startnow = startnow && this->ScoreUploadSequentialQueue.empty();
12211220
auto songs = SONGMAN->GetSongs(pack);
12221221
for (auto so : songs)
12231222
for (auto c : so->GetAllSteps())
12241223
ForceUploadScoresForChart(c->GetChartKey(), false);
12251224

12261225
if (startnow) {
1226+
this->sequentialScoreUploadTotalWorkload =
1227+
this->ScoreUploadSequentialQueue.size();
12271228
LOG->Trace("Starting sequential upload of %d scores",
1228-
ForceUploadScoreQueue.size());
1229-
BeginSequentialUploadOfAccumulatedQueue();
1229+
this->ScoreUploadSequentialQueue.size());
1230+
uploadSequentially();
12301231
}
12311232
}
12321233
void
12331234
DownloadManager::ForceUploadAllScores()
12341235
{
1236+
bool not_already_uploading = this->ScoreUploadSequentialQueue.empty();
1237+
12351238
auto songs = SONGMAN->GetSongs(GROUP_ALL);
12361239
for (auto so : songs)
12371240
for (auto c : so->GetAllSteps())
12381241
ForceUploadScoresForChart(c->GetChartKey(), false);
12391242

1240-
LOG->Trace("Starting sequential upload of %d scores",
1241-
ForceUploadScoreQueue.size());
1242-
BeginSequentialUploadOfAccumulatedQueue();
1243+
if (not_already_uploading) {
1244+
this->sequentialScoreUploadTotalWorkload =
1245+
this->ScoreUploadSequentialQueue.size();
1246+
LOG->Trace("Starting sequential upload of %d scores",
1247+
this->ScoreUploadSequentialQueue.size());
1248+
uploadSequentially();
1249+
}
12431250
}
12441251
void
12451252
DownloadManager::EndSessionIfExists()

src/Etterna/Singletons/DownloadManager.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ class DownloadManager
227227
done); // Sends login request if not already logging in
228228
void OnLogin();
229229
bool UploadScores(); // Uploads all scores not yet uploaded to current
230-
void BeginSequentialUploadOfAccumulatedQueue();
231230
void ForceUploadScoresForChart(
232231
const std::string& ck,
233232
bool startnow = true); // forced upload wrapper for charts
@@ -311,7 +310,8 @@ class DownloadManager
311310
// most recent single score upload result -mina
312311
RString mostrecentresult = "";
313312
deque<pair<DownloadablePack*, bool>> DownloadQueue; // (pack,isMirror)
314-
deque<HighScore*> ForceUploadScoreQueue;
313+
deque<HighScore*> ScoreUploadSequentialQueue;
314+
unsigned int sequentialScoreUploadTotalWorkload{ 0 };
315315
const int maxPacksToDownloadAtOnce = 1;
316316
const float DownloadCooldownTime = 5.f;
317317
float timeSinceLastDownload = 0.f;

0 commit comments

Comments
 (0)