2021-12-05 17:54:36 +00:00
|
|
|
#pragma once
|
|
|
|
#include <climits>
|
2023-04-13 07:45:03 +00:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-04-13 04:57:58 +00:00
|
|
|
#include "Singleton.h"
|
|
|
|
#include "dCommonVars.h"
|
2023-04-14 08:32:52 +00:00
|
|
|
#include "LDFFormat.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-04-13 04:57:58 +00:00
|
|
|
namespace RakNet{
|
|
|
|
class BitStream;
|
2021-12-05 17:54:36 +00:00
|
|
|
};
|
|
|
|
|
2023-04-13 04:57:58 +00:00
|
|
|
typedef uint32_t GameID;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
class Leaderboard {
|
|
|
|
public:
|
2023-04-13 04:57:58 +00:00
|
|
|
struct Entry {
|
2023-04-13 07:45:03 +00:00
|
|
|
LWOOBJID playerID;
|
2023-04-13 04:57:58 +00:00
|
|
|
uint32_t time;
|
|
|
|
uint32_t score;
|
|
|
|
uint32_t placement;
|
|
|
|
time_t lastPlayed;
|
|
|
|
std::string playerName;
|
|
|
|
};
|
|
|
|
typedef std::vector<Entry> LeaderboardEntries;
|
|
|
|
|
|
|
|
// Enums for leaderboards
|
|
|
|
enum InfoType : uint32_t {
|
|
|
|
Top, // Top 11 all time players
|
|
|
|
MyStanding, // Ranking of the current player
|
|
|
|
Friends // Ranking between friends
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Type : uint32_t {
|
|
|
|
ShootingGallery,
|
|
|
|
Racing,
|
|
|
|
MonumentRace,
|
|
|
|
FootRace,
|
2023-04-17 22:19:13 +00:00
|
|
|
UnusedLeaderboard4, // There is no 4 defined anywhere in the cdclient, but it takes a Score.
|
|
|
|
Survival,
|
2023-04-14 04:55:09 +00:00
|
|
|
SurvivalNS,
|
|
|
|
Donations,
|
2023-04-13 04:57:58 +00:00
|
|
|
None = UINT_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
Leaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, const Leaderboard::Type = None);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize the Leaderboard to a BitStream
|
|
|
|
*
|
|
|
|
* Expensive! Leaderboards are very string intensive so be wary of performatnce calling this method.
|
|
|
|
*/
|
|
|
|
void Serialize(RakNet::BitStream* bitStream) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Based on the associated gameID, return true if the score provided
|
|
|
|
* is better than the current entries' score
|
|
|
|
* @param score
|
|
|
|
* @return true
|
|
|
|
* @return false
|
|
|
|
*/
|
2023-04-13 07:45:03 +00:00
|
|
|
bool IsScoreBetter(const uint32_t score) const { return false; };
|
2023-04-13 04:57:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the leaderboard from the database based on the associated gameID
|
|
|
|
*/
|
|
|
|
void SetupLeaderboard();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the leaderboard to the client specified by targetID.
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void Send(LWOOBJID targetID) const;
|
2023-04-13 07:45:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new entry to the leaderboard
|
|
|
|
* Used for debug only!
|
|
|
|
*/
|
|
|
|
void AddEntry(Entry entry) { entries.push_back(entry); }
|
2021-12-05 17:54:36 +00:00
|
|
|
private:
|
2023-04-14 08:32:52 +00:00
|
|
|
template<class TypeToWrite>
|
|
|
|
inline void WriteLeaderboardRow(std::ostringstream& leaderboard, const uint32_t& index, const std::string& key, const eLDFType& ldfType, const TypeToWrite& value) const;
|
2023-04-13 04:57:58 +00:00
|
|
|
LeaderboardEntries entries;
|
2021-12-05 17:54:36 +00:00
|
|
|
LWOOBJID relatedPlayer;
|
2023-04-13 04:57:58 +00:00
|
|
|
GameID gameID;
|
|
|
|
InfoType infoType;
|
|
|
|
Leaderboard::Type leaderboardType;
|
2021-12-05 17:54:36 +00:00
|
|
|
bool weekly;
|
|
|
|
};
|
|
|
|
|
2023-04-13 04:57:58 +00:00
|
|
|
class LeaderboardManager: public Singleton<LeaderboardManager> {
|
|
|
|
typedef std::map<GameID, Leaderboard::Type> LeaderboardCache;
|
2021-12-05 17:54:36 +00:00
|
|
|
public:
|
2023-04-13 04:57:58 +00:00
|
|
|
void SendLeaderboard(GameID gameID, Leaderboard::InfoType infoType, bool weekly, LWOOBJID targetID,
|
2021-12-05 17:54:36 +00:00
|
|
|
LWOOBJID playerID = LWOOBJID_EMPTY);
|
2023-04-18 08:26:35 +00:00
|
|
|
/**
|
|
|
|
* @brief Public facing Score saving method. This method is simply a wrapper to ensure va_end is called properly.
|
|
|
|
*
|
|
|
|
* @param playerID The player whos score to save
|
|
|
|
* @param gameID The ID of the game which was played
|
|
|
|
* @param argumentCount The number of arguments in the va_list
|
|
|
|
* @param ...
|
|
|
|
*/
|
|
|
|
void SaveScore(const LWOOBJID& playerID, GameID gameID, Leaderboard::Type leaderboardType, uint32_t argumentCount, ...);
|
2021-12-05 17:54:36 +00:00
|
|
|
private:
|
2023-04-18 08:26:35 +00:00
|
|
|
void SaveScore(const LWOOBJID& playerID, GameID gameID, Leaderboard::Type leaderboardType, va_list args);
|
2023-04-13 04:57:58 +00:00
|
|
|
void GetLeaderboard(uint32_t gameID, Leaderboard::InfoType infoType, bool weekly, LWOOBJID playerID = LWOOBJID_EMPTY);
|
2023-04-18 08:26:35 +00:00
|
|
|
Leaderboard::Type GetLeaderboardType(const GameID gameID);
|
2023-04-13 04:57:58 +00:00
|
|
|
LeaderboardCache leaderboardCache;
|
2021-12-05 17:54:36 +00:00
|
|
|
};
|
|
|
|
|