rabidus-test/Assets/Scripts/LeaderboardController.cs

125 lines
3.5 KiB
C#
Raw Normal View History

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;
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
{
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;
2023-07-24 16:38:13 +03:00
public UnityEvent<List<Player>> OnDailyInit;
public UnityEvent<List<Player>> OnGlobalInit;
public UnityEvent<int> OnDailyNewRecord;
public UnityEvent<int> OnGlobalNewRecord;
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()
2023-07-24 16:38:13 +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
}
private void RefreshDailyEntries()
2023-07-24 16:38:13 +03:00
{
_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 SavePlayerRecord(Player player)
{
CheckDailyLeaderboard(player);
CheckGlobalLeaderboard(player);
}
2023-07-24 16:38:13 +03:00
private void CheckDailyLeaderboard(Player player)
{
if (player.Score >= _dailyPlayerInfo.Players.Last().Score)
2023-07-24 16:38:13 +03:00
{
_dailyPlayerInfo.Players[_dailyPlayerInfo.Players.IndexOf(_dailyPlayerInfo.Players.Last())] = player;
RefreshDailyEntries();
OnDailyNewRecord?.Invoke(_dailyPlayerInfo.Players.IndexOf(player));
2023-07-24 16:38:13 +03:00
}
}
private void CheckGlobalLeaderboard(Player player)
{
if (player.Score >= _globalPlayerInfo.Players.Last().Score)
{
_globalPlayerInfo.Players[_globalPlayerInfo.Players.IndexOf(_globalPlayerInfo.Players.Last())] = player;
RefreshGlobalEntries();
OnGlobalNewRecord?.Invoke(_globalPlayerInfo.Players.IndexOf(player));
}
}
2023-07-24 16:38:13 +03:00
}