use of me.gloriouseggroll.quorrabot.event.gamewisp.GameWispChangeEvent in project quorrabot by GloriousEggroll.
the class SingularityAPI method StartService.
public void StartService() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
} };
try {
SSLContext mySSLContext = SSLContext.getInstance("TLS");
mySSLContext.init(null, null, null);
IO.Options opts = new IO.Options();
opts.sslContext = mySSLContext;
opts.hostnameVerifier = new NullHostnameVerifier();
webSocket = IO.socket(apiURL);
webSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
//com.gmt2001.Console.out.println("GameWisp API: Connected to Singularity");
webSocket.emit("authentication", new JSONObject().put("key", gwIdentifier).put("access_token", AccessToken));
}
});
webSocket.on("unauthorized", new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONObject jsonObject = new JSONObject(args[0].toString());
com.gmt2001.Console.err.println("GameWisp API: Authorization Failed: " + jsonObject.getString("message"));
com.gmt2001.Console.err.println("Token: " + AccessToken + " Session ID: " + SessionID + " Client ID: " + gwIdentifier);
}
});
webSocket.on("authenticated", new Emitter.Listener() {
@Override
public void call(Object... args) {
//com.gmt2001.Console.out.println("GameWisp API: Authenticated");
JSONObject jsonObject = new JSONObject(args[0].toString());
if (!jsonObject.has("session")) {
com.gmt2001.Console.err.println("GameWisp API: Missing Session in Authenticated Return JSON");
Authenticated = false;
return;
}
SessionID = jsonObject.getString("session");
Authenticated = true;
}
});
webSocket.on("app-channel-connected", new Emitter.Listener() {
@Override
public void call(Object... args) {
if (Authenticated) {
if (Quorrabot.enableDebugging) {
com.gmt2001.Console.out.println("GameWisp API: Connected to Channel");
} else {
com.gmt2001.Console.out.println("GameWisp API: Connected and Ready for Requests");
}
ChannelConnected = true;
} else {
com.gmt2001.Console.out.println("GameWisp API: Connected to Channel; Missing Session ID; Unusable Session");
ChannelConnected = false;
}
}
});
webSocket.on("subscriber-new", new Emitter.Listener() {
@Override
public void call(Object... args) {
com.gmt2001.Console.out.println("GameWisp API: subscriber-new received");
JSONObject jsonObject = new JSONObject(args[0].toString());
if (!jsonObject.has("data")) {
return;
}
if (!jsonObject.getJSONObject("data").has("usernames")) {
return;
}
if (!jsonObject.getJSONObject("data").getJSONObject("usernames").has("twitch")) {
return;
}
if (!jsonObject.getJSONObject("data").has("tier")) {
return;
}
if (!jsonObject.getJSONObject("data").getJSONObject("tier").has("level")) {
return;
}
String username = jsonObject.getJSONObject("data").getJSONObject("usernames").getString("twitch");
int tier = jsonObject.getJSONObject("data").getJSONObject("tier").getInt("level");
EventBus.instance().post(new GameWispSubscribeEvent(username, tier));
}
});
webSocket.on("subscriber-anniversary", new Emitter.Listener() {
@Override
public void call(Object... args) {
com.gmt2001.Console.out.println("GameWisp API: subscriber-anniversary received");
JSONObject jsonObject = new JSONObject(args[0].toString());
if (!jsonObject.has("data")) {
return;
}
if (!jsonObject.getJSONObject("data").has("subscriber")) {
return;
}
if (!jsonObject.getJSONObject("data").getJSONObject("subscriber").has("usernames")) {
return;
}
if (!jsonObject.getJSONObject("data").getJSONObject("subscriber").getJSONObject("usernames").has("twitch")) {
return;
}
if (!jsonObject.getJSONObject("data").has("month_count")) {
return;
}
String username = jsonObject.getJSONObject("data").getJSONObject("subscriber").getJSONObject("usernames").getString("twitch");
int tier = jsonObject.getJSONObject("data").getJSONObject("subscriber").getJSONObject("tier").getInt("level");
int months = jsonObject.getJSONObject("data").getInt("month_count");
EventBus.instance().post(new GameWispAnniversaryEvent(username, months, tier));
}
});
webSocket.on("subscriber-benefits-change", new Emitter.Listener() {
@Override
public void call(Object... args) {
com.gmt2001.Console.out.println("GameWisp API: subscriber-benefits-change received");
JSONObject jsonObject = new JSONObject(args[0].toString());
if (!jsonObject.has("data")) {
return;
}
if (!jsonObject.getJSONObject("data").has("usernames")) {
return;
}
if (!jsonObject.getJSONObject("data").getJSONObject("usernames").has("twitch")) {
return;
}
if (!jsonObject.has("tier")) {
return;
}
if (!jsonObject.getJSONObject("tier").has("level")) {
return;
}
String username = jsonObject.getJSONObject("data").getJSONObject("usernames").getString("twitch");
int tier = jsonObject.getJSONObject("tier").getInt("level");
EventBus.instance().post(new GameWispBenefitsEvent(username, tier));
}
});
/**
* Status Change Values:
* https://gamewisp.readme.io/docs/subscriber-new active - a
* currently active subscriber trial - a subscriber on a trial code
* grace_period - a canceled subscriber that is still received
* benefits billing_grace_period - a canceled subscriber still
* receiving benefits that was canceled due to a payment processing
* error inactive - a subscriber that is canceled and receiving no
* benefits twitch - a subscriber that is receiving free benefits
* from a partnered Twitch streamer.
*/
webSocket.on("subscriber-status-change", new Emitter.Listener() {
@Override
public void call(Object... args) {
com.gmt2001.Console.out.println("GameWisp API: subscriber-status-changed received");
JSONObject jsonObject = new JSONObject(args[0].toString());
if (!jsonObject.has("data")) {
return;
}
if (!jsonObject.getJSONObject("data").has("usernames")) {
return;
}
if (!jsonObject.getJSONObject("data").getJSONObject("usernames").has("twitch")) {
return;
}
if (!jsonObject.getJSONObject("data").has("status")) {
return;
}
String username = jsonObject.getJSONObject("data").getJSONObject("usernames").getString("twitch");
String status = jsonObject.getJSONObject("data").getString("status");
EventBus.instance().post(new GameWispChangeEvent(username, status));
}
});
webSocket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
com.gmt2001.Console.out.println("GameWisp API: Disconnected");
}
});
webSocket.connect();
} catch (Exception ex) {
com.gmt2001.Console.err.println("GameWisp API: Exception: " + ex.getMessage());
}
}
Aggregations