internal static void ClientMessageReceived(CrowdControlClient c, string raw)
        {
            try {
                Console.WriteLine("Message received: " + raw);
                // Decode the message from CC
                // { "id":1,"code":"kill","viewer":"sdk","type":1}
                var request = JsonSerializer.Deserialize <CrowdControlRequest>(raw);

                // Do the thing
                var eventName = EventNameDict[request.Code];
                var status    = 0;

                if (TimerCooldown.IsInitialised())
                {
                    try
                    {
                        EventLookup.Lookup(eventName, request.Viewer);
                    }
                    catch (Exception)
                    {
                        // On exception set failed flag
                        status = 1;
                    }
                }
                else
                {
                    // Game is not yet ready for events, retry until it is.
                    status = 3;
                }


                // Send the result to CC
                var response = new CrowdControlResponse();
                response.Id      = request.Id;
                response.Status  = status;
                response.Message = "Effect: " + request.Code + ": " + response.Status;

                var resJson = JsonSerializer.Serialize <CrowdControlResponse>(response);
                Console.WriteLine("Sending response: " + resJson);

                c.Send(resJson, false);
            } catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Esempio n. 2
0
        internal static void ClientMessageReceived(CrowdControlClient c, string raw)
        {
            try {
                Console.WriteLine("Message received: " + raw);
                // Decode the message from CC
                // { "id":1,"code":"kill","viewer":"sdk","type":1}
                var request = JsonSerializer.Deserialize <CrowdControlRequest>(raw);

                // Do the thing
                var eventName = EventNameDict[request.Code];
                var status    = 0;

                if (request.Id == 0 && request.Type == 255)
                {
                    // Test message, ignore
                    return;
                }

                if (TimerCooldown.IsInitialised())
                {
                    try
                    {
                        // Check to see if the event can fire currently fire, or is on cooldown.
                        if (!EventLookup.IsRunningOrCooldown(eventName))
                        {
                            // Not running or on cooldown, activate it if the type is "start"
                            if (request.Type == 1)
                            {
                                EventLookup.Lookup(eventName, request.Viewer);
                            }
                        }
                        else
                        {
                            // Event in use, retry.
                            status = 3;
                        }
                    }
                    catch (Exception)
                    {
                        // On exception set failed flag
                        status = 1;
                    }
                }
                else
                {
                    // Game is not yet ready for events, retry until it is.
                    status = 3;
                }


                // Send the result to CC
                var response = new CrowdControlResponse();
                response.Id      = request.Id;
                response.Status  = status;
                response.Message = "Effect: " + request.Code + ": " + response.Status;

                var resJson = JsonSerializer.Serialize <CrowdControlResponse>(response);
                Console.WriteLine("Sending response: " + resJson);

                c.Send(resJson, false);
            } catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }