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

Refactoring and Addition of Test cases #506

Open
wants to merge 6 commits into
base: master
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dependencies {
implementation 'com.formdev:flatlaf:2.6'

testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:3.12.4'
}

// The wrapper is a small batch/bash script that can be used to run Gradle on machines where it hasn't been directly
Expand Down
77 changes: 59 additions & 18 deletions src/chatty/ChannelState.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,41 +201,82 @@ public String getInfo() {
* Update the info text once a state has been updated.
*/
private void updateInfo() {
String result = "";
String sep = "|";
StringBuilder resultBuilder = new StringBuilder();

appendShieldMode(resultBuilder);
appendSlowMode(resultBuilder);
appendSubMode(resultBuilder);
appendFollowersOnly(resultBuilder);
appendR9kMode(resultBuilder);
appendEmoteOnly(resultBuilder);
appendLanguage(resultBuilder);

String result = resultBuilder.toString();
if (!result.isEmpty()) {
info = "[" + result + "]";
} else {
info = ""; // Reset info if result is empty
}
}

private void appendShieldMode(StringBuilder resultBuilder) {
if (shieldMode) {
result = StringUtil.append(result, sep, "Shield");
StringUtil.append(String.valueOf(resultBuilder), "|", "Shield");
}
}

private void appendSlowMode(StringBuilder resultBuilder) {
String sep = "|";
if (slowMode == SLOWMODE_ON_INVALID || slowMode > 86400) {
result = StringUtil.append(result, sep, "Slow: >day");
StringUtil.append(String.valueOf(resultBuilder), sep, "Slow: >day");
} else if (slowMode > 999) {
result = StringUtil.append(result, sep, "Slow: "+DateTime.duration(slowMode*1000, 1, 0));
StringUtil.append(String.valueOf(resultBuilder), sep, "Slow: " + DateTime.duration(slowMode * 1000, 1, 0));
} else if (slowMode > 0) {
result = StringUtil.append(result, sep, "Slow: "+slowMode);
StringUtil.append(String.valueOf(resultBuilder), sep, "Slow: " + slowMode);
}
}

private void appendSubMode(StringBuilder resultBuilder) {
if (subMode) {
result = StringUtil.append(result, sep, "Sub");
StringUtil.append(String.valueOf(resultBuilder), "|", "Sub");
}
}

private void appendFollowersOnly(StringBuilder resultBuilder) {
String sep = "|";
if (followersOnly == SLOWMODE_ON_INVALID) {
result = StringUtil.append(result, sep, "Followers: ?");
StringUtil.append(String.valueOf(resultBuilder), sep, "Followers: ?");
} else if (followersOnly > 0) {
result = StringUtil.append(result, sep, "Followers: "+DateTime.duration((long)followersOnly*60*1000, 1, DateTime.S, DateTime.Formatting.COMPACT));
StringUtil.append(String.valueOf(resultBuilder), sep, "Followers: " +
DateTime.duration((long) followersOnly * 60 * 1000, 1, DateTime.S, DateTime.Formatting.COMPACT));
} else if (followersOnly == 0) {
result = StringUtil.append(result, sep, "Followers");
StringUtil.append(String.valueOf(resultBuilder), sep, "Followers");
}
}

private void appendR9kMode(StringBuilder resultBuilder) {
if (r9kMode) {
result = StringUtil.append(result, sep, "r9k");
StringUtil.append(String.valueOf(resultBuilder), "|", "r9k");
}
}

private void appendEmoteOnly(StringBuilder resultBuilder) {
if (emoteOnly) {
result = StringUtil.append(result, sep, "EmoteOnly");
StringUtil.append(String.valueOf(resultBuilder), "|", "EmoteOnly");
}
}

private void appendLanguage(StringBuilder resultBuilder) {
if (lang != null && !lang.isEmpty()) {
result = StringUtil.append(result, sep, lang);
}
if (!result.isEmpty()) {
result = "["+result+"]";
StringUtil.append(String.valueOf(resultBuilder), "|", lang);
}
info = result;
}




}





