-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSteamLeaderboard.cpp
More file actions
110 lines (93 loc) · 3.64 KB
/
Copy pathSteamLeaderboard.cpp
File metadata and controls
110 lines (93 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "master.h"
#include "SteamLeaderboard.h"
#define LEADERBOARD_SCORE "High Scores"
#define LEADERBOARD_KILLS "Robots Destroyed"
using namespace pyrodactyl;
SteamLeaderboard::SteamLeaderboard()
{
loading = false;
handle_score = 0;
handle_kills = 0;
}
//-----------------------------------------------------------------------------
// Purpose: Gets handles for our leaderboards. If the leaderboards don't exist, do not create them.
// Each time this is called, we look up another leaderboard.
//-----------------------------------------------------------------------------
void SteamLeaderboard::FindLeaderboards()
{
//We're already downloading leaderboards, wait
if (loading)
return;
if (SteamUserValid())
{
SteamAPICall_t hSteamAPICall = 0;
if (handle_score == 0)
{
// find/create a leaderboard for the quickest win
hSteamAPICall = PlayerSteamStats()->FindLeaderboard(LEADERBOARD_SCORE);
}
else if (handle_kills == 0)
{
// find/create a leaderboard for the most feet traveled in 1 round
hSteamAPICall = PlayerSteamStats()->FindLeaderboard(LEADERBOARD_KILLS);
}
if (hSteamAPICall != 0)
{
// set the function to call when this API call has completed
m_SteamCallResultFindLeaderboard.Set(hSteamAPICall, this, &SteamLeaderboard::OnFindLeaderboard);
loading = true;
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Called when SteamUserStats()->FindLeaderboard() returns asynchronously
//-----------------------------------------------------------------------------
void SteamLeaderboard::OnFindLeaderboard(LeaderboardFindResult_t *pFindLeaderboardResult, bool bIOFailure)
{
loading = false;
// see if we encountered an error during the call
if (!pFindLeaderboardResult->m_bLeaderboardFound || bIOFailure)
return;
// check to see which leaderboard handle we just retrieved
const char *pchName = SteamUserStats()->GetLeaderboardName(pFindLeaderboardResult->m_hSteamLeaderboard);
if (strcmp(pchName, LEADERBOARD_SCORE) == 0)
handle_score = pFindLeaderboardResult->m_hSteamLeaderboard;
else if (strcmp(pchName, LEADERBOARD_KILLS) == 0)
handle_kills = pFindLeaderboardResult->m_hSteamLeaderboard;
//Look up any other leaderboards
FindLeaderboards();
//if the user is currently looking at a leaderboard, it might be one we didn't have a handle for yet. Update the leaderboard.
//TODO: update display here
}
//-----------------------------------------------------------------------------
// Purpose: Called when SteamUserStats()->UploadLeaderboardScore() returns asynchronously
//-----------------------------------------------------------------------------
void SteamLeaderboard::OnUploadScore(LeaderboardScoreUploaded_t *pScoreUploadedResult, bool bIOFailure)
{
if (!pScoreUploadedResult->m_bSuccess)
{
//TODO: error
}
if (pScoreUploadedResult->m_bScoreChanged)
{
//TODO: could display new high score
}
}
void SteamLeaderboard::AddScore(int score, int kills)
{
// if the user is valid, update the leaderboards with their score and kills
if (SteamUserValid())
{
//If the user has a higher score already, this value will be thrown out
if (handle_score != 0)
{
SteamAPICall_t hSteamAPICall = SteamUserStats()->UploadLeaderboardScore(handle_score, k_ELeaderboardUploadScoreMethodKeepBest, score, NULL, 0);
m_SteamCallResultUploadScore.Set(hSteamAPICall, this, &SteamLeaderboard::OnUploadScore);
}
if (handle_kills != 0)
{
SteamAPICall_t hSteamAPICall = SteamUserStats()->UploadLeaderboardScore(handle_kills, k_ELeaderboardUploadScoreMethodKeepBest, kills, NULL, 0);
m_SteamCallResultUploadScore.Set(hSteamAPICall, this, &SteamLeaderboard::OnUploadScore);
}
}
}