|
| 1 | +using UnityEngine; |
| 2 | +using UnityEngine.Networking; |
| 3 | +using System.Collections; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System; |
| 7 | +using System.Linq; |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * Early version of DataManager - contains different ways to access JSON data |
| 12 | + */ |
| 13 | + |
| 14 | + |
| 15 | +public class DataManagerOld : Singleton<DataManagerOld> { |
| 16 | + // singleton |
| 17 | + protected DataManagerOld () { } |
| 18 | + public static new DataManagerOld Instance; |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + //// listeners |
| 23 | + //void OnEnable () |
| 24 | + //{ |
| 25 | + // EventManager.StartListening ("GetNewData", GetNewData); |
| 26 | + //} |
| 27 | + //void OnDisable () |
| 28 | + //{ |
| 29 | + // EventManager.StopListening ("GetNewData", GetNewData); |
| 30 | + //} |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + //// HOST |
| 37 | + |
| 38 | + //[Serializable] |
| 39 | + //public enum HostType { |
| 40 | + // local, |
| 41 | + // remote |
| 42 | + //} |
| 43 | + //public HostType chosenHost; |
| 44 | + //string [] hosts = { |
| 45 | + // "https://127.0.0.1:5000/api/", |
| 46 | + // "https://tallysavestheinternet.com/api/" |
| 47 | + //}; |
| 48 | + |
| 49 | + //// ENDPOINT |
| 50 | + |
| 51 | + //[Serializable] |
| 52 | + //public enum EndpointType { |
| 53 | + // recent20, |
| 54 | + // rangeOneWeek, |
| 55 | + // rangePlusStreamOneDay, |
| 56 | + // rangePlusStream12Hour, |
| 57 | + // rangePlusStream6Hour, |
| 58 | + // rangePlusStream3Hour, |
| 59 | + // rangePlusStream1Hour, |
| 60 | + // rangePlusStreamFiveMinute |
| 61 | + //} |
| 62 | + //public EndpointType chosenEndpoint; |
| 63 | + //string [] endpoints = { |
| 64 | + // "feed/recent", // 20 recent |
| 65 | + // "feed/range/1/week", // 1 week |
| 66 | + // "feed/range/plusStream/1/day", // 1 day |
| 67 | + // "feed/range/plusStream/12/hour", // 6 hour |
| 68 | + // "feed/range/plusStream/6/hour", // 6 hour |
| 69 | + // "feed/range/plusStream/3/hour", // 1 hour |
| 70 | + // "feed/range/plusStream/1/hour", // 1 hour |
| 71 | + // "feed/range/plusStream/5/minute" // 5 minutes |
| 72 | + //}; |
| 73 | + |
| 74 | + //// FINAL PATH |
| 75 | + |
| 76 | + //// chosen host and endpoint for API |
| 77 | + //public string path; |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + //// META |
| 82 | + |
| 83 | + //// the number of events |
| 84 | + //public static int dataCount; |
| 85 | + //// the current data as string |
| 86 | + //public static string currentEventStr; |
| 87 | + //// as list |
| 88 | + //public static IList<FeedData> feeds = new List<FeedData> (); |
| 89 | + |
| 90 | + |
| 91 | + //public static Dictionary<string, FeedData> eventDictionary = new Dictionary<string, FeedData> (); |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + //private void Start () |
| 96 | + //{ |
| 97 | + // // start everything |
| 98 | + // //GetNewData (); |
| 99 | + //} |
| 100 | + |
| 101 | + |
| 102 | + //public void GetNewData () |
| 103 | + //{ |
| 104 | + // // update path |
| 105 | + // path = hosts [(int)chosenHost] + endpoints [(int)chosenEndpoint]; |
| 106 | + |
| 107 | + // //Debug.Log (DebugManager.GetSymbol ("asterisk") + " DataManager.GetNewData() path = " + path); |
| 108 | + // //DebugManager.Instance.UpdateDisplay ("DataManager.GetNewData() path = " + path); |
| 109 | + |
| 110 | + // StartCoroutine (GetRequest (path)); |
| 111 | + |
| 112 | + // // error test |
| 113 | + // // StartCoroutine(GetRequest("https://error.html")); |
| 114 | + //} |
| 115 | + |
| 116 | + //// do a get request for JSON data at url |
| 117 | + //public static IEnumerator GetRequest (string uri) |
| 118 | + //{ |
| 119 | + // using (UnityWebRequest webRequest = UnityWebRequest.Get (uri)) { |
| 120 | + |
| 121 | + // // Request and wait for the desired page. |
| 122 | + // yield return webRequest.SendWebRequest (); |
| 123 | + |
| 124 | + // //string [] pages = uri.Split ('/'); |
| 125 | + // //int page = pages.Length - 1; |
| 126 | + |
| 127 | + // if (webRequest.isNetworkError || webRequest.isHttpError) { |
| 128 | + // Debug.Log (DebugManager.GetSymbol ("asterisk") + " Error: " + webRequest.error); |
| 129 | + // } else { |
| 130 | + // //Debug.Log (DebugManager.GetSymbol ("asterisk") + " DataManager.GetNewData() " + uri + "\n" + webRequest.downloadHandler.text); |
| 131 | + // DebugManager.Instance.UpdateDisplay ("DataManager.GetNewData() uri = " + uri); |
| 132 | + |
| 133 | + // // parse JSON array |
| 134 | + // JArray a = JArray.Parse (webRequest.downloadHandler.text); |
| 135 | + |
| 136 | + // // loop through array and add each |
| 137 | + // foreach (JObject item in a) { |
| 138 | + // // base class properties |
| 139 | + // string _username = item.GetValue ("username").ToString (); |
| 140 | + // string _avatarPath = item.GetValue ("avatarPath").ToString (); |
| 141 | + // string _eventType = item.GetValue ("eventType").ToString (); |
| 142 | + // string _createdAtStr = item.GetValue ("createdAt").ToString (); |
| 143 | + // string _monsters = item.GetValue ("monsters").ToString (); |
| 144 | + // string _trackers = item.GetValue ("trackers").ToString (); |
| 145 | + |
| 146 | + // // parse string to ISO 8601 format |
| 147 | + // DateTime _createdAt = DateTime.Parse (_createdAtStr, null, System.Globalization.DateTimeStyles.RoundtripKind); |
| 148 | + |
| 149 | + // // parse eventData |
| 150 | + // JObject d = JObject.Parse (item.GetValue ("eventData").ToString ()); |
| 151 | + |
| 152 | + |
| 153 | + // FeedData output; |
| 154 | + |
| 155 | + // if (_eventType == "attack") { |
| 156 | + // output = new AttackData { |
| 157 | + // username = _username, |
| 158 | + // avatarPath = _avatarPath, |
| 159 | + // eventType = _eventType, |
| 160 | + // createdAt = _createdAt, |
| 161 | + // monsters = _monsters, |
| 162 | + // trackers = _trackers, |
| 163 | + |
| 164 | + // name = (string)d ["name"], |
| 165 | + // type = (string)d ["level"], |
| 166 | + // selected = (bool)d ["selected"] |
| 167 | + // }; |
| 168 | + // } else if (_eventType == "badge") { |
| 169 | + // output = new BadgeData { |
| 170 | + // username = _username, |
| 171 | + // avatarPath = _avatarPath, |
| 172 | + // eventType = _eventType, |
| 173 | + // createdAt = _createdAt, |
| 174 | + // monsters = _monsters, |
| 175 | + // trackers = _trackers, |
| 176 | + |
| 177 | + // name = (string)d ["name"], |
| 178 | + // level = (int)d ["level"] |
| 179 | + // }; |
| 180 | + // } else if (_eventType == "consumable") { |
| 181 | + // output = new ConsumableData { |
| 182 | + // username = _username, |
| 183 | + // avatarPath = _avatarPath, |
| 184 | + // eventType = _eventType, |
| 185 | + // createdAt = _createdAt, |
| 186 | + // monsters = _monsters, |
| 187 | + // trackers = _trackers, |
| 188 | + |
| 189 | + // name = (string)d ["name"], |
| 190 | + // slug = (string)d ["slug"], |
| 191 | + // stat = (string)d ["stat"], |
| 192 | + // type = (string)d ["type"], |
| 193 | + // value = (int)d ["value"] |
| 194 | + // }; |
| 195 | + // } else if (_eventType == "disguise") { |
| 196 | + // output = new DisguiseData { |
| 197 | + // username = _username, |
| 198 | + // avatarPath = _avatarPath, |
| 199 | + // eventType = _eventType, |
| 200 | + // createdAt = _createdAt, |
| 201 | + // monsters = _monsters, |
| 202 | + // trackers = _trackers, |
| 203 | + |
| 204 | + // name = (string)d ["name"], |
| 205 | + // type = (string)d ["type"] |
| 206 | + // }; |
| 207 | + // } else if (_eventType == "monster") { |
| 208 | + // output = new MonsterData { |
| 209 | + // username = _username, |
| 210 | + // avatarPath = _avatarPath, |
| 211 | + // eventType = _eventType, |
| 212 | + // createdAt = _createdAt, |
| 213 | + // monsters = _monsters, |
| 214 | + // trackers = _trackers, |
| 215 | + |
| 216 | + // mid = (int)d ["mid"], |
| 217 | + // level = (int)d ["level"], |
| 218 | + // captured = (int)d ["captured"], |
| 219 | + // }; |
| 220 | + // } else if (_eventType == "tracker") { |
| 221 | + // output = new TrackerData { |
| 222 | + // username = _username, |
| 223 | + // avatarPath = _avatarPath, |
| 224 | + // eventType = _eventType, |
| 225 | + // createdAt = _createdAt, |
| 226 | + // monsters = _monsters, |
| 227 | + // trackers = _trackers, |
| 228 | + |
| 229 | + // tracker = (string)d ["tracker"], |
| 230 | + // captured = (int)d ["captured"], |
| 231 | + // }; |
| 232 | + // } else { // if (_eventType == "stream") |
| 233 | + // output = new StreamData { |
| 234 | + // username = _username, |
| 235 | + // avatarPath = _avatarPath, |
| 236 | + // eventType = _eventType, |
| 237 | + // createdAt = _createdAt, |
| 238 | + // monsters = _monsters, |
| 239 | + // trackers = _trackers, |
| 240 | + |
| 241 | + // score = (int)d ["score"], |
| 242 | + // clicks = (int)d ["clicks"], |
| 243 | + // likes = (int)d ["likes"], |
| 244 | + // }; |
| 245 | + // } |
| 246 | + |
| 247 | + // feeds.Add (output); |
| 248 | + // // Timeline.Instance.buffer.Add (_createdAt, output); |
| 249 | + // Timeline.Instance.buffer.Add (output); |
| 250 | + |
| 251 | + |
| 252 | + // // SAVE FOR CREATING BUFFER |
| 253 | + // //// create key |
| 254 | + // //string key = _createdAtStr + "_" + _username + "_" + _eventType; |
| 255 | + // //FeedData val; |
| 256 | + // //if (!eventDictionary.TryGetValue (key, out val)) { |
| 257 | + // // eventDictionary.Add (key, output); |
| 258 | + // //} |
| 259 | + |
| 260 | + |
| 261 | + |
| 262 | + // //Debug.Log(_eventType); |
| 263 | + // } |
| 264 | + |
| 265 | + // // SAVE FOR CREATING BUFFER |
| 266 | + // ///// Acquire keys and sort them. |
| 267 | + // //var list = eventDictionary.Keys.ToList (); |
| 268 | + // //list.Sort (); |
| 269 | + |
| 270 | + // //// Loop through keys. |
| 271 | + // //foreach (var key in list) { |
| 272 | + // // Debug.Log (key + ": " + eventDictionary [key]); |
| 273 | + // //} |
| 274 | + |
| 275 | + |
| 276 | + |
| 277 | + // // OLD METHOD - KEEPING FOR REFERENCEx |
| 278 | + // //feeds = a.Select(p => new FeedData |
| 279 | + // //{ |
| 280 | + // // username = (string)p["username"], |
| 281 | + // // avatarPath = (string)p["avatarPath"], |
| 282 | + // // eventType = (string)p["eventType"], |
| 283 | + // // createdAt = (DateTime)p["createdAt"], |
| 284 | + |
| 285 | + // // //type = (string)p["type"], |
| 286 | + // // //name = (string)p["name"], |
| 287 | + // // //level = (int)p["level"], |
| 288 | + // // //stat = (string)p["stat"], |
| 289 | + // // //captured = (int)p["captured"], |
| 290 | + // //}).ToList(); |
| 291 | + // ////Debug.Log(a[0]); |
| 292 | + // //Debug.Log(feeds.ToString()); |
| 293 | + |
| 294 | + |
| 295 | + |
| 296 | + |
| 297 | + // // FOR DEBUGGING - MAY OR MAY NOT HAVE A GARBAGE COLLECTION ISSUE |
| 298 | + |
| 299 | + // //// reset currentEventStr |
| 300 | + // //currentEventStr = ""; |
| 301 | + |
| 302 | + // //foreach (var feed in feeds) { |
| 303 | + |
| 304 | + // // var line = |
| 305 | + |
| 306 | + // // feed.createdAt + "\t" + |
| 307 | + // // feed.username + ", " + |
| 308 | + // // feed.eventType + ", " + |
| 309 | + |
| 310 | + // // //feed.type + ", " + feed.name + ", " + feed.level + |
| 311 | + // // //", " + feed.type |
| 312 | + |
| 313 | + // // "" |
| 314 | + // // ; |
| 315 | + |
| 316 | + // // currentEventStr += line + "<br>"; |
| 317 | + |
| 318 | + // // //Debug.Log(line); |
| 319 | + // //} |
| 320 | + |
| 321 | + |
| 322 | + |
| 323 | + |
| 324 | + // // update count |
| 325 | + // dataCount = feeds.Count; |
| 326 | + |
| 327 | + // // trigger data updated event |
| 328 | + // EventManager.TriggerEvent ("DataDownloaded"); |
| 329 | + |
| 330 | + |
| 331 | + |
| 332 | + // } |
| 333 | + // } |
| 334 | + //} |
| 335 | +} |
0 commit comments