Skip to content

Commit

Permalink
feat(socket): add tier support on sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoodie committed Oct 6, 2019
1 parent 2e46f3e commit 6cabf65
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.daemon=false

mod_id=twitchspawn
mod_group=net.programmer.igoodie
mod_version=1.4.3
mod_version=1.4.4

minecraft_version=1.14.4
forge_version=28.0.83
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public static int simulateModule(CommandContext<CommandSource> context, String s
simulatedEvent.subscriptionMonths = nbt.getInt("months");
simulatedEvent.raiderCount = nbt.getInt("raiders");
simulatedEvent.viewerCount = nbt.getInt("viewers");
simulatedEvent.subscriptionTier = nbt.contains("tier", 3) ? nbt.getInt("tier") : -1;
}

ConfigManager.RULESET_COLLECTION.handleEvent(simulatedEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.socket.client.IO;
import io.socket.client.Socket;
import net.programmer.igoodie.twitchspawn.configuration.CredentialsConfig;
import net.programmer.igoodie.twitchspawn.util.JSONUtils;
import org.json.JSONObject;

import java.net.URISyntaxException;
import java.util.LinkedList;
Expand Down Expand Up @@ -65,4 +67,23 @@ protected IO.Options generateOptions(CredentialsConfig.Streamer streamer) {

protected abstract void onLiveEvent(Socket socket, CredentialsConfig.Streamer streamer, Object... args);

protected int extractTier(JSONObject message, String tierFieldName) {
String tierString = JSONUtils.extractFrom(message, tierFieldName, String.class, null);

if (tierString == null)
return -1;

if (tierString.equalsIgnoreCase("Prime"))
return 0; // tier = 0 stands for Prime

if (tierString.equalsIgnoreCase("1000"))
return 1;
if (tierString.equalsIgnoreCase("2000"))
return 2;
if (tierString.equalsIgnoreCase("3000"))
return 3;

return -1; // Unknown tier String
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ protected void onLiveEvent(Socket socket, CredentialsConfig.Streamer streamer, O
eventArguments.subscriptionMonths = JSONUtils.extractNumberFrom(data, "amount", 0).intValue();
// eventArguments.raiderCount = JSONUtils.extractNumberFrom(message, "raiders", 0).intValue(); // Raids aren't supported (?)
eventArguments.viewerCount = JSONUtils.extractNumberFrom(data, "amount ", 0).intValue();
eventArguments.subscriptionTier = extractTier(data, "tier");

// Pass the model to the handler
ConfigManager.RULESET_COLLECTION.handleEvent(eventArguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ protected void onLiveEvent(Socket socket, CredentialsConfig.Streamer streamer, O
eventArguments.subscriptionMonths = JSONUtils.extractNumberFrom(message, "months", 0).intValue();
eventArguments.raiderCount = JSONUtils.extractNumberFrom(message, "raiders", 0).intValue();
eventArguments.viewerCount = JSONUtils.extractNumberFrom(message, "viewers", 0).intValue();
eventArguments.subscriptionTier = extractTier(message, "sub_plan");

// Pass the model to the handler
ConfigManager.RULESET_COLLECTION.handleEvent(eventArguments);
Expand All @@ -113,10 +114,10 @@ private JSONArray extractMessages(JSONObject event) {
try {
Object messageField = event.get("message");

if(messageField instanceof JSONArray)
if (messageField instanceof JSONArray)
return JSONUtils.extractFrom(event, "message", JSONArray.class, new JSONArray());

else if(messageField instanceof JSONObject)
else if (messageField instanceof JSONObject)
return new JSONArray().put(messageField);

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static EventArguments createRandom(String streamerNickname) {
public String donationCurrency;

public int subscriptionMonths;
public int subscriptionTier = -1; // 0=Prime, 1=T1, 2=T2, 3=T3

public int viewerCount;
public int raiderCount;
Expand All @@ -58,6 +59,7 @@ public void randomize(String actorNickname, String message) {
this.donationAmount = random.nextDouble() * 1000;
this.donationCurrency = new String[]{"USD", "TRY", "EUR"}[random.nextInt(3)];
this.subscriptionMonths = random.nextInt(100 - 1) + 1;
this.subscriptionTier = random.nextInt(3 + 1);
this.viewerCount = random.nextInt(100 - 1) + 1;
this.raiderCount = random.nextInt(100 - 1) + 1;
}
Expand All @@ -72,7 +74,17 @@ public String toString() {
Object value = field.get(this);
Object defaultValue = Defaults.defaultValue(field.getType());

if (value != null && !value.equals(defaultValue)) {
if (value == null)
continue;

if (!value.equals(defaultValue)) {
sb.append(delimiter);
sb.append(field.getName()).append("=").append(value);
delimiter = ", ";
}

// Exception for tier field. (where default 0=Prime)
else if (field.getName().equalsIgnoreCase("tier") && value.equals(0)) {
sb.append(delimiter);
sb.append(field.getName()).append("=").append(value);
delimiter = ", ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public enum TSLPredicateProperty {
"subscriptionMonths",
"months", "subscription_months"
),
TIER(
"subscriptionTier",
"tier", "subscription_tier"
),
VIEWERS(
"viewerCount",
"viewers", "viewer_count"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public static String fromArgs(String expression, EventArguments args) {
if (expression.equals("months") && args.subscriptionMonths != 0)
return String.valueOf(args.subscriptionMonths);

if (expression.equals("tier") && args.subscriptionTier != -1)
return args.subscriptionTier == 0 ? "Prime" : String.valueOf(args.subscriptionTier);

if (expression.equals("viewers") && args.viewerCount != 0)
return String.valueOf(args.viewerCount);

Expand Down

0 comments on commit 6cabf65

Please sign in to comment.