From 0ab7533ba477073840dc70b35a121b3d8409e503 Mon Sep 17 00:00:00 2001 From: Mateus Pires Date: Fri, 14 Oct 2016 20:47:47 -0300 Subject: [PATCH] Implementing dictionary json parse --- Assets/Scripts/Animation List/Trie.cs | 40 ++++++++++++++++++++++++++++++++++++++-- Assets/Scripts/PlayerManager.cs | 56 ++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 82 insertions(+), 14 deletions(-) diff --git a/Assets/Scripts/Animation List/Trie.cs b/Assets/Scripts/Animation List/Trie.cs index b9c4d06..7012ef1 100644 --- a/Assets/Scripts/Animation List/Trie.cs +++ b/Assets/Scripts/Animation List/Trie.cs @@ -1,7 +1,9 @@ -using System; +using LAViD.VLibras.Utils; +using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; namespace LAViD.VLibras.Generic { @@ -15,6 +17,40 @@ namespace LAViD.VLibras.Generic { private Node root; - // public Trie() + public Trie(string json) + { + JSONTrie trie = new JSONTrie(json); + } + } + + class JSONTrie + { + public string[] characters; + public Dictionary charactersKeys; + public JSONNode root; + + public JSONTrie(string json) + { + Regex regex = new Regex("{\"(\\w+)\": (.*), \"(\\w+)\": (.*), \"(\\w+)\": (.*)}"); + Match match = regex.Match(json); + + PlayerLogger.Log("Captures:"); + + foreach (Capture capture in match.Captures) + { + PlayerLogger.Log(capture.Index + ", " + capture.Value); + } + } + } + + class JSONNode + { + public bool end; + public JSONNode children; + + public JSONNode(string json) + { + + } } } diff --git a/Assets/Scripts/PlayerManager.cs b/Assets/Scripts/PlayerManager.cs index 0448f31..7878883 100644 --- a/Assets/Scripts/PlayerManager.cs +++ b/Assets/Scripts/PlayerManager.cs @@ -3,6 +3,7 @@ using System.Collections; using System; using UnityEngine.UI; using LAViD.VLibras.Utils; +using LAViD.VLibras.Generic; public class PlayerManager : GenericPlayerManager { @@ -18,6 +19,8 @@ public class PlayerManager : GenericPlayerManager { private const string SERVER_URL = "http://traducao.vlibras.gov.br/translate?text="; private const int VERSION = 1; + private Trie signs; + public enum ERROR_STATUS_MESSAGE { INTERNET_CONNECTION_FAILURE, @@ -69,8 +72,30 @@ public class PlayerManager : GenericPlayerManager { Screen.fullScreen = false; + WWW dictionaryRequest = new WWW("http://dicionario.vlibras.gov.br/signs"); + StartCoroutine(WaitForResponse(dictionaryRequest, + // Success + delegate(WWW www) + { + this.signs = new Trie(www.text); + }, + // Error + delegate (WWW www) + { + if (www.error == null) + PlayerLogger.Log("PM", "S", "Timeout."); + else + PlayerLogger.Log("PM", "S", "WWW error: " + www.error); + } + )); + } + + public IEnumerator LoadDictionary() + { - } + + yield return null; + } public void playDict(string word) { @@ -121,11 +146,6 @@ public class PlayerManager : GenericPlayerManager { protected override WWW loadAssetBundle(string aniName) { - if (this.regionHash == 1) - { - // Check if it exists in trie - } - string address = BASE_URL + this.regionPath + WWW.EscapeURL(aniName); PlayerLogger.Log("PM", "lAB", "Requesting bundle: " + address); @@ -160,8 +180,6 @@ public class PlayerManager : GenericPlayerManager { this.screenManager.switchScreen("translate"); } - - protected override WWW getCheckConnectionRequest() { return new WWW(BASE_URL); @@ -205,18 +223,22 @@ public class PlayerManager : GenericPlayerManager { return false;*/ } - protected override IEnumerator WaitForResponse(WWW www) + protected delegate void SuccessEvent(WWW request); + protected delegate void ErrorEvent(WWW request); + + protected IEnumerator WaitForResponse(WWW www, SuccessEvent success, ErrorEvent error) { PlayerLogger.Log("PM", "WFR", "Stating time check."); - + const float timeoutLimit = 20f; float timer = 0; - - while ( ! www.isDone) + + while (!www.isDone) { if (timer > timeoutLimit) { PlayerLogger.Log("PM", "WFR", "Timeout (" + timer + ")."); + if (error != null) error(www); yield break; } @@ -230,6 +252,16 @@ public class PlayerManager : GenericPlayerManager { } PlayerLogger.Log("PM", "WFR", "Done (" + timer + ")."); + + if (www.error == null) { + if (success != null) success(www); + } else { + if (error != null) error(www); + } + } + + protected override IEnumerator WaitForResponse(WWW www) { + yield return WaitForResponse(www, null, null); } private IEnumerator translate(string gloss) -- libgit2 0.21.2