Implemented getting and setting high scores from server
This commit is contained in:
parent
c103ffb339
commit
d3e2d425bf
17 changed files with 346 additions and 17 deletions
|
|
@ -1,21 +1,51 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Scores;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace DungeonSelection
|
||||
{
|
||||
public class DungeonDisplayData
|
||||
{
|
||||
public DungeonDisplayData(DungeonData dungeonData, List<ScoreDisplay> highScores)
|
||||
{
|
||||
DungeonData = dungeonData;
|
||||
HighScores = highScores;
|
||||
}
|
||||
public DungeonData DungeonData;
|
||||
public List<ScoreDisplay> HighScores;
|
||||
}
|
||||
|
||||
public class ScoreDisplay
|
||||
{
|
||||
public ScoreDisplay(string playerName, int score)
|
||||
{
|
||||
PlayerName = playerName;
|
||||
Score = score;
|
||||
}
|
||||
|
||||
public string PlayerName;
|
||||
public int Score;
|
||||
}
|
||||
|
||||
public class DungeonSelector : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private DungeonData[] dungeons;
|
||||
[SerializeField] private ScoreManager scoreManager;
|
||||
private DungeonData _currentlySelectedDungeonData;
|
||||
private int _currentlySelectedDungeonIndex = 0;
|
||||
private Dictionary<int, List<ScoreDisplay>> scoresByLevelId = new Dictionary<int, List<ScoreDisplay>>();
|
||||
|
||||
private bool _scoresReceived = false;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
UIEvents.NextDungeonClicked += SelectNextDungeon;
|
||||
UIEvents.PreviousDungeonClicked += SelectPreviousDungeon;
|
||||
UIEvents.EnterDungeonClicked += LoadDungeon;
|
||||
scoreManager.ScoresRecieved += HandleScoresRecieved;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
|
|
@ -23,6 +53,7 @@ namespace DungeonSelection
|
|||
UIEvents.NextDungeonClicked -= SelectNextDungeon;
|
||||
UIEvents.PreviousDungeonClicked -= SelectPreviousDungeon;
|
||||
UIEvents.EnterDungeonClicked -= LoadDungeon;
|
||||
scoreManager.ScoresRecieved -= HandleScoresRecieved;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
|
|
@ -30,7 +61,18 @@ namespace DungeonSelection
|
|||
GameEvents.DungeonSelectionStarted?.Invoke();
|
||||
|
||||
_currentlySelectedDungeonData = dungeons[_currentlySelectedDungeonIndex];
|
||||
GameEvents.ShowingDungeon?.Invoke(_currentlySelectedDungeonData);
|
||||
StartCoroutine(WaitForScoresAndShowDungeon());
|
||||
}
|
||||
|
||||
private IEnumerator WaitForScoresAndShowDungeon()
|
||||
{
|
||||
while (!_scoresReceived)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
scoresByLevelId.TryGetValue(_currentlySelectedDungeonData.Id, out var scoreDisplays);
|
||||
GameEvents.ShowingDungeon?.Invoke(new DungeonDisplayData(_currentlySelectedDungeonData, scoreDisplays ?? new List<ScoreDisplay>()));
|
||||
if (_currentlySelectedDungeonIndex == 0)
|
||||
{
|
||||
GameEvents.ShowingFirstDugeon?.Invoke();
|
||||
|
|
@ -47,7 +89,8 @@ namespace DungeonSelection
|
|||
if (_currentlySelectedDungeonIndex + 1 >= dungeons.Length) return;
|
||||
_currentlySelectedDungeonIndex++;
|
||||
_currentlySelectedDungeonData = dungeons[_currentlySelectedDungeonIndex];
|
||||
GameEvents.ShowingDungeon?.Invoke(_currentlySelectedDungeonData);
|
||||
scoresByLevelId.TryGetValue(_currentlySelectedDungeonData.Id, out var scoreDisplays);
|
||||
GameEvents.ShowingDungeon?.Invoke(new DungeonDisplayData(_currentlySelectedDungeonData, scoreDisplays ?? new List<ScoreDisplay>()));
|
||||
if (_currentlySelectedDungeonIndex == dungeons.Length - 1)
|
||||
{
|
||||
GameEvents.ShowingLastDungeon?.Invoke();
|
||||
|
|
@ -59,11 +102,37 @@ namespace DungeonSelection
|
|||
if (_currentlySelectedDungeonIndex - 1 < 0) return;
|
||||
_currentlySelectedDungeonIndex--;
|
||||
_currentlySelectedDungeonData = dungeons[_currentlySelectedDungeonIndex];
|
||||
GameEvents.ShowingDungeon?.Invoke(_currentlySelectedDungeonData);
|
||||
scoresByLevelId.TryGetValue(_currentlySelectedDungeonData.Id, out var scoreDisplays);
|
||||
GameEvents.ShowingDungeon?.Invoke(new DungeonDisplayData(_currentlySelectedDungeonData, scoreDisplays ?? new List<ScoreDisplay>()));
|
||||
if (_currentlySelectedDungeonIndex == 0)
|
||||
{
|
||||
GameEvents.ShowingFirstDugeon?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleScoresRecieved(ScoreEntry[] scores)
|
||||
{
|
||||
scoresByLevelId = new Dictionary<int, List<ScoreDisplay>>();
|
||||
foreach (ScoreEntry score in scores)
|
||||
{
|
||||
if (scoresByLevelId.TryGetValue(score.level_id, out var scoresDisplay))
|
||||
{
|
||||
scoresDisplay.Add(new ScoreDisplay(score.player, score.score));
|
||||
}
|
||||
else
|
||||
{
|
||||
scoresByLevelId[score.level_id] = new List<ScoreDisplay>()
|
||||
{ new ScoreDisplay(score.player, score.score) };
|
||||
}
|
||||
}
|
||||
|
||||
foreach (List<ScoreDisplay> scoresDisplay in scoresByLevelId.Values)
|
||||
{
|
||||
// Sort highest to lowest
|
||||
scoresDisplay.Sort((a, b) => b.Score.CompareTo(a.Score));
|
||||
}
|
||||
|
||||
_scoresReceived = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue