This commit is contained in:
EmosewaMC
2023-05-30 04:11:37 -07:00
parent f26e66da4d
commit 0107d05d55
7 changed files with 123 additions and 175 deletions

View File

@@ -1,7 +1,8 @@
#pragma once
#include <climits>
#ifndef __LEADERBOARDMANAGER__H__
#define __LEADERBOARDMANAGER__H__
#include <map>
#include <memory>
#include <string_view>
#include <vector>
#include "Singleton.h"
@@ -62,7 +63,7 @@ public:
/**
* Builds the leaderboard from the database based on the associated gameID
*
*
* @param resultStart The index to start the leaderboard at. Zero indexed.
* @param resultEnd The index to end the leaderboard at. Zero indexed.
*/
@@ -74,9 +75,9 @@ public:
void Send(LWOOBJID targetID);
// Helper functions to get the columns, ordering and insert format for a leaderboard
static const std::string GetColumns(Type leaderboardType);
static const std::string GetInsertFormat(Type leaderboardType);
static const std::string GetOrdering(Type leaderboardType);
static const std::string_view GetColumns(Type leaderboardType);
static const std::string_view GetInsertFormat(Type leaderboardType);
static const std::string_view GetOrdering(Type leaderboardType);
private:
inline void WriteLeaderboardRow(std::ostringstream& leaderboard, const uint32_t& index, LDFBaseData* data);
@@ -98,9 +99,40 @@ private:
bool weekly;
};
class LeaderboardManager : public Singleton<LeaderboardManager> {
namespace LeaderboardManager {
class Score {
public:
Score() {
primaryScore = 0;
secondaryScore = 0;
tertiaryScore = 0;
}
Score(uint32_t primaryScore, uint32_t secondaryScore = 0, uint32_t tertiaryScore = 0) {
this->primaryScore = primaryScore;
this->secondaryScore = secondaryScore;
this->tertiaryScore = tertiaryScore;
}
bool operator<(const Score& rhs) const {
return primaryScore < rhs.primaryScore || (primaryScore == rhs.primaryScore && secondaryScore < rhs.secondaryScore) || (primaryScore == rhs.primaryScore && secondaryScore == rhs.secondaryScore && tertiaryScore < rhs.tertiaryScore);
}
bool operator>(const Score& rhs) const {
return primaryScore > rhs.primaryScore || (primaryScore == rhs.primaryScore && secondaryScore > rhs.secondaryScore) || (primaryScore == rhs.primaryScore && secondaryScore == rhs.secondaryScore && tertiaryScore > rhs.tertiaryScore);
}
void SetPrimaryScore(const uint32_t score) { primaryScore = score; }
uint32_t GetPrimaryScore() const { return primaryScore; }
void SetSecondaryScore(const uint32_t score) { secondaryScore = score; }
uint32_t GetSecondaryScore() const { return secondaryScore; }
void SetTertiaryScore(const uint32_t score) { tertiaryScore = score; }
uint32_t GetTertiaryScore() const { return tertiaryScore; }
private:
uint32_t primaryScore;
uint32_t secondaryScore;
uint32_t tertiaryScore;
};
using LeaderboardCache = std::map<GameID, Leaderboard::Type>;
public:
void SendLeaderboard(GameID gameID, Leaderboard::InfoType infoType, bool weekly, LWOOBJID playerID, LWOOBJID targetID, uint32_t resultStart = 0, uint32_t resultEnd = 10);
/**
@@ -111,12 +143,13 @@ public:
* @param argumentCount The number of arguments in the va_list
* @param ... The score to save
*/
void SaveScore(const LWOOBJID& playerID, GameID gameID, Leaderboard::Type leaderboardType, uint32_t argumentCount, ...);
void SaveScore(const LWOOBJID& playerID, GameID gameID, Leaderboard::Type leaderboardType, uint32_t primaryScore, uint32_t secondaryScore = 0, uint32_t tertiaryScore = 0);
static Leaderboard::Type GetLeaderboardType(const GameID gameID);
static LeaderboardCache leaderboardCache;
private:
void SaveScore(const LWOOBJID& playerID, GameID gameID, Leaderboard::Type leaderboardType, va_list args);
void GetLeaderboard(uint32_t gameID, Leaderboard::InfoType infoType, bool weekly, LWOOBJID playerID = LWOOBJID_EMPTY);
std::string FormatInsert(const Leaderboard::Type& type, const Score& score, const bool useUpdate);
Leaderboard::Type GetLeaderboardType(const GameID gameID);
extern LeaderboardCache leaderboardCache;
};
#endif //!__LEADERBOARDMANAGER__H__