11 changes: 8 additions & 3 deletions src/chatty/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -1055,12 +1055,17 @@ private static UserNotice findPointsMerge(UserNotice newNotice) {
UserNotice found = null;
for (Map.Entry<UserNotice, javax.swing.Timer> entry : pointsMerge.entrySet()) {
UserNotice stored = entry.getKey();
// Attached messages seem to be trimmed depending on source
if (stored.tags.getCustomRewardId() != null
&& stored.tags.getCustomRewardId().equals(newNotice.tags.getCustomRewardId())) {

// Attached messages seem to be trimmed depending on source
boolean hasCustomRewardId = stored.tags.getCustomRewardId() != null;
boolean hasSameCustomRewardId = hasCustomRewardId && stored.tags.getCustomRewardId()
.equals(newNotice.tags.getCustomRewardId());

if (hasSameCustomRewardId) {
found = stored;
entry.getValue().stop();
}

}
if (found != null) {
pointsMerge.remove(found);
Expand Down
45 changes: 45 additions & 0 deletions src/chatty/ModsHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package chatty;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class ModsHandler {

private final Set<String> silentModsRequestChannel = Collections.synchronizedSet(new HashSet<String>());
private TwitchConnection c;

public ModsHandler(TwitchConnection c) {
this.c = c;
}

public void modsSilent(String channel) {
if (onChannel(channel, true)) {
printLine(channel, "Trying to fix moderators..");
requestModsSilent(channel);
}
}

public void requestModsSilent(String channel) {
if (onChannel(channel, false)) {
silentModsRequestChannel.add(channel);
c.sendSpamProtectedMessage(channel, ".mods", false);
}
}

public boolean removeModsSilent(String channel) {
return silentModsRequestChannel.remove(channel);
}

public boolean waitingForModsSilent() {
return !silentModsRequestChannel.isEmpty();
}

private boolean onChannel(String channel, boolean message) {
return c.onChannel(channel, message);
}

private void printLine(String channel, String message) {
c.info(channel, message, null);
}
}
27 changes: 4 additions & 23 deletions src/chatty/TwitchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
import chatty.util.seventv.SevenTV;
import chatty.util.seventv.WebPUtil;
import chatty.util.srl.SpeedrunsLive;
import chatty.User;
import java.awt.Color;
import java.io.File;
import java.lang.management.ManagementFactory;
Expand Down Expand Up @@ -300,7 +301,7 @@ public TwitchClient(Map<String, String> args) {
// Create after Logging is created, since that resets some stuff
ircLogger = new IrcLogger();

createTestUser("tduva", "");
User.createTestUser("tduva", "");

api = new TwitchApi(new TwitchApiResults(), new MyStreamInfoListener());
addTwitchApiResultListeners();
Expand Down Expand Up @@ -651,27 +652,7 @@ private void checkNewVersion() {
* @param name
* @param channel
*/
private void createTestUser(String name, String channel) {
testUser = new User(name, name, Room.createRegular(channel));
testUser.setColor(new Color(94, 0, 211));
// Force color correction for longer userinfo color label
testUser.setColor(new Color(255, 255, 255));
//testUser.setColor(new Color(0,216,107));
//testUser.setBot(true);
//testUser.setTurbo(true);
testUser.setModerator(true);
testUser.setSubscriber(true);
//testUser.setAdmin(true);
//testUser.setStaff(true);
//testUser.setBroadcaster(true);
// LinkedHashMap<String, String> badgesTest = new LinkedHashMap<>();
// badgesTest.put("global_mod", "1");
// badgesTest.put("moderator", "1");
// badgesTest.put("premium", "1");
// badgesTest.put("bits", "1000000");
// testUser.setTwitchBadges(badgesTest);
}


/**
* Close all channels except the ones in the given Array.
*
Expand Down Expand Up @@ -1796,7 +1777,7 @@ private void testCommands(Room room, String command, String parameter) {
g.switchToChannel(parameter);
} else if (command.equals("settestuser")) {
String[] split = parameter.split(" ");
createTestUser(split[0], split[1]);
User.createTestUser(split[0], split[1]);
} else if (command.equals("getemoteset")) {
g.printLine(g.emoticons.getEmoticonsBySet(parameter).toString());
} else if (command.equals("testcolor")) {
Expand Down
28 changes: 1 addition & 27 deletions src/chatty/TwitchCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,33 +506,7 @@ private void printLine(String channel, String message) {
c.info(channel, message, null);
}

//==================
// Silent Mods List
//==================
// For updating who is mod, probably mostly obsolete

public void modsSilent(String channel) {
if (onChannel(channel, true)) {
printLine(channel, "Trying to fix moderators..");
requestModsSilent(channel);
}
}

public void requestModsSilent(String channel) {
if (onChannel(channel, false)) {
silentModsRequestChannel.add(channel);
c.sendSpamProtectedMessage(channel, ".mods", false);
}
}

public boolean removeModsSilent(String channel) {
return silentModsRequestChannel.remove(channel);
}

public boolean waitingForModsSilent() {
return !silentModsRequestChannel.isEmpty();
}


/**
* Prase the list of mods as returned from the Twitch Chat. The
* comma-separated list should start after the first colon ("The moderators
Expand Down
7 changes: 4 additions & 3 deletions src/chatty/TwitchConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class TwitchConnection {
public enum JoinError {
NOT_REGISTERED, ALREADY_JOINED, INVALID_NAME, ROOM
}


private ModsHandler modsHandler;
private static final Logger LOGGER = Logger.getLogger(TwitchConnection.class.getName());

private final ConnectionListener listener;
Expand Down Expand Up @@ -102,6 +103,7 @@ public TwitchConnection(final ConnectionListener listener, Settings settings,
irc = new IrcConnection(label);
this.listener = listener;
this.settings = settings;
this.modsHandler = new ModsHandler(this);
this.twitchCommands = new TwitchCommands(this);
this.rooms = rooms;
spamProtection = new SpamProtection();
Expand Down Expand Up @@ -1300,8 +1302,7 @@ private void parseModeratorsList(String text, String channel) {
* a) has to be checked first, because b) might remove the channel,
* so a) might be true even if it shouldn't be
*/
if (!twitchCommands.waitingForModsSilent()
|| (channel != null && !twitchCommands.removeModsSilent(channel))) {
if (!modsHandler.waitingForModsSilent() || (channel != null && !modsHandler.removeModsSilent(channel))) {
info(channel, "[Info] " + text, null);

// Output appropriate message
Expand Down
10 changes: 10 additions & 0 deletions src/chatty/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,16 @@ public synchronized Color getDisplayColor() {
}
return color;
}

public static User createTestUser(String name, String channel) {
User testUser = new User(name, name, Room.createRegular(channel));
testUser.setColor(new Color(94, 0, 211));
// Force color correction for longer userinfo color label
testUser.setColor(new Color(255, 255, 255));
testUser.setModerator(true);
testUser.setSubscriber(true);
return testUser;
}

/**
* Only return custom or corrected color.
Expand Down
19 changes: 2 additions & 17 deletions src/chatty/util/api/eventsub/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public class Connection extends JWSClient {

private volatile String sessionId;
private volatile Connection replacesConnection;
private volatile int connectionTimeoutSeconds;


public Connection(URI server, MessageHandler handler, TwitchApi api) {
super(server);
this.handler = handler;
Expand All @@ -37,21 +36,7 @@ public Connection(URI server, MessageHandler handler, TwitchApi api) {
public void setConnectionTimeout(int seconds) {
this.connectionTimeoutSeconds = seconds;
}

public void checkTimeout() {
if (!isOpen() && getConnectionSeconds() > 30) {
return;
}
if (connectionTimeoutSeconds > 0) {
if (getLastReceivedSecondsAgo() > connectionTimeoutSeconds * 2) {
forceReconnect();
}
else if (getLastReceivedSecondsAgo() > connectionTimeoutSeconds) {
reconnect();
}
}
}


public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
Expand Down
30 changes: 15 additions & 15 deletions src/chatty/util/colors/ColorCorrectionNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@ public static Color correctReadability(Color foreground, Color background, int t
}

public static Color makeBrighter(Color c, float factor) {
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
// Remaining to max value
int rr = 255 - r;
int rg = 255 - g;
int rb = 255 - b;
// Add a factor of remaining
int cr = r + (int)(rr * factor);
int cg = g + (int)(rg * factor);
int cb = b + (int)(rb * factor);
return new Color(cr, cg, cb);
int originalRed = c.getRed();
int originalGreen = c.getGreen();
int originalBlue = c.getBlue();

// Calculate remaining to max value for each color component
int remainingRed = 255 - originalRed;
int remainingGreen = 255 - originalGreen;
int remainingBlue = 255 - originalBlue;

// Add a factor of remaining to each color component
int brighterRed = originalRed + (int) (remainingRed * factor);
int brighterGreen = originalGreen + (int) (remainingGreen * factor);
int brighterBlue = originalBlue + (int) (remainingBlue * factor);

return new Color(brighterRed, brighterGreen, brighterBlue);
}

public static Color makeDarker(Color c, float factor) {
Expand Down
Loading