summaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
Diffstat (limited to 'src/client')
-rw-r--r--src/client/Client.cpp54
-rw-r--r--src/client/Client.h13
-rw-r--r--src/client/GameSave.cpp8
-rw-r--r--src/client/GameSave.h7
-rw-r--r--src/client/SaveFile.cpp43
-rw-r--r--src/client/SaveFile.h31
-rw-r--r--src/client/SaveInfo.cpp103
-rw-r--r--src/client/SaveInfo.h68
8 files changed, 301 insertions, 26 deletions
diff --git a/src/client/Client.cpp b/src/client/Client.cpp
index 886dfbb..23a487c 100644
--- a/src/client/Client.cpp
+++ b/src/client/Client.cpp
@@ -22,7 +22,7 @@
#include "interface/Point.h"
-#include "search/Save.h"
+#include "client/SaveInfo.h"
Client::Client():
authUser(0, "")
@@ -153,9 +153,11 @@ User Client::GetAuthUser()
return authUser;
}
-RequestStatus Client::UploadSave(Save * save)
+RequestStatus Client::UploadSave(SaveInfo * save)
{
lastError = "";
+ int gameDataLength;
+ char * gameData = NULL;
int dataStatus;
char * data;
int dataLength = 0;
@@ -163,9 +165,23 @@ RequestStatus Client::UploadSave(Save * save)
userIDStream << authUser.ID;
if(authUser.ID)
{
+ if(!save->GetGameSave())
+ {
+ lastError = "Empty game save";
+ return RequestFailure;
+ }
+
+ gameData = save->GetGameSave()->Serialise(gameDataLength);
+
+ if(!gameData)
+ {
+ lastError = "Cannot upload game save";
+ return RequestFailure;
+ }
+
char * postNames[] = { "Name", "Description", "Data:save.bin", "Publish", NULL };
- char * postDatas[] = { (char *)(save->name.c_str()), (char *)(save->Description.c_str()), (char *)(save->GetData()), (char *)(save->Published?"Public":"Private") };
- int postLengths[] = { save->name.length(), save->Description.length(), save->GetDataLength(), save->Published?6:7 };
+ char * postDatas[] = { (char *)(save->name.c_str()), (char *)(save->Description.c_str()), gameData, (char *)(save->Published?"Public":"Private") };
+ int postLengths[] = { save->name.length(), save->Description.length(), gameDataLength, save->Published?6:7 };
//std::cout << postNames[0] << " " << postDatas[0] << " " << postLengths[0] << std::endl;
data = http_multipart_post("http://" SERVER "/Save.api", postNames, postDatas, postLengths, (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength);
}
@@ -178,6 +194,7 @@ RequestStatus Client::UploadSave(Save * save)
{
if(strncmp((const char *)data, "OK", 2)!=0)
{
+ if(gameData) free(gameData);
free(data);
lastError = std::string((const char *)data);
return RequestFailure;
@@ -198,16 +215,18 @@ RequestStatus Client::UploadSave(Save * save)
}
}
free(data);
+ if(gameData) free(gameData);
return RequestOkay;
}
else if(data)
{
free(data);
}
+ if(gameData) free(gameData);
return RequestFailure;
}
-Save * Client::GetStamp(string stampID)
+SaveFile * Client::GetStamp(string stampID)
{
std::ifstream stampFile;
stampFile.open(string(STAMPS_DIR PATH_SEP + stampID + ".stm").c_str(), ios::binary);
@@ -221,10 +240,10 @@ Save * Client::GetStamp(string stampID)
stampFile.read((char *)tempData, fileSize);
stampFile.close();
-
- Save * tempSave = new Save(0, 0, 0, 0, "", stampID);
- tempSave->SetData(tempData, fileSize);
- return tempSave;
+ SaveFile * file = new SaveFile(string(STAMPS_DIR PATH_SEP + stampID + ".stm").c_str());
+ GameSave * tempSave = new GameSave((char *)tempData, fileSize);
+ file->SetGameSave(tempSave);
+ return file;
}
else
{
@@ -245,7 +264,7 @@ void Client::DeleteStamp(string stampID)
}
}
-string Client::AddStamp(Save * saveData)
+string Client::AddStamp(GameSave * saveData)
{
unsigned t=(unsigned)time(NULL);
if (lastStampTime!=t)
@@ -267,9 +286,12 @@ string Client::AddStamp(Save * saveData)
mkdir(STAMPS_DIR, 0755);
#endif
+ int gameDataLength;
+ char * gameData = saveData->Serialise(gameDataLength);
+
std::ofstream stampStream;
stampStream.open(string(STAMPS_DIR PATH_SEP + saveID.str()+".stm").c_str(), ios::binary);
- stampStream.write((const char *)saveData->data, saveData->dataLength);
+ stampStream.write((const char *)gameData, gameDataLength);
stampStream.close();
stampIDs.push_back(saveID.str());
@@ -683,7 +705,7 @@ failure:
return RequestFailure;
}
-Save * Client::GetSave(int saveID, int saveDate)
+SaveInfo * Client::GetSave(int saveID, int saveDate)
{
lastError = "";
std::stringstream urlStream;
@@ -733,7 +755,7 @@ Save * Client::GetSave(int saveID, int saveDate)
tempTags.push_back(tempTag.Value());
}
- Save * tempSave = new Save(
+ SaveInfo * tempSave = new SaveInfo(
tempID.Value(),
tempDate.Value(),
tempScoreUp.Value(),
@@ -849,11 +871,11 @@ std::vector<SaveComment*> * Client::GetComments(int saveID, int start, int count
return commentArray;
}
-std::vector<Save*> * Client::SearchSaves(int start, int count, string query, string sort, std::string category, int & resultCount)
+std::vector<SaveInfo*> * Client::SearchSaves(int start, int count, string query, string sort, std::string category, int & resultCount)
{
lastError = "";
resultCount = 0;
- std::vector<Save*> * saveArray = new std::vector<Save*>();
+ std::vector<SaveInfo*> * saveArray = new std::vector<SaveInfo*>();
std::stringstream urlStream;
char * data;
int dataStatus, dataLength;
@@ -904,7 +926,7 @@ std::vector<Save*> * Client::SearchSaves(int start, int count, string query, str
json::String tempUsername = savesArray[j]["Username"];
json::String tempName = savesArray[j]["Name"];
saveArray->push_back(
- new Save(
+ new SaveInfo(
tempID.Value(),
tempDate.Value(),
tempScoreUp.Value(),
diff --git a/src/client/Client.h b/src/client/Client.h
index b00ea93..53f0e79 100644
--- a/src/client/Client.h
+++ b/src/client/Client.h
@@ -9,7 +9,8 @@
#include "HTTP.h"
#include "preview/Comment.h"
#include "search/Thumbnail.h"
-#include "search/Save.h"
+#include "client/SaveInfo.h"
+#include "client/SaveFile.h"
#include "Singleton.h"
#include "User.h"
@@ -52,21 +53,21 @@ public:
~Client();
RequestStatus ExecVote(int saveID, int direction);
- RequestStatus UploadSave(Save * save);
+ RequestStatus UploadSave(SaveInfo * save);
- Save * GetStamp(string stampID);
+ SaveFile * GetStamp(string stampID);
void DeleteStamp(string stampID);
- string AddStamp(Save * saveData);
+ string AddStamp(GameSave * saveData);
vector<string> GetStamps();
unsigned char * GetSaveData(int saveID, int saveDate, int & dataLength);
LoginStatus Login(string username, string password, User & user);
void ClearThumbnailRequests();
- std::vector<Save*> * SearchSaves(int start, int count, string query, string sort, string category, int & resultCount);
+ std::vector<SaveInfo*> * SearchSaves(int start, int count, string query, string sort, string category, int & resultCount);
std::vector<SaveComment*> * GetComments(int saveID, int start, int count);
Thumbnail * GetPreview(int saveID, int saveDate);
Thumbnail * GetThumbnail(int saveID, int saveDate);
- Save * GetSave(int saveID, int saveDate);
+ SaveInfo * GetSave(int saveID, int saveDate);
RequestStatus DeleteSave(int saveID);
RequestStatus ReportSave(int saveID, std::string message);
RequestStatus UnpublishSave(int saveID);
diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp
index 9c7cf5e..8ad03a2 100644
--- a/src/client/GameSave.cpp
+++ b/src/client/GameSave.cpp
@@ -11,7 +11,7 @@
#include "Config.h"
#include "bson/BSON.h"
#include "GameSave.h"
-#include "SimulationData.h"
+#include "simulation/SimulationData.h"
GameSave::GameSave(GameSave & save) :
waterEEnabled(save.waterEEnabled),
@@ -23,7 +23,8 @@ airMode(save.airMode),
signs(save.signs)
{
setSize(save.width, save.height);
-
+
+ particlesCount = save.particlesCount;
copy(save.particles, save.particles+NPART, particles);
copy(save.blockMapPtr, save.blockMapPtr+((height/CELL)*(width/CELL)), blockMapPtr);
copy(save.fanVelXPtr, save.fanVelXPtr+((height/CELL)*(width/CELL)), fanVelXPtr);
@@ -52,6 +53,7 @@ void GameSave::setSize(int newWidth, int newHeight)
this->width = (newWidth/CELL)*CELL;
this->height = (newHeight/CELL)*CELL;
+ particlesCount = 0;
particles = new Particle[NPART];
blockMap = new unsigned char*[height/CELL];
blockMapPtr = new unsigned char[(height/CELL)*(width/CELL)];
@@ -1458,4 +1460,4 @@ GameSave::~GameSave()
delete[] fanVelY;
}
}
-} \ No newline at end of file
+}
diff --git a/src/client/GameSave.h b/src/client/GameSave.h
index 2978f52..283ed4e 100644
--- a/src/client/GameSave.h
+++ b/src/client/GameSave.h
@@ -9,8 +9,13 @@
#define The_Powder_Toy_GameSave_h
#include <vector>
+#include <string>
+#include "Config.h"
#include "Misc.h"
-#include "simulation/StorageClasses.h"
+#include "simulation/Sign.h"
+#include "simulation/Particle.h"
+
+using namespace std;
struct ParseException: public exception {
enum ParseResult { OK = 0, Corrupt, WrongVersion, InvalidDimensions, InternalError, MissingElement };
diff --git a/src/client/SaveFile.cpp b/src/client/SaveFile.cpp
new file mode 100644
index 0000000..6675aae
--- /dev/null
+++ b/src/client/SaveFile.cpp
@@ -0,0 +1,43 @@
+/*
+ * SaveFile.cpp
+ *
+ * Created on: Jun 6, 2012
+ * Author: Simon
+ */
+
+#include "SaveFile.h"
+
+SaveFile::SaveFile(SaveFile & save):
+ gameSave(NULL)
+{
+ if(save.gameSave)
+ gameSave = new GameSave(*save.gameSave);
+}
+
+SaveFile::SaveFile(string filename):
+ filename(filename),
+ gameSave(NULL)
+ {
+ //Load file
+}
+
+GameSave * SaveFile::GetGameSave()
+{
+ return gameSave;
+}
+
+void SaveFile::SetGameSave(GameSave * save)
+{
+ gameSave = save;
+}
+
+string SaveFile::GetName()
+{
+ return filename;
+}
+
+SaveFile::~SaveFile() {
+ if(gameSave)
+ delete gameSave;
+}
+
diff --git a/src/client/SaveFile.h b/src/client/SaveFile.h
new file mode 100644
index 0000000..8b4005a
--- /dev/null
+++ b/src/client/SaveFile.h
@@ -0,0 +1,31 @@
+/*
+ * SaveFile.h
+ *
+ * Created on: Jun 6, 2012
+ * Author: Simon
+ */
+
+#ifndef SAVEFILE_H_
+#define SAVEFILE_H_
+
+#include <string>
+#include "GameSave.h"
+
+using namespace std;
+
+class SaveFile {
+public:
+ SaveFile(SaveFile & save);
+ SaveFile(string filename);
+
+ GameSave * GetGameSave();
+ void SetGameSave(GameSave * save);
+ string GetName();
+
+ virtual ~SaveFile();
+private:
+ GameSave * gameSave;
+ string filename;
+};
+
+#endif /* SAVEFILE_H_ */
diff --git a/src/client/SaveInfo.cpp b/src/client/SaveInfo.cpp
new file mode 100644
index 0000000..4e096ef
--- /dev/null
+++ b/src/client/SaveInfo.cpp
@@ -0,0 +1,103 @@
+/*
+ * Save.cpp
+ *
+ * Created on: Jan 26, 2012
+ * Author: Simon
+ */
+
+#include "client/SaveInfo.h"
+#include "client/Client.h"
+
+SaveInfo::SaveInfo(SaveInfo & save) :
+ userName(save.userName), name(save.name), Description(save.Description), date(
+ save.date), Published(save.Published), id(save.id), votesUp(
+ save.votesUp), votesDown(save.votesDown), gameSave(NULL), vote(save.vote), tags(save.tags) {
+ if(save.gameSave)
+ gameSave = new GameSave(*save.gameSave);
+}
+
+SaveInfo::SaveInfo(int _id, int _date, int _votesUp, int _votesDown, string _userName,
+ string _name) :
+ id(_id), votesUp(_votesUp), votesDown(_votesDown), userName(_userName), name(
+ _name), Description("No description provided"), date(_date), Published(
+ true), gameSave(NULL), vote(0), tags() {
+}
+
+SaveInfo::SaveInfo(int _id, int date_, int _votesUp, int _votesDown, int _vote, string _userName,
+ string _name, string description_, bool published_, vector<string> tags_) :
+ id(_id), votesUp(_votesUp), votesDown(_votesDown), userName(_userName), name(
+ _name), Description(description_), date(date_), Published(
+ published_), gameSave(NULL), vote(_vote), tags(tags_) {
+}
+
+SaveInfo::~SaveInfo()
+{
+ if(gameSave)
+ {
+ delete gameSave;
+ }
+}
+
+void SaveInfo::SetName(string name) {
+ this->name = name;
+}
+string SaveInfo::GetName() {
+ return name;
+}
+
+void SaveInfo::SetVote(int vote)
+{
+ this->vote = vote;
+}
+int SaveInfo::GetVote()
+{
+ return vote;
+}
+
+void SaveInfo::SetUserName(string userName) {
+ this->userName = userName;
+}
+
+string SaveInfo::GetUserName() {
+ return userName;
+}
+
+void SaveInfo::SetID(int id) {
+ this->id = id;
+}
+int SaveInfo::GetID() {
+ return id;
+}
+
+void SaveInfo::SetVotesUp(int votesUp) {
+ this->votesUp = votesUp;
+}
+int SaveInfo::GetVotesUp() {
+ return votesUp;
+}
+
+void SaveInfo::SetVotesDown(int votesDown) {
+ this->votesDown = votesDown;
+}
+int SaveInfo::GetVotesDown() {
+ return votesDown;
+}
+
+void SaveInfo::SetTags(vector<string> tags)
+{
+ this->tags = tags;
+}
+vector<string> SaveInfo::GetTags()
+{
+ return tags;
+}
+
+GameSave * SaveInfo::GetGameSave()
+{
+ return gameSave;
+}
+
+void SaveInfo::SetGameSave(GameSave * saveGame)
+{
+ gameSave = saveGame;
+}
diff --git a/src/client/SaveInfo.h b/src/client/SaveInfo.h
new file mode 100644
index 0000000..5360f90
--- /dev/null
+++ b/src/client/SaveInfo.h
@@ -0,0 +1,68 @@
+#ifndef SAVE_H
+#define SAVE_H
+
+#include <vector>
+#include <string>
+#include <stdlib.h>
+#include <iostream>
+#include <string.h>
+#include "GameSave.h"
+
+using namespace std;
+
+class SaveInfo
+{
+private:
+public:
+ int id;
+ int date;
+ int votesUp, votesDown;
+ bool Favourite;
+
+ GameSave * gameSave;
+
+ SaveInfo(SaveInfo & save);
+
+ SaveInfo(int _id, int _date, int _votesUp, int _votesDown, string _userName, string _name);
+
+ SaveInfo(int _id, int date_, int _votesUp, int _votesDown, int _vote, string _userName, string _name, string description_, bool published_, vector<string> tags);
+
+ ~SaveInfo();
+
+ string userName;
+ string name;
+
+ string Description;
+
+ vector<string> tags;
+
+ int vote;
+
+ bool Published;
+
+ void SetName(string name);
+ string GetName();
+
+ void SetUserName(string userName);
+ string GetUserName();
+
+ void SetID(int id);
+ int GetID();
+
+ void SetVote(int vote);
+ int GetVote();
+
+ void SetVotesUp(int votesUp);
+ int GetVotesUp();
+
+ void SetVotesDown(int votesDown);
+ int GetVotesDown();
+
+ void SetTags(vector<string> tags);
+ vector<string> GetTags();
+
+ GameSave * GetGameSave();
+ void SetGameSave(GameSave * gameSave);
+};
+
+#endif // SAVE_H