184 lines
5.7 KiB
C#
184 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Steamworks;
|
|
using UnityEngine;
|
|
|
|
[Serializable]
|
|
public class LeaderboardSettings
|
|
{
|
|
public string leaderboardName = string.Empty;
|
|
|
|
public string fisheryName = string.Empty;
|
|
|
|
public LeaderboardsManager.LeaderboardType leaderboardType = LeaderboardsManager.LeaderboardType.COUNT;
|
|
|
|
public const int leaderboardEntriesMax = 150;
|
|
|
|
public int leaderboardEntriesCount;
|
|
|
|
public int[] leaderboardMyDetails = new int[7];
|
|
|
|
public List<GameObject> objectsToInform = new List<GameObject>();
|
|
|
|
public SteamLeaderboard_t leaderboardHandle;
|
|
|
|
public LeaderboardEntry_t[] leaderboardEntries = new LeaderboardEntry_t[150];
|
|
|
|
public LeaderboardEntry_t leaderboardMyEntry;
|
|
|
|
public CallResult<LeaderboardFindResult_t> onLeaderboardFindResult_t;
|
|
|
|
public CallResult<LeaderboardScoreUploaded_t> onLeaderboardScoreUploaded_t;
|
|
|
|
public CallResult<LeaderboardScoresDownloaded_t> onLeaderboardScoresDownloaded_t;
|
|
|
|
public CallResult<LeaderboardScoresDownloaded_t> onLeaderboardMyScoresDownloaded_t;
|
|
|
|
public LeaderboardFindResult_t findResult_t;
|
|
|
|
public LeaderboardScoreUploaded_t scoresUploaded_t;
|
|
|
|
public LeaderboardScoresDownloaded_t scoresDownloaded_t;
|
|
|
|
public LeaderboardScoresDownloaded_t myScoresDownloaded_t;
|
|
|
|
public LeaderboardSettings(string name)
|
|
{
|
|
leaderboardName = name;
|
|
onLeaderboardFindResult_t = CallResult<LeaderboardFindResult_t>.Create(OnFindLeaderboard);
|
|
onLeaderboardScoreUploaded_t = CallResult<LeaderboardScoreUploaded_t>.Create(OnUploadScore);
|
|
onLeaderboardScoresDownloaded_t = CallResult<LeaderboardScoresDownloaded_t>.Create(OnDownloadScores);
|
|
onLeaderboardMyScoresDownloaded_t = CallResult<LeaderboardScoresDownloaded_t>.Create(OnDownloadMyScore);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
}
|
|
|
|
public void FindLeaderboard(bool createIfNotExist)
|
|
{
|
|
if (!(GlobalSettings.Instance == null) && SteamManager.Initialized)
|
|
{
|
|
SteamAPICall_t hAPICall = ((!createIfNotExist) ? SteamUserStats.FindLeaderboard(leaderboardName) : SteamUserStats.FindOrCreateLeaderboard(leaderboardName, ELeaderboardSortMethod.k_ELeaderboardSortMethodDescending, ELeaderboardDisplayType.k_ELeaderboardDisplayTypeNumeric));
|
|
onLeaderboardFindResult_t.Set(hAPICall);
|
|
}
|
|
}
|
|
|
|
public void OnFindLeaderboard(LeaderboardFindResult_t value, bool ioFailure)
|
|
{
|
|
if (leaderboardType == LeaderboardsManager.LeaderboardType.Score)
|
|
{
|
|
Debug.Log("OnFindLeaderboard " + leaderboardName + ": " + value.m_hSteamLeaderboard.ToString() + " " + value.m_hSteamLeaderboard.m_SteamLeaderboard + " " + value.m_bLeaderboardFound + " " + ioFailure);
|
|
}
|
|
findResult_t = value;
|
|
if (value.m_bLeaderboardFound > 0)
|
|
{
|
|
leaderboardHandle = value.m_hSteamLeaderboard;
|
|
for (int i = 0; i < objectsToInform.Count; i++)
|
|
{
|
|
objectsToInform[i].SendMessage("OnFindLeaderboard", this);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UploadScore(int score, int[] info)
|
|
{
|
|
if (!SteamManager.Initialized)
|
|
{
|
|
return;
|
|
}
|
|
SteamAPICall_t hAPICall = SteamUserStats.UploadLeaderboardScore(leaderboardHandle, ELeaderboardUploadScoreMethod.k_ELeaderboardUploadScoreMethodForceUpdate, score, info, info.Length);
|
|
onLeaderboardScoreUploaded_t.Set(hAPICall);
|
|
if (leaderboardType == LeaderboardsManager.LeaderboardType.Score)
|
|
{
|
|
string text = "UploadScore: " + score;
|
|
for (int i = 0; i < info.Length; i++)
|
|
{
|
|
text = text + " " + info[i];
|
|
}
|
|
Debug.Log(text);
|
|
}
|
|
}
|
|
|
|
public void OnUploadScore(LeaderboardScoreUploaded_t value, bool ioFailure)
|
|
{
|
|
if (leaderboardType == LeaderboardsManager.LeaderboardType.Score)
|
|
{
|
|
Debug.Log("OnUploadScore " + leaderboardName + ": " + value.m_bScoreChanged + " " + value.m_nScore + " " + ioFailure);
|
|
}
|
|
scoresUploaded_t = value;
|
|
for (int i = 0; i < objectsToInform.Count; i++)
|
|
{
|
|
objectsToInform[i].SendMessage("OnUploadScore", this);
|
|
}
|
|
}
|
|
|
|
public bool DownloadScores()
|
|
{
|
|
if (!SteamManager.Initialized)
|
|
{
|
|
return false;
|
|
}
|
|
SteamAPICall_t hAPICall = SteamUserStats.DownloadLeaderboardEntries(leaderboardHandle, ELeaderboardDataRequest.k_ELeaderboardDataRequestGlobal, 1, 150);
|
|
onLeaderboardScoresDownloaded_t.Set(hAPICall);
|
|
if (leaderboardType == LeaderboardsManager.LeaderboardType.Score)
|
|
{
|
|
Debug.Log("DownloadScores");
|
|
}
|
|
if (leaderboardType == LeaderboardsManager.LeaderboardType.Score)
|
|
{
|
|
DownloadMyScore();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void OnDownloadScores(LeaderboardScoresDownloaded_t value, bool ioFailure)
|
|
{
|
|
if (leaderboardType == LeaderboardsManager.LeaderboardType.Score)
|
|
{
|
|
Debug.Log("OnDownloadScores: " + value.m_cEntryCount + " " + ioFailure);
|
|
}
|
|
int num = 0;
|
|
scoresDownloaded_t = value;
|
|
leaderboardEntriesCount = Mathf.Min(value.m_cEntryCount, 150);
|
|
for (int i = 0; i < objectsToInform.Count; i++)
|
|
{
|
|
objectsToInform[i].SendMessage("OnDownloadScores", this);
|
|
}
|
|
}
|
|
|
|
public bool DownloadMyScore()
|
|
{
|
|
if (!SteamManager.Initialized)
|
|
{
|
|
return false;
|
|
}
|
|
CSteamID[] array = new CSteamID[1]
|
|
{
|
|
new CSteamID(GlobalSettings.Instance.GetPlatformProfileID())
|
|
};
|
|
SteamAPICall_t hAPICall = SteamUserStats.DownloadLeaderboardEntriesForUsers(leaderboardHandle, array, array.Length);
|
|
onLeaderboardMyScoresDownloaded_t.Set(hAPICall);
|
|
if (leaderboardType == LeaderboardsManager.LeaderboardType.Score)
|
|
{
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void OnDownloadMyScore(LeaderboardScoresDownloaded_t value, bool ioFailure)
|
|
{
|
|
if (leaderboardType == LeaderboardsManager.LeaderboardType.Score)
|
|
{
|
|
}
|
|
myScoresDownloaded_t = value;
|
|
if (value.m_cEntryCount != 0)
|
|
{
|
|
SteamUserStats.GetDownloadedLeaderboardEntry(value.m_hSteamLeaderboardEntries, 0, out leaderboardMyEntry, leaderboardMyDetails, leaderboardMyDetails.Length);
|
|
}
|
|
for (int i = 0; i < objectsToInform.Count; i++)
|
|
{
|
|
objectsToInform[i].SendMessage("OnDownloadMyScore", this);
|
|
}
|
|
}
|
|
}
|