some little cleanup

If /times is entered with no params it now shows last 5 times of server in general
This commit is contained in:
Sebastian Wojtowicz 2011-02-15 14:58:42 +01:00 committed by GreYFoXGTi
parent 7455ebfee9
commit 84196b1ef3
4 changed files with 135 additions and 88 deletions

View file

@ -57,7 +57,7 @@ CONSOLE_COMMAND("rules", "", CFGFLAG_SERVER, ConRules, this, "Shows the server r
CONSOLE_COMMAND("team", "?i", CFGFLAG_SERVER, ConJoinTeam, this, "Lets you join team i (shows your team if left blank)", -1) CONSOLE_COMMAND("team", "?i", CFGFLAG_SERVER, ConJoinTeam, this, "Lets you join team i (shows your team if left blank)", -1)
CONSOLE_COMMAND("top5", "?i", CFGFLAG_SERVER, ConTop5, this, "Shows five ranks of the ladder beginning with rank i (1 by default)", -1) CONSOLE_COMMAND("top5", "?i", CFGFLAG_SERVER, ConTop5, this, "Shows five ranks of the ladder beginning with rank i (1 by default)", -1)
#if defined(CONF_SQL) #if defined(CONF_SQL)
CONSOLE_COMMAND("times", "?si", CFGFLAG_SERVER, ConTimes, this, "Shows last 5 times beginning with time i of player with name s (i = 1 by default)", -1) CONSOLE_COMMAND("times", "?s?i", CFGFLAG_SERVER, ConTimes, this, "/times ?s?i shows last 5 times of the server or of a player beginning with name s starting with time i (i = 1 by default)", -1)
#endif #endif
CONSOLE_COMMAND("showothers", "", CFGFLAG_SERVER, ConShowOthers, this, "Whether to showplayers from other teams or not (off by default)", -1) CONSOLE_COMMAND("showothers", "", CFGFLAG_SERVER, ConShowOthers, this, "Whether to showplayers from other teams or not (off by default)", -1)

View file

@ -767,11 +767,20 @@ void CGameContext::ConTimes(IConsole::IResult *pResult, void *pUserData, int Cli
if(!pPlayer) if(!pPlayer)
return; return;
if(pResult->NumArguments() > 0 && pResult->NumArguments() < 3) if(pResult->NumArguments() == 0)
{
pScore->ShowTimes(pPlayer->GetCID(),1);
return;
}
else if(pResult->NumArguments() < 3)
{ {
if (pResult->NumArguments() == 1) if (pResult->NumArguments() == 1)
{ {
pScore->ShowTimes(pPlayer->GetCID(), (str_comp(pResult->GetString(0), "me") == 0) ? pSelf->Server()->ClientName(ClientID) : pResult->GetString(0),1); if(pResult->GetInteger(0) != 0)
pScore->ShowTimes(pPlayer->GetCID(),pResult->GetInteger(0));
else
pScore->ShowTimes(pPlayer->GetCID(), (str_comp(pResult->GetString(0), "me") == 0) ? pSelf->Server()->ClientName(ClientID) : pResult->GetString(0),1);
return; return;
} }
else if (pResult->GetInteger(1) != 0) else if (pResult->GetInteger(1) != 0)
@ -781,8 +790,8 @@ void CGameContext::ConTimes(IConsole::IResult *pResult, void *pUserData, int Cli
} }
} }
pSelf->Console()->PrintResponse(IConsole::OUTPUT_LEVEL_STANDARD, "info", "/times needs 1 or 2 parameter. 1. = name, 2. = start with number"); pSelf->Console()->PrintResponse(IConsole::OUTPUT_LEVEL_STANDARD, "info", "/times needs 0, 1 or 2 parameter. 1. = name, 2. = start number");
pSelf->Console()->PrintResponse(IConsole::OUTPUT_LEVEL_STANDARD, "info", "Example: /times me, /times Hans, /times \"Papa Smurf\" 5"); pSelf->Console()->PrintResponse(IConsole::OUTPUT_LEVEL_STANDARD, "info", "Example: /times, /times me, /times Hans, /times \"Papa Smurf\" 5");
pSelf->Console()->PrintResponse(IConsole::OUTPUT_LEVEL_STANDARD, "info", "Bad: /times Papa Smurf 5 # Good: /times \"Papa Smurf\" 5 "); pSelf->Console()->PrintResponse(IConsole::OUTPUT_LEVEL_STANDARD, "info", "Bad: /times Papa Smurf 5 # Good: /times \"Papa Smurf\" 5 ");
} }
} }

