diff --git a/src/game/server/score/sql_score.cpp b/src/game/server/score/sql_score.cpp index 124a45fe5..b23533a8b 100644 --- a/src/game/server/score/sql_score.cpp +++ b/src/game/server/score/sql_score.cpp @@ -82,6 +82,27 @@ std::shared_ptr CSqlScore::NewSqlPlayerResult(int ClientID) return pCurPlayer->m_SqlQueryResult; } +void CSqlScore::ExecPlayerThread( + bool (*pFuncPtr) (CSqlServer*, const CSqlData *, bool), + const char* pThreadName, + int ClientID, + const char* pName, + int Offset +) { + auto pResult = NewSqlPlayerResult(ClientID); + if(pResult == nullptr) + return; + CSqlPlayerRequest *Tmp = new CSqlPlayerRequest(pResult); + Tmp->m_Name = pName; + Tmp->m_Map = g_Config.m_SvMap; + Tmp->m_RequestingPlayer = Server()->ClientName(ClientID); + Tmp->m_Offset = Offset; + + thread_init_and_detach(CSqlExecData::ExecSqlFunc, + new CSqlExecData(pFuncPtr, Tmp), + pThreadName); +} + LOCK CSqlScore::ms_FailureFileLock = lock_create(); CSqlScore::CSqlScore(CGameContext *pGameServer) : @@ -399,32 +420,23 @@ bool CSqlScore::MapVoteThread(CSqlServer* pSqlServer, const CSqlData void CSqlScore::MapInfo(int ClientID, const char* MapName) { - auto pResult = NewSqlPlayerResult(ClientID); - if(pResult == nullptr) - return; - CSqlMapData *Tmp = new CSqlMapData(pResult); - - Tmp->m_ClientID = ClientID; - Tmp->m_RequestedMap = MapName; - Tmp->m_Name = Server()->ClientName(ClientID); - str_copy(Tmp->m_aFuzzyMap, MapName, sizeof(Tmp->m_aFuzzyMap)); - sqlstr::ClearString(Tmp->m_aFuzzyMap, sizeof(Tmp->m_aFuzzyMap)); - sqlstr::FuzzyString(Tmp->m_aFuzzyMap, sizeof(Tmp->m_aFuzzyMap)); - - thread_init_and_detach(CSqlExecData::ExecSqlFunc, - new CSqlExecData(MapInfoThread, Tmp), - "map info"); + ExecPlayerThread(MapInfoThread, "map info", ClientID, MapName, 0); } bool CSqlScore::MapInfoThread(CSqlServer* pSqlServer, const CSqlData *pGameData, bool HandleFailure) { - const CSqlMapData *pData = dynamic_cast(pGameData); + const CSqlPlayerRequest *pData = dynamic_cast(pGameData); if (HandleFailure) return true; try { + char aFuzzyMap[128]; + str_copy(aFuzzyMap, pData->m_Name.Str(), sizeof(aFuzzyMap)); + sqlstr::ClearString(aFuzzyMap, sizeof(aFuzzyMap)); + sqlstr::FuzzyString(aFuzzyMap, sizeof(aFuzzyMap)); + char aBuf[1024]; str_format(aBuf, sizeof(aBuf), "SELECT l.Map, l.Server, Mapper, Points, Stars, " @@ -446,18 +458,18 @@ bool CSqlScore::MapInfoThread(CSqlServer* pSqlServer, const CSqlDataGetPrefix(), pSqlServer->GetPrefix(), pSqlServer->GetPrefix(), pSqlServer->GetPrefix(), - pData->m_Name.ClrStr(), + pData->m_RequestingPlayer.ClrStr(), pSqlServer->GetPrefix(), - pData->m_aFuzzyMap, - pData->m_RequestedMap.ClrStr(), - pData->m_RequestedMap.ClrStr() + aFuzzyMap, + pData->m_Name.ClrStr(), + pData->m_Name.ClrStr() ); pSqlServer->executeSqlQuery(aBuf); if(pSqlServer->GetResults()->rowsCount() != 1) { str_format(pData->m_pResult->m_aaMessages[0], sizeof(pData->m_pResult->m_aaMessages[0]), - "No map like \"%s\" found.", pData->m_RequestedMap.Str()); + "No map like \"%s\" found.", pData->m_Name.Str()); } else { @@ -804,17 +816,7 @@ bool CSqlScore::SaveTeamScoreThread(CSqlServer* pSqlServer, const CSqlDatam_Player = pName; - Tmp->m_Map = g_Config.m_SvMap; - Tmp->m_RequestingPlayer = Server()->ClientName(ClientID); - - thread_init_and_detach(CSqlExecData::ExecSqlFunc, - new CSqlExecData(ShowRankThread, Tmp), - "show rank"); + ExecPlayerThread(ShowRankThread, "show rank", ClientID, pName, 0); } bool CSqlScore::ShowRankThread(CSqlServer* pSqlServer, const CSqlData *pGameData, bool HandleFailure) @@ -843,7 +845,7 @@ bool CSqlScore::ShowRankThread(CSqlServer* pSqlServer, const CSqlDataGetPrefix(), pData->m_Map.ClrStr(), - pData->m_Player.ClrStr() + pData->m_Name.ClrStr() ); pSqlServer->executeSqlQuery(aBuf); @@ -851,7 +853,7 @@ bool CSqlScore::ShowRankThread(CSqlServer* pSqlServer, const CSqlDataGetResults()->rowsCount() != 1) { str_format(pData->m_pResult->m_aaMessages[0], sizeof(pData->m_pResult->m_aaMessages[0]), - "%s is not ranked", pData->m_Player.Str()); + "%s is not ranked", pData->m_Name.Str()); } else { @@ -888,17 +890,7 @@ bool CSqlScore::ShowRankThread(CSqlServer* pSqlServer, const CSqlDatam_Player = pName; - Tmp->m_Map = g_Config.m_SvMap; - Tmp->m_RequestingPlayer = Server()->ClientName(ClientID); - - thread_init_and_detach(CSqlExecData::ExecSqlFunc, - new CSqlExecData(ShowTeamRankThread, Tmp), - "show team rank"); + ExecPlayerThread(ShowTeamRankThread, "show team rank", ClientID, pName, 0); } bool CSqlScore::ShowTeamRankThread(CSqlServer* pSqlServer, const CSqlData *pGameData, bool HandleFailure) @@ -948,7 +940,7 @@ bool CSqlScore::ShowTeamRankThread(CSqlServer* pSqlServer, const CSqlDatam_Map.ClrStr(), pSqlServer->GetPrefix(), pData->m_Map.ClrStr(), - pData->m_Player.ClrStr(), + pData->m_Name.ClrStr(), pSqlServer->GetPrefix() ); @@ -959,7 +951,7 @@ bool CSqlScore::ShowTeamRankThread(CSqlServer* pSqlServer, const CSqlDatam_pResult->m_aaMessages[0], sizeof(pData->m_pResult->m_aaMessages[0]), - "%s has no team ranks", pData->m_Player.Str()); + "%s has no team ranks", pData->m_Name.Str()); } else { diff --git a/src/game/server/score/sql_score.h b/src/game/server/score/sql_score.h index d14114831..55268caff 100644 --- a/src/game/server/score/sql_score.h +++ b/src/game/server/score/sql_score.h @@ -84,16 +84,19 @@ struct CSqlMapData : CSqlData int m_ClientID; sqlstr::CSqlString<128> m_RequestedMap; - char m_aFuzzyMap[128]; sqlstr::CSqlString m_Name; }; struct CSqlPlayerRequest : CSqlData { using CSqlData::CSqlData; - sqlstr::CSqlString m_Player; - sqlstr::CSqlString m_Map; + // object being requested, either map (128 bytes) or player (16 bytes) + sqlstr::CSqlString<128> m_Name; + // current map + sqlstr::CSqlString<128> m_Map; sqlstr::CSqlString m_RequestingPlayer; + // relevant for /top5 kind of requests + int m_Offset; }; // used for mapvote @@ -221,6 +224,14 @@ class CSqlScore: public IScore // returns new SqlResult bound to the player, if no current Thread is active for this player std::shared_ptr NewSqlPlayerResult(int ClientID); + // Creates for player database requests + void ExecPlayerThread( + bool (*pFuncPtr) (CSqlServer*, const CSqlData *, bool), + const char* pThreadName, + int ClientID, + const char* pName, + int OffSet + ); public: // keeps track of score-threads