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

174 lines
5.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using System;
using UnityEngine.Analytics;
using Unity.Services.Core;
using Unity.Services.Analytics;
using System.Threading.Tasks;
public class Uiika : MonoBehaviour
{
[Header("UIDocument")]
public UIDocument UI;
public VisualTreeAsset tabletemplate;
[Header("Parser")]
public ThanksStASSForTheNiceMethod Caller;
public Button RequestButton; // UI êîìïîíåíòû -->
public TextField datebegin;
public TextField dateend;
public TextField timebegin;
public TextField timeend;
public TextField SerialNumber;
public IMGUIContainer loadimg;
public DropdownField SerialNumberDrop;
public Label forcodes; // <-- UI êîìïîíåíòû
public DateTime DatBegin;
public DateTime DatEnd;
public TimeSpan TimeBegin;
public TimeSpan TimeEnd;
public string SerialNumberStr;
// Start is called before the first frame update
public async void Start()
{
var root = UI.rootVisualElement;
loadimg = root.Q<IMGUIContainer>("loadimg");
forcodes = root.Q<Label>("forcodes");
datebegin = root.Q<TextField>("datebegin");
dateend = root.Q<TextField>("dateend");
timebegin = root.Q<TextField>("timebegin");
timeend = root.Q<TextField>("timeend");
SerialNumber = root.Q<TextField>("tagsn");
SerialNumberDrop = root.Q<DropdownField>("SerialNumberDrop");
RequestButton = root.Q<Button>("RequestButton");
RequestButton.clicked += RequestButtonPressed; //Áèíä êíîïêè RequestButton
await InitializeUnityServices();
// Íà÷àëî ñáîðà äàííûõ
StartDataCollection();
// Îòïðàâêà ñîáûòèÿ 'gameStarted'
Analytics.CustomEvent("gameStarted", new Dictionary<string, object>
{
{ "level", 1 }, // Óðîâåíü, íà êîòîðîì íà÷èíàåòñÿ èãðà
{ "player_name", "Player1" } // Èìÿ èãðîêà (ìîæíî çàìåíèòü íà ðåàëüíîå èìÿ èãðîêà)
});
Debug.Log("Event 'gameStarted' sent to Unity Analytics.");
}
private async Task InitializeUnityServices()
{
try
{
await UnityServices.InitializeAsync();
Debug.Log("Unity Services initialized successfully.");
}
catch (System.Exception e)
{
Debug.LogError($"Failed to initialize Unity Services: {e.Message}");
}
}
private void StartDataCollection()
{
try
{
AnalyticsService.Instance.StartDataCollection();
Debug.Log("Data collection started.");
}
catch (System.Exception e)
{
Debug.LogError($"Failed to start data collection: {e.Message}");
}
}
public void Remember()
{
datebegin.value = string.Format("{0:dd.MM.yyyy}", DatBegin);
dateend.value = string.Format("{0:dd.MM.yyyy}", DatEnd);
timebegin.value = string.Format("{0:hh\\:mm\\:ss}", TimeBegin);
timeend.value = string.Format("{0:hh\\:mm\\:ss}", TimeEnd);
SerialNumber.value = SerialNumberStr;
}
static string MakeUrl(DateTime dDateBegin,
DateTime dDateEnd,
TimeSpan dTimeBegin,
TimeSpan dTimeEnd, string tagSerialNumber)
{
const string sheme = "http";
const string host = "10.21.0.167";//"192.168.0.110" ""localhost"" "192.168.10.3" "10.21.0.167";
const string port = "8001";//"8008";
const string method = "api/v2/service/get_tag_log_mo/?method=GetTagLogMo";
string returnValue =
$"{sheme}://{host}:{port}/{method}"
+ $"&date_start={string.Format("{0:yyyy-MM-dd}", dDateBegin)}%20{string.Format("{0:hh\\:mm\\:ss}", dTimeBegin)}"
+ $"&date_end={string.Format("{0:yyyy-MM-dd}", dDateEnd)}%20{string.Format("{0:hh\\:mm\\:ss}", dTimeEnd)}"
+ $"&tag={tagSerialNumber}";
Debug.Log(returnValue);
return returnValue;
}
void RequestButtonPressed()
{
DateTime dDateBegin;
DateTime dDateEnd;
TimeSpan dTimeBegin;
TimeSpan dTimeEnd;
RequestButton.SetEnabled(false); //Îòêëþ÷åíèå êíîïêè RequestButton, äëÿ èçáåæàíèÿ ïîâòîðíûõ äàííûõ
forcodes.visible = false;
Caller.ClearList();
Debug.Log(datebegin.text + " " + dateend.text + " " + timebegin.text + " " + timeend.text);
if (DateTime.TryParse(datebegin.text, out dDateBegin) && DateTime.TryParse(dateend.text, out dDateEnd) && TimeSpan.TryParse(timebegin.text, out dTimeBegin) && TimeSpan.TryParse(timeend.text, out dTimeEnd))
{
DatBegin = dDateBegin;
DatEnd = dDateEnd;
TimeBegin = dTimeBegin;
TimeEnd = dTimeEnd;
SerialNumberStr = SerialNumber.text;
Caller.URL = MakeUrl(DatBegin, DatEnd, TimeBegin, TimeEnd, SerialNumberStr);
Caller.Request();
Debug.Log("Inputs fields OK");
loadimg.visible = true;
}
else
{
RequestButton.SetEnabled(true);
Debug.Log("Input fields no OK");
forcodes.visible = true;
forcodes.text = "Â äàííûõ îáíàðóæåíà îøèáêà.";
loadimg.visible = false;
}
}
public void SetResponseCode(string Code)
{
forcodes.text = "Îòâåò íà çàïðîñ: " + Code;
}
public void MakeTable()
{
foreach(string x in Caller.MarksTime)
{
TemplateContainer itemTableContainer = tabletemplate.Instantiate();
UI.rootVisualElement.Q<ScrollView>("TableBox2").Add(itemTableContainer);
string[] strArray = x.Split("!");
int second = int.Parse(strArray[1]);
TimeSpan timeSpan = TimeSpan.FromSeconds(second);
itemTableContainer.Q<Label>("MarkTimeSec").text = string.Format("{0:D2}:{1:D2}:{2:D2}ñ", timeSpan.Hours,timeSpan.Minutes,timeSpan.Seconds);
itemTableContainer.Q<Label>("MarkOtmetka").text = strArray[0];
}
}
}