Emscripten powershell build script updated to properly terminate on errors. Add in proper throws when an invalid response is given from the server. Add in function response callbacks to all server request functions.

pull/28/head
sigonasr2 1 year ago
parent 497db80b9b
commit 7bfa728ccb
  1. 2
      Crawler/Crawler.cpp
  2. 1
      Crawler/Crawler.h
  3. 12
      Crawler/MainMenuWindow.cpp
  4. 6
      Crawler/MenuLabel.h
  5. 26
      Crawler/SaveFile.cpp
  6. 8
      Crawler/SaveFile.h
  7. 2
      Crawler/Version.h
  8. 2
      Crawler/assets/saves/metadata.dat
  9. 10
      Crawler/emscripten_build.ps1
  10. 8
      Crawler/olcPixelGameEngine.h

@ -2565,7 +2565,7 @@ void Crawler::ResetGame(){
} }
void Crawler::OnRequestCompleted(const std::string_view receivedData)const{ void Crawler::OnRequestCompleted(const std::string_view receivedData)const{
std::cout<<"Received in engine: "<<receivedData<<std::endl; responseCallback(receivedData);
} }
std::string operator ""_FS(const char*key,std::size_t len){ std::string operator ""_FS(const char*key,std::size_t len){

@ -115,6 +115,7 @@ private:
bool gameEnd=false; bool gameEnd=false;
std::vector<Monster>monstersToBeSpawned; std::vector<Monster>monstersToBeSpawned;
time_t gameStarted; time_t gameStarted;
std::function<void(std::string_view)>responseCallback;
void ValidateGameStatus(); void ValidateGameStatus();
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__

@ -58,7 +58,19 @@ void Menu::InitializeMainMenuWindow(){
})END; })END;
mainMenuWindow->ADD("Load Game Button",MenuComponent)({{12,36},{72,24}},"Load Game",[](MenuFuncData data){ mainMenuWindow->ADD("Load Game Button",MenuComponent)({{12,36},{72,24}},"Load Game",[](MenuFuncData data){
SaveFile::UpdateSaveGameData(); SaveFile::UpdateSaveGameData();
#ifdef __EMSCRIPTEN__
SaveFile::Server_GetLoadInfo([](std::string_view response){
if(response!="ERR"){
std::ofstream file("save_file_path"_S+"metadata.dat");
file<<response;
Menu::OpenMenu(LOAD_GAME);
}else{
std::cout<<"WARNING! Failed to retrieve load game information from the server!"<<std::endl;
}
});
#else
Menu::OpenMenu(LOAD_GAME); Menu::OpenMenu(LOAD_GAME);
#endif
return true; return true;
})END; })END;
mainMenuWindow->ADD("Quit Game Button",MenuComponent)({{12,68},{72,24}},"Quit Game",[](MenuFuncData data){ mainMenuWindow->ADD("Quit Game Button",MenuComponent)({{12,68},{72,24}},"Quit Game",[](MenuFuncData data){

@ -78,8 +78,8 @@ protected:
vf2d adjustedScale={scale,scale}; vf2d adjustedScale={scale,scale};
vf2d labelTextSize= vf2d labelTextSize=
proportional? proportional?
vf2d(game->GetWrappedTextSizeProp(label,rect.size.x-8,adjustedScale)): vf2d(game->GetWrappedTextSizeProp(label,rect.size.x-2,adjustedScale)):
vf2d(game->GetWrappedTextSize(label,rect.size.x-8,adjustedScale)); vf2d(game->GetWrappedTextSize(label,rect.size.x-2,adjustedScale));
if(fitToLabel){ if(fitToLabel){
labelTextSize= labelTextSize=
@ -93,7 +93,7 @@ protected:
} }
} }
vf2d drawPos=vf2d{-4.f,0.f}+rect.middle()-vf2d{labelTextSize}/2; //Assume centered. vf2d drawPos=vf2d{-1,0}+rect.middle()-vf2d{labelTextSize}/2; //Assume centered.
if(!centered){ if(!centered){
drawPos=vf2d{rect.pos.x+2,rect.middle().y-labelTextSize.y/2}; //We should at least vertically align here. drawPos=vf2d{rect.pos.x+2,rect.middle().y-labelTextSize.y/2}; //We should at least vertically align here.
} }

@ -116,13 +116,13 @@ const void SaveFile::SaveGame(){
utils::datafile::INITIAL_SETUP_COMPLETE=true; utils::datafile::INITIAL_SETUP_COMPLETE=true;
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
Server_SaveFile(); Server_SaveFile([](std::string_view response){});
#endif #endif
} }
const void SaveFile::LoadGame(){ const void SaveFile::LoadGame(){
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
Server_GetFile(); Server_GetFile([](std::string_view response){});
#else #else
std::filesystem::create_directories("save_file_path"_S); std::filesystem::create_directories("save_file_path"_S);
utils::datafile loadFile; utils::datafile loadFile;
@ -224,23 +224,39 @@ const std::string SaveFile::CreateServerRequest(const SaveFileOperation::Operati
operationName=std::format("SAVE_FILE"); operationName=std::format("SAVE_FILE");
//Data should contain the entire contents of our save file. //Data should contain the entire contents of our save file.
}break; }break;
case SaveFileOperation::SAVE_METADATA_FILE:{
operationName=std::format("SAVE_METADATA_FILE");
//Data should contain the entire contents of our metadata save file.
}break;
} }
dataString+="&operation="+operationName; dataString+="&operation="+operationName;
dataString+="&checksum="+std::to_string(CalculateChecksum(operationName,data)); dataString+="&checksum="+std::to_string(CalculateChecksum(operationName,data));
dataString+="&data="+std::string(data); dataString+="&data="+std::string(data);
return "save_server"_S+dataString; return "save_server"_S+dataString;
} }
const void SaveFile::Server_GetLoadInfo(){ const void SaveFile::Server_GetLoadInfo(std::function<void(std::string_view)>respCallbackFunc){
game->SendRequest(CreateServerRequest(SaveFileOperation::GET_LOAD_FILES,"0"),""); game->SendRequest(CreateServerRequest(SaveFileOperation::GET_LOAD_FILES,"0"),"");
game->responseCallback=respCallbackFunc;
} }
const void SaveFile::Server_GetFile(){ const void SaveFile::Server_GetFile(std::function<void(std::string_view)>respCallbackFunc){
game->SendRequest(CreateServerRequest(SaveFileOperation::GET_FILE,std::to_string(saveFileID)),""); game->SendRequest(CreateServerRequest(SaveFileOperation::GET_FILE,std::to_string(saveFileID)),"");
game->responseCallback=respCallbackFunc;
} }
const void SaveFile::Server_SaveFile(){ const void SaveFile::Server_SaveFile(std::function<void(std::string_view)>respCallbackFunc){
std::stringstream fileContents; std::stringstream fileContents;
std::ifstream file("save_file_path"_S+std::format("save.{:04}",saveFileID)); std::ifstream file("save_file_path"_S+std::format("save.{:04}",saveFileID));
while(file.good()){ while(file.good()){
fileContents<<char(file.get()); fileContents<<char(file.get());
} }
game->SendRequest(CreateServerRequest(SaveFileOperation::SAVE_FILE,std::to_string(saveFileID)+"|"+fileContents.str()),""); game->SendRequest(CreateServerRequest(SaveFileOperation::SAVE_FILE,std::to_string(saveFileID)+"|"+fileContents.str()),"");
game->responseCallback=respCallbackFunc;
}
const void SaveFile::Server_SaveMetadataFile(std::function<void(std::string_view)>respCallbackFunc){
std::stringstream fileContents;
std::ifstream file("save_file_path"_S+"metadata.dat");
while(file.good()){
fileContents<<char(file.get());
}
game->SendRequest(CreateServerRequest(SaveFileOperation::SAVE_METADATA_FILE,fileContents.str()),"");
game->responseCallback=respCallbackFunc;
} }

