Ajout du scoreboard en ligne
This commit is contained in:
parent
e570e8018d
commit
75614e4bc8
@ -1,20 +1,24 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using System;
|
||||
|
||||
[Serializable]
|
||||
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 List<ScoreboardEntry> entries = new List<ScoreboardEntry>();
|
||||
private string apiUrl = "https://awesomerunner.lagaudiere.uk/score";
|
||||
public List<ScoreboardEntry> entries = new List<ScoreboardEntry>();
|
||||
|
||||
public event Action<ScoreboardEntry> OnEntryAdded;
|
||||
|
||||
@ -22,79 +26,111 @@ public class Scoreboard : MonoBehaviour, ICommandTranslator
|
||||
|
||||
private void Start()
|
||||
{
|
||||
GameSession.Instance.AddCommandTranslator(this, false);
|
||||
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);
|
||||
StartCoroutine(GetScoreboard());
|
||||
}
|
||||
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);
|
||||
SortScoreboardCardsDatasByHighscore(scoreboardCardDatas);
|
||||
scoreboardView.AddPlayerCards(scoreboardCardDatas);
|
||||
UpdateScoreboardView();
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
entries.Add(entry);
|
||||
OnEntryAdded?.Invoke(entry);
|
||||
SaveScoreboardEntriesTable();
|
||||
}
|
||||
|
||||
public void SaveScoreboardEntriesTable()
|
||||
{
|
||||
SortScoreboardEntriesByHighscore(entries);
|
||||
ScoreboardEntriesTable scoreboardEntriesTable = new ScoreboardEntriesTable(entries);
|
||||
string jsonScoreboardEntries = JsonUtility.ToJson(scoreboardEntriesTable);
|
||||
PlayerPrefs.SetString("ScoreboardEntriesTableTest", jsonScoreboardEntries);
|
||||
PlayerPrefs.Save();
|
||||
UpdateScoreboardView();
|
||||
StartCoroutine(PostScoreboardEntry(entry));
|
||||
}
|
||||
|
||||
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:
|
||||
if (state.IsPressed == true)
|
||||
scoreboardView.Show(true);
|
||||
if (state.IsReleased == true)
|
||||
scoreboardView.Show(false);
|
||||
break;
|
||||
default:
|
||||
scoreboardView.Show(false);
|
||||
break;
|
||||
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(json);
|
||||
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
||||
request.downloadHandler = new DownloadHandlerBuffer();
|
||||
request.SetRequestHeader("Content-Type", "application/json");
|
||||
|
||||
yield return request.SendWebRequest();
|
||||
|
||||
if (request.result == UnityWebRequest.Result.Success)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
@ -5,13 +6,13 @@ using UnityEngine;
|
||||
[System.Serializable]
|
||||
public class ScoreboardEntry
|
||||
{
|
||||
[SerializeField] private float score;
|
||||
[SerializeField] private string name;
|
||||
public float Score { get { return score; } private set { score = value; } }
|
||||
public string Name { get { return name; } private set { name = value; } }
|
||||
public ScoreboardEntry(string name,float score)
|
||||
public string pseudo;
|
||||
public int score;
|
||||
|
||||
public ScoreboardEntry(string pseudo, int score)
|
||||
{
|
||||
this.name = name;
|
||||
this.pseudo = pseudo;
|
||||
this.score = score;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,22 +21,37 @@ public class ScoreboardView : BaseView
|
||||
}
|
||||
public void AddPlayerCards(List<PlayerScoreboardCardData> cardsData)
|
||||
{
|
||||
Debug.Log("Adding " + cardsData.Count + " player cards.");
|
||||
|
||||
foreach (var cardData in cardsData)
|
||||
{
|
||||
Debug.Log("Card Data: " + cardData.playerName + " - " + cardData.playerScore);
|
||||
AddPlayerCard(cardData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void AddPlayerCard(PlayerScoreboardCardData cardData)
|
||||
{
|
||||
//if (playerCards.ContainsKey(cardData.playerName))
|
||||
// return;
|
||||
if (layoutGroup == null)
|
||||
{
|
||||
Debug.LogError("layoutGroup is not assigned!");
|
||||
return;
|
||||
}
|
||||
|
||||
PlayerScoreboardCard playerScoreboardCard = Instantiate(cardPrefab);
|
||||
playerScoreboardCard.transform.SetParent(layoutGroup.transform, false);
|
||||
playerScoreboardCard.UpdateCard(cardData);
|
||||
|
||||
Debug.Log("Player card created for: " + cardData.playerName);
|
||||
|
||||
playerCards.Add(playerScoreboardCard);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void RemovePlayerCard(string cardTag)
|
||||
{
|
||||
//if (playerCards.ContainsKey(cardTag))
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user