Commit 0ab7533ba477073840dc70b35a121b3d8409e503

Authored by Mateus Lustosa
1 parent 847efc48

Implementing dictionary json parse

Assets/Scripts/Animation List/Trie.cs
1 -using System; 1 +using LAViD.VLibras.Utils;
  2 +using System;
2 using System.Collections.Generic; 3 using System.Collections.Generic;
3 using System.Linq; 4 using System.Linq;
4 using System.Text; 5 using System.Text;
  6 +using System.Text.RegularExpressions;
5 7
6 namespace LAViD.VLibras.Generic 8 namespace LAViD.VLibras.Generic
7 { 9 {
@@ -15,6 +17,40 @@ namespace LAViD.VLibras.Generic @@ -15,6 +17,40 @@ namespace LAViD.VLibras.Generic
15 { 17 {
16 private Node root; 18 private Node root;
17 19
18 - // public Trie() 20 + public Trie(string json)
  21 + {
  22 + JSONTrie trie = new JSONTrie(json);
  23 + }
  24 + }
  25 +
  26 + class JSONTrie
  27 + {
  28 + public string[] characters;
  29 + public Dictionary<string, int> charactersKeys;
  30 + public JSONNode root;
  31 +
  32 + public JSONTrie(string json)
  33 + {
  34 + Regex regex = new Regex("{\"(\\w+)\": (.*), \"(\\w+)\": (.*), \"(\\w+)\": (.*)}");
  35 + Match match = regex.Match(json);
  36 +
  37 + PlayerLogger.Log("Captures:");
  38 +
  39 + foreach (Capture capture in match.Captures)
  40 + {
  41 + PlayerLogger.Log(capture.Index + ", " + capture.Value);
  42 + }
  43 + }
  44 + }
  45 +
  46 + class JSONNode
  47 + {
  48 + public bool end;
  49 + public JSONNode children;
  50 +
  51 + public JSONNode(string json)
  52 + {
  53 +
  54 + }
19 } 55 }
20 } 56 }
Assets/Scripts/PlayerManager.cs
@@ -3,6 +3,7 @@ using System.Collections; @@ -3,6 +3,7 @@ using System.Collections;
3 using System; 3 using System;
4 using UnityEngine.UI; 4 using UnityEngine.UI;
5 using LAViD.VLibras.Utils; 5 using LAViD.VLibras.Utils;
  6 +using LAViD.VLibras.Generic;
