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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Assets.Scripts.API
{
public class Server : MonoBehaviour
{
public static string HOST_PROD = "https://quantifly.plnech.fr";
public static string HOST_DEV = "http://localhost:8990";
public ScoresRes Scores;
public Server ()
{
}
public void Start()
{
GetScores();
}
public void GetScores()
{
StartCoroutine(GetScoresAsync());
}
IEnumerator GetScoresAsync() {
UnityWebRequest www = UnityWebRequest.Get(HOST_PROD + "/scores/");
yield return www.Send();
if (www.isError) {
Debug.LogError ("Error:" + www.error);
} else {
Debug.Log ("Got response: " + www.downloadHandler.text);
Scores = ScoresRes.CreateFromJson (www.downloadHandler.text);
Debug.Log ("Got scores: " + Scores);
}
}
public static void PostScore(int score, String playerName, String playerTeam = null)
{
WWWForm form = new WWWForm();
form.AddField("name", playerName);
form.AddField("team", playerTeam);
form.AddField("score", score);
new WWW(HOST_PROD + "/score/", form);
}
[Serializable]
public class ScoresRes {
public string status;
public Score[] scores;
public ScoresRes() {}
public static ScoresRes CreateFromJson(string scoresJSON)
{
return JsonUtility.FromJson<ScoresRes>(scoresJSON);
}
public override string ToString()
{
var s = (from e in scores
where e != null
select e.ToString()).ToArray();
return "Status: " + status + "Scores:\n" + string.Join(",", s);
}
}
}
}