Merge pull request #21 from sigonasr2/add-pause-to-HamsterNet

Add pause to hamster net
post-jam-minor-fix
sigonasr2, Sig, Sigo 3 months ago committed by GitHub
commit 8c5f326fa8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      CMakeLists.txt
  2. 94
      src/HamsterNet.cpp
  3. 5
      src/HamsterNet.h

@ -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()

@ -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<double, std::milli> duration = std::chrono::system_clock::now() - m_tp1;
return static_cast<int>(duration.count());
return static_cast<int>(duration.count() - m_pause_time);
}
std::pair<HamsterNet::FinishTime,bool> HamsterNet::FinishRace()
@ -238,9 +310,27 @@ std::pair<HamsterNet::FinishTime,bool> HamsterNet::FinishRace()
std::chrono::duration<double, std::milli> duration = m_tp2 - m_tp1;
m_time = static_cast<int>(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<double, std::milli> duration = m_pause_tp2 - m_pause_tp1;
m_pause_time += static_cast<int>(duration.count());
return (hamsterNet__endPause() == 1);
}
std::vector<LeaderboardEntry> HamsterNet::GetLeaderboard(const std::string& map, const int offset, const int limit, const std::string& sortBy, bool ascending)
{
std::vector<LeaderboardEntry> leaderboard;

@ -32,15 +32,20 @@ public:
int GetCurrentRaceTime();
std::pair<FinishTime,bool> FinishRace();
bool StartPause();
bool EndPause();
std::vector<LeaderboardEntry> 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<std::chrono::system_clock> m_tp1, m_tp2;
std::chrono::time_point<std::chrono::system_clock> 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;
};

Loading…
Cancel
Save