Read SaveID from database on /load

This commit is contained in:
Zwelf 2020-06-05 14:16:44 +02:00
parent dbf6ac88f6
commit c278d8dbc3
3 changed files with 44 additions and 13 deletions

View file

@ -1466,11 +1466,10 @@ void CSqlScore::SaveTeam(int ClientID, const char* Code, const char* Server)
CSqlTeamSave *Tmp = new CSqlTeamSave(SaveResult);
Tmp->m_Code = Code;
Tmp->m_Map = g_Config.m_SvMap;
FormatUuid(RandomUuid(), Tmp->m_Uuid, UUID_MAXSTRSIZE);
Tmp->m_pResult->m_SaveID = RandomUuid();
str_copy(Tmp->m_Server, Server, sizeof(Tmp->m_Server));
str_copy(Tmp->m_ClientName, this->Server()->ClientName(ClientID), sizeof(Tmp->m_ClientName));
// TODO: log event in teehistorian
pController->m_Teams.KillSavedTeam(ClientID, Team);
char aBuf[512];
// TODO: better message, maybe hint that one should wait until the next message to leave
@ -1486,6 +1485,9 @@ bool CSqlScore::SaveTeamThread(CSqlServer* pSqlServer, const CSqlData<CSqlSaveRe
{
const CSqlTeamSave *pData = dynamic_cast<const CSqlTeamSave *>(pGameData);
char aSaveID[UUID_MAXSTRSIZE];
FormatUuid(pData->m_pResult->m_SaveID, aSaveID, UUID_MAXSTRSIZE);
sqlstr::CSqlString<65536> SaveState = pData->m_pResult->m_SavedTeam.GetString();
if(HandleFailure)
{
@ -1503,7 +1505,7 @@ bool CSqlScore::SaveTeamThread(CSqlServer* pSqlServer, const CSqlData<CSqlSaveRe
"INSERT IGNORE INTO %%s_saves(Savegame, Map, Code, Timestamp, Server, SaveID, DDNet7) "
"VALUES ('%s', '%s', '%s', CURRENT_TIMESTAMP(), '%s', '%s', false)",
SaveState.ClrStr(), pData->m_Map.ClrStr(),
pData->m_Code.ClrStr(), pData->m_Server, pData->m_Uuid
pData->m_Code.ClrStr(), pData->m_Server, aSaveID
);
io_write(File, aBuf, str_length(aBuf));
io_write_newline(File);
@ -1537,11 +1539,12 @@ bool CSqlScore::SaveTeamThread(CSqlServer* pSqlServer, const CSqlData<CSqlSaveRe
if (pSqlServer->GetResults()->rowsCount() == 0)
{
char aBuf[65536];
str_format(aBuf, sizeof(aBuf),
"INSERT IGNORE INTO %s_saves(Savegame, Map, Code, Timestamp, Server, SaveID, DDNet7) "
"VALUES ('%s', '%s', '%s', CURRENT_TIMESTAMP(), '%s', '%s', false)",
pSqlServer->GetPrefix(), SaveState.ClrStr(), pData->m_Map.ClrStr(),
pData->m_Code.ClrStr(), pData->m_Server, pData->m_Uuid
pData->m_Code.ClrStr(), pData->m_Server, aSaveID
);
dbg_msg("sql", "%s", aBuf);
pSqlServer->executeSql(aBuf);
@ -1631,7 +1634,10 @@ bool CSqlScore::LoadTeamThread(CSqlServer* pSqlServer, const CSqlData<CSqlSaveRe
str_format(aBuf, sizeof(aBuf), "lock tables %s_saves write;", pSqlServer->GetPrefix());
pSqlServer->executeSql(aBuf);
str_format(aBuf, sizeof(aBuf),
"SELECT Savegame, Server, UNIX_TIMESTAMP(CURRENT_TIMESTAMP)-UNIX_TIMESTAMP(Timestamp) AS Ago "
"SELECT "
"Savegame, Server, "
"UNIX_TIMESTAMP(CURRENT_TIMESTAMP)-UNIX_TIMESTAMP(Timestamp) AS Ago, "
"(UNHEX(REPLACE(SaveID, '-',''))) AS SaveID "
"FROM %s_saves "
"where Code = '%s' AND Map = '%s' AND DDNet7 = false AND Savegame LIKE '%%\\n%s\\t%%';",
pSqlServer->GetPrefix(), pData->m_Code.ClrStr(), pData->m_Map.ClrStr(), pData->m_RequestingPlayer.ClrStr());
@ -1659,6 +1665,21 @@ bool CSqlScore::LoadTeamThread(CSqlServer* pSqlServer, const CSqlData<CSqlSaveRe
g_Config.m_SvSaveGamesDelay - Since);
goto end;
}
if(pSqlServer->GetResults()->isNull("SaveID"))
{
memset(pData->m_pResult->m_SaveID.m_aData, 0, sizeof(pData->m_pResult->m_SaveID.m_aData));
}
else
{
auto SaveID = pSqlServer->GetResults()->getBlob("SaveID");
SaveID->read((char *) pData->m_pResult->m_SaveID.m_aData, 16);
if(SaveID->gcount() != 16)
{
strcpy(pData->m_pResult->m_aMessage, "Unable to load savegame: SaveID corrupted");
goto end;
}
}
auto SaveString = pSqlServer->GetResults()->getString("Savegame");
int Num = pData->m_pResult->m_SavedTeam.LoadString(SaveString.c_str());

View file

@ -58,6 +58,7 @@ public:
char m_aBroadcast[512];
CSaveTeam m_SavedTeam;
int m_RequestingPlayer;
CUuid m_SaveID;
};
class CSqlMapResult {
@ -172,7 +173,6 @@ struct CSqlTeamSave : CSqlData<CSqlSaveResult>
sqlstr::CSqlString<128> m_Map;
sqlstr::CSqlString<128> m_Code;
char m_Uuid[UUID_MAXSTRSIZE];
char m_Server[5];
};
@ -181,7 +181,7 @@ struct CSqlTeamLoad : CSqlData<CSqlSaveResult>
using CSqlData<CSqlSaveResult>::CSqlData;
sqlstr::CSqlString<128> m_Code;
sqlstr::CSqlString<128> m_Map;
sqlstr::CSqlString<128> m_RequestingPlayer;
sqlstr::CSqlString<MAX_NAME_LENGTH> m_RequestingPlayer;
int m_ClientID;
// struct holding all player names in the team or an empty string
char m_aClientNames[MAX_CLIENTS][MAX_NAME_LENGTH];

View file

@ -337,9 +337,7 @@ void CGameTeams::ForceLeaveTeam(int ClientID)
SetTeamLock(m_Core.Team(ClientID), false);
ResetInvited(m_Core.Team(ClientID));
m_Practice[m_Core.Team(ClientID)] = false;
#if defined(CONF_SQL)
m_pSaveTeamResult[m_Core.Team(ClientID)] = nullptr;
#endif
// do not reset SaveTeamResult, because it should be logged into teehistorian even if the team leaves
}
}
@ -694,19 +692,31 @@ void CGameTeams::ProcessSaveTeam()
GameServer()->SendBroadcast(m_pSaveTeamResult[Team]->m_aBroadcast, -1);
if(m_pSaveTeamResult[Team]->m_aMessage[0] != '\0')
GameServer()->SendChatTeam(Team, m_pSaveTeamResult[Team]->m_aMessage);
// TODO: log load/save success/fail in teehistorian
switch(m_pSaveTeamResult[Team]->m_Status)
{
case CSqlSaveResult::SAVE_SUCCESS:
{
ResetSavedTeam(m_pSaveTeamResult[Team]->m_RequestingPlayer, Team);
char aSaveID[UUID_MAXSTRSIZE];
FormatUuid(m_pSaveTeamResult[Team]->m_SaveID, aSaveID, UUID_MAXSTRSIZE);
dbg_msg("save", "Save successful: %s", aSaveID);
break;
}
case CSqlSaveResult::SAVE_FAILED:
case CSqlSaveResult::LOAD_SUCCESS:
m_pSaveTeamResult[Team]->m_SavedTeam.load(Team);
if(m_MembersCount[Team] > 0)
m_pSaveTeamResult[Team]->m_SavedTeam.load(Team);
break;
case CSqlSaveResult::LOAD_SUCCESS:
{
if(m_MembersCount[Team] > 0)
m_pSaveTeamResult[Team]->m_SavedTeam.load(Team);
char aSaveID[UUID_MAXSTRSIZE];
FormatUuid(m_pSaveTeamResult[Team]->m_SaveID, aSaveID, UUID_MAXSTRSIZE);
dbg_msg("save", "Load successful: %s", aSaveID);
break;
}
case CSqlSaveResult::LOAD_FAILED:
// just printing the error message to the team above is enough
break;
}
m_pSaveTeamResult[Team] = nullptr;