Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaderboard improvements #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions native/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ bool CallbackHandler::UploadScore(const std::string& leaderboardId, int score, i
}


static std::string toLeaderboardScore(const char* leaderboardName, int score, int detail, int rank){
static std::string toLeaderboardScore(const char* leaderboardName, int score, int detail, int rank, std::string userName, int numEntries){
std::ostringstream data;
data << leaderboardName << "," << score << "," << detail << "," << rank;
data << leaderboardName << "," << score << "," << detail << "," << rank << "," << userName << "," << numEntries;
return data.str();
}

void CallbackHandler::OnScoreUploaded(LeaderboardScoreUploaded_t *pCallback, bool bIOFailure){
if (pCallback->m_bSuccess && !bIOFailure){
std::string leaderboardName = SteamUserStats()->GetLeaderboardName(pCallback->m_hSteamLeaderboard);
std::string data = toLeaderboardScore(SteamUserStats()->GetLeaderboardName(pCallback->m_hSteamLeaderboard), pCallback->m_nScore, -1, pCallback->m_nGlobalRankNew);
std::string data = toLeaderboardScore(SteamUserStats()->GetLeaderboardName(pCallback->m_hSteamLeaderboard), pCallback->m_nScore, -1, pCallback->m_nGlobalRankNew, "", 1);
SendEvent(ScoreUploaded, true, data.c_str());
}else if (pCallback != NULL && pCallback->m_hSteamLeaderboard != 0) {
SendEvent(ScoreUploaded, false, SteamUserStats()->GetLeaderboardName(pCallback->m_hSteamLeaderboard));
Expand Down Expand Up @@ -95,14 +95,17 @@ void CallbackHandler::OnScoreDownloaded(LeaderboardScoresDownloaded_t *pCallback

if (haveData)
data << ";";
data << toLeaderboardScore(leaderboardId.c_str(), entry.m_nScore, details[0], entry.m_nGlobalRank).c_str();

std::string userName = SteamFriends()->GetFriendPersonaName(entry.m_steamIDUser);

data << toLeaderboardScore(leaderboardId.c_str(), entry.m_nScore, details[0], entry.m_nGlobalRank, userName, numEntries).c_str();
haveData = true;
}

if (haveData)
SendEvent(ScoreDownloaded, true, data.str().c_str());
else
SendEvent(ScoreDownloaded, true, toLeaderboardScore(leaderboardId.c_str(), -1, -1, -1).c_str());
SendEvent(ScoreDownloaded, true, toLeaderboardScore(leaderboardId.c_str(), -1, -1, -1, "", 0).c_str());
}

void CallbackHandler::RequestGlobalStats(){
Expand Down
42 changes: 31 additions & 11 deletions steam/Api.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ private enum LeaderboardOp
{
FIND(id:String);
UPLOAD(score:LeaderboardScore);
DOWNLOAD(id:String);
DOWNLOAD(download:LeaderboardDownload);
}

enum abstract SteamNotificationPosition(Int) to Int
Expand Down Expand Up @@ -151,11 +151,11 @@ class Api
return active && report("clearAchievement", [id], _ClearAchievement(@:privateAccess id.toUtf8()));
}

public static function downloadLeaderboardScore(id:String):Bool {
public static function downloadLeaderboardScore(leaderboardDownload:LeaderboardDownload):Bool {
if (!active) return false;
var startProcessingNow = (leaderboardOps.length == 0);
findLeaderboardIfNecessary(id);
leaderboardOps.add(LeaderboardOp.DOWNLOAD(id));
findLeaderboardIfNecessary(leaderboardDownload.leaderboardId);
leaderboardOps.add(LeaderboardOp.DOWNLOAD(leaderboardDownload));
if (startProcessingNow) processNextLeaderboardOp();
return true;
}
Expand Down Expand Up @@ -445,8 +445,8 @@ class Api
case UPLOAD(score):
if (!report("Leaderboard.UPLOAD", [score.toString()], _UploadScore(@:privateAccess score.leaderboardId.toUtf8(), score.score, score.detail)))
processNextLeaderboardOp();
case DOWNLOAD(id):
if (!report("Leaderboard.DOWNLOAD", [id], _DownloadScores(@:privateAccess id.toUtf8(), 0, 0)))
case DOWNLOAD(lb):
if (!report("Leaderboard.DOWNLOAD", [lb.toString()], _DownloadScores(@:privateAccess lb.leaderboardId.toUtf8(), lb.before, lb.after)))
processNextLeaderboardOp();
}
}
Expand Down Expand Up @@ -495,7 +495,7 @@ class Api
if (success) {
var scores = data.split(";");
for (score in scores) {
var score = LeaderboardScore.fromString(data);
var score = LeaderboardScore.fromString(score);
if (score != null && whenLeaderboardScoreDownloaded != null) whenLeaderboardScoreDownloaded(score);
}
}
Expand Down Expand Up @@ -552,24 +552,44 @@ class LeaderboardScore {
public var score:Int;
public var detail:Int;
public var rank:Int;
public var userName:String;
public var entriesCount:Int;

public function new(leaderboardId_:String, score_:Int, detail_:Int, rank_:Int=-1) {
public function new(leaderboardId_:String, score_:Int, detail_:Int, rank_:Int=-1, userName_:String=null, entriesCount_:Int=0) {
leaderboardId = leaderboardId_;
score = score_;
detail = detail_;
rank = rank_;
userName = userName_;
entriesCount = entriesCount_;
}

public function toString():String {
return leaderboardId + "," + score + "," + detail + "," + rank;
return leaderboardId + "," + score + "," + detail + "," + rank + "," + userName + "," + entriesCount;
}

public static function fromString(str:String):LeaderboardScore {
var tokens = str.split(",");
if (tokens.length == 4)
return new LeaderboardScore(tokens[0], Util.str2Int(tokens[1]), Util.str2Int(tokens[2]), Util.str2Int(tokens[3]));
if (tokens.length == 6)
return new LeaderboardScore(tokens[0], Util.str2Int(tokens[1]), Util.str2Int(tokens[2]), Util.str2Int(tokens[3]), tokens[4], Util.str2Int(tokens[5]));
else
return null;
}
}

class LeaderboardDownload {
public var leaderboardId:String;
public var before:Int;
public var after:Int;

public function new(leaderboardId_:String, before_:Int = 0, after_:Int = 0) {
leaderboardId = leaderboardId_;
before = before_;
after = after_;
}

public function toString():String {
return leaderboardId + "," + before + "," + after;
}
}