1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using System.Linq;
- using UnityEngine.Networking;
- public class PointCounter : MonoBehaviour
- {
- public Text total;
-
- public List<Text> players;
-
- private string serverUrl;
- public GameObject serverManager;
- private float period = 0.0f;
-
- void Start()
- {
- serverUrl = "https://smarthubs.media.tuwien.ac.at/api/get_all_player_points/";
- }
-
-
- void Update()
- {
- if (period > 4.0f)
- {
-
- string currentGame = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
-
- Debug.Log(currentGame);
- string url = serverUrl + currentGame.ToString();
- if(currentGame != "0"){
- Debug.Log(url);
- StartCoroutine(getValues(url));
- }
- period = 0;
- }
- period += UnityEngine.Time.deltaTime;
- }
- public IEnumerator getValues(string newServerUrl)
- {
- UnityWebRequest www = UnityWebRequest.Get(newServerUrl);
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
- }
- else
- {
-
-
- string playerPoints = www.downloadHandler.text;
- var result = playerPoints.Split(new[] { ',' })
- .Select(s => s.Split(new[] { ':' }))
- .ToDictionary(k => k[0].Replace("\"", "").Replace("}", "").Replace("{", "").Trim(), v => v[1].Replace("\"", "").Replace("}", "").Trim());
- int i = 0;
- int res = 0;
- foreach (KeyValuePair<string, string> entry in result)
- {
- players[i].text = entry.Value;
- res += int.Parse(entry.Value);
- i++;
- Debug.Log("the result is: " + result);
- }
- total.text = res.ToString();
- }
- }
- }
|