public void GetNewData() { // reset stats receivedTotal = 0; receivedNew = 0; receivedDuplicates = 0; // set status dataRequestStatus = DataRequestStatus.requesting; // if using local archive file if (selectedMode == ModeType.localArchive) { // jump straight to handle archived json HandleJsonResponse(localDataFile.text); } else { // update path path = GetEndpoint(); //Debug.Log (DebugManager.GetSymbol ("asterisk") + " DataManager.GetNewData() path = " + path); //DebugManager.Instance.UpdateDisplay ("DataManager.GetNewData() path = " + path); StartCoroutine(GetRequest(path)); // error test // StartCoroutine(GetRequest("https://error.html")); } }
/// <summary> /// Handle JSON data /// </summary> /// <param name="text">JSON data as string</param> public void HandleJsonResponse(string text) { Debug.Log("HandleJsonResponse()"); // set status dataRequestStatus = DataRequestStatus.receiving; // parse JSON array JArray a = JArray.Parse(text); // update count receivedTotal = a.Count; dataRequestStatus = DataRequestStatus.handling; DebugManager.Instance.UpdateDisplay("DataManager.GetNewData() dataRequestStatus = " + dataRequestStatus); // loop through array and add each foreach (JObject item in a) { // base class properties string _username = item.GetValue("username").ToString(); string _avatarPath = item.GetValue("avatarPath").ToString(); int _level = (int)item.GetValue("level"); int _clicks = (int)item.GetValue("clicks"); int _score = (int)item.GetValue("score"); int _time = (int)item.GetValue("time"); int _capturedTotal = (int)item.GetValue("capturedTotal"); int _missedTotal = (int)item.GetValue("missedTotal"); int _pageActionScrollDistance = (int)item.GetValue("pageActionScrollDistance"); int _trackersBlocked = (int)item.GetValue("trackersBlocked"); int _trackersSeen = (int)item.GetValue("trackersSeen"); string _eventType = item.GetValue("eventType").ToString(); string _createdAtStr = item.GetValue("createdAt").ToString(); string _monsters = item.GetValue("monsters").ToString(); string _trackers = item.GetValue("trackers").ToString(); // parse string to ISO 8601 format DateTime _createdAt = DateTime.Parse(_createdAtStr, null, System.Globalization.DateTimeStyles.RoundtripKind); // LIVE MODE ONLY - SKIP DUPLICATES if (selectedMode == ModeType.remoteLive) { // get any duplicate dates in buffer based on both conditions var bufferMatches = Timeline.Instance.buffer.FindAll(found => found.createdAt == _createdAt); var historyMatches = Timeline.Instance.history.FindAll(found => found.createdAt == _createdAt); // skip this iteration if (bufferMatches.Count > 0 || historyMatches.Count > 0) { Debug.Log("DUPLICATE createdAt = " + _createdAt + " AND eventType = " + _eventType); receivedDuplicates++; continue; } } // IF NOT A DUPLICATE THEN PROCEED receivedNew++; // parse eventData JObject d = JObject.Parse(item.GetValue("eventData").ToString()); // object to hold data FeedData output; if (_eventType == "attack") { output = new AttackData { _name = (string)d ["name"], _type = (string)d ["level"], _selected = (bool)d ["selected"] }; } else if (_eventType == "badge") { output = new BadgeData { _name = (string)d ["name"], _level = (int)d ["level"] }; } else if (_eventType == "consumable") { output = new ConsumableData { _name = (string)d ["name"], _slug = (string)d ["slug"], _stat = (string)d ["stat"], _type = (string)d ["type"], _value = (int)d ["value"] }; } else if (_eventType == "disguise") { output = new DisguiseData { _name = (string)d ["name"], _type = (string)d ["type"] }; } else if (_eventType == "monster") { output = new MonsterData { _mid = (int)d ["mid"], _level = (int)d ["level"], _captured = (int)d ["captured"], }; } else if (_eventType == "tracker") { output = new TrackerData { _tracker = (string)d ["tracker"], _captured = (int)d ["captured"], }; } else // if (_eventType == "stream") { output = new StreamData { _score = (int)d ["score"], _clicks = (int)d ["clicks"], _likes = (int)d ["likes"], }; } output.username = _username; output.avatarPath = _avatarPath; output.level = _level; output.clicks = _clicks; output.score = _score; output.time = _time; output.capturedTotal = _capturedTotal; output.missedTotal = _missedTotal; output.pageActionScrollDistance = _pageActionScrollDistance; output.trackersBlocked = _trackersBlocked; output.trackersSeen = _trackersSeen; output.clicks = _clicks; output.eventType = _eventType; output.createdAt = _createdAt; output.monsters = _monsters; output.trackers = _trackers; // add to feeds - now adding to buffer in Timeline //feeds.Add (output); // if live mode == true // then attempt to add to buffer Timeline.Instance.buffer.Add(output); //Debug.Log ("Added " + _username + " event " + _eventType + " at " + _createdAt); } // set status dataRequestStatus = DataRequestStatus.finished; // trigger data updated event EventManager.TriggerEvent("DataRequestFinished"); }