Start parsing unknown badges (i.e. game badges)

Prepare channel-specific data to be loaded (i.e. subscription badges)
This commit is contained in:
Rasmus Karlsson 2017-06-16 10:01:21 +02:00
parent 8715cf14ac
commit 56e521bc24
5 changed files with 114 additions and 38 deletions

View file

@ -233,29 +233,6 @@ void IrcManager::partChannel(const QString &channelName)
this->connectionMutex.unlock(); this->connectionMutex.unlock();
} }
void IrcManager::messageReceived(Communi::IrcMessage *message)
{
/*
qDebug() << "Message received: " << message->command();
// qInfo(message->command().toStdString().c_str());
//
for (const auto &param : message->parameters()) {
qDebug() << "Param: " << param;
}
*/
/*
const QString &command = message->command();
if (command == "CLEARCHAT") {
} else if (command == "ROOMSTATE") {
} else if (command == "USERSTATE") {
} else if (command == "WHISPER") {
} else if (command == "USERNOTICE") {
}
*/
}
void IrcManager::privateMessageReceived(Communi::IrcPrivateMessage *message) void IrcManager::privateMessageReceived(Communi::IrcPrivateMessage *message)
{ {
auto c = this->channelManager.getChannel(message->target().mid(1)); auto c = this->channelManager.getChannel(message->target().mid(1));
@ -268,6 +245,60 @@ void IrcManager::privateMessageReceived(Communi::IrcPrivateMessage *message)
} }
} }
void IrcManager::messageReceived(Communi::IrcMessage *message)
{
if (message->type() == Communi::IrcMessage::Type::Private) {
// We already have a handler for private messages
return;
}
const QString &command = message->command();
if (command == "ROOMSTATE") {
this->handleRoomStateMessage(message);
} else if (command == "CLEARCHAT") {
this->handleClearChatMessage(message);
} else if (command == "USERSTATE") {
this->handleUserStateMessage(message);
} else if (command == "WHISPER") {
this->handleWhisperMessage(message);
} else if (command == "USERNOTICE") {
this->handleUserNoticeMessage(message);
}
}
void IrcManager::handleRoomStateMessage(Communi::IrcMessage *message)
{
const auto &tags = message->tags();
auto iterator = tags.find("room-id");
if (iterator != tags.end()) {
std::string roomID = iterator.value().toString().toStdString();
this->resources.loadChannelData(roomID);
}
}
void IrcManager::handleClearChatMessage(Communi::IrcMessage *message)
{
// do nothing
}
void IrcManager::handleUserStateMessage(Communi::IrcMessage *message)
{
// do nothing
}
void IrcManager::handleWhisperMessage(Communi::IrcMessage *message)
{
// do nothing
}
void IrcManager::handleUserNoticeMessage(Communi::IrcMessage *message)
{
// do nothing
}
bool IrcManager::isTwitchBlockedUser(QString const &username) bool IrcManager::isTwitchBlockedUser(QString const &username)
{ {
QMutexLocker locker(&_twitchBlockedUsersMutex); QMutexLocker locker(&_twitchBlockedUsersMutex);

View file

@ -80,8 +80,14 @@ private:
void beginConnecting(); void beginConnecting();
void messageReceived(Communi::IrcMessage *message);
void privateMessageReceived(Communi::IrcPrivateMessage *message); void privateMessageReceived(Communi::IrcPrivateMessage *message);
void messageReceived(Communi::IrcMessage *message);
void handleRoomStateMessage(Communi::IrcMessage *message);
void handleClearChatMessage(Communi::IrcMessage *message);
void handleUserStateMessage(Communi::IrcMessage *message);
void handleWhisperMessage(Communi::IrcMessage *message);
void handleUserNoticeMessage(Communi::IrcMessage *message);
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -41,12 +41,11 @@ Resources::Resources(EmoteManager &emoteManager, WindowManager &windowManager)
util::urlJsonFetch(badgesUrl, [this, &emoteManager, &windowManager](QJsonObject &root) { util::urlJsonFetch(badgesUrl, [this, &emoteManager, &windowManager](QJsonObject &root) {
QJsonObject sets = root.value("badge_sets").toObject(); QJsonObject sets = root.value("badge_sets").toObject();
for (auto it = std::begin(sets); it != std::end(sets); ++it) { for (QJsonObject::iterator it = sets.begin(); it != sets.end(); ++it) {
QJsonObject versions = it.value().toObject().value("versions").toObject();
auto &badgeSet = this->badgeSets[it.key().toStdString()]; auto &badgeSet = this->badgeSets[it.key().toStdString()];
auto &versionsMap = badgeSet.versions;
std::map<std::string, BadgeVersion> &versionsMap = badgeSet.versions;
QJsonObject versions = it.value().value("versions").toObject();
for (auto versionIt = std::begin(versions); versionIt != std::end(versions); for (auto versionIt = std::begin(versions); versionIt != std::end(versions);
++versionIt) { ++versionIt) {
@ -91,4 +90,9 @@ void Resources::Channel::loadData()
*/ */
} }
void Resources::loadChannelData(const std::string &roomID, bool bypassCache)
{
qDebug() << "Load channel data for" << QString::fromStdString(roomID);
}
} // namespace chatterino } // namespace chatterino

View file

@ -71,6 +71,8 @@ public:
// channelId // channelId
std::map<std::string, Channel> channels; std::map<std::string, Channel> channels;
void loadChannelData(const std::string &roomID, bool bypassCache = false);
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -364,26 +364,25 @@ void TwitchMessageBuilder::appendTwitchBadges(const QStringList &badges, Resourc
} }
QString cheerAmountQS = badge.mid(5); QString cheerAmountQS = badge.mid(5);
std::string cheerAmountString = cheerAmountQS.toStdString(); std::string versionKey = cheerAmountQS.toStdString();
try { try {
auto &badgeSet = resources.badgeSets.at("bits"); auto &badgeSet = resources.badgeSets.at("bits");
try { try {
auto &badgeVersion = badgeSet.versions.at(cheerAmountString); auto &badgeVersion = badgeSet.versions.at(versionKey);
appendWord( appendWord(
Word(badgeVersion.badgeImage1x, Word::BadgeCheer, QString(), Word(badgeVersion.badgeImage1x, Word::BadgeCheer, QString(),
QString("Twitch " + QString::fromStdString(badgeVersion.title)))); QString("Twitch " + QString::fromStdString(badgeVersion.title))));
} catch (const std::exception &e) { } catch (const std::exception &e) {
printf("Exception caught: %s\n", e.what()); qDebug() << "Exception caught:" << e.what()
printf("When trying to fetch the badge version: %s\n", << "when trying to fetch badge version " << versionKey.c_str();
cheerAmountString.c_str());
// XXX: Version not found
} }
} catch (const std::exception &e) { } catch (const std::exception &e) {
printf("Exception caught: %s\n", e.what()); qDebug() << "No badge set with key"
// XXX << "bits"
<< ". Exception: " << e.what();
} }
} else if (badge == "staff/1") { } else if (badge == "staff/1") {
@ -421,6 +420,7 @@ void TwitchMessageBuilder::appendTwitchBadges(const QStringList &badges, Resourc
} break; } break;
} }
} else if (badge.startsWith("subscriber/")) { } else if (badge.startsWith("subscriber/")) {
qDebug() << "Subscriber badge:" << badge;
int index = badge.midRef(11).toInt(); int index = badge.midRef(11).toInt();
// TODO: Implement subscriber badges here // TODO: Implement subscriber badges here
switch (index) { switch (index) {
@ -430,7 +430,40 @@ void TwitchMessageBuilder::appendTwitchBadges(const QStringList &badges, Resourc
} break; } break;
} }
} else { } else {
printf("[TwitchMessageBuilder] Unhandled badge: %s\n", qPrintable(badge)); if (!resources.dynamicBadgesLoaded) {
// Do nothing
continue;
}
QStringList parts = badge.split('/');
if (parts.length() != 2) {
qDebug() << "Bad number of parts: " << parts.length() << " in " << parts;
continue;
}
Word::Type badgeType = Word::Type::BadgeMiscellaneous;
std::string badgeSetKey = parts[0].toStdString();
std::string versionKey = parts[1].toStdString();
try {
auto &badgeSet = resources.badgeSets.at(badgeSetKey);
try {
auto &badgeVersion = badgeSet.versions.at(versionKey);
appendWord(
Word(badgeVersion.badgeImage1x, badgeType, QString(),
QString("Twitch " + QString::fromStdString(badgeVersion.title))));
} catch (const std::exception &e) {
qDebug() << "Exception caught:" << e.what()
<< "when trying to fetch badge version " << versionKey.c_str();
}
} catch (const std::exception &e) {
qDebug() << "No badge set with key" << badgeSetKey.c_str()
<< ". Exception: " << e.what();
}
} }
} }
} }