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;
|
||||||
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);
|
private IEnumerator GetScoreboard()
|
||||||
if (entriesTable == null)
|
{
|
||||||
return;
|
Debug.Log("Fetching scoreboard...");
|
||||||
if (entriesTable.entries == null)
|
|
||||||
return;
|
using (UnityWebRequest request = UnityWebRequest.Get(apiUrl))
|
||||||
List<PlayerScoreboardCardData> scoreboardCardDatas = new List<PlayerScoreboardCardData>();
|
|
||||||
for (int i = 0; i < entriesTable.entries.Count; i++)
|
|
||||||
{
|
{
|
||||||
entries.Add(entriesTable.entries[i]);
|
yield return request.SendWebRequest();
|
||||||
OnEntryAdded?.Invoke(entriesTable.entries[i]);
|
|
||||||
PlayerScoreboardCardData cardData = new PlayerScoreboardCardData(entriesTable.entries[i].Name, entriesTable.entries[i].Score.ToString());
|
if (request.result == UnityWebRequest.Result.Success)
|
||||||
scoreboardCardDatas.Add(cardData);
|
{
|
||||||
|
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);
|
||||||
|
UpdateScoreboardView();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError("Erreur lors de la récupération du scoreboard : " + request.error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
SortScoreboardEntriesByHighscore(entries);
|
|
||||||
SortScoreboardCardsDatasByHighscore(scoreboardCardDatas);
|
|
||||||
scoreboardView.AddPlayerCards(scoreboardCardDatas);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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))
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user