mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 01:58:19 +00:00
Move race helper functions to own cpp file
Fix some style issues
This commit is contained in:
parent
b6a3a42aa6
commit
a396e40c5e
|
@ -589,6 +589,7 @@ if(CLIENT)
|
|||
components/particles.h
|
||||
components/players.cpp
|
||||
components/players.h
|
||||
components/race.cpp
|
||||
components/race.h
|
||||
components/race_demo.cpp
|
||||
components/race_demo.h
|
||||
|
|
48
src/game/client/components/race.cpp
Normal file
48
src/game/client/components/race.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include "race.h"
|
||||
|
||||
int CRaceHelper::TimeFromSecondsStr(const char *pStr) // x.xxx
|
||||
{
|
||||
int Time = str_toint(pStr) * 1000;
|
||||
while(*pStr >= '0' && *pStr <= '9')
|
||||
pStr++;
|
||||
if(*pStr == '.' || *pStr == ',')
|
||||
{
|
||||
pStr++;
|
||||
static const int s_aMult[3] = { 100, 10, 1 };
|
||||
for(int i = 0; pStr[i] >= '0' && pStr[i] <= '9' && i < 3; i++)
|
||||
Time += (pStr[i] - '0') * s_aMult[i];
|
||||
}
|
||||
return Time;
|
||||
}
|
||||
|
||||
int CRaceHelper::TimeFromStr(const char *pStr) // x minute(s) x.xxx second(s)
|
||||
{
|
||||
static const char * const s_pMinutesStr = " minute(s) ";
|
||||
static const char * const s_pSecondsStr = " second(s)";
|
||||
|
||||
const char *pSeconds = str_find(pStr, s_pSecondsStr);
|
||||
if(!pSeconds)
|
||||
return 0;
|
||||
|
||||
const char *pMinutes = str_find(pStr, s_pMinutesStr);
|
||||
if(pMinutes)
|
||||
return str_toint(pStr) * 60 * 1000 + TimeFromSecondsStr(pMinutes + str_length(s_pMinutesStr));
|
||||
else
|
||||
return TimeFromSecondsStr(pStr);
|
||||
}
|
||||
|
||||
int CRaceHelper::TimeFromFinishMessage(const char *pStr, char *pNameBuf, int NameBufSize) // xxx finished in: x minute(s) x.xxx second(s)
|
||||
{
|
||||
static const char * const s_pFinishedStr = " finished in: ";
|
||||
const char *pFinished = str_find(pStr, s_pFinishedStr);
|
||||
if(!pFinished)
|
||||
return 0;
|
||||
|
||||
int FinishedPos = pFinished - pStr;
|
||||
if(FinishedPos == 0 || FinishedPos >= NameBufSize)
|
||||
return 0;
|
||||
|
||||
str_copy(pNameBuf, pStr, FinishedPos + 1);
|
||||
|
||||
return TimeFromStr(pFinished + str_length(s_pFinishedStr));
|
||||
}
|
|
@ -7,49 +7,9 @@ class CRaceHelper
|
|||
{
|
||||
public:
|
||||
// these functions return the time in milliseconds, time 0 is invalid
|
||||
|
||||
static int TimeFromSecondsStr(const char *pStr) // x.xxx
|
||||
{
|
||||
int Time = str_toint(pStr) * 1000;
|
||||
while(*pStr >= '0' && *pStr <= '9') pStr++;
|
||||
if(*pStr == '.' || *pStr == ',')
|
||||
{
|
||||
pStr++;
|
||||
const int Mult[3] = { 100, 10, 1 };
|
||||
for(int i = 0; pStr[i] >= '0' && pStr[i] <= '9' && i < 3; i++)
|
||||
Time += (pStr[i] - '0') * Mult[i];
|
||||
}
|
||||
return Time;
|
||||
}
|
||||
|
||||
static int TimeFromStr(const char *pStr) // x minute(s) x.xx second(s)
|
||||
{
|
||||
static const char *pMinutesStr = " minute(s) ";
|
||||
static const char *pSecondsStr = " second(s)";
|
||||
|
||||
const char *pSeconds = str_find(pStr, pSecondsStr);
|
||||
if(!pSeconds)
|
||||
return 0;
|
||||
|
||||
const char *pMinutes = str_find(pStr, pMinutesStr);
|
||||
if(pMinutes)
|
||||
return str_toint(pStr) * 60 * 1000 + TimeFromSecondsStr(pMinutes + str_length(pMinutesStr));
|
||||
else
|
||||
return TimeFromSecondsStr(pStr);
|
||||
}
|
||||
|
||||
static int TimeFromFinishMessage(const char *pStr, char *pNameBuf, int NameBufSize) // xxx finished in: x minute(s) x.xx second(s)
|
||||
{
|
||||
static const char *pFinishedStr = " finished in: ";
|
||||
const char *pFinished = str_find(pStr, pFinishedStr);
|
||||
int FinishedPos = pFinished - pStr;
|
||||
if(!pFinished || FinishedPos == 0 || FinishedPos >= NameBufSize)
|
||||
return 0;
|
||||
|
||||
str_copy(pNameBuf, pStr, FinishedPos + 1);
|
||||
|
||||
return TimeFromStr(pFinished + str_length(pFinishedStr));
|
||||
}
|
||||
static int TimeFromSecondsStr(const char *pStr); // x.xxx
|
||||
static int TimeFromStr(const char *pStr); // x minute(s) x.xx second(s)
|
||||
static int TimeFromFinishMessage(const char *pStr, char *pNameBuf, int NameBufSize); // xxx finished in: x minute(s) x.xx second(s)
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue