1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
}
}
}
}