2023-07-24 16:38:13 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using UnityEngine;
|
2023-08-22 15:41:12 +03:00
|
|
|
using UnityEngine.Events;
|
2023-08-07 13:23:18 +03:00
|
|
|
using UnityEngine.UI;
|
2023-07-24 16:38:13 +03:00
|
|
|
|
2023-08-15 17:38:54 +03:00
|
|
|
public class LeaderboardController : MonoBehaviour
|
2023-07-24 16:38:13 +03:00
|
|
|
{
|
2023-10-10 15:03:37 +03:00
|
|
|
private const string DAILY_ID = "DAILY_LB";
|
|
|
|
private const string GLOBAL_ID = "GLOBAL_LB";
|
2023-08-07 13:23:18 +03:00
|
|
|
|
2023-10-10 15:03:37 +03:00
|
|
|
public static LeaderboardController Instance;
|
2023-08-07 13:23:18 +03:00
|
|
|
|
2023-10-10 15:03:37 +03:00
|
|
|
[SerializeField] private PlayerInfo _dailyPlayerInfo;
|
|
|
|
[SerializeField] private PlayerInfo _globalPlayerInfo;
|
2023-07-24 16:38:13 +03:00
|
|
|
|
2023-10-10 15:03:37 +03:00
|
|
|
public UnityEvent<List<Player>> OnDailyInit;
|
|
|
|
public UnityEvent<List<Player>> OnGlobalInit;
|
|
|
|
|
2023-11-29 13:10:35 +03:00
|
|
|
private bool _resetDaily = false;
|
|
|
|
private bool _resetGlobal = false;
|
|
|
|
|
|
|
|
|
2023-10-10 17:25:59 +03:00
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
//transform.parent = null;
|
2023-10-10 15:03:37 +03:00
|
|
|
|
2023-10-10 17:25:59 +03:00
|
|
|
if (Instance == null)
|
|
|
|
{
|
|
|
|
Instance = this;
|
|
|
|
//DontDestroyOnLoad(this);
|
|
|
|
}
|
2023-10-17 12:37:34 +03:00
|
|
|
else if (Instance != this)
|
2023-10-10 17:25:59 +03:00
|
|
|
{
|
|
|
|
Destroy(gameObject);
|
|
|
|
}
|
|
|
|
}
|
2023-10-10 15:03:37 +03:00
|
|
|
|
2023-11-29 13:10:35 +03:00
|
|
|
private IEnumerator Start()
|
2023-10-10 15:03:37 +03:00
|
|
|
{
|
2023-11-29 13:10:35 +03:00
|
|
|
yield return new WaitForSeconds(10);
|
|
|
|
if(_resetDaily)
|
|
|
|
PlayerPrefs.SetString(DAILY_ID, string.Empty);
|
|
|
|
if(_resetGlobal)
|
|
|
|
PlayerPrefs.SetString(GLOBAL_ID, string.Empty);
|
2023-11-07 11:48:48 +03:00
|
|
|
InitLeaderboards();
|
|
|
|
}
|
|
|
|
|
2023-11-29 13:10:35 +03:00
|
|
|
private void Update()
|
|
|
|
{
|
2023-11-29 13:17:14 +03:00
|
|
|
if(Input.GetKeyDown(KeyCode.J))
|
2023-11-29 13:10:35 +03:00
|
|
|
_resetDaily = true;
|
2023-11-29 13:17:14 +03:00
|
|
|
if(Input.GetKeyDown(KeyCode.K))
|
2023-11-29 13:10:35 +03:00
|
|
|
_resetGlobal = true;
|
|
|
|
}
|
|
|
|
|
2023-11-07 11:48:48 +03:00
|
|
|
public void InitLeaderboards()
|
|
|
|
{
|
2023-10-10 15:03:37 +03:00
|
|
|
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()
|
2023-07-24 16:38:13 +03:00
|
|
|
{
|
2023-10-10 15:03:37 +03:00
|
|
|
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;
|
2023-07-24 16:38:13 +03:00
|
|
|
}
|
|
|
|
|
2023-10-11 11:58:06 +03:00
|
|
|
public void RefreshEntries()
|
|
|
|
{
|
|
|
|
RefreshDailyEntries();
|
|
|
|
RefreshGlobalEntries();
|
|
|
|
}
|
|
|
|
|
2023-10-10 15:03:37 +03:00
|
|
|
private void RefreshDailyEntries()
|
2023-07-24 16:38:13 +03:00
|
|
|
{
|
2023-10-10 15:03:37 +03:00
|
|
|
_dailyPlayerInfo.Players = _dailyPlayerInfo.Players.OrderByDescending(x => x.Score).ToList();
|
|
|
|
OnDailyInit?.Invoke(_dailyPlayerInfo.Players);
|
|
|
|
}
|
2023-08-07 13:23:18 +03:00
|
|
|
|
2023-10-10 15:03:37 +03:00
|
|
|
private void RefreshGlobalEntries()
|
|
|
|
{
|
|
|
|
_globalPlayerInfo.Players = _globalPlayerInfo.Players.OrderByDescending(x => x.Score).ToList();
|
|
|
|
OnGlobalInit?.Invoke(_globalPlayerInfo.Players);
|
|
|
|
}
|
|
|
|
|
2023-10-10 17:25:59 +03:00
|
|
|
public void UpdatePlayerScore(Player player)
|
2023-10-10 15:03:37 +03:00
|
|
|
{
|
|
|
|
CheckDailyLeaderboard(player);
|
|
|
|
CheckGlobalLeaderboard(player);
|
|
|
|
}
|
2023-07-24 16:38:13 +03:00
|
|
|
|
2023-10-10 17:25:59 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 15:03:37 +03:00
|
|
|
private void CheckDailyLeaderboard(Player player)
|
|
|
|
{
|
2023-10-10 17:25:59 +03:00
|
|
|
if (!_dailyPlayerInfo.Players.Contains(player) && player.Score >= _dailyPlayerInfo.Players.Last().Score)
|
|
|
|
{
|
|
|
|
InsertDailyLeaderboardEntry(player);
|
|
|
|
RefreshDailyEntries();
|
|
|
|
}
|
|
|
|
else if (_dailyPlayerInfo.Players.Contains(player))
|
2023-07-24 16:38:13 +03:00
|
|
|
{
|
2023-10-10 15:03:37 +03:00
|
|
|
RefreshDailyEntries();
|
2023-07-24 16:38:13 +03:00
|
|
|
}
|
|
|
|
}
|
2023-08-07 13:23:18 +03:00
|
|
|
|
2023-10-10 17:25:59 +03:00
|
|
|
private void InsertDailyLeaderboardEntry(Player player)
|
2023-08-07 13:23:18 +03:00
|
|
|
{
|
2023-10-10 17:25:59 +03:00
|
|
|
_dailyPlayerInfo.Players[_dailyPlayerInfo.Players.IndexOf(_dailyPlayerInfo.Players.Last())] = player;
|
|
|
|
}
|
2023-10-10 15:03:37 +03:00
|
|
|
|
2023-10-10 17:25:59 +03:00
|
|
|
private void InsertGlobalLeaderboardEntry(Player player)
|
|
|
|
{
|
|
|
|
_globalPlayerInfo.Players[_globalPlayerInfo.Players.IndexOf(_globalPlayerInfo.Players.Last())] = player;
|
|
|
|
}
|
|
|
|
|
2023-10-11 15:44:05 +03:00
|
|
|
public bool CheckCurrentPlayerDailyTop()
|
|
|
|
{
|
|
|
|
return _dailyPlayerInfo.Players.Contains(PlayerSetup.Instance.CurrentPlayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CheckCurrentPlayerGlobalTop()
|
|
|
|
{
|
|
|
|
return _globalPlayerInfo.Players.Contains(PlayerSetup.Instance.CurrentPlayer);
|
|
|
|
}
|
|
|
|
|
2023-10-11 17:33:14 +03:00
|
|
|
public int GetCurrentPlayerDailyPos() => _dailyPlayerInfo.Players.IndexOf(PlayerSetup.Instance.CurrentPlayer) + 1;
|
|
|
|
public int GetCurrentPlayerGlobalPos() => _globalPlayerInfo.Players.IndexOf(PlayerSetup.Instance.CurrentPlayer) + 1;
|
2023-10-11 15:44:05 +03:00
|
|
|
|
2023-10-10 17:25:59 +03:00
|
|
|
public void SaveData()
|
|
|
|
{
|
|
|
|
SaveLoadController.Instance.Save(_dailyPlayerInfo, DAILY_ID);
|
|
|
|
SaveLoadController.Instance.Save(_globalPlayerInfo, GLOBAL_ID);
|
2023-08-07 13:23:18 +03:00
|
|
|
}
|
2023-07-24 16:38:13 +03:00
|
|
|
}
|