-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(socket): implement StreamElements support
- Loading branch information
Showing
10 changed files
with
224 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/main/java/net/programmer/igoodie/twitchspawn/tracer/StreamElementsSocketTracer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package net.programmer.igoodie.twitchspawn.tracer; | ||
|
||
import io.socket.client.Socket; | ||
import net.programmer.igoodie.twitchspawn.TwitchSpawn; | ||
import net.programmer.igoodie.twitchspawn.configuration.ConfigManager; | ||
import net.programmer.igoodie.twitchspawn.configuration.CredentialsConfig; | ||
import net.programmer.igoodie.twitchspawn.tslanguage.EventArguments; | ||
import net.programmer.igoodie.twitchspawn.tslanguage.event.TSLEventPair; | ||
import net.programmer.igoodie.twitchspawn.tslanguage.keyword.TSLEventKeyword; | ||
import net.programmer.igoodie.twitchspawn.util.JSONUtils; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
public class StreamElementsSocketTracer extends SocketIOTracer { | ||
|
||
public Set<Socket> authorized; | ||
|
||
public StreamElementsSocketTracer(TraceManager manager) { | ||
super(Platform.STREAMELEMENTS, manager); | ||
this.authorized = new HashSet<>(); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
TwitchSpawn.LOGGER.info("Starting StreamElements Tracer..."); | ||
|
||
// Create socket for every credential with StreamElements platform | ||
ConfigManager.CREDENTIALS.streamers.stream() | ||
.filter(streamer -> streamer.platform.equals(Platform.STREAMELEMENTS)) | ||
.forEach(this::createSocket); | ||
|
||
this.sockets.forEach(Socket::connect); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
TwitchSpawn.LOGGER.info("Stopping StreamElements Tracer..."); | ||
|
||
this.sockets.forEach(Socket::disconnect); | ||
|
||
this.sockets.clear(); | ||
} | ||
|
||
@Override | ||
protected void onConnect(Socket socket, CredentialsConfig.Streamer streamer, Object... args) { | ||
try { | ||
JSONObject authArguments = new JSONObject(); | ||
authArguments.put("method", "jwt"); | ||
authArguments.put("token", streamer.token); | ||
socket.emit("authenticate", authArguments); | ||
|
||
} catch (JSONException ignored) {} // Must be impossible | ||
|
||
socket.on("authenticated", foo -> { | ||
TwitchSpawn.LOGGER.info("Connected to StreamElements Socket API with {}'s token successfully!", streamer.twitchNick); | ||
authorized.add(socket); | ||
}); | ||
|
||
// TODO: refactor dis, duh :V | ||
new Timer().schedule(new TimerTask() { | ||
@Override | ||
public void run() { | ||
if (manager.isRunning() && !authorized.contains(socket)) { | ||
TwitchSpawn.LOGGER.info("Disconnected from {}'s StreamElements Socket connection. (unauthorized)", streamer.minecraftNick); | ||
manager.stop(null, streamer.twitchNick + " unauthorized by the socket server"); | ||
} | ||
} | ||
}, 5000); | ||
} | ||
|
||
@Override | ||
protected void onDisconnect(Socket socket, CredentialsConfig.Streamer streamer, Object... args) { | ||
TwitchSpawn.LOGGER.info("Disconnected from {}'s StreamElements Socket connection. (intentional)", streamer.minecraftNick); | ||
} | ||
|
||
@Override | ||
protected void onLiveEvent(Socket socket, CredentialsConfig.Streamer streamer, Object... args) { | ||
JSONObject event = (JSONObject) args[0]; | ||
|
||
if (!event.has("data") || event.optJSONArray("data") == null) | ||
return; // Contains no data (in expected format), stop here | ||
|
||
String eventType = JSONUtils.extractFrom(event, "type", String.class, null); | ||
String eventAccount = JSONUtils.extractFrom(event, "provider", String.class, "streamelements"); | ||
|
||
JSONObject data = JSONUtils.extractFrom(event, "data", JSONObject.class, new JSONObject()); | ||
|
||
TwitchSpawn.LOGGER.info("Received StreamElements package {} -> {}", | ||
new TSLEventPair(eventType, eventAccount), data); | ||
|
||
// Unregistered event alias | ||
if (TSLEventKeyword.ofPair(eventType, eventAccount) == null) | ||
return; // Stop here, do not handle | ||
|
||
// Refine incoming data into EventArguments model | ||
EventArguments eventArguments = new EventArguments(eventType, eventAccount); | ||
eventArguments.streamerNickname = streamer.minecraftNick; | ||
eventArguments.actorNickname = JSONUtils.extractFrom(data, "username", String.class, null); | ||
eventArguments.message = JSONUtils.extractFrom(data, "message", String.class, null); | ||
eventArguments.donationAmount = JSONUtils.extractNumberFrom(data, "amount", 0.0).doubleValue(); | ||
eventArguments.donationCurrency = JSONUtils.extractFrom(data, "currency", String.class, null); | ||
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(); | ||
|
||
// Pass the model to the handler | ||
ConfigManager.RULESET_COLLECTION.handleEvent(eventArguments); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.