Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Early support for current Twitch badges #12

Open
wants to merge 4 commits into
base: issue-11
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/main/java/com/glitchcog/fontificator/bot/ChatViewerBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,16 +462,13 @@ private TwitchPrivmsg parseRawTwitchMessage(String rawMessage)
log("Error parsing subscriber value \"" + turboStr + "\" in Twitch header");
}
}
privmsg.setPrime(paramMap.get("badges") != null && paramMap.get("badges").contains("premium"));

String userTypeStr = paramMap.get("user-type");
if (displayName != null && !displayName.trim().isEmpty() && displayName.equalsIgnoreCase(this.controlPanel.getChannelNoHash()))
if( paramMap.get("badges") != null )
{
// Set the broadcaster badge based on the display name matching the channel connected to, since Twitch
// doesn't put this usertype into its IRC tags.
privmsg.setUserType(UserType.BROADCASTER);
privmsg.addBadges( paramMap.get("badges").split(",") );
}
else if (userTypeStr != null && !userTypeStr.trim().isEmpty())
String userTypeStr = paramMap.get("user-type");
if (userTypeStr != null && !userTypeStr.trim().isEmpty())
{
// If the string value is something weird, the enum will just return NONE
privmsg.setUserType(UserType.getByKey(userTypeStr));
Expand Down
89 changes: 41 additions & 48 deletions src/main/java/com/glitchcog/fontificator/bot/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,81 +409,74 @@ private SpriteCharacterKey[] parseIntoText(EmojiManager emojiManager, ConfigMess
final boolean userIsModerator = privmsg.getUserType() == UserType.MOD;

// Bank to pull Twitch badges from
TypedEmojiMap twitchBadgeBank = emojiManager.getEmojiByType(EmojiType.TWITCH_BADGE);
TypedEmojiMap twitchLocalBadgeBank = emojiManager.getEmojiByType(EmojiType.TWITCH_BADGE);
TypedEmojiMap twitchBadgeBank = emojiManager.getEmojiByType(EmojiType.TWITCH_BADGE_GLOBAL);

// Bank to pull FrankerFaceZ badges from
TypedEmojiMap ffzBadgeBank = emojiManager.getEmojiByType(EmojiType.FRANKERFACEZ_BADGE);
Map<String, LazyLoadEmoji> ffzBadgeMap = new LinkedHashMap<String, LazyLoadEmoji>();

// Get the badge for the type of user, if the usertype has a badge (but not if it's an ffzBot)
if (privmsg.getUserType() != null && privmsg.getUserType() != UserType.NONE)
if (emojiConfig.isFfzBadgesEnabled())
{
LazyLoadEmoji testBadge;
// FFZ badges are enabled, the user is a moderator, and the custom FFZ moderator badge exists
if (emojiConfig.isFfzBadgesEnabled() && userIsModerator && ffzBadgeBank.getEmoji(UserType.MOD.getKey()) != null)
{
badges.put(UserType.MOD.getKey(), ffzBadgeBank.getEmoji(UserType.MOD.getKey()));
}
else if (emojiConfig.isTwitchBadgesEnabled() && (testBadge = twitchBadgeBank.getEmoji(privmsg.getUserType().getKey())) != null)
// Channel-specific mod badge override
if( userIsModerator && ffzBadgeBank.getEmoji(UserType.MOD.getBadge()) != null)
{
badges.put(privmsg.getUserType().getKey(), testBadge);
ffzBadgeMap.put(UserType.MOD.getBadge(), ffzBadgeBank.getEmoji(UserType.MOD.getBadge()));
}
}

LazyLoadEmoji replacementBadge = null;

if (emojiConfig.isFfzBadgesEnabled())
{
// User badges
Map<Integer, Set<String>> ffzBadgeUsers = emojiManager.getFfzBadgeUsers();
for (Integer ffzBadgeType : ffzBadgeUsers.keySet())
{
final String ffzBadgeKey = ffzBadgeType == null ? null : Integer.toString(ffzBadgeType);
Set<String> users = ffzBadgeUsers.get(ffzBadgeType);
if (users.contains(username.toLowerCase()))
{
LazyLoadEmoji ffzBadge = ffzBadgeBank.getEmoji(ffzBadgeType);
if (ffzBadge.isReplacement() && badges.containsKey(ffzBadge.getReplaces()))
{
badges.put(ffzBadge.getReplaces(), ffzBadge);
if (userIsModerator && true)
{
replacementBadge = ffzBadge;
}
ffzBadgeMap.put(ffzBadgeKey, ffzBadgeBank.getEmoji(ffzBadgeType));
}
}
}

if (emojiConfig.isTwitchBadgesEnabled()) {
for(String badgestr : privmsg.getBadges()) {
LazyLoadEmoji globalbadge = twitchBadgeBank.getEmoji(badgestr);
LazyLoadEmoji localbadge = twitchLocalBadgeBank.getEmoji(badgestr);
LazyLoadEmoji target_badge = null;

if( localbadge != null ) {
target_badge = localbadge;
} else if( globalbadge != null ) {
target_badge = globalbadge;
}

if( target_badge != null ) {
// Handle the special case of an FFZ badge replacing a twitch badge.
// Note that the twitch badge's key might be moderator/0 or moderator/1, but the FFZ badge
// with identifier "moderator" overrides them
LazyLoadEmoji ffzReplacement = ffzBadgeMap.remove(target_badge.getPrimaryidentifier());

if( ffzReplacement != null ) {
// The FFZ badge is there and overrides
badges.put(target_badge.getPrimaryidentifier(), ffzReplacement);
}
else
{
badges.put(ffzBadgeKey, ffzBadge);
// Other twitch badges are just inserted in the list
badges.put(badgestr, target_badge);
}
}
}
}

// Optional subscriber badge
if (emojiConfig.isTwitchBadgesEnabled())
{
final String subStr = "subscriber";
if (privmsg.isSubscriber() && twitchBadgeBank.getEmoji(subStr) != null)
{
badges.put(subStr, twitchBadgeBank.getEmoji(subStr));
}
}

// Optional turbo badge
final String turboStr = "turbo";
if (emojiConfig.isTwitchBadgesEnabled() && privmsg.isTurbo() && twitchBadgeBank.getEmoji(turboStr) != null)
{
badges.put(turboStr, twitchBadgeBank.getEmoji(turboStr));
}

final String primeStr = "prime";
if (emojiConfig.isTwitchBadgesEnabled() && privmsg.isPrime() && twitchBadgeBank.getEmoji(primeStr) != null)
// Add each badges map item onto the sprite character key list
for (LazyLoadEmoji lle : ffzBadgeMap.values())
{
badges.put(primeStr, twitchBadgeBank.getEmoji(primeStr));
keyList.add(new SpriteCharacterKey(lle, true));
}

// Add each badges map item onto the sprite character key list
for (LazyLoadEmoji lle : badges.values())
{
SpriteCharacterKey sck = new SpriteCharacterKey(lle, true);
if (userIsModerator && lle == replacementBadge)
if (userIsModerator && lle.isReplacement())
{
sck.setEmojiBgColorOverride(ConfigEmoji.MOD_BADGE_COLOR);
}
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/glitchcog/fontificator/bot/TwitchPrivmsg.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.glitchcog.fontificator.bot;

import java.awt.Color;
import java.util.Arrays;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;

/**
Expand Down Expand Up @@ -33,6 +35,11 @@ public class TwitchPrivmsg
*/
private Map<Integer, EmoteAndIndices> emotes;

/**
* The list of badges
*/
private ArrayList<String> badges;

/**
* Whether the user has a Twitch subscription to the channel to which the message is posted
*/
Expand Down Expand Up @@ -85,6 +92,7 @@ public void setDefaults()
postCount = 0;
color = null;
emotes = new HashMap<Integer, EmoteAndIndices>();
badges = new ArrayList<String>();
turbo = false;
prime = false;
subscriber = false;
Expand Down Expand Up @@ -171,13 +179,21 @@ public Map<Integer, EmoteAndIndices> getEmotes()
/**
* Add to the list of emotes
*
* @param emoteSets
* @param emoteSet
*/
public void addEmote(EmoteAndIndices emoteSet)
{
emotes.put(emoteSet.getBegin(), emoteSet);
}

public ArrayList<String> getBadges() {
return badges;
}

public void addBadges(String newbadges[]) {
this.badges.addAll( Arrays.asList(newbadges) );
}

/**
* Get whether the user has a Twitch subscription to the channel to which the message is posted
*
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/glitchcog/fontificator/bot/UserType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@

public enum UserType
{
NONE(""), MOD("mod"), GLOBAL_MOD("global_mod"), ADMIN("admin"), STAFF("staff"), BROADCASTER("broadcaster");
NONE(""), MOD("mod", "moderator"), GLOBAL_MOD("global_mod"), ADMIN("admin"), STAFF("staff");

private final String key;
private final String badge;

private UserType(String key)
{
this(key, key);
}

private UserType(String key, String badge)
{
this.key = key;
this.badge = badge;
}

public String getKey()
{
return key;
}

public String getBadge()
{
return badge;
}

public static UserType getByKey(String key)
{
for (UserType type : values())
Expand Down
48 changes: 42 additions & 6 deletions src/main/java/com/glitchcog/fontificator/config/ConfigEmoji.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public class ConfigEmoji extends Config
*/
private String twitchBadgesLoadedChannel;

/**
* The loaded Twitch badges channel, or null if none is loaded
*/
private String twitchBadgesLoadedGlobal;

/**
* The loaded FrankerFaceZ badges channel, or null if none is loaded
*/
Expand Down Expand Up @@ -404,6 +409,8 @@ public boolean isTypeEnabledAndLoaded(EmojiType type)
// return ControlPanelEmoji.TWITCH_EMOTE_VERSION.equals(type) && twitchEnabled != null && twitchEnabled && twitchLoaded != null && twitchLoaded;
case TWITCH_BADGE:
return twitchBadgesEnabled != null && twitchBadgesEnabled && twitchBadgesLoadedChannel != null;
case TWITCH_BADGE_GLOBAL:
return twitchBadgesEnabled != null && twitchBadgesEnabled && twitchBadgesLoadedGlobal != null;
default:
// If it doesn't have a coded EmojiType, then we don't know it
return false;
Expand Down Expand Up @@ -506,6 +513,7 @@ public int hashCode()
result = prime * result + ((twitchEnabled == null) ? 0 : twitchEnabled.hashCode());
result = prime * result + ((twitchLoaded == null) ? 0 : twitchLoaded.hashCode());
result = prime * result + ((twitchBadgesLoadedChannel == null) ? 0 : twitchBadgesLoadedChannel.hashCode());
result = prime * result + ((twitchBadgesLoadedGlobal == null) ? 0 : twitchBadgesLoadedGlobal.hashCode());
return result;
}

Expand Down Expand Up @@ -712,10 +720,21 @@ else if (!twitchLoaded.equals(other.twitchLoaded))
return false;
}
}
if (twitchBadgesLoadedGlobal == null)
{
if (other.twitchBadgesLoadedGlobal != null)
{
return false;
}
}
else if (!twitchBadgesLoadedChannel.equals(other.twitchBadgesLoadedChannel))
{
return false;
}
else if (!twitchBadgesLoadedGlobal.equals(other.twitchBadgesLoadedGlobal))
{
return false;
}
return true;
}

