Ajout du scoreboard en ligne

This commit is contained in:
louis 2025-03-06 17:36:49 +01:00
parent e570e8018d
commit 75614e4bc8
4 changed files with 1522 additions and 75 deletions

View File

@ -1,20 +1,24 @@
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking;
using System;
[Serializable]
public class ScoreboardEntriesTable public class ScoreboardEntriesTable
{ {
public ScoreboardEntriesTable(List<ScoreboardEntry> entries) public List<ScoreboardEntry> listeScore;
public ScoreboardEntriesTable()
{ {
this.entries = entries; listeScore = new List<ScoreboardEntry>();
} }
public List<ScoreboardEntry> entries = new List<ScoreboardEntry>();
} }
public class Scoreboard : MonoBehaviour, ICommandTranslator public class Scoreboard : MonoBehaviour
{ {
[SerializeField] private int maxEntries; private string apiUrl = "https://awesomerunner.lagaudiere.uk/score";
private List<ScoreboardEntry> entries = new List<ScoreboardEntry>(); public List<ScoreboardEntry> entries = new List<ScoreboardEntry>();
public event Action<ScoreboardEntry> OnEntryAdded; public event Action<ScoreboardEntry> OnEntryAdded;
@ -22,79 +26,111 @@ public class Scoreboard : MonoBehaviour, ICommandTranslator
private void Start() private void Start()
{ {
GameSession.Instance.AddCommandTranslator(this, false); StartCoroutine(GetScoreboard());
string jsonScoreboardEntries = PlayerPrefs.GetString("ScoreboardEntriesTableTest"); //Binary file
ScoreboardEntriesTable entriesTable = JsonUtility.FromJson<ScoreboardEntriesTable>(jsonScoreboardEntries);
if (entriesTable == null)
return;
if (entriesTable.entries == null)
return;
List<PlayerScoreboardCardData> scoreboardCardDatas = new List<PlayerScoreboardCardData>();
for (int i = 0; i < entriesTable.entries.Count; i++)
{
entries.Add(entriesTable.entries[i]);
OnEntryAdded?.Invoke(entriesTable.entries[i]);
PlayerScoreboardCardData cardData = new PlayerScoreboardCardData(entriesTable.entries[i].Name, entriesTable.entries[i].Score.ToString());
scoreboardCardDatas.Add(cardData);
} }
private IEnumerator GetScoreboard()
{
Debug.Log("Fetching scoreboard...");
using (UnityWebRequest request = UnityWebRequest.Get(apiUrl))
{
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
string json = request.downloadHandler.text;
Debug.Log("Received JSON: " + json);
// Désérialiser directement en un tableau
ScoreboardEntry[] scoreboardEntriesArray = JsonHelper.FromJson<ScoreboardEntry>(json);
if (scoreboardEntriesArray != null)
{
entries = new List<ScoreboardEntry>(scoreboardEntriesArray);
SortScoreboardEntriesByHighscore(entries); SortScoreboardEntriesByHighscore(entries);
SortScoreboardCardsDatasByHighscore(scoreboardCardDatas); UpdateScoreboardView();
scoreboardView.AddPlayerCards(scoreboardCardDatas); }
}
else
{
Debug.LogError("Erreur lors de la récupération du scoreboard : " + request.error);
}
}
} }
public void AddScoreboardEntry(string entryName, int entryScore)
{
ScoreboardEntry entry = new ScoreboardEntry(entryName, entryScore);
entries.Add(entry);
OnEntryAdded?.Invoke(entry);
}
public void SortScoreboardEntriesByHighscore(List<ScoreboardEntry> entries)
{
entries.Sort((x,y) => y.Score.CompareTo(x.Score));
}
public void SortScoreboardCardsDatasByHighscore(List<PlayerScoreboardCardData> scoreboardCardDatas)
{
scoreboardCardDatas.Sort((x, y) =>
{
int parsedScore1 = 0;
int parsedScore2 = 0;
int.TryParse(x.playerScore, out parsedScore1);
int.TryParse(y.playerScore, out parsedScore2);
return parsedScore2.CompareTo(parsedScore1);
});
}
public void AddScoreboardEntry(ScoreboardEntry entry) public void AddScoreboardEntry(ScoreboardEntry entry)
{ {
entries.Add(entry); entries.Add(entry);
OnEntryAdded?.Invoke(entry);
SaveScoreboardEntriesTable();
}
public void SaveScoreboardEntriesTable()
{
SortScoreboardEntriesByHighscore(entries); SortScoreboardEntriesByHighscore(entries);
ScoreboardEntriesTable scoreboardEntriesTable = new ScoreboardEntriesTable(entries); UpdateScoreboardView();
string jsonScoreboardEntries = JsonUtility.ToJson(scoreboardEntriesTable); StartCoroutine(PostScoreboardEntry(entry));
PlayerPrefs.SetString("ScoreboardEntriesTableTest", jsonScoreboardEntries);
PlayerPrefs.Save();
} }
public void TranslateCommand(ECommand command, PressedState state) private IEnumerator PostScoreboardEntry(ScoreboardEntry entry)
{ {
switch (command) string json = JsonUtility.ToJson(entry);
using (UnityWebRequest request = new UnityWebRequest(apiUrl, "POST"))
{ {
case ECommand.OPEN_SCOREBOARD: byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(json);
if (state.IsPressed == true) request.uploadHandler = new UploadHandlerRaw(bodyRaw);
scoreboardView.Show(true); request.downloadHandler = new DownloadHandlerBuffer();
if (state.IsReleased == true) request.SetRequestHeader("Content-Type", "application/json");
scoreboardView.Show(false);
break; yield return request.SendWebRequest();
default:
scoreboardView.Show(false); if (request.result == UnityWebRequest.Result.Success)
break; {
Debug.Log("Score ajouté avec succès !");
OnEntryAdded?.Invoke(entry);
}
else
{
Debug.LogError("Erreur lors de l'ajout du score : " + request.error);
} }
} }
} }
private void SortScoreboardEntriesByHighscore(List<ScoreboardEntry> entries)
{
entries.Sort((x, y) => y.score.CompareTo(x.score));
}
private void UpdateScoreboardView()
{
Debug.Log("Updating scoreboard view...");
List<PlayerScoreboardCardData> scoreboardCardDatas = new List<PlayerScoreboardCardData>();
foreach (var entry in entries)
{
OnEntryAdded?.Invoke(entry);
scoreboardCardDatas.Add(new PlayerScoreboardCardData(entry.pseudo, entry.score.ToString()));
}
scoreboardView.AddPlayerCards(scoreboardCardDatas);
}
}
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
string wrappedJson = "{\"items\":" + json + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(wrappedJson);
return wrapper.items;
}
[System.Serializable]
private class Wrapper<T>
{
public T[] items;
}
}
[Serializable]
public class ScoreboardEntriesWrapper
{
public ScoreboardEntry[] listeScore;
}

