mirror of
https://github.com/DarkflameUniverse/DarkflameServer
synced 2024-08-30 18:43:58 +00:00
The query is supposed to get SMALLER
Still better than 9 different queries all with 1 minor change i guess.
This commit is contained in:
parent
b5e3bd22e3
commit
a3626a3b53
@ -16,19 +16,23 @@
|
|||||||
#include "CDActivitiesTable.h"
|
#include "CDActivitiesTable.h"
|
||||||
#include "Metrics.hpp"
|
#include "Metrics.hpp"
|
||||||
|
|
||||||
Leaderboard::Leaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, const Leaderboard::Type leaderboardType) {
|
Leaderboard::Leaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, LWOOBJID relatedPlayer, const Leaderboard::Type leaderboardType) {
|
||||||
this->gameID = gameID;
|
this->gameID = gameID;
|
||||||
this->weekly = weekly;
|
this->weekly = weekly;
|
||||||
this->infoType = infoType;
|
this->infoType = infoType;
|
||||||
this->leaderboardType = leaderboardType;
|
this->leaderboardType = leaderboardType;
|
||||||
|
this->relatedPlayer = relatedPlayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class TypeToWrite>
|
Leaderboard::~Leaderboard() {
|
||||||
void Leaderboard::WriteLeaderboardRow(std::ostringstream& leaderboard, const uint32_t& index, const std::string& key, const eLDFType& ldfType, const TypeToWrite& value) const {
|
for (auto& entry : entries) for (auto data : entry) delete data;
|
||||||
leaderboard << "Result[0].Row[" << index << "]." << key << '=' << ldfType << ':' << value << '\n';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Leaderboard::Serialize(RakNet::BitStream* bitStream) const {
|
void Leaderboard::WriteLeaderboardRow(std::ostringstream& leaderboard, const uint32_t& index, LDFBaseData* data) {
|
||||||
|
leaderboard << "Result[0].Row[" << index << "]." << data->GetString() << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
void Leaderboard::Serialize(RakNet::BitStream* bitStream) {
|
||||||
std::ostringstream leaderboard;
|
std::ostringstream leaderboard;
|
||||||
|
|
||||||
leaderboard << "ADO.Result=7:1\n"; // Unused in 1.10.64, but is in captures
|
leaderboard << "ADO.Result=7:1\n"; // Unused in 1.10.64, but is in captures
|
||||||
@ -36,54 +40,235 @@ void Leaderboard::Serialize(RakNet::BitStream* bitStream) const {
|
|||||||
leaderboard << "Result[0].Index=0:RowNumber\n"; // "Primary key"
|
leaderboard << "Result[0].Index=0:RowNumber\n"; // "Primary key"
|
||||||
leaderboard << "Result[0].RowCount=1:" << entries.size() << '\n'; // number of rows
|
leaderboard << "Result[0].RowCount=1:" << entries.size() << '\n'; // number of rows
|
||||||
|
|
||||||
auto index = 0;
|
int32_t index = 0;
|
||||||
for (const auto& entry : entries) {
|
for (auto& entry : entries) {
|
||||||
// Each minigame has its own "points" system
|
for (auto data : entry) {
|
||||||
|
WriteLeaderboardRow(leaderboard, index, data);
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize the thing to a BitStream
|
||||||
|
bitStream->Write(leaderboard.str().c_str(), leaderboard.tellp());
|
||||||
|
}
|
||||||
|
|
||||||
|
#define MAX_QUERY_LENGTH 1526
|
||||||
|
|
||||||
|
void Leaderboard::SetupLeaderboard() {
|
||||||
|
bool isTopQuery = this->infoType == InfoType::Top;
|
||||||
|
bool isMyStandingQuery = this->infoType == InfoType::MyStanding;
|
||||||
|
bool isFriendsQuery = this->infoType == InfoType::Friends;
|
||||||
|
std::string baseLookupStr;
|
||||||
|
|
||||||
|
if (!isTopQuery) {
|
||||||
|
baseLookupStr = "SELECT id FROM leaderboard WHERE game_id = ? AND character_id = ? LIMIT 1";
|
||||||
|
} else {
|
||||||
|
baseLookupStr = "SELECT id FROM leaderboard WHERE game_id = ? ORDER BY %s LIMIT 1";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string queryBase =
|
||||||
|
" \
|
||||||
|
WITH leaderboardsRanked AS ( \
|
||||||
|
SELECT leaderboard.*, charinfo.name, \
|
||||||
|
RANK() OVER \
|
||||||
|
( \
|
||||||
|
ORDER BY %s \
|
||||||
|
) AS ranking \
|
||||||
|
FROM leaderboard JOIN charinfo on charinfo.id = leaderboard.character_id \
|
||||||
|
WHERE game_id = ? %s \
|
||||||
|
), \
|
||||||
|
myStanding AS ( \
|
||||||
|
SELECT \
|
||||||
|
ranking as myRank \
|
||||||
|
FROM leaderboardsRanked \
|
||||||
|
WHERE id = ? \
|
||||||
|
), \
|
||||||
|
lowestRanking AS ( \
|
||||||
|
SELECT MAX(ranking) AS lowestRank \
|
||||||
|
FROM leaderboardsRanked \
|
||||||
|
) \
|
||||||
|
SELECT %s, character_id, UNIX_TIMESTAMP(last_played) as lastPlayed, leaderboardsRanked.name FROM leaderboardsRanked, myStanding, lowestRanking \
|
||||||
|
WHERE leaderboardsRanked.ranking \
|
||||||
|
BETWEEN \
|
||||||
|
LEAST(GREATEST(myRank - 5, 1), lowestRanking.lowestRank - 10) \
|
||||||
|
AND \
|
||||||
|
LEAST(GREATEST(myRank + 5, 11), lowestRanking.lowestRank) \
|
||||||
|
ORDER BY ranking ASC;";
|
||||||
|
// Setup query based on activity.
|
||||||
|
// Where clause will vary based on what query we are doing
|
||||||
|
// Get base based on InfoType
|
||||||
|
// Fill in base with arguments based on leaderboard type
|
||||||
|
// If this is a friends query we need to join another table and add even more to the where clause.
|
||||||
|
|
||||||
|
const char* friendsQuery =
|
||||||
|
" AND (character_id IN (SELECT fr.requested_player FROM (SELECT CASE "
|
||||||
|
"WHEN player_id = ? THEN friend_id "
|
||||||
|
"WHEN friend_id = ? THEN player_id "
|
||||||
|
"END AS requested_player FROM friends) AS fr "
|
||||||
|
"JOIN charinfo AS ci ON ci.id = fr.requested_player "
|
||||||
|
"WHERE fr.requested_player IS NOT NULL) OR character_id = ?) ";
|
||||||
|
|
||||||
|
char baseStandingBuffer[1024];
|
||||||
|
char lookupBuffer[MAX_QUERY_LENGTH];
|
||||||
|
|
||||||
|
switch (leaderboardType) {
|
||||||
|
case Type::ShootingGallery: {
|
||||||
|
const char* orderBase = "score DESC, streak DESC, hitPercentage DESC";
|
||||||
|
const char* selectBase = "hitPercentage, score, streak";
|
||||||
|
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), orderBase, selectBase);
|
||||||
|
if (isFriendsQuery) snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), orderBase, friendsQuery, selectBase);
|
||||||
|
else snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), orderBase, "", selectBase);
|
||||||
|
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), orderBase);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Type::Racing:
|
||||||
|
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "bestTime ASC, bestLapTime ASC, numWins DESC", "bestLapTime, bestTime, numWins");
|
||||||
|
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "bestTime ASC, bestLapTime ASC, numWins DESC");
|
||||||
|
break;
|
||||||
|
case Type::UnusedLeaderboard4:
|
||||||
|
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "score DESC", "score");
|
||||||
|
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "score DESC");
|
||||||
|
break;
|
||||||
|
case Type::MonumentRace:
|
||||||
|
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "bestTime ASC", "bestTime");
|
||||||
|
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "bestTime ASC");
|
||||||
|
break;
|
||||||
|
case Type::FootRace:
|
||||||
|
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "bestTime DESC", "bestTime");
|
||||||
|
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "bestTime DESC");
|
||||||
|
break;
|
||||||
|
case Type::Survival:
|
||||||
|
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "score DESC, bestTime DESC", "score, bestTime");
|
||||||
|
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "score DESC, bestTime DESC");
|
||||||
|
// If the config option default_survival_scoring is 1, reverse the order of the points and time columns
|
||||||
|
break;
|
||||||
|
case Type::SurvivalNS:
|
||||||
|
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "bestTime DESC, score DESC", "bestTime, score");
|
||||||
|
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "bestTime DESC, score DESC");
|
||||||
|
break;
|
||||||
|
case Type::Donations:
|
||||||
|
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "score DESC", "score");
|
||||||
|
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "score DESC");
|
||||||
|
break;
|
||||||
|
case Type::None:
|
||||||
|
Game::logger->Log("LeaderboardManager", "Attempting to get leaderboard for type none. Is this intended?");
|
||||||
|
// This type is included here simply to resolve a compiler warning on mac about unused enum types
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Game::logger->Log("LeaderboardManager", "lookup query is %s", (!isTopQuery) ? baseLookupStr.c_str() : baseStandingBuffer);
|
||||||
|
std::unique_ptr<sql::PreparedStatement> baseQuery(Database::CreatePreppedStmt((!isTopQuery) ? baseLookupStr : baseStandingBuffer));
|
||||||
|
baseQuery->setInt(1, this->gameID);
|
||||||
|
if (!isTopQuery) {
|
||||||
|
baseQuery->setInt(2, this->relatedPlayer);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<sql::ResultSet> baseResult(baseQuery->executeQuery());
|
||||||
|
if (baseResult->rowsCount() == 0) return;
|
||||||
|
baseResult->next();
|
||||||
|
// Get the ID of the row fetched.
|
||||||
|
uint32_t relatedPlayerLeaderboardId = baseResult->getInt("id");
|
||||||
|
|
||||||
|
// create and execute query here
|
||||||
|
Game::logger->Log("LeaderboardManager", "filled in query is %s %i %i %i", lookupBuffer, this->gameID, this->relatedPlayer, relatedPlayerLeaderboardId);
|
||||||
|
std::unique_ptr<sql::PreparedStatement> query(Database::CreatePreppedStmt(lookupBuffer));
|
||||||
|
query->setInt(1, this->gameID);
|
||||||
|
if (isFriendsQuery) {
|
||||||
|
query->setInt(2, this->relatedPlayer);
|
||||||
|
query->setInt(3, this->relatedPlayer);
|
||||||
|
query->setInt(4, this->relatedPlayer);
|
||||||
|
query->setInt(5, relatedPlayerLeaderboardId);
|
||||||
|
} else {
|
||||||
|
query->setInt(2, relatedPlayerLeaderboardId);
|
||||||
|
}
|
||||||
|
std::unique_ptr<sql::ResultSet> result(query->executeQuery());
|
||||||
|
|
||||||
|
if (result->rowsCount() == 0) return;
|
||||||
|
|
||||||
|
uint32_t myRanking = 1;
|
||||||
|
uint32_t myCharacterId = 0;
|
||||||
|
int32_t lowestRanking = result->rowsCount() - 5;
|
||||||
|
uint32_t startRanking = 1; // Default to top 11
|
||||||
|
if (this->infoType == InfoType::MyStanding) {
|
||||||
|
// Find my ranking in the leaderboard
|
||||||
|
while (result->next()) {
|
||||||
|
if (result->getInt("character_id") != myCharacterId) myRanking++;
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
// Once you've found my ranking, figure out if we need to adjust the
|
||||||
|
// row pointer to get the top 11 or the bottom 11.
|
||||||
|
|
||||||
|
if (lowestRanking > 0 && myRanking >= lowestRanking) { // I am in the bottom 10, so set row pointer back to the top of the bottom 6
|
||||||
|
for (uint32_t i = myRanking - lowestRanking; i > lowestRanking; i--) {
|
||||||
|
result->previous();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (myRanking >= 6) startRanking = myRanking - 5; // If i am not in the top 5, set row pointer to 5 above my ranking
|
||||||
|
else if (myRanking > result->rowsCount()) { // If i am in the bottom 10, set the row pointer to the top of the bottom 11
|
||||||
|
startRanking = result->rowsCount() - 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint32_t i = myRanking - 5; i > 0; i--) { // Adjust first row gotten to be 5 above my ranking.
|
||||||
|
result->previous();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this->entries.reserve(11);
|
||||||
|
for (uint32_t i = 0; i < 11 && result->next(); i++) {
|
||||||
|
constexpr int32_t MAX_NUM_DATA_PER_ROW = 9;
|
||||||
|
this->entries.push_back(std::vector<LDFBaseData*>());
|
||||||
|
auto& entry = this->entries.back();
|
||||||
|
entry.reserve(MAX_NUM_DATA_PER_ROW);
|
||||||
|
entry.push_back(new LDFData<uint64_t>(u"CharacterID", result->getInt("character_id")));
|
||||||
|
entry.push_back(new LDFData<uint64_t>(u"LastPlayed", result->getUInt64("lastPlayed")));
|
||||||
|
entry.push_back(new LDFData<int32_t>(u"NumPlayed", 1));
|
||||||
|
entry.push_back(new LDFData<std::u16string>(u"name", GeneralUtils::ASCIIToUTF16(result->getString("name").c_str())));
|
||||||
|
entry.push_back(new LDFData<int32_t>(u"RowNumber", startRanking + i));
|
||||||
switch (leaderboardType) {
|
switch (leaderboardType) {
|
||||||
case Type::ShootingGallery:
|
case Type::ShootingGallery:
|
||||||
WriteLeaderboardRow(leaderboard, index, "HitPercentage", eLDFType::LDF_TYPE_FLOAT, 0.0f);
|
entry.push_back(new LDFData<float>(u"HitPercentage", result->getDouble("hitPercentage")));
|
||||||
// HitPercentage:3 between 0 and 1
|
// HitPercentage:3 between 0 and 1
|
||||||
WriteLeaderboardRow(leaderboard, index, "Score", eLDFType::LDF_TYPE_S32, entry.score);
|
entry.push_back(new LDFData<int32_t>(u"Score", result->getInt("score")));
|
||||||
// Score:1
|
// Score:1
|
||||||
WriteLeaderboardRow(leaderboard, index, "Streak", eLDFType::LDF_TYPE_S32, 0);
|
entry.push_back(new LDFData<int32_t>(u"Streak", result->getInt("streak")));
|
||||||
// Streak:1
|
// Streak:1
|
||||||
break;
|
break;
|
||||||
case Type::Racing:
|
case Type::Racing:
|
||||||
WriteLeaderboardRow(leaderboard, index, "BestLapTime", eLDFType::LDF_TYPE_FLOAT, 0.0f);
|
entry.push_back(new LDFData<float>(u"BestLapTime", result->getDouble("bestLapTime")));
|
||||||
// BestLapTime:3
|
// BestLapTime:3
|
||||||
WriteLeaderboardRow(leaderboard, index, "BestTime", eLDFType::LDF_TYPE_FLOAT, 0.0f);
|
entry.push_back(new LDFData<float>(u"BestTime", result->getDouble("bestTime")));
|
||||||
// BestTime:3
|
// BestTime:3
|
||||||
WriteLeaderboardRow(leaderboard, index, "License", eLDFType::LDF_TYPE_S32, 0);
|
entry.push_back(new LDFData<int32_t>(u"License", 1));
|
||||||
// License:1 - 1 if player has completed mission 637 and 0 otherwise
|
// License:1 - 1 if player has completed mission 637 and 0 otherwise
|
||||||
WriteLeaderboardRow(leaderboard, index, "NumWins", eLDFType::LDF_TYPE_S32, 0);
|
entry.push_back(new LDFData<int32_t>(u"NumWins", result->getInt("numWins")));
|
||||||
// NumWins:1
|
// NumWins:1
|
||||||
break;
|
break;
|
||||||
case Type::UnusedLeaderboard4:
|
case Type::UnusedLeaderboard4:
|
||||||
WriteLeaderboardRow(leaderboard, index, "Points", eLDFType::LDF_TYPE_S32, entry.score);
|
entry.push_back(new LDFData<int32_t>(u"Score", result->getInt("score")));
|
||||||
// Points:1
|
// Points:1
|
||||||
break;
|
break;
|
||||||
case Type::MonumentRace:
|
case Type::MonumentRace:
|
||||||
WriteLeaderboardRow(leaderboard, index, "Time", eLDFType::LDF_TYPE_S32, entry.time);
|
entry.push_back(new LDFData<int32_t>(u"Time", result->getInt("bestTime")));
|
||||||
// Time:1(?)
|
// Time:1(?)
|
||||||
break;
|
break;
|
||||||
case Type::FootRace:
|
case Type::FootRace:
|
||||||
WriteLeaderboardRow(leaderboard, index, "Time", eLDFType::LDF_TYPE_S32, entry.time);
|
entry.push_back(new LDFData<int32_t>(u"Time", result->getInt("bestTime")));
|
||||||
// Time:1
|
// Time:1
|
||||||
break;
|
break;
|
||||||
case Type::Survival:
|
case Type::Survival:
|
||||||
WriteLeaderboardRow(leaderboard, index, "Points", eLDFType::LDF_TYPE_S32, entry.score);
|
entry.push_back(new LDFData<int32_t>(u"Score", result->getInt("score")));
|
||||||
// Points:1
|
// Points:1
|
||||||
WriteLeaderboardRow(leaderboard, index, "Time", eLDFType::LDF_TYPE_S32, entry.time);
|
entry.push_back(new LDFData<int32_t>(u"Time", result->getInt("bestTime")));
|
||||||
// Time:1
|
// Time:1
|
||||||
break;
|
break;
|
||||||
case Type::SurvivalNS:
|
case Type::SurvivalNS:
|
||||||
WriteLeaderboardRow(leaderboard, index, "Time", eLDFType::LDF_TYPE_S32, entry.time);
|
entry.push_back(new LDFData<int32_t>(u"Time", result->getInt("bestTime")));
|
||||||
// Time:1
|
// Time:1
|
||||||
WriteLeaderboardRow(leaderboard, index, "Wave", eLDFType::LDF_TYPE_S32, entry.score);
|
entry.push_back(new LDFData<int32_t>(u"Score", result->getInt("score")));
|
||||||
// Wave:1
|
// Wave:1
|
||||||
break;
|
break;
|
||||||
case Type::Donations:
|
case Type::Donations:
|
||||||
WriteLeaderboardRow(leaderboard, index, "Score", eLDFType::LDF_TYPE_S32, entry.score);
|
entry.push_back(new LDFData<int32_t>(u"Score", result->getInt("score")));
|
||||||
// Score:1
|
// Score:1
|
||||||
// Something? idk yet.
|
// Something? idk yet.
|
||||||
break;
|
break;
|
||||||
@ -93,97 +278,12 @@ void Leaderboard::Serialize(RakNet::BitStream* bitStream) const {
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
index++;
|
|
||||||
}
|
}
|
||||||
|
for (auto& entry : entries) {
|
||||||
// Serialize the thing to a BitStream
|
for (auto data : entry) {
|
||||||
bitStream->Write(leaderboard.str().c_str(), leaderboard.tellp());
|
Game::logger->Log("LeaderboardManager", "entry is %s", data->GetString().c_str());
|
||||||
}
|
|
||||||
|
|
||||||
void Leaderboard::SetupLeaderboard() {
|
|
||||||
std::string queryBase =
|
|
||||||
"SELECT %s, character_id, UNIX_TIMESTAMP(last_played), charinfo.name as lastPlayed"
|
|
||||||
"FROM leaderboard JOIN charinfo"
|
|
||||||
"ON charinfo.id = leaderboard.character_id"
|
|
||||||
"WHERE game_id = ?"
|
|
||||||
"ORDER BY %s";
|
|
||||||
// Setup query based on activity.
|
|
||||||
// Where clause will vary based on what query we are doing
|
|
||||||
// Get base based on InfoType
|
|
||||||
// Fill in base with arguments based on leaderboard type
|
|
||||||
char queryBuffer[1024];
|
|
||||||
switch (leaderboardType) {
|
|
||||||
case Type::ShootingGallery:
|
|
||||||
snprintf(queryBuffer, 1024, queryBase.c_str(), "hitPercentage, score, streak", "score DESC, streak DESC, hitPercentage DESC");
|
|
||||||
break;
|
|
||||||
case Type::Racing:
|
|
||||||
snprintf(queryBuffer, 1024, queryBase.c_str(), "bestLapTime, bestTime, numWins", "bestTime ASC, bestLapTime ASC, numWins DESC");
|
|
||||||
break;
|
|
||||||
case Type::UnusedLeaderboard4:
|
|
||||||
snprintf(queryBuffer, 1024, queryBase.c_str(), "points", "points DESC");
|
|
||||||
break;
|
|
||||||
case Type::MonumentRace:
|
|
||||||
snprintf(queryBuffer, 1024, queryBase.c_str(), "time", "time ASC");
|
|
||||||
break;
|
|
||||||
case Type::FootRace:
|
|
||||||
snprintf(queryBuffer, 1024, queryBase.c_str(), "time", "time DESC");
|
|
||||||
break;
|
|
||||||
case Type::Survival:
|
|
||||||
snprintf(queryBuffer, 1024, queryBase.c_str(), "points, time", "points DESC, time DESC");
|
|
||||||
// If the config option default_survival_scoring is 1, reverse the order of the points and time columns
|
|
||||||
break;
|
|
||||||
case Type::SurvivalNS:
|
|
||||||
snprintf(queryBuffer, 1024, queryBase.c_str(), "time, wave", "time DESC, wave DESC");
|
|
||||||
break;
|
|
||||||
case Type::Donations:
|
|
||||||
snprintf(queryBuffer, 1024, queryBase.c_str(), "score", "score DESC");
|
|
||||||
break;
|
|
||||||
case Type::None:
|
|
||||||
Game::logger->Log("LeaderboardManager", "Attempting to get leaderboard for type none. Is this intended?");
|
|
||||||
// This type is included here simply to resolve a compiler warning on mac about unused enum types
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Game::logger->Log("LeaderboardManager", "filled in query is %s", queryBuffer);
|
|
||||||
// create and execute query here
|
|
||||||
std::unique_ptr<sql::PreparedStatement> query(Database::CreatePreppedStmt(queryBuffer));
|
|
||||||
query->setInt(1, this->gameID);
|
|
||||||
std::unique_ptr<sql::ResultSet> result(query->executeQuery());
|
|
||||||
if (result->rowsCount() == 0) return;
|
|
||||||
|
|
||||||
uint32_t myRanking = 1;
|
|
||||||
uint32_t myCharacterId = 0;
|
|
||||||
// Find my ranking in the leaderboard
|
|
||||||
while (result->next()) {
|
|
||||||
if (result->getInt("character_id") != myCharacterId) myRanking++;
|
|
||||||
else break;
|
|
||||||
}
|
|
||||||
// Once you've found my ranking, figure out if we need to adjust the
|
|
||||||
// row pointer to get the top 11 or the bottom 11.
|
|
||||||
|
|
||||||
int32_t lowestRanking = result->rowsCount() - 5;
|
|
||||||
if (lowestRanking > 0 && myRanking >= lowestRanking) { // I am in the bottom 10, so set row pointer back to the top of the bottom 6
|
|
||||||
for (uint32_t i = myRanking - lowestRanking; i > lowestRanking; i--) {
|
|
||||||
result->previous();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t startRanking = 1; // Default to top 11
|
|
||||||
if (myRanking >= 6) startRanking = myRanking - 5; // If i am not in the top 5, set row pointer to 5 above my ranking
|
|
||||||
else if (myRanking > result->rowsCount()) { // If i am in the bottom 10, set the row pointer to the top of the bottom 11
|
|
||||||
startRanking = result->rowsCount() - 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (uint32_t i = myRanking - 5; i > 0; i--) { // Adjust first row gotten to be 5 above my ranking.
|
|
||||||
result->previous();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (uint32_t i = 11; i > 0; i--) {
|
|
||||||
this->entries.push_back(LDFData<uint64_t>(u"CharacterID", result->getInt("character_id")));
|
|
||||||
this->entries.push_back(LDFData<uint64_t>(u"LastPlayed", result->getUInt64("lastPlayed")));
|
|
||||||
this->entries.push_back(LDFData<int32_t>(u"NumPlayed", 1));
|
|
||||||
this->entries.push_back(LDFData<std::u16string>(u"name", GeneralUtils::ASCIIToUTF16(result->getString("name").c_str())));
|
|
||||||
this->entries.push_back(LDFData<int32_t>(u"RowNumber", startRanking + i));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Leaderboard::Send(LWOOBJID targetID) const {
|
void Leaderboard::Send(LWOOBJID targetID) const {
|
||||||
@ -271,7 +371,7 @@ void LeaderboardManager::SaveScore(const LWOOBJID& playerID, GameID gameID, Lead
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
Game::logger->Log("LeaderboardManager", "Unknown leaderboard type %i. Cannot save score!", leaderboardType);
|
Game::logger->Log("LeaderboardManager", "Unknown leaderboard type %i. Cannot save score!", leaderboardType);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -279,8 +379,8 @@ void LeaderboardManager::SaveScore(const LWOOBJID& playerID, GameID gameID, Lead
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LeaderboardManager::SendLeaderboard(uint32_t gameID, Leaderboard::InfoType infoType, bool weekly, LWOOBJID targetID, LWOOBJID playerID) {
|
void LeaderboardManager::SendLeaderboard(uint32_t gameID, Leaderboard::InfoType infoType, bool weekly, LWOOBJID targetID, LWOOBJID playerID) {
|
||||||
// Create the leaderboard here and then send it right after. On the stack.
|
// Create the leaderboard here and then send it right after. On the stack.
|
||||||
Leaderboard leaderboard(gameID, infoType, weekly, GetLeaderboardType(gameID));
|
Leaderboard leaderboard(gameID, infoType, weekly, playerID, GetLeaderboardType(gameID));
|
||||||
leaderboard.SetupLeaderboard();
|
leaderboard.SetupLeaderboard();
|
||||||
leaderboard.Send(targetID);
|
leaderboard.Send(targetID);
|
||||||
}
|
}
|
||||||
|
@ -15,15 +15,8 @@ typedef uint32_t GameID;
|
|||||||
|
|
||||||
class Leaderboard {
|
class Leaderboard {
|
||||||
public:
|
public:
|
||||||
// struct Entry {
|
using LeaderboardEntry = std::vector<LDFBaseData*>;
|
||||||
// LWOOBJID playerID;
|
using LeaderboardEntries = std::vector<LeaderboardEntry>;
|
||||||
// uint32_t time;
|
|
||||||
// uint32_t score;
|
|
||||||
// uint32_t placement;
|
|
||||||
// time_t lastPlayed;
|
|
||||||
// std::string playerName;
|
|
||||||
// };
|
|
||||||
typedef std::vector<LDFBaseData> LeaderboardEntries;
|
|
||||||
|
|
||||||
// Enums for leaderboards
|
// Enums for leaderboards
|
||||||
enum InfoType : uint32_t {
|
enum InfoType : uint32_t {
|
||||||
@ -44,14 +37,16 @@ public:
|
|||||||
None = UINT_MAX
|
None = UINT_MAX
|
||||||
};
|
};
|
||||||
|
|
||||||
Leaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, const Leaderboard::Type = None);
|
Leaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, LWOOBJID relatedPlayer, const Leaderboard::Type = None);
|
||||||
|
|
||||||
|
~Leaderboard();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serialize the Leaderboard to a BitStream
|
* Serialize the Leaderboard to a BitStream
|
||||||
*
|
*
|
||||||
* Expensive! Leaderboards are very string intensive so be wary of performatnce calling this method.
|
* Expensive! Leaderboards are very string intensive so be wary of performatnce calling this method.
|
||||||
*/
|
*/
|
||||||
void Serialize(RakNet::BitStream* bitStream) const;
|
void Serialize(RakNet::BitStream* bitStream);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Based on the associated gameID, return true if the score provided
|
* Based on the associated gameID, return true if the score provided
|
||||||
@ -71,15 +66,8 @@ public:
|
|||||||
* Sends the leaderboard to the client specified by targetID.
|
* Sends the leaderboard to the client specified by targetID.
|
||||||
*/
|
*/
|
||||||
void Send(LWOOBJID targetID) const;
|
void Send(LWOOBJID targetID) const;
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a new entry to the leaderboard
|
|
||||||
* Used for debug only!
|
|
||||||
*/
|
|
||||||
void AddEntry(LDFBaseData& entry) { entries.push_back(entry); }
|
|
||||||
private:
|
private:
|
||||||
template<class TypeToWrite>
|
inline void WriteLeaderboardRow(std::ostringstream& leaderboard, const uint32_t& index, LDFBaseData* data);
|
||||||
inline void WriteLeaderboardRow(std::ostringstream& leaderboard, const uint32_t& index, const std::string& key, const eLDFType& ldfType, const TypeToWrite& value) const;
|
|
||||||
LeaderboardEntries entries;
|
LeaderboardEntries entries;
|
||||||
LWOOBJID relatedPlayer;
|
LWOOBJID relatedPlayer;
|
||||||
GameID gameID;
|
GameID gameID;
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include "dServer.h"
|
#include "dServer.h"
|
||||||
#include "EntityInfo.h"
|
#include "EntityInfo.h"
|
||||||
#include "EntityManager.h"
|
#include "EntityManager.h"
|
||||||
|
#include "Database.h"
|
||||||
#include "dConfig.h"
|
#include "dConfig.h"
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
@ -32,6 +33,7 @@ protected:
|
|||||||
Game::logger = new dLogger("./testing.log", true, true);
|
Game::logger = new dLogger("./testing.log", true, true);
|
||||||
Game::server = new dServerMock();
|
Game::server = new dServerMock();
|
||||||
Game::config = new dConfig("worldconfig.ini");
|
Game::config = new dConfig("worldconfig.ini");
|
||||||
|
Database::Connect(Game::config->GetValue("mysql_host"), Game::config->GetValue("mysql_database"), Game::config->GetValue("mysql_username"), Game::config->GetValue("mysql_password"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TearDownDependencies() {
|
void TearDownDependencies() {
|
||||||
|
@ -15,14 +15,7 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TestLeaderboard(Leaderboard& leaderboard, int32_t entries) {
|
void TestLeaderboard(Leaderboard& leaderboard, int32_t entries) {
|
||||||
Leaderboard::Entry entry;
|
bitStream.Reset();
|
||||||
entry.playerID = UINT64_MAX;
|
|
||||||
entry.time = 100;
|
|
||||||
entry.score = 100;
|
|
||||||
entry.placement = 1;
|
|
||||||
entry.lastPlayed = 0;
|
|
||||||
entry.playerName = "TestThreeWords";
|
|
||||||
for (int32_t i = 0; i < entries; i++) leaderboard.AddEntry(entry);
|
|
||||||
Metrics::StartMeasurement(MetricVariable::Leaderboard);
|
Metrics::StartMeasurement(MetricVariable::Leaderboard);
|
||||||
for (int32_t i = 0; i < MAX_MEASURMENT_POINTS; i++) leaderboard.Serialize(&bitStream);
|
for (int32_t i = 0; i < MAX_MEASURMENT_POINTS; i++) leaderboard.Serialize(&bitStream);
|
||||||
Metrics::EndMeasurement(MetricVariable::Leaderboard);
|
Metrics::EndMeasurement(MetricVariable::Leaderboard);
|
||||||
@ -32,14 +25,15 @@ protected:
|
|||||||
bitStream.Reset();
|
bitStream.Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RunTests(Leaderboard::Type type) {
|
void RunTests(uint32_t gameID, Leaderboard::Type type, Leaderboard::InfoType infoType) {
|
||||||
Game::logger->Log("LeaderboardTests", "Testing leaderboard %i for Serialize speed", type);
|
Game::logger->Log("LeaderboardTests", "Testing leaderboard %i for Serialize speed", infoType);
|
||||||
Leaderboard leaderboard(0, Leaderboard::InfoType::Top, false, type);
|
Leaderboard leaderboard(gameID, infoType, false, 14231, type);
|
||||||
leaderboard.SetupLeaderboard();
|
leaderboard.SetupLeaderboard();
|
||||||
// TestLeaderboard(leaderboard, 1);
|
leaderboard.Serialize(&bitStream);
|
||||||
// TestLeaderboard(leaderboard, 10);
|
TestLeaderboard(leaderboard, 1);
|
||||||
// TestLeaderboard(leaderboard, 100);
|
TestLeaderboard(leaderboard, 10);
|
||||||
// TestLeaderboard(leaderboard, 1000);
|
TestLeaderboard(leaderboard, 100);
|
||||||
|
TestLeaderboard(leaderboard, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
CBITSTREAM;
|
CBITSTREAM;
|
||||||
@ -78,20 +72,22 @@ protected:
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
TEST_F(LeaderboardTests, LeaderboardSpeedTest) {
|
TEST_F(LeaderboardTests, LeaderboardSpeedTest) {
|
||||||
RunTests(Leaderboard::Type::ShootingGallery);
|
RunTests(1864, Leaderboard::Type::ShootingGallery , Leaderboard::InfoType::Top);
|
||||||
|
RunTests(1864, Leaderboard::Type::ShootingGallery, Leaderboard::InfoType::MyStanding);
|
||||||
|
RunTests(1864, Leaderboard::Type::ShootingGallery, Leaderboard::InfoType::Friends);
|
||||||
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::ShootingGallery, 3, 3000, 15.0f, 100);
|
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::ShootingGallery, 3, 3000, 15.0f, 100);
|
||||||
RunTests(Leaderboard::Type::Racing);
|
// RunTests(0, Leaderboard::Type::Racing);
|
||||||
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::Racing, 2, 260.0f, 250.0f);
|
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::Racing, 2, 260.0f, 250.0f);
|
||||||
RunTests(Leaderboard::Type::MonumentRace);
|
// RunTests(0, Leaderboard::Type::MonumentRace);
|
||||||
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::MonumentRace, 1, 150);
|
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::MonumentRace, 1, 150);
|
||||||
RunTests(Leaderboard::Type::FootRace);
|
// RunTests(0, Leaderboard::Type::FootRace);
|
||||||
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::FootRace, 1, 150);
|
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::FootRace, 1, 150);
|
||||||
RunTests(Leaderboard::Type::UnusedLeaderboard4);
|
// RunTests(0, Leaderboard::Type::UnusedLeaderboard4);
|
||||||
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::UnusedLeaderboard4, 1, 100);
|
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::UnusedLeaderboard4, 1, 100);
|
||||||
RunTests(Leaderboard::Type::Survival);
|
// RunTests(0, Leaderboard::Type::Survival);
|
||||||
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::Survival, 2, 3000, 15);
|
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::Survival, 2, 3000, 15);
|
||||||
RunTests(Leaderboard::Type::SurvivalNS);
|
// RunTests(0, Leaderboard::Type::SurvivalNS);
|
||||||
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::SurvivalNS, 2, 300, 15);
|
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::SurvivalNS, 2, 300, 15);
|
||||||
RunTests(Leaderboard::Type::Donations);
|
// RunTests(0, Leaderboard::Type::Donations);
|
||||||
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::Donations, 1, 300000);
|
// LeaderboardManager::Instance().SaveScore(0, 0, Leaderboard::Type::Donations, 1, 300000);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user