Scripts/ThanksStASSForTheNiceMethod.cs
Станислав Хозяенок df6e2fe069 first commit
2025-04-07 10:57:03 +07:00

179 lines
6.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.Networking;
using Newtonsoft.Json;
using UnityEngine.UIElements;
//using UnityEngine.Analytics;
public class ThanksStASSForTheNiceMethod : MonoBehaviour
{
[Header("Ññûëêà íà ìåòîä è àâòîðèçàöèÿ")]
public string URL;
public string Auth;
[Header("Ëèñòû ñ äàííûìè")]
public List<Vector3> LocationsMO; //Òî÷êè ÌÎ
public List<Vector3> LocationsRTLS; //Òî÷êè ÐÒËÑ
public List<String> BlockNamesAliases; //Íàçâàíèå îòìåòîê
public List<String> Time; //Âðåìÿ
public List<String> MarksTime;
[Header("UIDocument")]
public UIDocument UI;
[Header("Ýêðàí ñ ïåðåìåùåíèåì")]
public VisualTreeAsset GameUIPrefab; //Ýêðàí ñ ïåðåùåíèåì
[Header("ÈÈ")]
public PersonScript Personchikn; // ÈÈ
[Header("Ñêðèïò âûïîëíÿþùèé çàïðîñ")]
public Uiika UIka;
public void Request()
{
GenerateRequest();
}
public void ClearList() //Î÷èñòêà äàííûõ
{
LocationsMO.Clear();
LocationsRTLS.Clear();
BlockNamesAliases.Clear();
Time.Clear();
MarksTime.Clear();
}
void GenerateRequest() //Ãåíåðàöèÿ çàïðîñà
{
//StartCoroutine(SpawnTestItems());
StartCoroutine(ProcessRequest(URL));
}
private IEnumerator SpawnTestItems()
{
string uri = "http://localhost:8001/api/v2/marks/getMarksLabelsByArrPoints";
using (UnityWebRequest request = UnityWebRequest.Get(uri))
{
//request.SetRequestHeader("Authorization", Auth);
//request.SetRequestHeader("Access-Control-Allow-Origin", "*");
request.timeout = 60;
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
var responseObject = JsonConvert.DeserializeObject<Assets.Scripts.API.MTEGetMarksLabelsByArrPoint.ApiResponse>(request.downloadHandler.text);
try
{
foreach (var item in responseObject.respose.items)
{
Vector3 pos = new Vector3(item.center_point.x, item.center_point.z, item.center_point.y);
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = pos;
// Ñîçäàíèå ìàòåðèàëà
Material redMaterial = new Material(Shader.Find("Standard"));
redMaterial.color = Color.red;
// Ïðèìåíåíèå ìàòåðèàëà ê ñôåðå
sphere.GetComponent<Renderer>().material = redMaterial;
}
} catch (Exception e)
{
Debug.LogException(e);
}
}
else
{
Debug.Log("Received: " + request.downloadHandler.text);
}
}
}
private IEnumerator ProcessRequest(string uri)
{
//Analytics.CustomEvent("check_uri", new Dictionary<string, object>
//{
// { "level", 1 },
// { "player_name", "Player1" }
//});
using (UnityWebRequest request = UnityWebRequest.Get(uri))
{
//request.SetRequestHeader("Authorization", Auth);
//request.SetRequestHeader("Access-Control-Allow-Origin", "*");
request.timeout = 60;
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
Debug.Log("Error: " + request.error);
UIka.RequestButton.SetEnabled(true);
UIka.forcodes.visible = true;
UIka.forcodes.text = "Â äàííûé ìîìåíò ñåðâèñ âðåìåííî íåäîñòóïåí.";
UIka.loadimg.visible = false;
}
else
{
Debug.Log("Received: " + request.downloadHandler.text);
var MethodJSON = JsonConvert.DeserializeObject<MethodJSON>(request.downloadHandler.text);
try
{
foreach (Item x in MethodJSON.respose.items) //Ïàðñèíã äàííûõ
{
LocationsMO.Add(new Vector3(x.x_mo, x.z_mo, x.y_mo));
LocationsRTLS.Add(new Vector3(x.x, x.z, x.y));
Time.Add(x.time.ToLocalTime().ToString());
if (x.block_names_aliases.Count > 1)
{
BlockNamesAliases.Add(x.block_names_aliases[0] + " " + x.block_names_aliases[1]);
}
else
{
BlockNamesAliases.Add(x.block_names_aliases[0]);
}
UIka.SetResponseCode(request.responseCode.ToString());
}
}
catch (NullReferenceException)
{
Debug.Log("ItemsNull");
UIka.loadimg.visible = false;
}
try
{
foreach (MarksTime y in MethodJSON.respose.marks_time)
{
MarksTime.Add(y.name + "!" + y.count_seconds);
}
}
catch (NullReferenceException)
{
Debug.Log("MarksTimeNull");
UIka.loadimg.visible = false;
}
if (LocationsMO.Count > 1)
{
Debug.Log("No problemo");
UIka.SetResponseCode(request.responseCode.ToString()); //Óñòàíîâêà êîäà
UI.visualTreeAsset = GameUIPrefab; //Óñòàíîâêà ýêðàíà äëÿ ïåðåìåùåíèÿ
Personchikn.InitMePlease(); //Âûçîâ èíèöèàëèçàöèÿ â ÈÈ
Debug.Log("Got data, here go");
}
else
{
UIka.RequestButton.SetEnabled(true);
UIka.forcodes.visible = true;
UIka.forcodes.text = "Â äàííûõ îáíàðóæåíà îøèáêà.";
UIka.forcodes.text = MethodJSON.error;
UIka.loadimg.visible = false;
}
}
}
}
}