2021-12-05 17:54:36 +00:00
|
|
|
#include "LeaderboardManager.h"
|
|
|
|
#include <utility>
|
|
|
|
#include "Database.h"
|
|
|
|
#include "EntityManager.h"
|
|
|
|
#include "Character.h"
|
2021-12-22 03:15:29 +00:00
|
|
|
#include "Game.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "GameMessages.h"
|
|
|
|
#include "dLogger.h"
|
2021-12-22 03:15:29 +00:00
|
|
|
#include "dConfig.h"
|
2022-12-18 15:46:04 +00:00
|
|
|
#include "CDClientManager.h"
|
2023-03-17 14:36:21 +00:00
|
|
|
#include "GeneralUtils.h"
|
|
|
|
#include "Entity.h"
|
2023-04-14 08:32:52 +00:00
|
|
|
#include "LDFFormat.h"
|
2023-04-13 07:45:03 +00:00
|
|
|
#include <sstream>
|
2023-03-17 14:36:21 +00:00
|
|
|
|
|
|
|
#include "CDActivitiesTable.h"
|
2023-04-13 07:45:03 +00:00
|
|
|
#include "Metrics.hpp"
|
2023-04-26 09:10:57 +00:00
|
|
|
|
2023-05-09 08:42:11 +00:00
|
|
|
LeaderboardManager::LeaderboardCache LeaderboardManager::leaderboardCache = {};
|
|
|
|
|
2023-05-03 07:38:38 +00:00
|
|
|
Leaderboard::Leaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, LWOOBJID relatedPlayer, const Leaderboard::Type leaderboardType) {
|
2021-12-05 17:54:36 +00:00
|
|
|
this->gameID = gameID;
|
|
|
|
this->weekly = weekly;
|
|
|
|
this->infoType = infoType;
|
|
|
|
this->leaderboardType = leaderboardType;
|
2023-05-03 07:38:38 +00:00
|
|
|
this->relatedPlayer = relatedPlayer;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 07:38:38 +00:00
|
|
|
Leaderboard::~Leaderboard() {
|
|
|
|
for (auto& entry : entries) for (auto data : entry) delete data;
|
2023-04-14 08:32:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 07:38:38 +00:00
|
|
|
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) {
|
2023-05-09 02:35:19 +00:00
|
|
|
bitStream->Write(gameID);
|
2023-05-09 08:42:11 +00:00
|
|
|
bitStream->Write(infoType);
|
2023-05-09 02:35:19 +00:00
|
|
|
|
2023-04-13 07:45:03 +00:00
|
|
|
std::ostringstream leaderboard;
|
2023-05-09 08:42:11 +00:00
|
|
|
Game::logger->Log("LeaderboardManager", "game is %i info type %i ", gameID, infoType);
|
2023-04-13 07:45:03 +00:00
|
|
|
leaderboard << "ADO.Result=7:1\n"; // Unused in 1.10.64, but is in captures
|
|
|
|
leaderboard << "Result.Count=1:1\n"; // number of results, always 1?
|
|
|
|
leaderboard << "Result[0].Index=0:RowNumber\n"; // "Primary key"
|
|
|
|
leaderboard << "Result[0].RowCount=1:" << entries.size() << '\n'; // number of rows
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-05-09 01:36:28 +00:00
|
|
|
int32_t rowNumber = 0;
|
2023-05-03 07:38:38 +00:00
|
|
|
for (auto& entry : entries) {
|
2023-05-09 01:36:28 +00:00
|
|
|
for (auto* data : entry) {
|
2023-05-09 08:42:11 +00:00
|
|
|
Game::logger->Log("LeaderboardManager", "writing data %s", data->GetString().c_str());
|
2023-05-09 01:36:28 +00:00
|
|
|
WriteLeaderboardRow(leaderboard, rowNumber, data);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2023-05-09 01:36:28 +00:00
|
|
|
rowNumber++;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2023-05-09 08:42:11 +00:00
|
|
|
Game::logger->Log("LeaderboardManager", "leaderboard is %s", leaderboard.str().c_str());
|
2023-04-13 04:57:58 +00:00
|
|
|
// Serialize the thing to a BitStream
|
2023-05-09 08:42:11 +00:00
|
|
|
uint32_t leaderboardSize = leaderboard.tellp();
|
|
|
|
bitStream->Write<uint32_t>(leaderboardSize);
|
|
|
|
// Doing this all in 1 call so there is no possbility of a dangling pointer.
|
|
|
|
bitStream->WriteAlignedBytes(reinterpret_cast<const unsigned char*>(GeneralUtils::ASCIIToUTF16(leaderboard.str()).c_str()), leaderboard.tellp() * 2);
|
|
|
|
if (leaderboardSize > 0) bitStream->Write<uint16_t>(0);
|
2023-05-09 02:35:19 +00:00
|
|
|
bitStream->Write0();
|
|
|
|
bitStream->Write0();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 23:53:36 +00:00
|
|
|
bool Leaderboard::GetRankingQuery(std::string& lookupReturn) const {
|
|
|
|
if (this->infoType == InfoType::Top) {
|
|
|
|
lookupReturn = "SELECT id FROM leaderboard WHERE game_id = ? ORDER BY %s LIMIT 1";
|
|
|
|
return true;
|
2023-05-03 07:38:38 +00:00
|
|
|
} else {
|
2023-05-04 23:53:36 +00:00
|
|
|
lookupReturn = "SELECT id FROM leaderboard WHERE game_id = ? AND character_id = ? LIMIT 1";
|
|
|
|
return false;
|
2023-05-03 07:38:38 +00:00
|
|
|
}
|
2023-05-04 23:53:36 +00:00
|
|
|
}
|
2023-05-03 07:38:38 +00:00
|
|
|
|
2023-05-09 01:36:28 +00:00
|
|
|
void Leaderboard::QueryToLdf(std::unique_ptr<sql::ResultSet>& rows) {
|
|
|
|
if (rows->rowsCount() == 0) return;
|
2023-05-03 07:38:38 +00:00
|
|
|
|
2023-05-09 01:36:28 +00:00
|
|
|
this->entries.reserve(rows->rowsCount());
|
|
|
|
while (rows->next()) {
|
|
|
|
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", rows->getInt("character_id")));
|
|
|
|
entry.push_back(new LDFData<uint64_t>(u"LastPlayed", rows->getUInt64("lastPlayed")));
|
|
|
|
entry.push_back(new LDFData<int32_t>(u"NumPlayed", 1));
|
|
|
|
entry.push_back(new LDFData<std::u16string>(u"name", GeneralUtils::ASCIIToUTF16(rows->getString("name").c_str())));
|
2023-05-09 08:42:11 +00:00
|
|
|
entry.push_back(new LDFData<uint64_t>(u"RowNumber", rows->getInt("ranking")));
|
2023-05-09 01:36:28 +00:00
|
|
|
switch (leaderboardType) {
|
|
|
|
case Type::ShootingGallery:
|
|
|
|
entry.push_back(new LDFData<float>(u"HitPercentage", rows->getDouble("hitPercentage")));
|
|
|
|
// HitPercentage:3 between 0 and 1
|
|
|
|
entry.push_back(new LDFData<int32_t>(u"Score", rows->getInt("score")));
|
|
|
|
// Score:1
|
|
|
|
entry.push_back(new LDFData<int32_t>(u"Streak", rows->getInt("streak")));
|
|
|
|
// Streak:1
|
|
|
|
break;
|
|
|
|
case Type::Racing:
|
|
|
|
entry.push_back(new LDFData<float>(u"BestLapTime", rows->getDouble("bestLapTime")));
|
|
|
|
// BestLapTime:3
|
|
|
|
entry.push_back(new LDFData<float>(u"BestTime", rows->getDouble("bestTime")));
|
|
|
|
// BestTime:3
|
|
|
|
entry.push_back(new LDFData<int32_t>(u"License", 1));
|
|
|
|
// License:1 - 1 if player has completed mission 637 and 0 otherwise
|
|
|
|
entry.push_back(new LDFData<int32_t>(u"NumWins", rows->getInt("numWins")));
|
|
|
|
// NumWins:1
|
|
|
|
break;
|
|
|
|
case Type::UnusedLeaderboard4:
|
2023-05-09 08:42:11 +00:00
|
|
|
entry.push_back(new LDFData<int32_t>(u"Points", rows->getInt("score")));
|
2023-05-09 01:36:28 +00:00
|
|
|
// Points:1
|
|
|
|
break;
|
|
|
|
case Type::MonumentRace:
|
|
|
|
entry.push_back(new LDFData<int32_t>(u"Time", rows->getInt("bestTime")));
|
|
|
|
// Time:1(?)
|
|
|
|
break;
|
|
|
|
case Type::FootRace:
|
|
|
|
entry.push_back(new LDFData<int32_t>(u"Time", rows->getInt("bestTime")));
|
|
|
|
// Time:1
|
|
|
|
break;
|
|
|
|
case Type::Survival:
|
2023-05-09 08:42:11 +00:00
|
|
|
entry.push_back(new LDFData<int32_t>(u"Points", rows->getInt("score")));
|
2023-05-09 01:36:28 +00:00
|
|
|
// Points:1
|
|
|
|
entry.push_back(new LDFData<int32_t>(u"Time", rows->getInt("bestTime")));
|
|
|
|
// Time:1
|
|
|
|
break;
|
|
|
|
case Type::SurvivalNS:
|
2023-05-09 08:42:11 +00:00
|
|
|
entry.push_back(new LDFData<int32_t>(u"Wave", rows->getInt("score")));
|
2023-05-09 01:36:28 +00:00
|
|
|
// Wave:1
|
|
|
|
entry.push_back(new LDFData<int32_t>(u"Time", rows->getInt("bestTime")));
|
|
|
|
// Time:1
|
|
|
|
break;
|
|
|
|
case Type::Donations:
|
2023-05-09 08:42:11 +00:00
|
|
|
entry.push_back(new LDFData<int32_t>(u"Points", rows->getInt("score")));
|
2023-05-09 01:36:28 +00:00
|
|
|
// Score:1
|
|
|
|
break;
|
|
|
|
case Type::None:
|
|
|
|
// This type is included here simply to resolve a compiler warning on mac about unused enum types
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-03 07:38:38 +00:00
|
|
|
|
2023-05-09 01:36:28 +00:00
|
|
|
std::string Leaderboard::GetColumns(Leaderboard::Type leaderboardType) {
|
|
|
|
std::string columns;
|
|
|
|
switch (leaderboardType) {
|
|
|
|
case Type::ShootingGallery:
|
|
|
|
columns = "hitPercentage, score, streak";
|
|
|
|
break;
|
|
|
|
case Type::Racing:
|
|
|
|
columns = "bestLapTime, bestTime, numWins";
|
|
|
|
break;
|
|
|
|
case Type::UnusedLeaderboard4:
|
|
|
|
columns = "score";
|
|
|
|
break;
|
|
|
|
case Type::MonumentRace:
|
|
|
|
columns = "bestTime";
|
|
|
|
break;
|
|
|
|
case Type::FootRace:
|
|
|
|
columns = "bestTime";
|
|
|
|
break;
|
|
|
|
case Type::Survival:
|
|
|
|
columns = "bestTime, score";
|
|
|
|
break;
|
|
|
|
case Type::SurvivalNS:
|
|
|
|
columns = "bestTime, score";
|
|
|
|
break;
|
|
|
|
case Type::Donations:
|
|
|
|
columns = "score";
|
|
|
|
break;
|
|
|
|
case Type::None:
|
|
|
|
// This type is included here simply to resolve a compiler warning on mac about unused enum types
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Leaderboard::GetInsertFormat(Leaderboard::Type leaderboardType) {
|
|
|
|
std::string columns;
|
|
|
|
switch (leaderboardType) {
|
|
|
|
case Type::ShootingGallery:
|
|
|
|
columns = "hitPercentage=%f, score=%i, streak=%i";
|
|
|
|
break;
|
|
|
|
case Type::Racing:
|
|
|
|
columns = "bestLapTime=%i, bestTime=%i, numWins=numWins + %i";
|
|
|
|
break;
|
|
|
|
case Type::UnusedLeaderboard4:
|
|
|
|
columns = "score=%i";
|
|
|
|
break;
|
|
|
|
case Type::MonumentRace:
|
|
|
|
columns = "bestTime=%i";
|
|
|
|
break;
|
|
|
|
case Type::FootRace:
|
|
|
|
columns = "bestTime=%i";
|
|
|
|
break;
|
|
|
|
case Type::Survival:
|
|
|
|
columns = "bestTime=%i, score=%i";
|
|
|
|
break;
|
|
|
|
case Type::SurvivalNS:
|
|
|
|
columns = "bestTime=%i, score=%i";
|
|
|
|
break;
|
|
|
|
case Type::Donations:
|
|
|
|
columns = "score=%i";
|
|
|
|
break;
|
|
|
|
case Type::None:
|
|
|
|
// This type is included here simply to resolve a compiler warning on mac about unused enum types
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Leaderboard::GetOrdering(Leaderboard::Type leaderboardType) {
|
2023-05-04 21:28:53 +00:00
|
|
|
std::string orderBase;
|
2023-05-01 04:30:41 +00:00
|
|
|
switch (leaderboardType) {
|
2023-05-09 01:36:28 +00:00
|
|
|
case Type::ShootingGallery:
|
2023-05-04 21:28:53 +00:00
|
|
|
orderBase = "score DESC, streak DESC, hitPercentage DESC";
|
2023-05-01 04:30:41 +00:00
|
|
|
break;
|
|
|
|
case Type::Racing:
|
2023-05-04 21:28:53 +00:00
|
|
|
orderBase = "bestTime ASC, bestLapTime ASC, numWins DESC";
|
2023-05-01 04:30:41 +00:00
|
|
|
break;
|
|
|
|
case Type::UnusedLeaderboard4:
|
2023-05-04 21:28:53 +00:00
|
|
|
orderBase = "score DESC";
|
2023-05-01 04:30:41 +00:00
|
|
|
break;
|
|
|
|
case Type::MonumentRace:
|
2023-05-04 21:28:53 +00:00
|
|
|
orderBase = "bestTime ASC";
|
2023-05-01 04:30:41 +00:00
|
|
|
break;
|
|
|
|
case Type::FootRace:
|
2023-05-04 21:28:53 +00:00
|
|
|
orderBase = "bestTime DESC";
|
2023-05-01 04:30:41 +00:00
|
|
|
break;
|
|
|
|
case Type::Survival:
|
2023-05-04 21:28:53 +00:00
|
|
|
orderBase = "score DESC, bestTime DESC";
|
2023-05-01 04:30:41 +00:00
|
|
|
break;
|
|
|
|
case Type::SurvivalNS:
|
2023-05-04 21:28:53 +00:00
|
|
|
orderBase = "bestTime DESC, score DESC";
|
2023-05-01 04:30:41 +00:00
|
|
|
break;
|
|
|
|
case Type::Donations:
|
2023-05-04 21:28:53 +00:00
|
|
|
orderBase = "score DESC";
|
2023-05-01 04:30:41 +00:00
|
|
|
break;
|
|
|
|
case Type::None:
|
|
|
|
// This type is included here simply to resolve a compiler warning on mac about unused enum types
|
|
|
|
break;
|
|
|
|
}
|
2023-05-09 01:36:28 +00:00
|
|
|
return orderBase;
|
|
|
|
}
|
|
|
|
|
2023-05-09 02:35:19 +00:00
|
|
|
void Leaderboard::SetupLeaderboard(uint32_t resultStart, uint32_t resultEnd) {
|
2023-05-09 02:59:10 +00:00
|
|
|
resultStart++;
|
|
|
|
resultEnd++;
|
2023-05-09 01:36:28 +00:00
|
|
|
std::string queryBase =
|
|
|
|
R"QUERY(
|
|
|
|
WITH leaderboardsRanked AS (
|
|
|
|
SELECT leaderboard.*, charinfo.name,
|
|
|
|
RANK() OVER
|
|
|
|
(
|
|
|
|
ORDER BY %s, UNIX_TIMESTAMP(last_played) ASC, id DESC
|
|
|
|
) 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, leaderboardsRanked.ranking FROM leaderboardsRanked, myStanding, lowestRanking
|
|
|
|
WHERE leaderboardsRanked.ranking
|
|
|
|
BETWEEN
|
2023-05-09 02:35:19 +00:00
|
|
|
LEAST(GREATEST(CAST(myRank AS SIGNED) - 5, %i), lowestRanking.lowestRank - 10)
|
2023-05-09 01:36:28 +00:00
|
|
|
AND
|
2023-05-09 02:35:19 +00:00
|
|
|
LEAST(GREATEST(myRank + 5, %i), lowestRanking.lowestRank)
|
2023-05-09 01:36:28 +00:00
|
|
|
ORDER BY ranking ASC;
|
|
|
|
)QUERY";
|
|
|
|
|
|
|
|
const char* friendsQuery =
|
|
|
|
R"QUERY( 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 = ?
|
|
|
|
)
|
|
|
|
)QUERY";
|
|
|
|
|
|
|
|
std::string orderBase = GetOrdering(this->leaderboardType);
|
|
|
|
std::string selectBase = GetColumns(this->leaderboardType);
|
2023-05-04 21:48:26 +00:00
|
|
|
|
2023-05-04 23:53:36 +00:00
|
|
|
constexpr uint16_t STRING_LENGTH = 1526;
|
|
|
|
char lookupBuffer[STRING_LENGTH];
|
2023-05-09 01:36:28 +00:00
|
|
|
// If we are getting the friends leaderboard, add the friends query, otherwise fill it in with nothing.
|
2023-05-09 02:35:19 +00:00
|
|
|
if (this->infoType == InfoType::Friends) {
|
|
|
|
snprintf(lookupBuffer, STRING_LENGTH, queryBase.c_str(),
|
2023-05-09 02:59:10 +00:00
|
|
|
orderBase.c_str(), friendsQuery, selectBase.c_str(), resultStart, resultEnd);
|
2023-05-09 02:35:19 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
snprintf(lookupBuffer, STRING_LENGTH, queryBase.c_str(),
|
2023-05-09 02:59:10 +00:00
|
|
|
orderBase.c_str(), "", selectBase.c_str(), resultStart, resultEnd);
|
2023-05-09 02:35:19 +00:00
|
|
|
}
|
2023-05-04 21:28:53 +00:00
|
|
|
|
2023-05-04 23:53:36 +00:00
|
|
|
std::string baseLookupStr;
|
|
|
|
char baseRankingBuffer[STRING_LENGTH];
|
|
|
|
bool neededFormatting = GetRankingQuery(baseLookupStr);
|
2023-05-09 01:36:28 +00:00
|
|
|
|
|
|
|
// If we need to format the base ranking query, do so, otherwise just copy the query since it's already formatted.
|
2023-05-04 23:53:36 +00:00
|
|
|
if (neededFormatting) snprintf(baseRankingBuffer, STRING_LENGTH, baseLookupStr.c_str(), orderBase.c_str());
|
|
|
|
else std::copy(baseLookupStr.begin(), baseLookupStr.end() + 1, baseRankingBuffer);
|
|
|
|
|
|
|
|
std::unique_ptr<sql::PreparedStatement> baseQuery(Database::CreatePreppedStmt(baseRankingBuffer));
|
2023-05-03 07:38:38 +00:00
|
|
|
baseQuery->setInt(1, this->gameID);
|
2023-05-04 23:53:36 +00:00
|
|
|
if (!neededFormatting) {
|
2023-05-03 07:38:38 +00:00
|
|
|
baseQuery->setInt(2, this->relatedPlayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<sql::ResultSet> baseResult(baseQuery->executeQuery());
|
2023-05-09 08:42:11 +00:00
|
|
|
Game::logger->Log("LeaderboardManager", "%s", baseRankingBuffer);
|
2023-05-09 01:36:28 +00:00
|
|
|
if (!baseResult->next()) return; // In this case, there are no entries in the leaderboard for this game.
|
|
|
|
|
2023-05-03 07:38:38 +00:00
|
|
|
uint32_t relatedPlayerLeaderboardId = baseResult->getInt("id");
|
|
|
|
|
2023-05-09 01:36:28 +00:00
|
|
|
// Create and execute the actual save here
|
2023-05-09 08:42:11 +00:00
|
|
|
Game::logger->Log("LeaderboardManager", "query is %s", lookupBuffer);
|
2023-05-03 07:38:38 +00:00
|
|
|
std::unique_ptr<sql::PreparedStatement> query(Database::CreatePreppedStmt(lookupBuffer));
|
2023-05-09 01:36:28 +00:00
|
|
|
|
2023-05-01 04:30:41 +00:00
|
|
|
query->setInt(1, this->gameID);
|
2023-05-04 23:53:36 +00:00
|
|
|
if (this->infoType == InfoType::Friends) {
|
2023-05-03 07:38:38 +00:00
|
|
|
query->setInt(2, this->relatedPlayer);
|
|
|
|
query->setInt(3, this->relatedPlayer);
|
|
|
|
query->setInt(4, this->relatedPlayer);
|
|
|
|
query->setInt(5, relatedPlayerLeaderboardId);
|
|
|
|
} else {
|
|
|
|
query->setInt(2, relatedPlayerLeaderboardId);
|
|
|
|
}
|
2023-05-01 04:30:41 +00:00
|
|
|
|
2023-05-09 01:36:28 +00:00
|
|
|
std::unique_ptr<sql::ResultSet> result(query->executeQuery());
|
|
|
|
QueryToLdf(result);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 02:35:19 +00:00
|
|
|
void Leaderboard::Send(LWOOBJID targetID) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* player = EntityManager::Instance()->GetEntity(relatedPlayer);
|
|
|
|
if (player != nullptr) {
|
|
|
|
GameMessages::SendActivitySummaryLeaderboardData(targetID, this, player->GetSystemAddress());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:26:35 +00:00
|
|
|
void LeaderboardManager::SaveScore(const LWOOBJID& playerID, GameID gameID, Leaderboard::Type leaderboardType, uint32_t argumentCount, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, argumentCount);
|
|
|
|
SaveScore(playerID, gameID, leaderboardType, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-05-07 11:09:10 +00:00
|
|
|
std::string FormatInsert(const std::string& columns, const std::string& format, va_list args, bool useUpdate) {
|
2023-05-08 09:46:55 +00:00
|
|
|
const char* insertClause = "INSERT";
|
|
|
|
const char* updateClause = "UPDATE";
|
|
|
|
const char* queryType = useUpdate ? updateClause : insertClause;
|
2023-05-07 11:09:10 +00:00
|
|
|
|
2023-05-08 10:55:10 +00:00
|
|
|
const char* insertFilter = ", character_id = ?, game_id = ?, timesPlayed = 1";
|
|
|
|
const char* updateFilter = ", timesPlayed = timesPlayed + 1 WHERE character_id = ? AND game_id = ?";
|
2023-05-08 09:46:55 +00:00
|
|
|
const char* usedFilter = useUpdate ? updateFilter : insertFilter;
|
2023-05-07 11:09:10 +00:00
|
|
|
|
2023-04-18 08:26:35 +00:00
|
|
|
constexpr uint16_t STRING_LENGTH = 400;
|
|
|
|
char formattedInsert[STRING_LENGTH];
|
2023-05-08 09:46:55 +00:00
|
|
|
auto queryBase = "%s leaderboard SET %s %s;";
|
2023-05-07 11:09:10 +00:00
|
|
|
snprintf(formattedInsert, STRING_LENGTH, queryBase, queryType, format.c_str(), usedFilter);
|
|
|
|
|
2023-04-18 08:26:35 +00:00
|
|
|
char finishedQuery[STRING_LENGTH];
|
2023-05-04 23:53:36 +00:00
|
|
|
vsnprintf(finishedQuery, STRING_LENGTH, formattedInsert, args);
|
2023-04-18 08:26:35 +00:00
|
|
|
return finishedQuery;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-05-09 01:36:28 +00:00
|
|
|
void LeaderboardManager::SaveScore(const LWOOBJID& playerID, GameID gameID, Leaderboard::Type leaderboardType, va_list activityScore) {
|
|
|
|
std::string selectedColumns = Leaderboard::GetColumns(leaderboardType);
|
|
|
|
std::string insertFormat = Leaderboard::GetInsertFormat(leaderboardType);
|
2023-05-07 07:31:38 +00:00
|
|
|
const char* lookup = "SELECT %s FROM leaderboard WHERE character_id = ? AND game_id = ?;";
|
2023-05-07 11:09:10 +00:00
|
|
|
|
2023-05-07 07:31:38 +00:00
|
|
|
constexpr uint16_t STRING_LENGTH = 400;
|
|
|
|
char lookupBuffer[STRING_LENGTH];
|
|
|
|
snprintf(lookupBuffer, STRING_LENGTH, lookup, selectedColumns.c_str());
|
2023-05-07 11:09:10 +00:00
|
|
|
|
2023-05-07 07:31:38 +00:00
|
|
|
std::unique_ptr<sql::PreparedStatement> query(Database::CreatePreppedStmt(lookupBuffer));
|
|
|
|
query->setInt(1, playerID);
|
|
|
|
query->setInt(2, gameID);
|
|
|
|
std::unique_ptr<sql::ResultSet> myScoreResult(query->executeQuery());
|
|
|
|
|
2023-05-09 01:36:28 +00:00
|
|
|
std::va_list argsCopy;
|
|
|
|
va_copy(argsCopy, activityScore);
|
2023-05-07 11:09:10 +00:00
|
|
|
std::string saveQuery;
|
|
|
|
if (myScoreResult->next()) {
|
|
|
|
switch (leaderboardType) {
|
|
|
|
case Leaderboard::Type::ShootingGallery: {
|
|
|
|
int32_t oldScore = myScoreResult->getInt("score");
|
|
|
|
int32_t score;
|
|
|
|
score = va_arg(argsCopy, int32_t);
|
|
|
|
|
|
|
|
float oldHitPercentage = myScoreResult->getFloat("hitPercentage");
|
|
|
|
float hitPercentage;
|
|
|
|
hitPercentage = va_arg(argsCopy, double);
|
|
|
|
|
|
|
|
int32_t oldStreak = myScoreResult->getInt("streak");
|
|
|
|
int32_t streak;
|
|
|
|
streak = va_arg(argsCopy, int32_t);
|
|
|
|
|
2023-05-09 01:36:28 +00:00
|
|
|
if (score > oldScore || // If score is better
|
2023-05-07 11:09:10 +00:00
|
|
|
(score == oldScore && hitPercentage > oldHitPercentage) || // or if the score is tied and the hitPercentage is better
|
|
|
|
(score == oldScore && hitPercentage == oldHitPercentage && streak > oldStreak)) { // or if the score and hitPercentage are tied and the streak is better
|
2023-05-09 01:36:28 +00:00
|
|
|
saveQuery = FormatInsert(selectedColumns, insertFormat, activityScore, true);
|
2023-05-07 11:09:10 +00:00
|
|
|
}
|
|
|
|
break;
|
2023-05-07 07:31:38 +00:00
|
|
|
}
|
2023-05-07 11:09:10 +00:00
|
|
|
case Leaderboard::Type::Racing: {
|
2023-05-09 01:36:28 +00:00
|
|
|
uint32_t oldLapTime = myScoreResult->getFloat("bestLapTime");
|
|
|
|
uint32_t lapTime;
|
|
|
|
lapTime = va_arg(argsCopy, uint32_t);
|
2023-05-07 11:09:10 +00:00
|
|
|
|
2023-05-09 01:36:28 +00:00
|
|
|
uint32_t oldTime = myScoreResult->getFloat("bestTime");
|
|
|
|
uint32_t newTime;
|
|
|
|
newTime = va_arg(argsCopy, uint32_t);
|
2023-05-07 11:09:10 +00:00
|
|
|
|
|
|
|
bool won;
|
|
|
|
won = va_arg(argsCopy, int32_t);
|
2023-05-09 01:36:28 +00:00
|
|
|
|
|
|
|
if (newTime < oldTime ||
|
|
|
|
(newTime == oldTime && lapTime < oldLapTime)) {
|
|
|
|
saveQuery = FormatInsert(selectedColumns, insertFormat, activityScore, true);
|
|
|
|
} else if (won) {
|
|
|
|
std::unique_ptr<sql::PreparedStatement> incrementStatement(Database::CreatePreppedStmt("UPDATE leaderboard SET numWins = numWins + 1 WHERE character_id = ? AND game_id = ?;"));
|
|
|
|
incrementStatement->setInt(1, playerID);
|
|
|
|
incrementStatement->setInt(2, gameID);
|
|
|
|
incrementStatement->executeUpdate();
|
|
|
|
}
|
2023-05-07 11:09:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Leaderboard::Type::UnusedLeaderboard4: {
|
|
|
|
int32_t oldScore = myScoreResult->getInt("score");
|
|
|
|
int32_t points;
|
|
|
|
points = va_arg(argsCopy, int32_t);
|
2023-05-09 01:36:28 +00:00
|
|
|
|
2023-05-08 09:46:55 +00:00
|
|
|
if (points > oldScore) {
|
2023-05-09 01:36:28 +00:00
|
|
|
saveQuery = FormatInsert(selectedColumns, insertFormat, activityScore, true);
|
2023-05-08 09:46:55 +00:00
|
|
|
}
|
2023-05-07 11:09:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Leaderboard::Type::MonumentRace: {
|
|
|
|
int32_t oldTime = myScoreResult->getInt("bestTime");
|
|
|
|
int32_t time;
|
|
|
|
time = va_arg(argsCopy, int32_t);
|
2023-05-09 01:36:28 +00:00
|
|
|
|
2023-05-08 09:46:55 +00:00
|
|
|
if (time < oldTime) {
|
2023-05-09 01:36:28 +00:00
|
|
|
saveQuery = FormatInsert(selectedColumns, insertFormat, activityScore, true);
|
2023-05-08 09:46:55 +00:00
|
|
|
}
|
2023-05-07 11:09:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Leaderboard::Type::FootRace: {
|
|
|
|
int32_t oldTime = myScoreResult->getInt("bestTime");
|
|
|
|
int32_t time;
|
|
|
|
time = va_arg(argsCopy, int32_t);
|
2023-05-09 01:36:28 +00:00
|
|
|
|
2023-05-09 08:42:11 +00:00
|
|
|
if (time > oldTime) {
|
2023-05-09 01:36:28 +00:00
|
|
|
saveQuery = FormatInsert(selectedColumns, insertFormat, activityScore, true);
|
2023-05-08 09:46:55 +00:00
|
|
|
}
|
2023-05-07 11:09:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Leaderboard::Type::Survival: {
|
2023-05-09 01:36:28 +00:00
|
|
|
int32_t oldTime = myScoreResult->getInt("bestTime");
|
|
|
|
int32_t time;
|
|
|
|
time = va_arg(argsCopy, int32_t);
|
|
|
|
|
2023-05-07 11:09:10 +00:00
|
|
|
int32_t oldPoints = myScoreResult->getInt("score");
|
|
|
|
int32_t points;
|
|
|
|
points = va_arg(argsCopy, int32_t);
|
|
|
|
|
2023-05-08 09:46:55 +00:00
|
|
|
if (points > oldPoints || (points == oldPoints && time < oldTime)) {
|
2023-05-09 01:36:28 +00:00
|
|
|
saveQuery = FormatInsert(selectedColumns, insertFormat, activityScore, true);
|
2023-05-08 09:46:55 +00:00
|
|
|
}
|
2023-05-07 11:09:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Leaderboard::Type::SurvivalNS: {
|
|
|
|
int32_t oldTime = myScoreResult->getInt("bestTime");
|
|
|
|
int32_t time;
|
|
|
|
time = va_arg(argsCopy, int32_t);
|
|
|
|
|
|
|
|
int32_t oldWave = myScoreResult->getInt("score");
|
|
|
|
int32_t wave;
|
|
|
|
wave = va_arg(argsCopy, int32_t);
|
2023-05-09 01:36:28 +00:00
|
|
|
|
2023-05-08 09:46:55 +00:00
|
|
|
if (time < oldTime || (time == oldTime && wave > oldWave)) {
|
2023-05-09 01:36:28 +00:00
|
|
|
saveQuery = FormatInsert(selectedColumns, insertFormat, activityScore, true);
|
2023-05-08 09:46:55 +00:00
|
|
|
}
|
2023-05-07 11:09:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Leaderboard::Type::Donations: {
|
|
|
|
int32_t oldScore = myScoreResult->getInt("score");
|
|
|
|
int32_t score;
|
|
|
|
score = va_arg(argsCopy, int32_t);
|
2023-05-09 01:36:28 +00:00
|
|
|
|
2023-05-08 09:46:55 +00:00
|
|
|
if (score > oldScore) {
|
2023-05-09 01:36:28 +00:00
|
|
|
saveQuery = FormatInsert(selectedColumns, insertFormat, activityScore, true);
|
2023-05-08 09:46:55 +00:00
|
|
|
}
|
2023-05-07 11:09:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-05-09 01:36:28 +00:00
|
|
|
case Leaderboard::Type::None:
|
2023-05-07 11:09:10 +00:00
|
|
|
default:
|
|
|
|
Game::logger->Log("LeaderboardManager", "Unknown leaderboard type %i. Cannot save score!", leaderboardType);
|
2023-05-09 08:42:11 +00:00
|
|
|
va_end(argsCopy);
|
|
|
|
return;
|
2023-05-07 11:09:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-05-09 01:36:28 +00:00
|
|
|
saveQuery = FormatInsert(selectedColumns, insertFormat, activityScore, false);
|
2023-05-07 07:31:38 +00:00
|
|
|
}
|
2023-05-09 01:36:28 +00:00
|
|
|
std::unique_ptr<sql::PreparedStatement> saveStatement;
|
2023-05-07 11:09:10 +00:00
|
|
|
if (!saveQuery.empty()) {
|
2023-05-09 01:36:28 +00:00
|
|
|
Game::logger->Log("LeaderboardManager", "Executing update with query %s", saveQuery.c_str());
|
|
|
|
std::unique_ptr<sql::PreparedStatement> updateStatement(Database::CreatePreppedStmt(saveQuery));
|
|
|
|
saveStatement = std::move(updateStatement);
|
2023-05-08 10:55:10 +00:00
|
|
|
} else {
|
|
|
|
Game::logger->Log("LeaderboardManager", "No new score to save, incrementing numTimesPlayed");
|
|
|
|
// Increment the numTimes this player has played this game.
|
2023-05-09 01:36:28 +00:00
|
|
|
std::unique_ptr<sql::PreparedStatement> updateStatement(Database::CreatePreppedStmt("UPDATE leaderboard SET timesPlayed = timesPlayed + 1 WHERE character_id = ? AND game_id = ?;"));
|
|
|
|
saveStatement = std::move(updateStatement);
|
2023-05-07 07:31:38 +00:00
|
|
|
}
|
2023-05-09 01:36:28 +00:00
|
|
|
saveStatement->setInt(1, playerID);
|
|
|
|
saveStatement->setInt(2, gameID);
|
|
|
|
saveStatement->execute();
|
2023-05-07 07:31:38 +00:00
|
|
|
va_end(argsCopy);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 02:59:10 +00:00
|
|
|
void LeaderboardManager::SendLeaderboard(uint32_t gameID, Leaderboard::InfoType infoType, bool weekly, LWOOBJID playerID, uint32_t resultStart, uint32_t resultEnd) {
|
2023-05-03 07:38:38 +00:00
|
|
|
Leaderboard leaderboard(gameID, infoType, weekly, playerID, GetLeaderboardType(gameID));
|
2023-05-09 02:59:10 +00:00
|
|
|
leaderboard.SetupLeaderboard(resultStart, resultEnd);
|
|
|
|
leaderboard.Send(playerID);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 04:57:58 +00:00
|
|
|
Leaderboard::Type LeaderboardManager::GetLeaderboardType(const GameID gameID) {
|
|
|
|
auto lookup = leaderboardCache.find(gameID);
|
|
|
|
if (lookup != leaderboardCache.end()) return lookup->second;
|
|
|
|
|
2023-03-17 14:36:21 +00:00
|
|
|
auto* activitiesTable = CDClientManager::Instance().GetTable<CDActivitiesTable>();
|
2021-12-05 17:54:36 +00:00
|
|
|
std::vector<CDActivities> activities = activitiesTable->Query([=](const CDActivities& entry) {
|
|
|
|
return (entry.ActivityID == gameID);
|
|
|
|
});
|
2023-05-09 08:42:11 +00:00
|
|
|
auto type = !activities.empty() ? static_cast<Leaderboard::Type>(activities.at(0).leaderboardType) : Leaderboard::Type::None;
|
2023-04-13 04:57:58 +00:00
|
|
|
leaderboardCache.insert_or_assign(gameID, type);
|
|
|
|
return type;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|