rabidus-test/Assets/Scripts/LeaderboardController.cs

175 lines
4.9 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;
2023-10-10 17:25:59 +03:00
private void Awake()
{
//transform.parent = null;
2023-10-10 17:25:59 +03:00
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()
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
}
2023-10-11 11:58:06 +03:00
public void RefreshEntries()
{
RefreshDailyEntries();
RefreshGlobalEntries();
}
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);
}
2023-10-10 17:25:59 +03:00
public void UpdatePlayerScore(Player player)
{
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();
}
}
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
{
RefreshDailyEntries();
2023-07-24 16:38:13 +03:00
}
}
2023-10-10 17:25:59 +03:00
private void InsertDailyLeaderboardEntry(Player player)
{
2023-10-10 17:25:59 +03:00
_dailyPlayerInfo.Players[_dailyPlayerInfo.Players.IndexOf(_dailyPlayerInfo.Players.Last())] = player;
}
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-07-24 16:38:13 +03:00
}