6 7
7 public class PlayerManager : GenericPlayerManager { 8 public class PlayerManager : GenericPlayerManager {
8 9
@@ -18,6 +19,8 @@ public class PlayerManager : GenericPlayerManager { @@ -18,6 +19,8 @@ public class PlayerManager : GenericPlayerManager {
18 private const string SERVER_URL = "http://traducao.vlibras.gov.br/translate?text="; 19 private const string SERVER_URL = "http://traducao.vlibras.gov.br/translate?text=";
19 private const int VERSION = 1; 20 private const int VERSION = 1;
20 21
  22 + private Trie signs;
  23 +
21 public enum ERROR_STATUS_MESSAGE 24 public enum ERROR_STATUS_MESSAGE
22 { 25 {
23 INTERNET_CONNECTION_FAILURE, 26 INTERNET_CONNECTION_FAILURE,
@@ -69,8 +72,30 @@ public class PlayerManager : GenericPlayerManager { @@ -69,8 +72,30 @@ public class PlayerManager : GenericPlayerManager {
69 72
70 Screen.fullScreen = false; 73 Screen.fullScreen = false;
71 74
  75 + WWW dictionaryRequest = new WWW("http://dicionario.vlibras.gov.br/signs");
  76 + StartCoroutine(WaitForResponse(dictionaryRequest,
  77 + // Success
  78 + delegate(WWW www)
  79 + {
  80 + this.signs = new Trie(www.text);
  81 + },
  82 + // Error
  83 + delegate (WWW www)
  84 + {
  85 + if (www.error == null)
  86 + PlayerLogger.Log("PM", "S<d>", "Timeout.");
  87 + else
  88 + PlayerLogger.Log("PM", "S<d>", "WWW error: " + www.error);
  89 + }
  90 + ));
  91 + }
  92 +
  93 + public IEnumerator LoadDictionary()
  94 + {
72 95
73 - } 96 +
  97 + yield return null;
  98 + }
74 99
75 public void playDict(string word) 100 public void playDict(string word)
76 { 101 {
@@ -121,11 +146,6 @@ public class PlayerManager : GenericPlayerManager { @@ -121,11 +146,6 @@ public class PlayerManager : GenericPlayerManager {
121 146
122 protected override WWW loadAssetBundle(string aniName) 147 protected override WWW loadAssetBundle(string aniName)
123 { 148 {
124 - if (this.regionHash == 1)  
125 - {  
126 - // Check if it exists in trie  
127 - }  
128 -  
129 string address = BASE_URL + this.regionPath + WWW.EscapeURL(aniName); 149 string address = BASE_URL + this.regionPath + WWW.EscapeURL(aniName);
130 150
131 PlayerLogger.Log("PM", "lAB", "Requesting bundle: " + address); 151 PlayerLogger.Log("PM", "lAB", "Requesting bundle: " + address);
@@ -160,8 +180,6 @@ public class PlayerManager : GenericPlayerManager { @@ -160,8 +180,6 @@ public class PlayerManager : GenericPlayerManager {
160 this.screenManager.switchScreen("translate"); 180 this.screenManager.switchScreen("translate");
161 } 181 }
162 182
163 -  
164 -  
165 protected override WWW getCheckConnectionRequest() 183 protected override WWW getCheckConnectionRequest()
166 { 184 {
167 return new WWW(BASE_URL); 185 return new WWW(BASE_URL);
@@ -205,18 +223,22 @@ public class PlayerManager : GenericPlayerManager { @@ -205,18 +223,22 @@ public class PlayerManager : GenericPlayerManager {
205 return false;*/ 223 return false;*/
206 } 224 }
207 225
208 - protected override IEnumerator WaitForResponse(WWW www) 226 + protected delegate void SuccessEvent(WWW request);
  227 + protected delegate void ErrorEvent(WWW request);
  228 +
  229 + protected IEnumerator WaitForResponse(WWW www, SuccessEvent success, ErrorEvent error)
209 { 230 {
210 PlayerLogger.Log("PM", "WFR", "Stating time check."); 231 PlayerLogger.Log("PM", "WFR", "Stating time check.");
211 - 232 +
212 const float timeoutLimit = 20f; 233 const float timeoutLimit = 20f;
213 float timer = 0; 234 float timer = 0;
214 -  
215 - while ( ! www.isDone) 235 +
  236 + while (!www.isDone)
216 { 237 {
217 if (timer > timeoutLimit) 238 if (timer > timeoutLimit)
218 { 239 {
219 PlayerLogger.Log("PM", "WFR", "Timeout (" + timer + ")."); 240 PlayerLogger.Log("PM", "WFR", "Timeout (" + timer + ").");
  241 + if (error != null) error(www);
220 yield break; 242 yield break;
221 } 243 }
222 244
@@ -230,6 +252,16 @@ public class PlayerManager : GenericPlayerManager { @@ -230,6 +252,16 @@ public class PlayerManager : GenericPlayerManager {
230 } 252 }
231 253
232 PlayerLogger.Log("PM", "WFR", "Done (" + timer + ")."); 254 PlayerLogger.Log("PM", "WFR", "Done (" + timer + ").");
  255 +
  256 + if (www.error == null) {
  257 + if (success != null) success(www);
  258 + } else {
  259 + if (error != null) error(www);
  260 + }
  261 + }
  262 +
  263 + protected override IEnumerator WaitForResponse(WWW www) {
  264 + yield return WaitForResponse(www, null, null);
233 } 265 }
234 266
235 private IEnumerator translate(string gloss) 267 private IEnumerator translate(string gloss)