Expand All @@ -740,6 +759,7 @@ public void deepCopy(ConfigEmoji copy)
this.ffzEnabled = copy.ffzEnabled;
this.twitchLoaded = copy.twitchLoaded;
this.twitchBadgesLoadedChannel = copy.twitchBadgesLoadedChannel;
this.twitchBadgesLoadedGlobal = copy.twitchBadgesLoadedGlobal;
this.ffzLoadedChannel = copy.ffzLoadedChannel;
this.ffzGlobalLoaded = copy.ffzGlobalLoaded;
this.bttvEnabled = copy.bttvEnabled;
Expand Down Expand Up @@ -810,20 +830,31 @@ public boolean isFfzBadgesLoaded(String testChannel)
}

/**
* Set whether the Twitch badges have been loaded
* Set whether the Twitch channel badges have been loaded
*
* @param twitchLoadedChannel
* from which the Twitch emotes are loaded
* @param twitchBadgesLoadedChannel
* from which the Twitch channel badges are loaded
*/
public void setTwitchBadgesLoaded(String twitchBadgesLoadedChannel)
public void setTwitchBadgesLoadedChannel(String twitchBadgesLoadedChannel)
{
this.twitchBadgesLoadedChannel = twitchBadgesLoadedChannel;
}

/**
* Set whether the Twitch global badges have been loaded
*
* @param twitchBadgesLoadedGlobal
* from which the Twitch global badges are loaded
*/
public void setTwitchBadgesLoadedGlobal(String twitchBadgesLoadedGlobal)
{
this.twitchBadgesLoadedGlobal = twitchBadgesLoadedGlobal;
}

