diff --git a/CMakeLists.txt b/CMakeLists.txt index a663c006b..3f0ef24f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/game/client/components/race.cpp b/src/game/client/components/race.cpp new file mode 100644 index 000000000..79e30fb82 --- /dev/null +++ b/src/game/client/components/race.cpp @@ -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)); +} diff --git a/src/game/client/components/race.h b/src/game/client/components/race.h index 1f9cabe04..fcfcdf111 100644 --- a/src/game/client/components/race.h +++ b/src/game/client/components/race.h @@ -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