Projet-IA-Madelaine/Scripts/Statistics/StatsMenuController.cs
2024-06-12 21:03:42 +02:00

181 lines
5.7 KiB
C#
Raw Blame History

using Assets.Scripts;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using XCharts.Runtime;
public class StatsMenuController : SingletonMB<StatsMenuController>
{
#region PROPERTIES
public List<SessionData> Sessions { get; private set; }
public Canvas Canvas { get { return _canvas; } private set { _canvas = value; } }
[SerializeField] private Canvas _canvas;
#endregion
#region VARIABLES
[SerializeField] private GameObject graphPanelTemplate;
private UIDocument _document;
private Button _exitButton;
private Button _storageButton;
private List<LineChart> lineCharts;
#endregion
#region EVENTS
private void OnExitButton(ClickEvent e)
{
SceneManager.LoadScene("MainMenu");
}
private void OnStorageButton(ClickEvent e)
{
string path = Path.Combine(Application.persistentDataPath, "SessionsData");
#if UNITY_EDITOR
UnityEditor.EditorUtility.RevealInFinder(path);
#elif UNITY_STANDALONE_WIN
path = path.Replace("/", "\\"); // Remplacer les / par \ pour Windows
Process.Start("explorer.exe", path);
#elif UNITY_STANDALONE_OSX
Process.Start("open", path);
#elif UNITY_STANDALONE_LINUX
Process.Start("xdg-open", path);
#else
Debug.LogWarning("Opening the file explorer is not supported on this platform.");
#endif
}
#endregion
#region ENDPOINTS
public void OnSessionSelected(SessionData session)
{
if (lineCharts != null)
{
for (int i = 0; i < lineCharts.Count; i++)
{
Destroy(lineCharts[i]);
}
lineCharts.Clear();
}
UnityEngine.Debug.Log("Session Chart " + session.SessionNumber);
int n = 0;
foreach (var attempt in session.Attempts)
{
n++;
if (n > 4) break;
var instance = Instantiate(graphPanelTemplate, Canvas.transform).GetComponent<GraphPanelScript>().LineChart;
instance.RemoveAllSerie();
lineCharts.Add(instance);
var position = new Vector3(480, -184, -1);
var nextColumnPosition = new Vector3(instance.chartWidth + 2, 0, 0);
var nextLinePosition = new Vector3(0, -instance.chartHeight - 2, 0);
switch (lineCharts.IndexOf(instance))
{
case 0:
instance.GetComponent<RectTransform>().anchoredPosition = position;
break;
case 1:
instance.GetComponent<RectTransform>().anchoredPosition = position + nextColumnPosition;
break;
case 2:
instance.GetComponent<RectTransform>().anchoredPosition = position + nextLinePosition;
break;
case 3:
instance.GetComponent<RectTransform>().anchoredPosition = position + nextLinePosition + nextColumnPosition;
break;
}
// Definir nom de la chart
var chartName = instance.EnsureChartComponent<Title>();
chartName.text = $"Scores dans le temps | round {attempt.AttemptNumber}";
// D<>finir les noms des axes
var xAxis = instance.EnsureChartComponent<XAxis>();
xAxis.type = Axis.AxisType.Value;
AxisName xName = new();
xName.name = "Temps (ticks)";
xAxis.axisName = xName;
var yAxis = instance.EnsureChartComponent<YAxis>();
yAxis.type = Axis.AxisType.Value;
AxisName yName = new();
yName.name = "Score";
yAxis.axisName = yName;
// Ajouter une l<>gende
var legend = instance.EnsureChartComponent<Legend>();
legend.show = true;
foreach (var playerAttempt in attempt.PlayersAttempts)
{
var scoreOverTime = playerAttempt.Value.PlayerScoresOverTicksTime;
// Ajouter une nouvelle s<>rie avec une l<>gende
var serie = instance.AddSerie<Line>(playerAttempt.Value.PlayerName);
serie.serieName = playerAttempt.Value.PlayerName;
//serie.legendName = playerAttempt.Value.PlayerName;
scoreOverTime.ForEach(score =>
{
serie.AddXYData(score.X, score.Y);
});
}
}
}
#endregion
#region METHODS
private List<SessionData> LoadSessionData()
{
string directoryPath = Path.Combine(Application.persistentDataPath, "SessionsData");
var sessionDataList = new List<SessionData>();
var files = Directory.GetFiles(directoryPath, "*.json");
foreach (var file in files)
{
var json = File.ReadAllText(file);
var sessionData = JsonConvert.DeserializeObject<SessionData>(json);
sessionDataList.Add(sessionData);
}
return sessionDataList.OrderBy<SessionData, int>(s => s.SessionNumber).ToList();
}
#endregion
#region LIFECYCLE
protected override void Awake()
{
base.Awake();
Sessions = LoadSessionData();
_document = GetComponent<UIDocument>();
_exitButton = _document.rootVisualElement.Q("ExitButton") as Button;
_exitButton.RegisterCallback<ClickEvent>(OnExitButton);
_storageButton = _document.rootVisualElement.Q("StorageButton") as Button;
_storageButton.RegisterCallback<ClickEvent>(OnStorageButton);
lineCharts = new();
}
#endregion
}