/**
* Set whether the FrankerFaceZ badges have been loaded
*
* @param ffzLoadedChannel
* @param ffzBadgesLoadedChannel
* from which the FrankerFaceZ emotes are loaded
*/
public void setFfzBadgesLoaded(String ffzBadgesLoadedChannel)
Expand Down Expand Up @@ -960,6 +991,10 @@ public void setWorkCompleted(EmojiJob job)
{
this.twitchBadgesLoadedChannel = job.getChannel();
}
else if (EmojiType.TWITCH_BADGE_GLOBAL.equals(emojiType))
{
this.twitchBadgesLoadedGlobal = job.getChannel();
}
else if (EmojiType.FRANKERFACEZ_BADGE.equals(emojiType))
{
this.ffzBadgesLoadedChannel = job.getChannel();
Expand Down Expand Up @@ -1022,6 +1057,7 @@ public void resetWorkCompleted()
this.twitchLoaded = false;
this.twitchCached = false;
this.twitchBadgesLoadedChannel = null;
this.twitchBadgesLoadedGlobal = null;
this.ffzBadgesLoadedChannel = null;
this.ffzLoadedChannel = null;
this.ffzGlobalLoaded = false;
Expand All @@ -1039,7 +1075,7 @@ public void resetWorkCompleted()
public boolean isAnyWorkDone()
{
// @formatter:off
return twitchBadgesLoadedChannel != null || isTwitchLoaded() || isTwitchCached() ||
return twitchBadgesLoadedChannel != null || twitchBadgesLoadedGlobal != null || isTwitchLoaded() || isTwitchCached() ||
ffzBadgesLoadedChannel != null || ffzLoadedChannel != null || isFfzGlobalLoaded() || isFfzCached() ||
bttvLoadedChannel != null || isBttvGlobalLoaded() || isBttvCached();
// @formatter:on
Expand Down
Loading