All save/load features implemented and connected to online server for web build.

pull/28/head
sigonasr2 1 year ago
parent fc9d9590d3
commit 73289de3aa
  1. 13
      Crawler/MainMenuWindow.cpp
  2. 94
      Crawler/SaveFile.cpp
  3. 2
      Crawler/UserIDMenu.cpp
  4. 2
      Crawler/Version.h
  5. 2
      Crawler/assets/saves/metadata.dat
  6. 53
      Crawler/backend.js
  7. 7
      Crawler/olcPixelGameEngine.h

@ -62,24 +62,17 @@ void Menu::InitializeMainMenuWindow(){
return true;
})END;
mainMenuWindow->ADD("Load Game Button",MenuComponent)({{12,36},{72,24}},"Load Game",[](MenuFuncData data){
SaveFile::UpdateSaveGameData();
#ifdef __EMSCRIPTEN__
data.menu.S(A::NEXT_MENU)="Load Game";
if(SaveFile::GetUserID().length()==0){
game->TextEntryEnable(true);
Menu::OpenMenu(USER_ID);
}else{
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;
}
});
SaveFile::UpdateSaveGameData();
Menu::OpenMenu(LOAD_GAME);
}
#else
SaveFile::UpdateSaveGameData();
Menu::OpenMenu(LOAD_GAME);
#endif
return true;

