175 lines
4.9 KiB
C#
175 lines
4.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
public class LeaderboardController : MonoBehaviour
|
|
{
|
|
private const string DAILY_ID = "DAILY_LB";
|
|
private const string GLOBAL_ID = "GLOBAL_LB";
|
|
|
|
public static LeaderboardController Instance;
|
|
|
|
[SerializeField] private PlayerInfo _dailyPlayerInfo;
|
|
[SerializeField] private PlayerInfo _globalPlayerInfo;
|
|
|
|
public UnityEvent<List<Player>> OnDailyInit;
|
|
public UnityEvent<List<Player>> OnGlobalInit;
|
|
|
|
private void Awake()
|
|
{
|
|
//transform.parent = null;
|
|
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
//DontDestroyOnLoad(this);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
//yield return new WaitForSeconds(1);
|
|
InitDailyLeaderboard();
|
|
InitGlobalLeaderboard();
|
|
}
|
|
|
|
private void InitDailyLeaderboard()
|
|
{
|
|
StartCoroutine(InitDailyLeaderboard_Coroutine());
|
|
}
|
|
|
|
private void InitGlobalLeaderboard()
|
|
{
|
|
StartCoroutine(InitGlobalLeaderboard_Coroutine());
|
|
}
|
|
|
|
private IEnumerator InitDailyLeaderboard_Coroutine()
|
|
{
|
|
string lastSave = PlayerPrefs.GetString(DAILY_ID, string.Empty);
|
|
DateTime lastSaveDate;
|
|
|
|
if (DateTime.TryParse(lastSave, out lastSaveDate) && DateTime.Now.Day <= lastSaveDate.Day)
|
|
{
|
|
yield return SaveLoadController.Instance.Load(ref _dailyPlayerInfo, DAILY_ID);
|
|
}
|
|
else
|
|
{
|
|
yield return SaveLoadController.Instance.Save(_dailyPlayerInfo, DAILY_ID);
|
|
}
|
|
|
|
PlayerPrefs.SetString(DAILY_ID, DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
|
|
Debug.Log("End daily lb init");
|
|
|
|
RefreshDailyEntries();
|
|
|
|
yield return null;
|
|
}
|
|
|
|
|
|
private IEnumerator InitGlobalLeaderboard_Coroutine()
|
|
{
|
|
if (PlayerPrefs.GetInt(GLOBAL_ID, 0) == 1)
|
|
{
|
|
yield return SaveLoadController.Instance.Load(ref _globalPlayerInfo, GLOBAL_ID);
|
|
}
|
|
else
|
|
{
|
|
yield return SaveLoadController.Instance.Save(_globalPlayerInfo, GLOBAL_ID);
|
|
}
|
|
|
|
PlayerPrefs.SetInt(GLOBAL_ID, 1);
|
|
Debug.Log("End global lb init");
|
|
|
|
RefreshGlobalEntries();
|
|
|
|
yield return null;
|
|
}
|
|
|
|
public void RefreshEntries()
|
|
{
|
|
RefreshDailyEntries();
|
|
RefreshGlobalEntries();
|
|
}
|
|
|
|
private void RefreshDailyEntries()
|
|
{
|
|
_dailyPlayerInfo.Players = _dailyPlayerInfo.Players.OrderByDescending(x => x.Score).ToList();
|
|
OnDailyInit?.Invoke(_dailyPlayerInfo.Players);
|
|
}
|
|
|
|
private void RefreshGlobalEntries()
|
|
{
|
|
_globalPlayerInfo.Players = _globalPlayerInfo.Players.OrderByDescending(x => x.Score).ToList();
|
|
OnGlobalInit?.Invoke(_globalPlayerInfo.Players);
|
|
}
|
|
|
|
public void UpdatePlayerScore(Player player)
|
|
{
|
|
CheckDailyLeaderboard(player);
|
|
CheckGlobalLeaderboard(player);
|
|
}
|
|
|
|
private void CheckGlobalLeaderboard(Player player)
|
|
{
|
|
if (!_globalPlayerInfo.Players.Contains(player) && player.Score >= _globalPlayerInfo.Players.Last().Score)
|
|
{
|
|
InsertGlobalLeaderboardEntry(player);
|
|
RefreshGlobalEntries();
|
|
}
|
|
else if (_globalPlayerInfo.Players.Contains(player))
|
|
{
|
|
RefreshGlobalEntries();
|
|
}
|
|
}
|
|
|
|
private void CheckDailyLeaderboard(Player player)
|
|
{
|
|
if (!_dailyPlayerInfo.Players.Contains(player) && player.Score >= _dailyPlayerInfo.Players.Last().Score)
|
|
{
|
|
InsertDailyLeaderboardEntry(player);
|
|
RefreshDailyEntries();
|
|
}
|
|
else if (_dailyPlayerInfo.Players.Contains(player))
|
|
{
|
|
RefreshDailyEntries();
|
|
}
|
|
}
|
|
|
|
private void InsertDailyLeaderboardEntry(Player player)
|
|
{
|
|
_dailyPlayerInfo.Players[_dailyPlayerInfo.Players.IndexOf(_dailyPlayerInfo.Players.Last())] = player;
|
|
}
|
|
|
|
private void InsertGlobalLeaderboardEntry(Player player)
|
|
{
|
|
_globalPlayerInfo.Players[_globalPlayerInfo.Players.IndexOf(_globalPlayerInfo.Players.Last())] = player;
|
|
}
|
|
|
|
public bool CheckCurrentPlayerDailyTop()
|
|
{
|
|
return _dailyPlayerInfo.Players.Contains(PlayerSetup.Instance.CurrentPlayer);
|
|
}
|
|
|
|
public bool CheckCurrentPlayerGlobalTop()
|
|
{
|
|
return _globalPlayerInfo.Players.Contains(PlayerSetup.Instance.CurrentPlayer);
|
|
}
|
|
|
|
public int GetCurrentPlayerDailyPos() => _dailyPlayerInfo.Players.IndexOf(PlayerSetup.Instance.CurrentPlayer) + 1;
|
|
public int GetCurrentPlayerGlobalPos() => _globalPlayerInfo.Players.IndexOf(PlayerSetup.Instance.CurrentPlayer) + 1;
|
|
|
|
public void SaveData()
|
|
{
|
|
SaveLoadController.Instance.Save(_dailyPlayerInfo, DAILY_ID);
|
|
SaveLoadController.Instance.Save(_globalPlayerInfo, GLOBAL_ID);
|
|
}
|
|
}
|