Skip to content

Commit

Permalink
Cache all bundles on startup
Browse files Browse the repository at this point in the history
This used to be a blocking request
  • Loading branch information
nico-abram committed Jul 24, 2018
1 parent 5266735 commit a1758a3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
37 changes: 23 additions & 14 deletions src/DownloadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,27 +1071,35 @@ void DownloadManager::RequestChartLeaderBoard(string chartkey)
SendRequest("/charts/"+chartkey+"/leaderboards", vector<pair<string, string>>(), done, true);
}

vector<DownloadablePack*> DownloadManager::GetCoreBundle(string whichoneyo) {
vector<DownloadablePack*> bundle;
auto done = [this, &bundle](HTTPRequest& req, CURLMsg *) {
void DownloadManager::RefreshCoreBundles() {
auto done = [](HTTPRequest& req, CURLMsg *) {
try {
json j= json::parse(req.result);
auto packs = j.find("data");
if (packs == j.end())
json j = json::parse(req.result);
auto bundles = j.find("data");
if (bundles == j.end())
return;
auto& dlPacks = DLMAN->downloadablePacks;
for (auto pack : (*packs)) {
auto name = pack["attributes"].value("packName", "");
auto dlPack = find_if(dlPacks.begin(), dlPacks.end(),
[&name](DownloadablePack x) { return x.name == name; });
if(dlPack != dlPacks.end())
for (auto bundle : (*bundles)) {
auto bundleName = bundle.value("id", "");
auto packs = bundle["attributes"]["packs"];
(DLMAN->bundles)[bundleName] = {};
auto& bundle = (DLMAN->bundles)[bundleName];
for (auto pack : packs) {
auto name = pack.value("packname", "");
auto dlPack = find_if(dlPacks.begin(), dlPacks.end(),
[&name](DownloadablePack x) { return x.name == name; });
if (dlPack != dlPacks.end())
bundle.emplace_back(&(*dlPack));
}
}
}
catch (exception e) { }
catch (exception e) {}
};
SendRequest("packs/collection/" + whichoneyo, vector<pair<string, string>>(), done, false, false, false);
return bundle;
SendRequest("packs/collections/", {}, done, false);
}

vector<DownloadablePack*> DownloadManager::GetCoreBundle(string whichoneyo) {
return bundles.count(whichoneyo) ? bundles[whichoneyo] : vector<DownloadablePack*>();
}

void DownloadManager::DownloadCoreBundle(string whichoneyo) {
Expand Down Expand Up @@ -1370,6 +1378,7 @@ void DownloadManager::RefreshPackList(string url)
} catch (exception e) {}
}
} catch (exception e) { }
DLMAN->RefreshCoreBundles();
};
SendRequestToURL(url, {}, done, false, false, true);
return;
Expand Down
6 changes: 4 additions & 2 deletions src/DownloadManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ class DownloadManager
void RefreshUserData();
void RefreshUserRank();
void RefreshTop25(Skillset ss);
void DownloadCoreBundle(string whichoneyo);
map<string, vector<DownloadablePack*>> bundles;
void RefreshCoreBundles();
vector<DownloadablePack*> GetCoreBundle(string whichoneyo);
OnlineTopScore GetTopSkillsetScore(unsigned int rank, Skillset ss, bool &result);
float GetSkillsetRating(Skillset ss);
int GetSkillsetRank(Skillset ss);
Expand All @@ -219,8 +223,6 @@ class DownloadManager
const int maxPacksToDownloadAtOnce = 1;
const float DownloadCooldownTime = 5.f;
float timeSinceLastDownload = 0.f;
void DownloadCoreBundle(string whichoneyo);
vector<DownloadablePack*> GetCoreBundle(string whichoneyo);
Packlist pl;

// Lua
Expand Down

0 comments on commit a1758a3

Please sign in to comment.