From fa8972deb6dfec5d33bad293ffcbd39020ba2ac5 Mon Sep 17 00:00:00 2001 From: Zwelf Date: Sat, 30 May 2020 18:37:38 +0200 Subject: [PATCH] Thread safe /top5points --- src/game/server/score/sql_score.cpp | 56 +++++++++++++++++------------ 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/src/game/server/score/sql_score.cpp b/src/game/server/score/sql_score.cpp index 8ea1b11cd..fcbe407cd 100644 --- a/src/game/server/score/sql_score.cpp +++ b/src/game/server/score/sql_score.cpp @@ -1300,25 +1300,19 @@ bool CSqlScore::ShowPointsThread(CSqlServer* pSqlServer, const CSqlDatam_Num = Offset; - Tmp->m_ClientID = ClientID; - - thread_init_and_detach(ExecSqlFunc, new CSqlExecData(ShowTopPointsThread, Tmp), "show top points"); - */ + ExecPlayerThread(ShowTopPointsThread, "show top points", ClientID, "", Offset); } bool CSqlScore::ShowTopPointsThread(CSqlServer* pSqlServer, const CSqlData *pGameData, bool HandleFailure) { - /* - const CSqlScoreData *pData = dynamic_cast(pGameData); + const CSqlPlayerRequest *pData = dynamic_cast(pGameData); + auto paMessages = pData->m_pResult->m_aaMessages; if (HandleFailure) return true; - int LimitStart = maximum(abs(pData->m_Num)-1, 0); - const char *pOrder = pData->m_Num >= 0 ? "ASC" : "DESC"; + int LimitStart = maximum(abs(pData->m_Offset)-1, 0); + const char *pOrder = pData->m_Offset >= 0 ? "ASC" : "DESC"; try { @@ -1326,20 +1320,43 @@ bool CSqlScore::ShowTopPointsThread(CSqlServer* pSqlServer, const CSqlDataexecuteSql("SET @prev := NULL;"); pSqlServer->executeSql("SET @rank := 1;"); pSqlServer->executeSql("SET @pos := 0;"); - str_format(aBuf, sizeof(aBuf), "SELECT Rank, Points, Name FROM (SELECT Name, (@pos := @pos+1) pos, (@rank := IF(@prev = Points,@rank, @pos)) Rank, (@prev := Points) Points FROM (SELECT Name, Points FROM %s_points GROUP BY Name ORDER BY Points DESC) as a) as b ORDER BY Rank %s LIMIT %d, 5;", pSqlServer->GetPrefix(), pOrder, LimitStart); + str_format(aBuf, sizeof(aBuf), + "SELECT Rank, Points, Name " + "FROM (" + "SELECT Name, " + "(@pos := @pos+1) pos, " + "(@rank := IF(@prev = Points,@rank, @pos)) Rank, " + "(@prev := Points) Points " + "FROM (" + "SELECT Name, Points " + "FROM %s_points " + "GROUP BY Name " + "ORDER BY Points DESC" + ") as a" + ") as b " + "ORDER BY Rank %s " + "LIMIT %d, 5;", + pSqlServer->GetPrefix(), pOrder, LimitStart + ); pSqlServer->executeSqlQuery(aBuf); // show top points - pData->GameServer()->SendChatTarget(pData->m_ClientID, "-------- Top Points --------"); + strcpy(paMessages[0], "-------- Top Points --------"); + int Line = 1; while(pSqlServer->GetResults()->next()) { - str_format(aBuf, sizeof(aBuf), "%d. %s Points: %d", pSqlServer->GetResults()->getInt("Rank"), pSqlServer->GetResults()->getString("Name").c_str(), pSqlServer->GetResults()->getInt("Points")); - pData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf); + int Rank = pSqlServer->GetResults()->getInt("Rank"); + auto Name = pSqlServer->GetResults()->getString("Name"); + int Points = pSqlServer->GetResults()->getInt("Points"); + str_format(paMessages[Line], sizeof(paMessages[0]), + "%d. %s Points: %d", Rank, Name.c_str(), Points); + Line++; } - pData->GameServer()->SendChatTarget(pData->m_ClientID, "-------------------------------"); + strcpy(paMessages[Line], "-------------------------------"); + pData->m_pResult->m_Done = true; dbg_msg("sql", "Showing toppoints done"); return true; } @@ -1348,13 +1365,6 @@ bool CSqlScore::ShowTopPointsThread(CSqlServer* pSqlServer, const CSqlData