Search in sources :

Example 1 with GameWispBenefitsEvent

use of me.gloriouseggroll.quorrabot.event.gamewisp.GameWispBenefitsEvent 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());
    }
}
Also used : Options(io.socket.client.IO.Options) Emitter(io.socket.emitter.Emitter) GameWispSubscribeEvent(me.gloriouseggroll.quorrabot.event.gamewisp.GameWispSubscribeEvent) IO(io.socket.client.IO) GameWispChangeEvent(me.gloriouseggroll.quorrabot.event.gamewisp.GameWispChangeEvent) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) IllegalArgumentException(java.lang.IllegalArgumentException) InterruptedException(java.lang.InterruptedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) Options(io.socket.client.IO.Options) GameWispBenefitsEvent(me.gloriouseggroll.quorrabot.event.gamewisp.GameWispBenefitsEvent) JSONObject(org.json.JSONObject) GameWispAnniversaryEvent(me.gloriouseggroll.quorrabot.event.gamewisp.GameWispAnniversaryEvent) X509TrustManager(javax.net.ssl.X509TrustManager) JSONObject(org.json.JSONObject)

Aggregations

IO (io.socket.client.IO)1 Options (io.socket.client.IO.Options)1 Emitter (io.socket.emitter.Emitter)1 IOException (java.io.IOException)1 IllegalArgumentException (java.lang.IllegalArgumentException)1 InterruptedException (java.lang.InterruptedException)1 URISyntaxException (java.net.URISyntaxException)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 SSLContext (javax.net.ssl.SSLContext)1 TrustManager (javax.net.ssl.TrustManager)1 X509TrustManager (javax.net.ssl.X509TrustManager)1 GameWispAnniversaryEvent (me.gloriouseggroll.quorrabot.event.gamewisp.GameWispAnniversaryEvent)1 GameWispBenefitsEvent (me.gloriouseggroll.quorrabot.event.gamewisp.GameWispBenefitsEvent)1 GameWispChangeEvent (me.gloriouseggroll.quorrabot.event.gamewisp.GameWispChangeEvent)1 GameWispSubscribeEvent (me.gloriouseggroll.quorrabot.event.gamewisp.GameWispSubscribeEvent)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1