2208: Implement /load without params to show some info r=Learath2 a=def-

about your existing saves. Not the code of course, but only how many saves you have and when you last saved on this map.

Co-authored-by: def <dennis@felsin9.de>
This commit is contained in:
bors[bot] 2020-05-23 20:20:12 +00:00 committed by GitHub
commit f18c42c5ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 77 additions and 4 deletions

View file

@ -676,10 +676,10 @@ void CGameContext::ConLoad(IConsole::IResult *pResult, void *pUserData)
return;
#endif
if (pResult->NumArguments() > 0)
if(pResult->NumArguments() > 0)
pSelf->Score()->LoadTeam(pResult->GetString(0), pResult->m_ClientID);
else
return;
pSelf->Score()->GetSaves(pResult->m_ClientID);
#if defined(CONF_SQL)
if(g_Config.m_SvUseSQL)

View file

@ -26,8 +26,8 @@ CHAT_COMMAND("specvoted", "", CFGFLAG_CHAT|CFGFLAG_SERVER, ConToggleSpecVoted, t
CHAT_COMMAND("dnd", "", CFGFLAG_CHAT|CFGFLAG_SERVER|CFGFLAG_NONTEEHISTORIC, ConDND, this, "Toggle Do Not Disturb (no chat and server messages)")
CHAT_COMMAND("mapinfo", "?r[map]", CFGFLAG_CHAT|CFGFLAG_SERVER, ConMapInfo, this, "Show info about the map with name r gives (current map by default)")
CHAT_COMMAND("timeout", "?s[code]", CFGFLAG_CHAT|CFGFLAG_SERVER, ConTimeout, this, "Set timeout protection code s")
CHAT_COMMAND("save", "r[code]", CFGFLAG_CHAT|CFGFLAG_SERVER, ConSave, this, "Save team with code r to current server. To save to another server, use '/save s r' where s = server (case-sensitive: GER, RUS, etc) and r = code")
CHAT_COMMAND("load", "r[code]", CFGFLAG_CHAT|CFGFLAG_SERVER, ConLoad, this, "Load with code r")
CHAT_COMMAND("save", "r[code]", CFGFLAG_CHAT|CFGFLAG_SERVER, ConSave, this, "Save team with code r to current server. To save to another server, use '/save s r' where s = server (case-sensitive: GER, RUS, etc) and r = code.")
CHAT_COMMAND("load", "?r[code]", CFGFLAG_CHAT|CFGFLAG_SERVER, ConLoad, this, "Load with code r. /load to check your existing saves")
CHAT_COMMAND("map", "?r[map]", CFGFLAG_CHAT|CFGFLAG_SERVER|CFGFLAG_NONTEEHISTORIC, ConMap, this, "Vote a map by name")
CHAT_COMMAND("rankteam", "?r[player name]", CFGFLAG_CHAT|CFGFLAG_SERVER, ConTeamRank, this, "Shows the team rank of player with name r (your team rank by default)")
CHAT_COMMAND("teamrank", "?r[player name]", CFGFLAG_CHAT|CFGFLAG_SERVER, ConTeamRank, this, "Shows the team rank of player with name r (your team rank by default)")

View file

@ -93,6 +93,7 @@ public:
virtual void SaveTeam(int Team, const char *pCode, int ClientID, const char *pServer) = 0;
virtual void LoadTeam(const char *pCode, int ClientID) = 0;
virtual void GetSaves(int ClientID) = 0;
// called when the server is shut down but not on mapchange/reload
virtual void OnShutdown() = 0;

View file

@ -358,6 +358,13 @@ void CFileScore::LoadTeam(const char* Code, int ClientID)
GameServer()->SendChatTarget(ClientID, aBuf);
}
void CFileScore::GetSaves(int ClientID)
{
char aBuf[512];
str_format(aBuf, sizeof(aBuf), "Save-function not supported in file based servers");
GameServer()->SendChatTarget(ClientID, aBuf);
}
void CFileScore::OnShutdown()
{
;

View file

@ -85,6 +85,7 @@ public:
virtual void RandomUnfinishedMap(std::shared_ptr<CRandomMapResult> *ppResult, int ClientID, int stars);
virtual void SaveTeam(int Team, const char* Code, int ClientID, const char* Server);
virtual void LoadTeam(const char* Code, int ClientID);
virtual void GetSaves(int ClientID);
virtual void OnShutdown();

View file

@ -1635,4 +1635,60 @@ bool CSqlScore::LoadTeamThread(CSqlServer* pSqlServer, const CSqlData *pGameData
return false;
}
void CSqlScore::GetSaves(int ClientID)
{
CSqlGetSavesData *Tmp = new CSqlGetSavesData();
Tmp->m_ClientID = ClientID;
Tmp->m_Name = Server()->ClientName(ClientID);
thread_init_and_detach(ExecSqlFunc, new CSqlExecData(GetSavesThread, Tmp, false), "get saves");
}
bool CSqlScore::GetSavesThread(CSqlServer* pSqlServer, const CSqlData *pGameData, bool HandleFailure)
{
const CSqlGetSavesData *pData = dynamic_cast<const CSqlGetSavesData *>(pGameData);
if (HandleFailure)
return true;
try
{
char aBuf[512];
str_format(aBuf, sizeof(aBuf), "SELECT count(*) as NumSaves, UNIX_TIMESTAMP(CURRENT_TIMESTAMP)-UNIX_TIMESTAMP(max(Timestamp)) as Ago FROM %s_saves WHERE Map='%s' AND Savegame regexp '\\n%s\\t';", pSqlServer->GetPrefix(), pData->m_Map.ClrStr(), pData->m_Name.ClrStr());
pSqlServer->executeSqlQuery(aBuf);
if(pSqlServer->GetResults()->next())
{
int NumSaves = pSqlServer->GetResults()->getInt("NumSaves");
int Ago = pSqlServer->GetResults()->getInt("Ago");
char aAgoString[40] = "\0";
char aLastSavedString[60] = "\0";
if(Ago)
{
sqlstr::AgoTimeToString(Ago, aAgoString);
str_format(aLastSavedString, sizeof(aLastSavedString), ", last saved %s ago", aAgoString);
}
str_format(aBuf, sizeof(aBuf), "%s has %d save%s on %s%s", pData->m_Name.Str(), NumSaves, NumSaves == 1 ? "" : "s", pData->m_Map.Str(), aLastSavedString);
pData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf);
}
dbg_msg("sql", "Showing saves done");
return true;
}
catch (sql::SQLException &e)
{
dbg_msg("sql", "MySQL Error: %s", e.what());
dbg_msg("sql", "ERROR: Could not get saves");
pData->GameServer()->SendChatTarget(pData->m_ClientID, "MySQL Error: Could not get saves");
}
catch (CGameContextError &e)
{
dbg_msg("sql", "WARNING: Aborted getting saves due to reload/change of map.");
return true;
}
return false;
}
#endif