View file

@ -388,70 +388,6 @@ void CSqlScore::ShowRankThread(void *pUser)
lock_release(gs_SqlLock); lock_release(gs_SqlLock);
} }
void CSqlScore::agoTimeToString(int agoTime, char agoString[]){
char aBuf[20];
int times[7] = {
60 * 60 * 24 * 365 ,
60 * 60 * 24 * 30 ,
60 * 60 * 24 * 7,
60 * 60 * 24 ,
60 * 60 ,
60 ,
1
};
char names[7][6] = {
"year",
"month",
"week",
"day",
"hour",
"min",
"sec"
};
int seconds = 0;
char name[6];
int count = 0;
int i = 0;
// finding biggest match
for(i = 0; i<7; i++){
seconds = times[i];
strcpy(name,names[i]);
count = floor(double(agoTime/seconds));
if(count != 0){
break;
}
}
if(count == 1){
str_format(aBuf, sizeof(aBuf), "%d %s", 1 , name);
}else{
str_format(aBuf, sizeof(aBuf), "%d %ss", count , name);
}
strcat(agoString,aBuf);
if (i + 1 < 7) {
// getting second piece now
int seconds2 = times[i+1];
char name2[6];
strcpy(name2,names[i+1]);
// add second piece if it's greater than 0
int count2 = floor(double((agoTime - (seconds * count)) / seconds2));
if (count2 != 0) {
if(count2 == 1){
str_format(aBuf, sizeof(aBuf), " and %d %s", 1 , name2);
}else{
str_format(aBuf, sizeof(aBuf), " and %d %ss", count2 , name2);
}
strcat(agoString,aBuf);
}
}
}
void CSqlScore::ShowRank(int ClientID, const char* pName, bool Search) void CSqlScore::ShowRank(int ClientID, const char* pName, bool Search)
{ {
CSqlScoreData *Tmp = new CSqlScoreData(); CSqlScoreData *Tmp = new CSqlScoreData();
@ -534,32 +470,54 @@ void CSqlScore::ShowTimesThread(void *pUser)
strcpy(originalName,pData->m_aName); strcpy(originalName,pData->m_aName);
pData->m_pSqlData->ClearString(pData->m_aName); pData->m_pSqlData->ClearString(pData->m_aName);
// check sort methode
char aBuf[512]; char aBuf[512];
str_format(aBuf, sizeof(aBuf), "SELECT Time, UNIX_TIMESTAMP(CURRENT_TIMESTAMP)-UNIX_TIMESTAMP(Timestamp) as Ago, UNIX_TIMESTAMP(Timestamp) as Stamp FROM %s_%s_race WHERE Name = '%s' ORDER BY Ago ASC LIMIT %d, 5;", pData->m_pSqlData->m_pPrefix, pData->m_pSqlData->m_aMap, pData->m_aName, pData->m_Num-1);
if(pData->m_Search) // last 5 times of a player
str_format(aBuf, sizeof(aBuf), "SELECT Time, UNIX_TIMESTAMP(CURRENT_TIMESTAMP)-UNIX_TIMESTAMP(Timestamp) as Ago, UNIX_TIMESTAMP(Timestamp) as Stamp FROM %s_%s_race WHERE Name = '%s' ORDER BY Ago ASC LIMIT %d, 5;", pData->m_pSqlData->m_pPrefix, pData->m_pSqlData->m_aMap, pData->m_aName, pData->m_Num-1);
else // last 5 times of server
str_format(aBuf, sizeof(aBuf), "SELECT Name, Time, UNIX_TIMESTAMP(CURRENT_TIMESTAMP)-UNIX_TIMESTAMP(Timestamp) as Ago, UNIX_TIMESTAMP(Timestamp) as Stamp FROM %s_%s_race ORDER BY Ago ASC LIMIT %d, 5;", pData->m_pSqlData->m_pPrefix, pData->m_pSqlData->m_aMap, pData->m_Num-1);
pData->m_pSqlData->m_pResults = pData->m_pSqlData->m_pStatement->executeQuery(aBuf); pData->m_pSqlData->m_pResults = pData->m_pSqlData->m_pStatement->executeQuery(aBuf);
// show top5 // show top5
pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, "--------- Last 5 Times ---------"); if(pData->m_pSqlData->m_pResults->rowsCount() == 0){
pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, "There are no times in the specified range");
goto end;
}
str_format(aBuf, sizeof(aBuf), "------------ Last Times No %d - %d ------------",pData->m_Num,pData->m_Num + pData->m_pSqlData->m_pResults->rowsCount() - 1);
pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf);
float pTime = 0;
int pSince = 0;
int pStamp = 0;
float Time = 0;
int since = 0;
int stamp = 0;
while(pData->m_pSqlData->m_pResults->next()) while(pData->m_pSqlData->m_pResults->next())
{ {
char agoString[40] = "\0"; char pAgoString[40] = "\0";
since = (int)pData->m_pSqlData->m_pResults->getInt("Ago"); pSince = (int)pData->m_pSqlData->m_pResults->getInt("Ago");
stamp = (int)pData->m_pSqlData->m_pResults->getInt("Stamp"); pStamp = (int)pData->m_pSqlData->m_pResults->getInt("Stamp");
Time = (float)pData->m_pSqlData->m_pResults->getDouble("Time"); pTime = (float)pData->m_pSqlData->m_pResults->getDouble("Time");
agoTimeToString(since,agoString);
agoTimeToString(pSince,pAgoString);
if(stamp == 0) // stamp is 00:00:00 cause it's an old entry from old times where there where no stamps yet if(pData->m_Search) // last 5 times of a player
str_format(aBuf, sizeof(aBuf), "Time: %d min %.2f sec, don't know how long ago", (int)(Time/60), Time-((int)Time/60*60)); {
else if(pStamp == 0) // stamp is 00:00:00 cause it's an old entry from old times where there where no stamps yet
str_format(aBuf, sizeof(aBuf), "Time: %d min %.2f sec, %s ago", (int)(Time/60), Time-((int)Time/60*60),agoString); str_format(aBuf, sizeof(aBuf), "%d min %.2f sec, don't know how long ago", (int)(pTime/60), pTime-((int)pTime/60*60));
else
str_format(aBuf, sizeof(aBuf), "%s ago, %d min %.2f sec", pAgoString,(int)(pTime/60), pTime-((int)pTime/60*60));
}
else // last 5 times of the server
{
if(pStamp == 0) // stamp is 00:00:00 cause it's an old entry from old times where there where no stamps yet
str_format(aBuf, sizeof(aBuf), "%s, %d m %.2f s, don't know when", pData->m_pSqlData->m_pResults->getString("Name").c_str(), (int)(pTime/60), pTime-((int)pTime/60*60));
else
str_format(aBuf, sizeof(aBuf), "%s, %s ago, %d m %.2f s", pData->m_pSqlData->m_pResults->getString("Name").c_str(), pAgoString, (int)(pTime/60), pTime-((int)pTime/60*60));
}
pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf); pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf);
} }
pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, "------------------------------------"); pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, "----------------------------------------------------");
dbg_msg("SQL", "Showing times done"); dbg_msg("SQL", "Showing times done");
@ -574,11 +532,10 @@ void CSqlScore::ShowTimesThread(void *pUser)
dbg_msg("SQL", aBuf); dbg_msg("SQL", aBuf);
dbg_msg("SQL", "ERROR: Could not show times"); dbg_msg("SQL", "ERROR: Could not show times");
} }
end:
// disconnect from database // disconnect from database
pData->m_pSqlData->Disconnect(); pData->m_pSqlData->Disconnect();
} }
delete pData; delete pData;
lock_release(gs_SqlLock); lock_release(gs_SqlLock);
@ -597,6 +554,21 @@ void CSqlScore::ShowTop5(int ClientID, int Debut)
#endif #endif
} }
void CSqlScore::ShowTimes(int ClientID, int Debut)
{
CSqlScoreData *Tmp = new CSqlScoreData();
Tmp->m_Num = Debut;
Tmp->m_ClientID = ClientID;
Tmp->m_pSqlData = this;
Tmp->m_Search = false;
void *TimesThread = thread_create(ShowTimesThread, Tmp);
#if defined(CONF_FAMILY_UNIX)
pthread_detach((pthread_t)TimesThread);
#endif
}
void CSqlScore::ShowTimes(int ClientID, const char* pName, int Debut) void CSqlScore::ShowTimes(int ClientID, const char* pName, int Debut)
{ {
CSqlScoreData *Tmp = new CSqlScoreData(); CSqlScoreData *Tmp = new CSqlScoreData();
@ -604,7 +576,8 @@ void CSqlScore::ShowTimes(int ClientID, const char* pName, int Debut)
Tmp->m_ClientID = ClientID; Tmp->m_ClientID = ClientID;
str_copy(Tmp->m_aName, pName, sizeof(Tmp->m_aName)); str_copy(Tmp->m_aName, pName, sizeof(Tmp->m_aName));
Tmp->m_pSqlData = this; Tmp->m_pSqlData = this;
Tmp->m_Search = true;
void *TimesThread = thread_create(ShowTimesThread, Tmp); void *TimesThread = thread_create(ShowTimesThread, Tmp);
#if defined(CONF_FAMILY_UNIX) #if defined(CONF_FAMILY_UNIX)
pthread_detach((pthread_t)TimesThread); pthread_detach((pthread_t)TimesThread);
@ -647,4 +620,68 @@ void CSqlScore::NormalizeMapname(char *pString) {
} }
} }
} }
void CSqlScore::agoTimeToString(int agoTime, char agoString[]){
char aBuf[20];
int times[7] = {
60 * 60 * 24 * 365 ,
60 * 60 * 24 * 30 ,
60 * 60 * 24 * 7,
60 * 60 * 24 ,
60 * 60 ,
60 ,
1
};
char names[7][6] = {
"year",
"month",
"week",
"day",
"hour",
"min",
"sec"
};
int seconds = 0;
char name[6];
int count = 0;
int i = 0;
// finding biggest match
for(i = 0; i<7; i++){
seconds = times[i];
strcpy(name,names[i]);
count = floor(agoTime/seconds);
if(count != 0){
break;
}
}
if(count == 1){
str_format(aBuf, sizeof(aBuf), "%d %s", 1 , name);
}else{
str_format(aBuf, sizeof(aBuf), "%d %ss", count , name);
}
strcat(agoString,aBuf);
if (i + 1 < 7) {
// getting second piece now
int seconds2 = times[i+1];
char name2[6];
strcpy(name2,names[i+1]);
// add second piece if it's greater than 0
int count2 = floor((agoTime - (seconds * count)) / seconds2);
if (count2 != 0) {
if(count2 == 1){
str_format(aBuf, sizeof(aBuf), " and %d %s", 1 , name2);
}else{
str_format(aBuf, sizeof(aBuf), " and %d %ss", count2 , name2);
}
strcat(agoString,aBuf);
}
}
}
#endif #endif

View file

@ -58,6 +58,7 @@ public:
virtual void SaveScore(int ClientID, float Time, CCharacter *pChar); virtual void SaveScore(int ClientID, float Time, CCharacter *pChar);
virtual void ShowRank(int ClientID, const char* pName, bool Search=false); virtual void ShowRank(int ClientID, const char* pName, bool Search=false);
virtual void ShowTimes(int ClientID, const char* pName, int Debut=1); virtual void ShowTimes(int ClientID, const char* pName, int Debut=1);
virtual void ShowTimes(int ClientID, int Debut=1);
virtual void ShowTop5(int ClientID, int Debut=1); virtual void ShowTop5(int ClientID, int Debut=1);
static void agoTimeToString(int agoTime, char agoStrign[]); static void agoTimeToString(int agoTime, char agoStrign[]);
}; };