示例#1
0
        private void ProcessQueues(bool forceToSend)
        {
            if (forceToSend || (processQueues && sdkIsReady))
            {
                int failedCount = _failedQueue.Count;
                for (int i = failedCount - 1; i >= 0; --i)
                {
                    ElephantRequest data    = _failedQueue[i];
                    int             tc      = data.tryCount % 6;
                    int             backoff = (int)(Math.Pow(2, tc) * 1000);

                    if (Utils.Timestamp() - data.lastTryTS > backoff)
                    {
                        _failedQueue.RemoveAt(i);
                        StartCoroutine(Post(data));
                    }
                }

                while (_queue.Count > 0)
                {
                    ElephantRequest data = _queue.Dequeue();
                    StartCoroutine(Post(data));
                }
            }
        }
示例#2
0
        void OnApplicationFocus(bool focus)
        {
            if (focus)
            {
                currentSession = SessionData.CreateSessionData();

                Log("Focus Gained");
                // rebuild queues from disk..
                RebuildQueue();

                // start queue processing
                processQueues = true;
            }
            else
            {
                Log("Focus Lost");
                // pause late update
                processQueues = false;

                // send session log
                var currentSession = ElephantCore.Instance.GetCurrentSession();
                currentSession.end_time = Utils.Timestamp();

                var sessionReq = new ElephantRequest(SESSION_EP, currentSession);
                AddToQueue(sessionReq);

                // process queues
                ProcessQueues(true);

                // drain queues and persist them to send after gaining focus
                SaveQueues();
            }
        }
示例#3
0
        public void AcceptGDPR()
        {
            ElephantCore.Instance.AcceptGDPR();
            var             data = GDPRRequest.CreateGdprRequest(ElephantCore.Instance.idfa, ElephantCore.Instance.idfv);
            ElephantRequest req  = new ElephantRequest(ElephantCore.GDPR_EP, data);

            ElephantCore.Instance.AddToQueue(req);
        }
示例#4
0
        IEnumerator Post(ElephantRequest elephantRequest)
        {
            Log(elephantRequest.tryCount + " - " + (Utils.Timestamp() - elephantRequest.lastTryTS) + " -> " +
                elephantRequest.url + " : " + elephantRequest.data);

            elephantRequest.tryCount++;
            elephantRequest.lastTryTS = Utils.Timestamp();

            var elephantData = new ElephantData(elephantRequest.data, GetCurrentSession().GetSessionID());

            string bodyJsonString = JsonUtility.ToJson(elephantData);

            string authToken = Utils.SignString(bodyJsonString, GameSecret);


#if UNITY_EDITOR
            var    request = new UnityWebRequest(elephantRequest.url, "POST");
            byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
            request.uploadHandler   = new UploadHandlerRaw(bodyRaw);
            request.downloadHandler = new DownloadHandlerBuffer();
            request.SetRequestHeader("Content-Type", "application/json");

            request.SetRequestHeader("Authorization", authToken);
            request.SetRequestHeader("GameID", ElephantCore.Instance.GameID);

            yield return(request.SendWebRequest());

            Log("Status Code: " + request.responseCode);

            if (request.responseCode != 200)
            {
                // failed will be retried
                if (_failedQueue.Count < MAX_FAILED_COUNT)
                {
                    _failedQueue.Add(elephantRequest);
                }
                else
                {
                    Log("Failed Queue size -> " + _failedQueue.Count);
                }
            }
#else
#if UNITY_IOS
            ElephantIOS.ElephantPost(elephantRequest.url, bodyJsonString, GameID, authToken, elephantRequest.tryCount);
#elif UNITY_ANDROID
            ElephantAndroid.ElephantPost(elephantRequest.url, bodyJsonString, GameID, authToken, elephantRequest.tryCount);
#endif
            yield return(null);
#endif
        }
示例#5
0
        private void SaveQueues()
        {
            while (_queue.Count > 0)
            {
                ElephantRequest data = _queue.Dequeue();
                _failedQueue.Add(data);
            }

            var queueJson = JsonUtility.ToJson(new QueueData(_failedQueue));

            Log("QUEUE -> " + queueJson);

            Utils.SaveToFile(QUEUE_DATA_FILE, queueJson);

            _failedQueue.Clear();
        }
示例#6
0
        public static void Transaction(string type, int level, long amount, long finalAmount, string source)
        {
            if (ElephantCore.Instance == null)
            {
                Debug.LogWarning("Elephant SDK isn't working correctly, make sure you put Elephant prefab into your first scene..");
                return;
            }

            var t = TransactionData.CreateTransactionData();

            t.type         = type;
            t.level        = level;
            t.amount       = amount;
            t.final_amount = finalAmount;
            t.source       = source;

            var req = new ElephantRequest(ElephantCore.TRANSACTION_EP, t);

            ElephantCore.Instance.AddToQueue(req);
        }
示例#7
0
        private static void CustomEvent(string type, int level, Params param = null)
        {
            if (ElephantCore.Instance == null)
            {
                Debug.LogWarning("Elephant SDK isn't working correctly, make sure you put Elephant prefab into your first scene..");
                return;
            }

            var ev = EventData.CreateEventData();

            ev.type  = type;
            ev.level = level;

            if (param != null)
            {
                MapParams(param, ev);
            }

            var req = new ElephantRequest(ElephantCore.EVENT_EP, ev);

            ElephantCore.Instance.AddToQueue(req);
        }
示例#8
0
 public void AddToQueue(ElephantRequest data)
 {
     this._queue.Enqueue(data);
 }