fix dynamic bit badges

This commit is contained in:
Rasmus Karlsson 2017-06-16 08:03:13 +02:00
parent 7525dae768
commit 95f60c34c5
3 changed files with 43 additions and 15 deletions

View file

@ -38,31 +38,41 @@ Resources::Resources(EmoteManager &emoteManager, WindowManager &windowManager)
{
QString badgesUrl("https://badges.twitch.tv/v1/badges/global/display?language=en");
util::urlJsonFetch(badgesUrl, [this](QJsonObject &root) {
util::urlJsonFetch(badgesUrl, [this, &emoteManager, &windowManager](QJsonObject &root) {
QJsonObject sets = root.value("badge_sets").toObject();
for (auto it = std::begin(sets); it != std::end(sets); ++it) {
printf("%s\n", qPrintable(it.key()));
auto &badgeSet = this->badgeSets[it.key().toStdString()];
std::map<std::string, BadgeVersion> &versionsMap = badgeSet.versions;
QJsonObject versions = sets.value("versions").toObject();
QJsonObject versions = it.value().value("versions").toObject();
for (auto versionIt = std::begin(versions); versionIt != std::end(versions);
++versionIt) {
/*
std::string kkey = versionIt.key().toStdString();
QJsonObject versionObj = versionIt.value().toObject();
versionsMap.emplace(std::make_pair(kkey, versionObj));
*/
BadgeVersion v(std::move(versionObj), emoteManager, windowManager);
versionsMap.emplace(kkey, v);
}
}
this->dynamicBadgesLoaded = true;
});
}
Resources::BadgeVersion::BadgeVersion(QJsonObject &&root)
Resources::BadgeVersion::BadgeVersion(QJsonObject &&root, EmoteManager &emoteManager,
WindowManager &windowManager)
: badgeImage1x(new messages::LazyLoadedImage(emoteManager, windowManager,
root.value("image_url_1x").toString()))
, badgeImage2x(new messages::LazyLoadedImage(emoteManager, windowManager,
root.value("image_url_2x").toString()))
, badgeImage4x(new messages::LazyLoadedImage(emoteManager, windowManager,
root.value("image_url_4x").toString()))
, description(root.value("description").toString().toStdString())
, title(root.value("title").toString().toStdString())
, clickAction(root.value("clickAction").toString().toStdString())
, clickURL(root.value("clickURL").toString().toStdString())
{
}

View file

@ -36,7 +36,8 @@ public:
struct BadgeVersion {
BadgeVersion() = delete;
explicit BadgeVersion(QJsonObject &&root);
explicit BadgeVersion(QJsonObject &&root, EmoteManager &emoteManager,
WindowManager &windowManager);
messages::LazyLoadedImage *badgeImage1x;
messages::LazyLoadedImage *badgeImage2x;
@ -44,7 +45,7 @@ public:
std::string description;
std::string title;
std::string clickAction;
std::string clickUrl;
std::string clickURL;
};
struct BadgeSet {
@ -53,7 +54,6 @@ public:
std::map<std::string, BadgeSet> badgeSets;
bool bitBadgesLoaded = false;
bool dynamicBadgesLoaded = false;
messages::LazyLoadedImage *buttonBan;

View file

@ -63,7 +63,7 @@ SharedMessage TwitchMessageBuilder::parse(const Communi::IrcPrivateMessage *ircM
const auto &channelResources = resources.channels[roomID];
if (iterator != tags.end()) {
auto badges = iterator.value().toString().split(',');
QStringList badges = iterator.value().toString().split(',');
b.appendTwitchBadges(badges, resources, emoteManager, channelResources);
}
@ -358,7 +358,7 @@ void TwitchMessageBuilder::appendTwitchBadges(const QStringList &badges, Resourc
}
if (badge.startsWith("bits/")) {
if (!resources.bitBadgesLoaded) {
if (!resources.dynamicBadgesLoaded) {
// Do nothing
continue;
}
@ -366,8 +366,26 @@ void TwitchMessageBuilder::appendTwitchBadges(const QStringList &badges, Resourc
QString cheerAmountQS = badge.mid(5);
std::string cheerAmountString = cheerAmountQS.toStdString();
appendWord(Word(resources.cheerBadges[cheerAmountString], Word::BadgeCheer, QString(),
QString("Twitch Cheer" + cheerAmountQS)));
try {
auto &badgeSet = resources.badgeSets.at("bits");
try {
auto &badgeVersion = badgeSet.versions.at(cheerAmountString);
appendWord(
Word(badgeVersion.badgeImage1x, Word::BadgeCheer, QString(),
QString("Twitch " + QString::fromStdString(badgeVersion.title))));
} catch (const std::exception &e) {
printf("Exception caught: %s\n", e.what());
printf("When trying to fetch the badge version: %s\n",
cheerAmountString.c_str());
// XXX: Version not found
}
} catch (const std::exception &e) {
printf("Exception caught: %s\n", e.what());
// XXX
}
} else if (badge == "staff/1") {
appendWord(
Word(resources.badgeStaff, Word::BadgeStaff, QString(), QString("Twitch Staff")));