Various bits fixes (#1443)

We now make sure we don't render any fake bits
Bits now show their real value properly
This commit is contained in:
apa420 2019-12-19 21:36:02 +01:00 committed by pajlada
parent 11442bba20
commit 0d227ab2f5
4 changed files with 36 additions and 1 deletions

View file

@ -759,6 +759,7 @@ void TwitchChannel::refreshCheerEmotes()
cheerEmote.color = QColor(tier.color); cheerEmote.color = QColor(tier.color);
cheerEmote.minBits = tier.minBits; cheerEmote.minBits = tier.minBits;
cheerEmote.regex = cheerEmoteSet.regex;
// TODO(pajlada): We currently hardcode dark here :| // TODO(pajlada): We currently hardcode dark here :|
// We will continue to do so for now since we haven't had to // We will continue to do so for now since we haven't had to

View file

@ -18,6 +18,7 @@ using EmotePtr = std::shared_ptr<const Emote>;
struct CheerEmote { struct CheerEmote {
QColor color; QColor color;
int minBits; int minBits;
QRegularExpression regex;
EmotePtr animatedEmote; EmotePtr animatedEmote;
EmotePtr staticEmote; EmotePtr staticEmote;

View file

@ -346,6 +346,7 @@ MessagePtr TwitchMessageBuilder::build()
if (iterator != this->tags.end()) if (iterator != this->tags.end())
{ {
this->hasBits_ = true; this->hasBits_ = true;
this->bitsLeft = iterator.value().toInt();
this->bits = iterator.value().toString(); this->bits = iterator.value().toString();
} }
@ -1263,12 +1264,41 @@ void TwitchMessageBuilder::appendChatterinoBadges()
Outcome TwitchMessageBuilder::tryParseCheermote(const QString &string) Outcome TwitchMessageBuilder::tryParseCheermote(const QString &string)
{ {
if (this->bitsLeft == 0)
{
return Failure;
}
auto cheerOpt = this->twitchChannel->cheerEmote(string); auto cheerOpt = this->twitchChannel->cheerEmote(string);
if (!cheerOpt) if (!cheerOpt)
{ {
return Failure; return Failure;
} }
auto &cheerEmote = *cheerOpt; auto &cheerEmote = *cheerOpt;
auto match = cheerEmote.regex.match(string);
if (!match.hasMatch())
{
return Failure;
}
int cheerValue = match.captured(1).toInt();
if (this->bitsLeft >= cheerValue)
{
this->bitsLeft -= cheerValue;
}
else
{
QString newString = string;
newString.chop(QString::number(cheerValue).length());
newString += QString::number(cheerValue - this->bitsLeft);
return tryParseCheermote(newString);
}
if (cheerEmote.staticEmote) if (cheerEmote.staticEmote)
{ {
this->emplace<EmoteElement>(cheerEmote.staticEmote, this->emplace<EmoteElement>(cheerEmote.staticEmote,
@ -1281,9 +1311,11 @@ Outcome TwitchMessageBuilder::tryParseCheermote(const QString &string)
} }
if (cheerEmote.color != QColor()) if (cheerEmote.color != QColor())
{ {
this->emplace<TextElement>(this->bits, MessageElementFlag::BitsAmount, this->emplace<TextElement>(match.captured(1),
MessageElementFlag::BitsAmount,
cheerEmote.color); cheerEmote.color);
} }
return Success; return Success;
} }

View file

@ -81,6 +81,7 @@ private:
QString roomID_; QString roomID_;
bool hasBits_ = false; bool hasBits_ = false;
QString bits; QString bits;
int bitsLeft;
bool historicalMessage_ = false; bool historicalMessage_ = false;
QString userId_; QString userId_;