diff --git a/Assets/Scripts/API.meta b/Assets/Scripts/API.meta new file mode 100644 index 0000000..e263db5 --- /dev/null +++ b/Assets/Scripts/API.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 33abefc05c7dc4118a7be63901f0c124 +folderAsset: yes +timeCreated: 1477229352 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/API/Score.cs b/Assets/Scripts/API/Score.cs new file mode 100644 index 0000000..31f572f --- /dev/null +++ b/Assets/Scripts/API/Score.cs @@ -0,0 +1,15 @@ +using System; + +namespace Assets.Scripts.API +{ + [Serializable] + public class Score + { + public string playerName; + public string team; + public int score; + public DateTime date; + + } +} + diff --git a/Assets/Scripts/API/Score.cs.meta b/Assets/Scripts/API/Score.cs.meta new file mode 100644 index 0000000..a79a0fc --- /dev/null +++ b/Assets/Scripts/API/Score.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: f7e4da0739d3143038cfaf75111723aa +timeCreated: 1477229352 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/API/Server.cs b/Assets/Scripts/API/Server.cs new file mode 100644 index 0000000..f7e52f5 --- /dev/null +++ b/Assets/Scripts/API/Server.cs @@ -0,0 +1,55 @@ +using System; +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using UnityEngine.Networking; + +namespace Assets.Scripts.API +{ + public class Server + { + public static string HOST = "http://localhost:8990"; + + public Server () + { + } + + public static List<Score> GetScores () + { + UnityWebRequest www = UnityWebRequest.Get(HOST + "/scores/"); + www.Send(); + + if (www.isError) { + Debug.LogError("Error:" + www.error); + return null; + } + else { + Debug.Log("Got response: " + www.downloadHandler.text); + ScoresRes res = JsonUtility.FromJson<ScoresRes>(www.downloadHandler.text); + Debug.Log("Got scores: " + res.scores); + + string mockText = "{\"status\":\"ok\",\"scores\":[" + + "{\"playerName\":\"John Smith\",\"team\":null,\"score\":10,\"date\":\"2016-10-23T12:42:54.810Z\",\"_id\":\"d21b623e9b394993be3224de89099194\"}," + + "{\"playerName\":\"Randy\",\"team\":\"iGEM Headquarters\",\"score\":42,\"date\":\"2016-10-23T12:45:45.774Z\",\"_id\":\"a3a66de22b30450492f8f6b1bf178123\"}" + + "]}"; + ScoresRes mock = JsonUtility.FromJson<ScoresRes>(mockText); //TODO: Why does JsonUtility fail to deserialize? /o\ + Debug.Log("Mock scores: " + mock); + return res.scores; + } + + } + + [Serializable] + private class ScoresRes { + public string status { get; set;} + public List<Score> scores { get; set;} + public ScoresRes() {} + + public ScoresRes(string status, List<Score> scores) { + this.scores = scores; + this.status = status; + } + } + } +} + diff --git a/Assets/Scripts/API/Server.cs.meta b/Assets/Scripts/API/Server.cs.meta new file mode 100644 index 0000000..338c0a3 --- /dev/null +++ b/Assets/Scripts/API/Server.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 647b863cee6104673a1238de59f93bcc +timeCreated: 1477229352 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Model.meta b/Assets/Scripts/Model.meta new file mode 100644 index 0000000..cb91f3e --- /dev/null +++ b/Assets/Scripts/Model.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: c56230d4575634b8896a178b68e9a055 +folderAsset: yes +timeCreated: 1477229352 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/SimpleEnnemy.cs b/Assets/Scripts/SimpleEnnemy.cs index 84e75af..ccb8c14 100644 --- a/Assets/Scripts/SimpleEnnemy.cs +++ b/Assets/Scripts/SimpleEnnemy.cs @@ -1,6 +1,7 @@ -using UnityEngine; -using System.Collections; +using Assets.Scripts.API; +using UnityEngine; using UnityEngine.UI; +using System.Collections; public class SimpleEnnemy : MonoBehaviour { @@ -27,5 +28,12 @@ public class SimpleEnnemy : MonoBehaviour { Debug.Log("Entered"); gameObject.SetActive(false); + //GetScores(); TODO: Successfully get scores from API } + + void GetScores() { + Debug.Log("Getting scores..."); + Server.GetScores(); + Debug.Log("Got scores."); + } }