@ -43,6 +43,7 @@ namespace SaveFileOperation{
enum Operation{ enum Operation{
GET_LOAD_FILES, GET_LOAD_FILES,
GET_FILE, GET_FILE,
SAVE_METADATA_FILE,
SAVE_FILE, SAVE_FILE,
}; };
}; };
@ -61,7 +62,8 @@ public:
//Called whenever the save game data is updated. //Called whenever the save game data is updated.
static const void UpdateSaveGameData(); static const void UpdateSaveGameData();
static const std::string CreateServerRequest(const SaveFileOperation::Operation operation,std::string_view data); static const std::string CreateServerRequest(const SaveFileOperation::Operation operation,std::string_view data);
static const void Server_GetLoadInfo(); static const void Server_GetLoadInfo(std::function<void(std::string_view)>respCallbackFunc);
static const void Server_GetFile(); static const void Server_GetFile(std::function<void(std::string_view)>respCallbackFunc);
static const void Server_SaveFile(); static const void Server_SaveFile(std::function<void(std::string_view)>respCallbackFunc);
static const void Server_SaveMetadataFile(std::function<void(std::string_view)>respCallbackFunc);
}; };

@ -39,7 +39,7 @@ All rights reserved.
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 2 #define VERSION_MINOR 2
#define VERSION_PATCH 1 #define VERSION_PATCH 1
#define VERSION_BUILD 5285 #define VERSION_BUILD 5287
#define stringify(a) stringify_(a) #define stringify(a) stringify_(a)
#define stringify_(a) #a #define stringify_(a) #a

@ -1,2 +1,2 @@
save0 = 534.977882, 1, 2, Ranger, Sig save0 = 641.829219, 1, 2, Ranger, Sig
save1 = 107.824620, 1, 1, Wizard, WIZ save1 = 107.824620, 1, 1, Wizard, WIZ

@ -1,12 +1,6 @@
$ErrorActionPreference = "Stop"
cd .. cd ..
./emscripten_build.ps1 ./emscripten_build.ps1
if( -not $? ) if ($LASTEXITCODE -eq 0) {
{
$msg = $Error[0].Exception.Message
"$msg"
exit
}
else
{
./emscripten_run.ps1 ./emscripten_run.ps1
} }

@ -7217,7 +7217,13 @@ namespace olc
virtual olc::rcode SendRequest(std::string_view url,std::string_view data)override{ virtual olc::rcode SendRequest(std::string_view url,std::string_view data)override{
EM_ASM({ EM_ASM({
requestResp=""; requestResp="";
fetch(UTF8ToString($0)).then((resp)=>resp.text()).then((data)=>{requestResp=data}); fetch(UTF8ToString($0)).then((resp)=>{
if(resp.ok){
resp.text();
}
throw new Error(resp.text());
}).then((data)=>{requestResp=data;})
.catch((err)=>{requestResp="ERR";});
},std::string(url).c_str()); },std::string(url).c_str());
return olc::rcode::OK; return olc::rcode::OK;
} }

Loading…
Cancel
Save