View file

@ -144,6 +144,12 @@ struct CSqlTeamLoad : CSqlData
char m_ClientName[MAX_NAME_LENGTH];
};
struct CSqlGetSavesData: CSqlData
{
int m_ClientID;
sqlstr::CSqlString<MAX_NAME_LENGTH> m_Name;
};
struct CSqlRandomMap : CSqlScoreData
{
std::shared_ptr<CRandomMapResult> m_pResult;
@ -183,6 +189,7 @@ class CSqlScore: public IScore
static bool RandomUnfinishedMapThread(CSqlServer* pSqlServer, const CSqlData *pGameData, bool HandleFailure = false);
static bool SaveTeamThread(CSqlServer* pSqlServer, const CSqlData *pGameData, bool HandleFailure = false);
static bool LoadTeamThread(CSqlServer* pSqlServer, const CSqlData *pGameData, bool HandleFailure = false);
static bool GetSavesThread(CSqlServer* pSqlServer, const CSqlData *pGameData, bool HandleFailure = false);
public:
@ -211,6 +218,7 @@ public:
virtual void RandomUnfinishedMap(std::shared_ptr<CRandomMapResult> *ppResult, int ClientID, int stars);
virtual void SaveTeam(int Team, const char* Code, int ClientID, const char* Server);
virtual void LoadTeam(const char* Code, int ClientID);
virtual void GetSaves(int ClientID);
virtual void OnShutdown();
};