Added PB tracking for both emscripten and native builds of the game.

sigonasr2 6 months ago
parent e2b5b873ef
commit c8e02a23a8
  1. 1
      PBData
  2. 1
      src/Hamster.cpp
  3. 2
      src/Hamster.h
  4. 69
      src/HamsterGame.cpp
  5. 22
      src/HamsterGame.h
  6. 9
      src/HamsterNet.cpp
  7. 4
      src/HamsterNet.h

@ -0,0 +1 @@
2147483647 2147483647 2147483647 2147483647 109086 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647

@ -289,6 +289,7 @@ void Hamster::CreateHamsters(const HamsterGame::GameMode mode){
}
void Hamster::MoveHamstersToSpawn(const geom2d::rect<int>startingLoc){
if(HAMSTER_LIST.size()==0)Hamster::CreateHamsters(HamsterGame::Game().GetGameMode());
int MAX_AI_FILES{100};
int aiFileCount{0};
while(MAX_AI_FILES>0){

@ -129,7 +129,7 @@ class Hamster{
float boostTimer{};
float canCollectWheelPowerupTimer{};
float SEARCH_RANGE{1.f};
HamsterAI::AIType aiLevel;
HamsterAI::AIType aiLevel{HamsterAI::AIType::NORMAL};
public:
Hamster(const vf2d spawnPos,const std::string&img,const PlayerControlled IsPlayerControlled=NPC);
static const Hamster&GetPlayer();

@ -13,6 +13,8 @@ std::unordered_map<std::string,Renderable>HamsterGame::GFX;
const std::string HamsterGame::ASSETS_DIR{"assets/"};
HamsterGame*HamsterGame::self{nullptr};
std::unordered_map<uint32_t,Animate2D::FrameSequence>HamsterGame::ANIMATED_TILE_IDS;
std::unordered_map<std::string,int>HamsterGame::mapPBs;
const int HamsterGame::UNPLAYED{std::numeric_limits<int>::max()};
HamsterGame::HamsterGame(const std::string&appName){
sAppName=appName;
@ -20,6 +22,7 @@ HamsterGame::HamsterGame(const std::string&appName){
}
bool HamsterGame::OnUserCreate(){
LoadPBs();
audio.SetBackgroundPlay(true);
olc::GFX3D::ConfigureDisplay();
camera=Camera2D{SCREEN_FRAME.size};
@ -195,6 +198,7 @@ void HamsterGame::LoadLevel(const std::string&mapName){
SetDrawTarget(nullptr);
audio.Play(bgm.at(currentMap.value().GetData().GetBGM()),true);
Hamster::MoveHamstersToSpawn(currentMap.value().GetData().GetSpawnZone());
}
void HamsterGame::UpdateGame(const float fElapsedTime){
@ -368,11 +372,10 @@ bool HamsterGame::OnUserUpdate(float fElapsedTime){
net.SetName("Sig");
net.SetColor("Yellow");
LoadLevel("StageV.tmx"); //THIS IS TEMPORARY.
Hamster::CreateHamsters(mode);
Hamster::MoveHamstersToSpawn(currentMap.value().GetData().GetSpawnZone());
camera.SetTarget(Hamster::GetPlayer().GetPos());
net.StartRace(currentMapName);
}
runTime+=fElapsedTime;
UpdateGame(fElapsedTime);
DrawGame();
@ -592,7 +595,67 @@ const Difficulty&HamsterGame::GetMapDifficulty()const{
}
void HamsterGame::OnPlayerFinishedRace(){
net.FinishRace();
std::pair<HamsterNet::FinishTime,bool>result{net.FinishRace()};
std::cout<<"Finish Time: "<<result.first<<std::endl;
if(result.second)HamsterGame::SavePB(GetCurrentMapName(),result.first);
}
void HamsterGame::SavePB(const std::string&mapName,int ms){
auto it=std::find(Game().mapNameList.begin(),Game().mapNameList.end(),mapName);
if(it==Game().mapNameList.end())throw std::runtime_error{std::format("WARNING! Somehow got a map name that is invalid! Map Name: {}. THIS SHOULD NOT BE HAPPENING!",mapName)};
bool beatPB{false};
beatPB=mapPBs[mapName]==UNPLAYED||ms<mapPBs[mapName];
if(beatPB){
mapPBs[mapName]=ms;
Game().emscripten_temp_val=std::to_string(ms);
#ifdef __EMSCRIPTEN__
emscripten_idb_async_store("hamster",Game().mapNameList[std::distance(Game().mapNameList.begin(),it)].c_str(),&Game().emscripten_temp_val,Game().emscripten_temp_val.length(),0,[](void*args){
std::cout<<"Success!"<<std::endl;
},
[](void*args){
std::cout<<"Failed"<<std::endl;
});
#else
std::ofstream file{"PBData"};
for(const std::string&mapName:Game().mapNameList){
file<<mapPBs[mapName]<<" ";
}
file.close();
#endif
}
}
void HamsterGame::LoadPBs(){
for(int i{0};const std::string&mapName:Game().mapNameList){
mapPBs[mapName]=UNPLAYED;
#ifdef __EMSCRIPTEN__
emscripten_idb_async_load("hamster",Game().mapNameList[i].c_str(),&Game().mapNameList[i],[](void*arg,void*data,int length){
std::string rawMetadata=(char*)data;
std::cout<<rawMetadata<<std::endl;
HamsterGame::mapPBs[*((std::string*)(arg))]=stoi(rawMetadata);
std::cout<<std::format("Success! PB for {} is {}",*((std::string*)(arg)),HamsterGame::mapPBs[*((std::string*)(arg))])<<std::endl;
},
[](void*arg){
std::cout<<std::format("Failed to retrieve PB for {}",*((std::string*)(arg)))<<std::endl;
});
#else
std::ifstream file{"PBData"};
int readCounter{0};
while(file.good()){
if(readCounter>=Game().mapNameList.size())break;
int readVal;
file>>readVal;
mapPBs[Game().mapNameList[readCounter]]=readVal;
readCounter++;
}
file.close();
#endif
i++;
}
}
const HamsterGame::GameMode HamsterGame::GetGameMode(){
return mode;
}
int main()

@ -70,6 +70,7 @@ public:
MARATHON,
};
const static std::string ASSETS_DIR;
const static int UNPLAYED;
HamsterGame()=delete;
HamsterGame(const std::string&appName);
static geom2d::rect<float>SCREEN_FRAME;
@ -81,6 +82,7 @@ public:
static const Renderable&GetGFX(const std::string&img);
static const Animate2D::Animation<AnimationState::AnimationState>&GetAnimations(const std::string&img);
static const Animate2D::FrameSequence&GetAnimation(const std::string&img,const AnimationState::AnimationState state);
static std::unordered_map<std::string,int>mapPBs;
static HamsterGame&Game();
static std::unordered_map<uint32_t,Animate2D::FrameSequence>ANIMATED_TILE_IDS;
const double GetRuntime()const;
@ -96,6 +98,9 @@ public:
virtual void OnTextEntryComplete(const std::string& sText)override;
const Difficulty&GetMapDifficulty()const;
void OnPlayerFinishedRace();
const GameMode GetGameMode();
static void SavePB(const std::string&mapName,int ms);
static void LoadPBs();
private:
void UpdateGame(const float fElapsedTime);
void DrawGame();
@ -135,7 +140,24 @@ private:
GameMode mode{GameMode::SINGLE_RACE};
HamsterNet net;
#ifndef __EMSCRIPTEN__
#ifndef __DEBUG__
SplashScreen splash;
#endif
#endif
bool netInitialized{false};
std::vector<std::string>mapNameList{
"StageI.tmx",
"StageII.tmx",
"StageIII.tmx",
"StageIV.tmx",
"StageV.tmx",
"StageVI.tmx",
"StageVII.tmx",
"StageVIII.tmx",
"StageIX.tmx",
"StageX.tmx",
"StageXI.tmx",
"StageXII.tmx",
};
std::string emscripten_temp_val{"123456"};
};

@ -227,13 +227,18 @@ bool HamsterNet::StartRace(const std::string& map)
return (hamsterNet__startRace() == 1);
}
bool HamsterNet::FinishRace()
int HamsterNet::GetCurrentRaceTime(const std::string& map){
std::chrono::duration<double, std::milli> duration = std::chrono::system_clock::now() - m_tp1;
return static_cast<int>(duration.count());
}
std::pair<HamsterNet::FinishTime,bool> HamsterNet::FinishRace()
{
m_tp2 = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> duration = m_tp2 - m_tp1;
m_time = static_cast<int>(duration.count());
return (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) == 1)};
}
std::vector<LeaderboardEntry> HamsterNet::GetLeaderboard(const std::string& map, const int offset, const int limit, const std::string& sortBy, bool ascending)

@ -28,7 +28,9 @@ public:
bool SetName(const std::string& name);
bool StartRace(const std::string& map);
bool FinishRace();
using FinishTime=int;
int GetCurrentRaceTime(const std::string& map);
std::pair<FinishTime,bool> FinishRace();
std::vector<LeaderboardEntry> GetLeaderboard(const std::string& map, const int offset = 0, const int limit = 100, const std::string& sortBy = "time", bool ascending = true);

Loading…
Cancel
Save