View File

@ -1,3 +1,4 @@
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
@ -5,13 +6,13 @@ using UnityEngine;
[System.Serializable] [System.Serializable]
public class ScoreboardEntry public class ScoreboardEntry
{ {
[SerializeField] private float score; public string pseudo;
[SerializeField] private string name; public int score;
public float Score { get { return score; } private set { score = value; } }
public string Name { get { return name; } private set { name = value; } } public ScoreboardEntry(string pseudo, int score)
public ScoreboardEntry(string name,float score)
{ {
this.name = name; this.pseudo = pseudo;
this.score = score; this.score = score;
} }
} }

View File

@ -21,22 +21,37 @@ public class ScoreboardView : BaseView
} }
public void AddPlayerCards(List<PlayerScoreboardCardData> cardsData) public void AddPlayerCards(List<PlayerScoreboardCardData> cardsData)
{ {
Debug.Log("Adding " + cardsData.Count + " player cards.");
foreach (var cardData in cardsData) foreach (var cardData in cardsData)
{ {
Debug.Log("Card Data: " + cardData.playerName + " - " + cardData.playerScore);
AddPlayerCard(cardData); AddPlayerCard(cardData);
} }
} }
private void AddPlayerCard(PlayerScoreboardCardData cardData) private void AddPlayerCard(PlayerScoreboardCardData cardData)
{ {
//if (playerCards.ContainsKey(cardData.playerName)) if (layoutGroup == null)
// return; {
Debug.LogError("layoutGroup is not assigned!");
return;
}
PlayerScoreboardCard playerScoreboardCard = Instantiate(cardPrefab); PlayerScoreboardCard playerScoreboardCard = Instantiate(cardPrefab);
playerScoreboardCard.transform.SetParent(layoutGroup.transform, false); playerScoreboardCard.transform.SetParent(layoutGroup.transform, false);
playerScoreboardCard.UpdateCard(cardData); playerScoreboardCard.UpdateCard(cardData);
Debug.Log("Player card created for: " + cardData.playerName);
playerCards.Add(playerScoreboardCard); playerCards.Add(playerScoreboardCard);
} }
public void RemovePlayerCard(string cardTag) public void RemovePlayerCard(string cardTag)
{ {
//if (playerCards.ContainsKey(cardTag)) //if (playerCards.ContainsKey(cardTag))