diff --git a/CMakeLists.txt b/CMakeLists.txt index 55c1a7b..d1c8cb9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -247,7 +247,7 @@ if (EMSCRIPTEN) -sUSE_LIBPNG=1 -sLLD_REPORT_UNDEFINED -s ASYNCIFY - -sASYNCIFY_IMPORTS=hamsterNet__initSession,hamsterNet__setRacerName,hamsterNet__startRace,hamsterNet__finishRace,hamsterNet__getLeaderboard + -sASYNCIFY_IMPORTS=hamsterNet__initSession,hamsterNet__setRacerName,hamsterNet__startRace,hamsterNet__finishRace,hamsterNet__getLeaderboard,hamsterNet__startPause,hamsterNet__endPause --preload-file ${SOURCE_DATA_DIR}@assets) else() target_link_options( @@ -259,7 +259,7 @@ if (EMSCRIPTEN) -sUSE_LIBPNG=1 -sLLD_REPORT_UNDEFINED -s ASYNCIFY - -sASYNCIFY_IMPORTS=hamsterNet__initSession,hamsterNet__setRacerName,hamsterNet__startRace,hamsterNet__finishRace,hamsterNet__getLeaderboard + -sASYNCIFY_IMPORTS=hamsterNet__initSession,hamsterNet__setRacerName,hamsterNet__startRace,hamsterNet__finishRace,hamsterNet__getLeaderboard,hamsterNet__startPause,hamsterNet__endPause ) endif() diff --git a/src/HamsterNet.cpp b/src/HamsterNet.cpp index 1a83593..855b110 100644 --- a/src/HamsterNet.cpp +++ b/src/HamsterNet.cpp @@ -125,6 +125,63 @@ EM_JS(int, hamsterNet__finishRace, (const char* raceMap, const char* raceColor, }); }); +EM_JS(int, hamsterNet__startPause, (), { + + + return Asyncify.handleSleep(function(wakeUp) { + fetch('/pause', { + method: 'POST', + credentials: 'same-origin', + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({raceId: Module.hamsterRaceId}) + }).then((response) => + { + return response.ok + ? response.json() + : Promise.reject(new Error("Unexpected response")); + }) + .then((message) => + { + wakeUp(1); + }) + .catch((err) => + { + console.error(err.message); + wakeUp(0); + }) + }); +}); + +EM_JS(int, hamsterNet__endPause, (), { + + return Asyncify.handleSleep(function(wakeUp) { + fetch('/pause', { + method: 'PATCH', + credentials: 'same-origin', + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({raceId: Module.hamsterRaceId}) + }).then((response) => + { + return response.ok + ? response.json() + : Promise.reject(new Error("Unexpected response")); + }) + .then((message) => + { + wakeUp(1); + }) + .catch((err) => + { + console.error(err.message); + wakeUp(0); + }) + }); +}); + EM_JS(int, hamsterNet__getLeaderboard, (const char* map, const char* sortBy, const int offset, const int limit, const int ascending), { return Asyncify.handleSleep(function(wakeUp) { @@ -197,6 +254,19 @@ extern "C" std::cout << "hamsterNet__getLeaderboard is not implemented on this platform, artificially succeeding.\n"; return 1; } + + int hamsterNet__startPause() + { + std::cout << "hamsterNet__startPause is not implemented on this platform, artificially succeeding.\n"; + return 1; + } + + int hamsterNet__endPause() + { + std::cout << "hamsterNet__endPause is not implemented on this platform, artificially succeeding.\n"; + return 1; + } + } #endif @@ -224,12 +294,14 @@ bool HamsterNet::StartRace(const std::string& map) m_map = map; m_tp1 = std::chrono::system_clock::now(); m_tp2 = std::chrono::system_clock::now(); + m_pause_time = 0; + return (hamsterNet__startRace() == 1); } int HamsterNet::GetCurrentRaceTime(){ std::chrono::duration duration = std::chrono::system_clock::now() - m_tp1; - return static_cast(duration.count()); + return static_cast(duration.count() - m_pause_time); } std::pair HamsterNet::FinishRace() @@ -238,9 +310,27 @@ std::pair HamsterNet::FinishRace() std::chrono::duration duration = m_tp2 - m_tp1; m_time = static_cast(duration.count()); - return {m_time,(hamsterNet__finishRace(m_map.c_str(), m_color.c_str(), m_time) == 1)}; + return {m_time,(hamsterNet__finishRace(m_map.c_str(), m_color.c_str(), m_time - m_pause_time) == 1)}; } +bool HamsterNet::StartPause() +{ + m_pause_tp1 = std::chrono::system_clock::now(); + m_pause_tp2 = std::chrono::system_clock::now(); + + return (hamsterNet__startPause() == 1); +} + +bool HamsterNet::EndPause() +{ + m_pause_tp2 = std::chrono::system_clock::now(); + std::chrono::duration duration = m_pause_tp2 - m_pause_tp1; + m_pause_time += static_cast(duration.count()); + + return (hamsterNet__endPause() == 1); +} + + std::vector HamsterNet::GetLeaderboard(const std::string& map, const int offset, const int limit, const std::string& sortBy, bool ascending) { std::vector leaderboard; diff --git a/src/HamsterNet.h b/src/HamsterNet.h index 7f8700b..5d7090f 100644 --- a/src/HamsterNet.h +++ b/src/HamsterNet.h @@ -32,16 +32,21 @@ public: int GetCurrentRaceTime(); std::pair FinishRace(); + bool StartPause(); + bool EndPause(); + std::vector GetLeaderboard(const std::string& map, const int offset = 0, const int limit = 100, const std::string& sortBy = "time", bool ascending = true); private: std::chrono::time_point m_tp1, m_tp2; + std::chrono::time_point m_pause_tp1, m_pause_tp2; std::string m_color; std::string m_name; std::string m_map; int m_time; - + int m_pause_time; + std::string raceId; };