@ -54,12 +54,10 @@ std::string SaveFile::username="";
const size_t SaveFile::GetSaveFileCount(){
size_t count=0;
if(std::filesystem::exists("save_file_path"_S)){
for(auto&path:std::filesystem::directory_iterator("save_file_path"_S)){
if(path.is_regular_file()&&path.path()!="metadata.dat"){
count++;
}
}
if(std::filesystem::exists("save_file_path"_S+"metadata.dat")){
utils::datafile metadata;
utils::datafile::Read(metadata,"save_file_path"_S+"metadata.dat");
return metadata.GetKeys().size();
}
return count;
}
@ -114,17 +112,25 @@ const void SaveFile::SaveGame(){
utils::datafile::Write(metadata,"save_file_path"_S+"metadata.dat");
utils::datafile::INITIAL_SETUP_COMPLETE=true;
#ifdef __EMSCRIPTEN__
Server_SaveFile([](std::string_view response){});
std::function<void(std::string_view response)>RetryResponse;
RetryResponse=[&](std::string_view response){
if(response!="ERR"){
Server_SaveFile([](std::string_view response){
if(response=="ERR"){
std::cout<<"WARNING! Could not save data to server!"<<std::endl;
}
});
}else{
std::cout<<"WARNING! Could not save metadata to server!"<<std::endl;
}
};
Server_SaveMetadataFile(RetryResponse);
#endif
}
const void SaveFile::LoadGame(){
#ifdef __EMSCRIPTEN__
Server_GetFile([](std::string_view response){});
#else
std::filesystem::create_directories("save_file_path"_S);
auto LoadFile=[](){
utils::datafile loadFile;
if(std::filesystem::exists("save_file_path"_S+std::format("save.{:04}",saveFileID))){
utils::datafile::Read(loadFile,"save_file_path"_S+std::format("save.{:04}",saveFileID));
@ -170,6 +176,22 @@ const void SaveFile::LoadGame(){
}else{
std::cout<<std::format("WARNING! File {} does not exist for loading!","save_file_path"_S+std::format("save.{:04}",saveFileID))<<std::endl;
}
};
#ifdef __EMSCRIPTEN__
Server_GetFile([&](std::string_view response){
if(response!="ERR"){
std::ofstream file("save_file_path"_S+std::format("save.{:04}",saveFileID));
file<<response;
file.close();
LoadFile();
}else{
std::cout<<"WARNING! Could not load save file!"<<std::endl;
}
});
#else
std::filesystem::create_directories("save_file_path"_S);
LoadFile();
#endif
}
@ -186,23 +208,39 @@ const void SaveFile::SetSaveFileID(size_t saveFileID){
}
const void SaveFile::UpdateSaveGameData(){
auto gameFilesList=Component<ScrollableWindowComponent>(LOAD_GAME,"Game Files List");
gameFilesList->RemoveAllComponents();
const size_t saveFileCount=GetSaveFileCount();
utils::datafile metadata;
utils::datafile::Read(metadata,"save_file_path"_S+"metadata.dat");
float offsetY=0;
for(size_t i=0;i<saveFileCount;i++){
if(metadata.HasProperty(std::format("save{}",i))){
gameFilesList->ADD(std::format("Load File Button - Save {}",i),LoadFileButton)({{0,offsetY},{gameFilesList->GetSize().x-13,48}},metadata[std::format("save{}",i)],i,[](MenuFuncData data){
LoadFileButton*comp=DYNAMIC_CAST<LoadFileButton*>(data.component);
saveFileID=comp->getSaveFileID();
SaveFile::LoadGame();
return true;
},ButtonAttr::NONE)END;
offsetY+=49;
auto LoadMetadataFile=[](){
auto gameFilesList=Component<ScrollableWindowComponent>(LOAD_GAME,"Game Files List");
gameFilesList->RemoveAllComponents();
const size_t saveFileCount=GetSaveFileCount();
utils::datafile metadata;
utils::datafile::Read(metadata,"save_file_path"_S+"metadata.dat");
float offsetY=0;
for(size_t i=0;i<saveFileCount;i++){
if(metadata.HasProperty(std::format("save{}",i))){
gameFilesList->ADD(std::format("Load File Button - Save {}",i),LoadFileButton)({{0,offsetY},{gameFilesList->GetSize().x-13,48}},metadata[std::format("save{}",i)],i,[](MenuFuncData data){
LoadFileButton*comp=DYNAMIC_CAST<LoadFileButton*>(data.component);
saveFileID=comp->getSaveFileID();
SaveFile::LoadGame();
return true;
},ButtonAttr::NONE)END;
offsetY+=49;
}
}
}
};
LoadMetadataFile();
#ifdef __EMSCRIPTEN__
Server_GetLoadInfo([&](std::string_view response){
if(response!="ERR"){
std::ofstream file("save_file_path"_S+"metadata.dat");
std::stringstream str;
file<<response;
file.close();
LoadMetadataFile();
}
});
#endif
}
const std::string SaveFile::CreateServerRequest(const SaveFileOperation::Operation operation,std::string_view data){

@ -57,9 +57,11 @@ void Menu::InitializeUserIDWindow(){
userIDWindow->ADD("Submit Button",MenuComponent)({{102,110},{48,12}},"Submit",[](MenuFuncData data){
SaveFile::SetUserID(Component<TextEntryLabel>(USER_ID,"User ID Input")->GetLabel());
if(Menu::menus[MAIN_MENU]->S(A::NEXT_MENU)=="New Game"){
Menu::CloseMenu();
Component<MenuComponent>(MAIN_MENU,"New Game Button")->Click();
}else
if(Menu::menus[MAIN_MENU]->S(A::NEXT_MENU)=="Load Game"){
Menu::CloseMenu();
Component<MenuComponent>(MAIN_MENU,"Load Game Button")->Click();
}else{
ERR("WARNING! Unknown Next Menu set! Current Value:"<<std::quoted(Menu::menus[MAIN_MENU]->S(A::NEXT_MENU)));

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

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

@ -0,0 +1,53 @@
app.post("/AiL",function (req, res) {
var username=decodeURI(req.body.username)
var operation=decodeURI(req.body.operation)
var checksum=decodeURI(req.body.checksum)
var data=decodeURI(req.body.data)
const saveDir="files/AiLsaves/";
if(username&&operation&&checksum&&data){
if(username.length<=24&&
(operation==="GET_LOAD_FILES"||operation==="GET_FILE"||operation==="SAVE_FILE"||operation==="SAVE_METADATA_FILE")&&
data.length<=1000000&&
Number(checksum)===username.length*8+data.length*2+operation.length){
//Checksum: Length of username*8+Length of data*2+Length of operation
if(!fs.existsSync(`${saveDir+username}`)){
fs.mkdirSync(`${saveDir+username}`);
}
switch(operation){
case "GET_LOAD_FILES":{
if(fs.existsSync(`${saveDir+username}/metadata.dat`)){
res.status(200).send(fs.readFileSync(`${saveDir+username}/metadata.dat`));
}else{
res.status(404).send("Not Found!");
}
}break;
case "GET_FILE":{
if(fs.existsSync(`${saveDir+username}/save.${data.padStart(4,'0')}`)){
res.status(200).send(fs.readFileSync(`${saveDir+username}/save.${data.padStart(4,'0')}`));
}else{
res.status(404).send("Not Found!");
}
}break;
case "SAVE_FILE":{
var fileData=data.split("|");
var fileNumb=fileData[0];
var saveFileData=fileData[1];
fs.writeFileSync(`${saveDir+username}/save.${fileNumb.padStart(4,'0')}`,saveFileData);
res.status(200).send("Saved!");
}break;
case "SAVE_METADATA_FILE":{
fs.writeFileSync(`${saveDir+username}/metadata.dat`,data);
res.status(200).send("Saved!");
}break;
default:{
res.status(418).send('I\'m definitely a teapot.');
}
}
}else{
res.status(418).send('I\'m definitely a teapot.');
}
}else{
res.status(400).send('Invalid input!');
}
});

@ -7223,9 +7223,10 @@ namespace olc
},
method: "POST",body:UTF8ToString($1)}).then((resp)=>{
if(resp.ok){
resp.text();
return resp.text();
}else{
throw new Error(resp.text());
}
throw new Error(resp.text());
}).then((data)=>{requestResp=data;})
.catch((err)=>{requestResp="ERR";});
},std::string(url).c_str(),std::string(data).c_str());
@ -7357,7 +7358,7 @@ namespace olc
{
olc::Platform::ptrPGE->olc_CoreUpdate();
char*ptr=(char*)EM_ASM_PTR({
if(requestResp!==""){
if(requestResp&&requestResp!==""){
var newPtr=stringToNewUTF8(requestResp);
requestResp="";
return newPtr;

Loading…
Cancel
Save