Make PlayerResult messages an union variant

as it is only used for DIRECT and ALL variants and for no other variants
This commit is contained in:
Zwelf 2020-06-20 13:56:06 +02:00
parent 4a810091b1
commit 67643e82b0
3 changed files with 31 additions and 28 deletions

View file

@ -874,23 +874,22 @@ void CPlayer::ProcessSqlResult(CSqlPlayerResult &Result)
{
if(Result.m_Done) // SQL request was successful
{
int NumMessages = (int)(sizeof(Result.m_aaMessages)/sizeof(Result.m_aaMessages[0]));
switch(Result.m_MessageKind)
{
case CSqlPlayerResult::DIRECT:
for(int i = 0; i < NumMessages; i++)
for(int i = 0; i < CSqlPlayerResult::MAX_MESSAGES; i++)
{
if(Result.m_aaMessages[i][0] == 0)
if(Result.m_Data.m_aaMessages[i][0] == 0)
break;
GameServer()->SendChatTarget(m_ClientID, Result.m_aaMessages[i]);
GameServer()->SendChatTarget(m_ClientID, Result.m_Data.m_aaMessages[i]);
}
break;
case CSqlPlayerResult::ALL:
for(int i = 0; i < NumMessages; i++)
for(int i = 0; i < CSqlPlayerResult::MAX_MESSAGES; i++)
{
if(Result.m_aaMessages[i][0] == 0)
if(Result.m_Data.m_aaMessages[i][0] == 0)
break;
GameServer()->SendChat(-1, CGameContext::CHAT_ALL, Result.m_aaMessages[i]);
GameServer()->SendChat(-1, CGameContext::CHAT_ALL, Result.m_Data.m_aaMessages[i]);
}
break;
case CSqlPlayerResult::BROADCAST:

View file

@ -36,8 +36,8 @@ void CSqlPlayerResult::SetVariant(Variant v)
{
case DIRECT:
case ALL:
for(int i = 0; i < (int)(sizeof(m_aaMessages)/sizeof(m_aaMessages[0])); i++)
m_aaMessages[i][0] = 0;
for(int i = 0; i < MAX_MESSAGES; i++)
m_Data.m_aaMessages[i][0] = 0;
break;
case BROADCAST:
m_Data.m_Broadcast[0] = 0;
@ -335,7 +335,7 @@ void CSqlScore::MapVote(int ClientID, const char* MapName)
bool CSqlScore::MapVoteThread(CSqlServer* pSqlServer, const CSqlData<CSqlPlayerResult> *pGameData, bool HandleFailure)
{
const CSqlPlayerRequest *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
auto paMessages = pData->m_pResult->m_aaMessages;
auto paMessages = pData->m_pResult->m_Data.m_aaMessages;
if (HandleFailure)
return true;
@ -444,7 +444,7 @@ bool CSqlScore::MapInfoThread(CSqlServer* pSqlServer, const CSqlData<CSqlPlayerR
if(pSqlServer->GetResults()->rowsCount() != 1)
{
str_format(pData->m_pResult->m_aaMessages[0], sizeof(pData->m_pResult->m_aaMessages[0]),
str_format(pData->m_pResult->m_Data.m_aaMessages[0], sizeof(pData->m_pResult->m_Data.m_aaMessages[0]),
"No map like \"%s\" found.", pData->m_Name.Str());
}
else
@ -499,7 +499,7 @@ bool CSqlScore::MapInfoThread(CSqlServer* pSqlServer, const CSqlData<CSqlPlayerR
);
}
str_format(pData->m_pResult->m_aaMessages[0], sizeof(pData->m_pResult->m_aaMessages[0]),
str_format(pData->m_pResult->m_Data.m_aaMessages[0], sizeof(pData->m_pResult->m_Data.m_aaMessages[0]),
"\"%s\" by %s on %s, %s, %d %s%s, %d %s by %d %s%s%s",
aMap, aMapper, aServer, aStars,
Points, Points == 1 ? "point" : "points",
@ -548,7 +548,7 @@ void CSqlScore::SaveScore(int ClientID, float Time, const char *pTimestamp, floa
bool CSqlScore::SaveScoreThread(CSqlServer* pSqlServer, const CSqlData<CSqlPlayerResult> *pGameData, bool HandleFailure)
{
const CSqlScoreData *pData = dynamic_cast<const CSqlScoreData *>(pGameData);
auto paMessages = pData->m_pResult->m_aaMessages;
auto paMessages = pData->m_pResult->m_Data.m_aaMessages;
if(HandleFailure)
{
@ -838,7 +838,7 @@ bool CSqlScore::ShowRankThread(CSqlServer* pSqlServer, const CSqlData<CSqlPlayer
if(pSqlServer->GetResults()->rowsCount() != 1)
{
str_format(pData->m_pResult->m_aaMessages[0], sizeof(pData->m_pResult->m_aaMessages[0]),
str_format(pData->m_pResult->m_Data.m_aaMessages[0], sizeof(pData->m_pResult->m_Data.m_aaMessages[0]),
"%s is not ranked", pData->m_Name.Str());
}
else
@ -849,13 +849,13 @@ bool CSqlScore::ShowRankThread(CSqlServer* pSqlServer, const CSqlData<CSqlPlayer
int Rank = pSqlServer->GetResults()->getInt("Rank");
if(g_Config.m_SvHideScore)
{
str_format(pData->m_pResult->m_aaMessages[0], sizeof(pData->m_pResult->m_aaMessages[0]),
str_format(pData->m_pResult->m_Data.m_aaMessages[0], sizeof(pData->m_pResult->m_Data.m_aaMessages[0]),
"Your time: %02d:%05.2f", (int)(Time/60), Time-((int)Time/60*60));
}
else
{
pData->m_pResult->m_MessageKind = CSqlPlayerResult::ALL;
str_format(pData->m_pResult->m_aaMessages[0], sizeof(pData->m_pResult->m_aaMessages[0]),
str_format(pData->m_pResult->m_Data.m_aaMessages[0], sizeof(pData->m_pResult->m_Data.m_aaMessages[0]),
"%d. %s Time: %02d:%05.2f, requested by %s",
Rank, pSqlServer->GetResults()->getString("Name").c_str(),
(int)(Time/60), Time-((int)Time/60*60), pData->m_RequestingPlayer.Str());
@ -927,7 +927,7 @@ bool CSqlScore::ShowTeamRankThread(CSqlServer* pSqlServer, const CSqlData<CSqlPl
if(Rows < 1)
{
str_format(pData->m_pResult->m_aaMessages[0], sizeof(pData->m_pResult->m_aaMessages[0]),
str_format(pData->m_pResult->m_Data.m_aaMessages[0], sizeof(pData->m_pResult->m_Data.m_aaMessages[0]),
"%s has no team ranks", pData->m_Name.Str());
}
else
@ -949,13 +949,13 @@ bool CSqlScore::ShowTeamRankThread(CSqlServer* pSqlServer, const CSqlData<CSqlPl
if(g_Config.m_SvHideScore)
{
str_format(pData->m_pResult->m_aaMessages[0], sizeof(pData->m_pResult->m_aaMessages[0]),
str_format(pData->m_pResult->m_Data.m_aaMessages[0], sizeof(pData->m_pResult->m_Data.m_aaMessages[0]),
"Your team time: %02d:%05.02f", (int)(Time/60), Time-((int)Time/60*60));
}
else
{
pData->m_pResult->m_MessageKind = CSqlPlayerResult::ALL;
str_format(pData->m_pResult->m_aaMessages[0], sizeof(pData->m_pResult->m_aaMessages[0]),
str_format(pData->m_pResult->m_Data.m_aaMessages[0], sizeof(pData->m_pResult->m_Data.m_aaMessages[0]),
"%d. %s Team time: %02d:%05.02f, requested by %s",
Rank, aNames, (int)(Time/60), Time-((int)Time/60*60), pData->m_RequestingPlayer.Str());
}
@ -1010,21 +1010,21 @@ bool CSqlScore::ShowTop5Thread(CSqlServer* pSqlServer, const CSqlData<CSqlPlayer
pSqlServer->executeSqlQuery(aBuf);
// show top5
strcpy(pData->m_pResult->m_aaMessages[0], "----------- Top 5 -----------");
strcpy(pData->m_pResult->m_Data.m_aaMessages[0], "----------- Top 5 -----------");
int Line = 1;
while(pSqlServer->GetResults()->next())
{
float Time = (float)pSqlServer->GetResults()->getDouble("Time");
int Rank = pSqlServer->GetResults()->getInt("Rank");
str_format(pData->m_pResult->m_aaMessages[Line], sizeof(pData->m_pResult->m_aaMessages[0]),
str_format(pData->m_pResult->m_Data.m_aaMessages[Line], sizeof(pData->m_pResult->m_Data.m_aaMessages[0]),
"%d. %s Time: %02d:%05.2f",
Rank, pSqlServer->GetResults()->getString("Name").c_str(),
(int)(Time/60), Time-((int)Time/60*60)
);
Line++;
}
strcpy(pData->m_pResult->m_aaMessages[Line], "-------------------------------");
strcpy(pData->m_pResult->m_Data.m_aaMessages[Line], "-------------------------------");
pData->m_pResult->m_Done = true;
dbg_msg("sql", "Showing top5 done");
@ -1047,7 +1047,7 @@ void CSqlScore::ShowTeamTop5(int ClientID, int Offset)
bool CSqlScore::ShowTeamTop5Thread(CSqlServer* pSqlServer, const CSqlData<CSqlPlayerResult> *pGameData, bool HandleFailure)
{
const CSqlPlayerRequest *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
auto paMessages = pData->m_pResult->m_aaMessages;
auto paMessages = pData->m_pResult->m_Data.m_aaMessages;
if (HandleFailure)
return true;
@ -1132,7 +1132,7 @@ void CSqlScore::ShowTimes(int ClientID, const char* pName, int Offset)
bool CSqlScore::ShowTimesThread(CSqlServer* pSqlServer, const CSqlData<CSqlPlayerResult> *pGameData, bool HandleFailure)
{
const CSqlPlayerRequest *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
auto paMessages = pData->m_pResult->m_aaMessages;
auto paMessages = pData->m_pResult->m_Data.m_aaMessages;
if (HandleFailure)
return true;
@ -1236,7 +1236,7 @@ void CSqlScore::ShowPoints(int ClientID, const char* pName)
bool CSqlScore::ShowPointsThread(CSqlServer* pSqlServer, const CSqlData<CSqlPlayerResult> *pGameData, bool HandleFailure)
{
const CSqlPlayerRequest *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
auto paMessages = pData->m_pResult->m_aaMessages;
auto paMessages = pData->m_pResult->m_Data.m_aaMessages;
if (HandleFailure)
return true;
@ -1293,7 +1293,7 @@ void CSqlScore::ShowTopPoints(int ClientID, int Offset)
bool CSqlScore::ShowTopPointsThread(CSqlServer* pSqlServer, const CSqlData<CSqlPlayerResult> *pGameData, bool HandleFailure)
{
const CSqlPlayerRequest *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
auto paMessages = pData->m_pResult->m_aaMessages;
auto paMessages = pData->m_pResult->m_Data.m_aaMessages;
if (HandleFailure)
return true;
@ -1821,7 +1821,7 @@ void CSqlScore::GetSaves(int ClientID)
bool CSqlScore::GetSavesThread(CSqlServer* pSqlServer, const CSqlData<CSqlPlayerResult> *pGameData, bool HandleFailure)
{
const CSqlPlayerRequest *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
auto paMessages = pData->m_pResult->m_aaMessages;
auto paMessages = pData->m_pResult->m_Data.m_aaMessages;
if (HandleFailure)
return true;

View file

@ -20,6 +20,10 @@ struct CSqlPlayerResult
std::atomic_bool m_Done;
CSqlPlayerResult();
enum {
MAX_MESSAGES = 7,
};
enum Variant
{
DIRECT,
@ -28,8 +32,8 @@ struct CSqlPlayerResult
MAP_VOTE,
PLAYER_INFO,
} m_MessageKind;
char m_aaMessages[7][512];
union {
char m_aaMessages[MAX_MESSAGES][512];
char m_Broadcast[1024];
struct {
float m_Time;