Skip to content

Commit

Permalink
undo authors changes on ctrl+z, prevent excessive nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed Jul 15, 2017
1 parent 5ee10d1 commit 6efedcd
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 43 deletions.
4 changes: 1 addition & 3 deletions src/client/Client.h
Expand Up @@ -97,12 +97,10 @@ class Client: public Singleton<Client> {
void MergeStampAuthorInfo(Json::Value linksToAdd);
void MergeAuthorInfo(Json::Value linksToAdd);
void OverwriteAuthorInfo(Json::Value overwrite) { authors = overwrite; }
Json::Value GetAuthorInfo() { return authors; }
void SaveAuthorInfo(Json::Value *saveInto);
void ClearAuthorInfo() { authors.clear(); }
bool IsAuthorsEmpty() { return authors.size() == 0; }
#if defined(DEBUG) || defined(SNAPSHOT)
std::string GetAuthorString() { return authors.toStyledString(); }
#endif

UpdateInfo GetUpdateInfo();

Expand Down
114 changes: 78 additions & 36 deletions src/client/GameSave.cpp
Expand Up @@ -3,6 +3,7 @@
#include <sstream>
#include <cmath>
#include <vector>
#include <set>
#include <bzlib.h>
#include "Config.h"
#include "Format.h"
Expand Down Expand Up @@ -1136,38 +1137,6 @@ void GameSave::readOPS(char * data, int dataLength)
free(partsSimIndex);
}

void GameSave::ConvertBsonToJson(bson_iterator *iter, Json::Value *j)
{
bson_iterator subiter;
bson_iterator_subiterator(iter, &subiter);
while (bson_iterator_next(&subiter))
{
std::string key = bson_iterator_key(&subiter);
if (bson_iterator_type(&subiter) == BSON_STRING)
(*j)[key] = bson_iterator_string(&subiter);
else if (bson_iterator_type(&subiter) == BSON_BOOL)
(*j)[key] = bson_iterator_bool(&subiter);
else if (bson_iterator_type(&subiter) == BSON_INT)
(*j)[key] = bson_iterator_int(&subiter);
else if (bson_iterator_type(&subiter) == BSON_LONG)
(*j)[key] = (Json::Value::Int64)bson_iterator_long(&subiter);
else if (bson_iterator_type(&subiter) == BSON_ARRAY)
{
bson_iterator arrayiter;
bson_iterator_subiterator(&subiter, &arrayiter);
while (bson_iterator_next(&arrayiter))
{
if (bson_iterator_type(&arrayiter) == BSON_OBJECT && !strcmp(bson_iterator_key(&arrayiter), "part"))
{
Json::Value tempPart;
ConvertBsonToJson(&arrayiter, &tempPart);
(*j)["links"].append(tempPart);
}
}
}
}
}

void GameSave::readPSv(char * data, int dataLength)
{
unsigned char * d = NULL, * c = (unsigned char *)data;
Expand Down Expand Up @@ -2377,8 +2346,68 @@ char * GameSave::serialiseOPS(unsigned int & dataLength)
return (char*)outputData;
}

void GameSave::ConvertBsonToJson(bson_iterator *iter, Json::Value *j)
{
bson_iterator subiter;
bson_iterator_subiterator(iter, &subiter);
while (bson_iterator_next(&subiter))
{
std::string key = bson_iterator_key(&subiter);
if (bson_iterator_type(&subiter) == BSON_STRING)
(*j)[key] = bson_iterator_string(&subiter);
else if (bson_iterator_type(&subiter) == BSON_BOOL)
(*j)[key] = bson_iterator_bool(&subiter);
else if (bson_iterator_type(&subiter) == BSON_INT)
(*j)[key] = bson_iterator_int(&subiter);
else if (bson_iterator_type(&subiter) == BSON_LONG)
(*j)[key] = (Json::Value::Int64)bson_iterator_long(&subiter);
else if (bson_iterator_type(&subiter) == BSON_ARRAY)
{
bson_iterator arrayiter;
bson_iterator_subiterator(&subiter, &arrayiter);
while (bson_iterator_next(&arrayiter))
{
if (bson_iterator_type(&arrayiter) == BSON_OBJECT && !strcmp(bson_iterator_key(&arrayiter), "part"))
{
Json::Value tempPart;
ConvertBsonToJson(&arrayiter, &tempPart);
(*j)["links"].append(tempPart);
}
else if (bson_iterator_type(&arrayiter) == BSON_INT && !strcmp(bson_iterator_key(&arrayiter), "saveID"))
{
(*j)["links"].append(bson_iterator_int(&arrayiter));
}
}
}
}
}

std::set<int> GetNestedSaveIDs(Json::Value j)
{
Json::Value::Members members = j.getMemberNames();
std::set<int> saveIDs = std::set<int>();
for (Json::Value::Members::iterator iter = members.begin(), end = members.end(); iter != end; ++iter)
{
std::string member = *iter;
if (member == "id")
saveIDs.insert(j[member].asInt());
else if (j[member].isArray())
{
for (Json::Value::ArrayIndex i = 0; i < j[member].size(); i++)
{
// only supports objects here because that is all we need
if (!j[member][i].isObject())
continue;
std::set<int> nestedSaveIDs = GetNestedSaveIDs(j[member][i]);
saveIDs.insert(nestedSaveIDs.begin(), nestedSaveIDs.end());
}
}
}
return saveIDs;
}

// converts a json object to bson
void GameSave::ConvertJsonToBson(bson *b, Json::Value j)
void GameSave::ConvertJsonToBson(bson *b, Json::Value j, int depth)
{
Json::Value::Members members = j.getMemberNames();
for (Json::Value::Members::iterator iter = members.begin(), end = members.end(); iter != end; ++iter)
Expand All @@ -2395,14 +2424,27 @@ void GameSave::ConvertJsonToBson(bson *b, Json::Value j)
else if (j[member].isArray())
{
bson_append_start_array(b, member.c_str());
std::set<int> saveIDs = std::set<int>();
for (Json::Value::ArrayIndex i = 0; i < j[member].size(); i++)
{
// only supports objects here because that is all we need
if (!j[member][i].isObject())
continue;
bson_append_start_object(b, "part");
ConvertJsonToBson(b, j[member][i]);
bson_append_finish_object(b);
if (depth > 4)
{
std::set<int> nestedSaveIDs = GetNestedSaveIDs(j[member][i]);
saveIDs.insert(nestedSaveIDs.begin(), nestedSaveIDs.end());
}
else
{
bson_append_start_object(b, "part");
ConvertJsonToBson(b, j[member][i], depth+1);
bson_append_finish_object(b);
}
}
for (std::set<int>::iterator iter = saveIDs.begin(), end = saveIDs.end(); iter != end; ++iter)
{
bson_append_int(b, "saveID", *iter);
}
bson_append_finish_array(b);
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/GameSave.h
Expand Up @@ -116,7 +116,7 @@ class GameSave
void readOPS(char * data, int dataLength);
void readPSv(char * data, int dataLength);
char * serialiseOPS(unsigned int & dataSize);
void ConvertJsonToBson(bson *b, Json::Value j);
void ConvertJsonToBson(bson *b, Json::Value j, int depth = 0);
void ConvertBsonToJson(bson_iterator *b, Json::Value *j);
};

Expand Down
5 changes: 5 additions & 0 deletions src/gui/game/GameController.cpp
Expand Up @@ -242,11 +242,14 @@ void GameController::HistoryRestore()
if (historyPosition == history.size())
{
Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot();
if (newSnap)
newSnap->Authors = Client::Ref().GetAuthorInfo();
delete gameModel->GetRedoHistory();
gameModel->SetRedoHistory(newSnap);
}
Snapshot * snap = history[newHistoryPosition];
gameModel->GetSimulation()->Restore(*snap);
Client::Ref().OverwriteAuthorInfo(snap->Authors);
gameModel->SetHistory(history);
gameModel->SetHistoryPosition(newHistoryPosition);
}
Expand All @@ -258,6 +261,7 @@ void GameController::HistorySnapshot()
Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot();
if (newSnap)
{
newSnap->Authors = Client::Ref().GetAuthorInfo();
while (historyPosition < history.size())
{
Snapshot * snap = history.back();
Expand Down Expand Up @@ -295,6 +299,7 @@ void GameController::HistoryForward()
if (!snap)
return;
gameModel->GetSimulation()->Restore(*snap);
Client::Ref().OverwriteAuthorInfo(snap->Authors);
gameModel->SetHistoryPosition(newHistoryPosition);
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/game/GameModel.cpp
Expand Up @@ -659,7 +659,7 @@ void GameModel::SetSave(SaveInfo * newSave)
saveData->authors["username"] = newSave->userName;
saveData->authors["title"] = newSave->name;
saveData->authors["description"] = newSave->Description;
saveData->authors["published"] = newSave->Published;
saveData->authors["published"] = (int)newSave->Published;
saveData->authors["date"] = newSave->updatedDate;
}
// This save was probably just created, and we didn't know the ID when creating it
Expand Down
2 changes: 1 addition & 1 deletion src/gui/game/GameView.cpp
Expand Up @@ -1492,7 +1492,7 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
case 'a':
if (ctrl)
{
std::string authorString = Client::Ref().GetAuthorString();
std::string authorString = Client::Ref().GetAuthorInfo().toStyledString();
new InformationMessage("Save authorship info", authorString, true);
}
break;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/save/ServerSaveActivity.cpp
Expand Up @@ -266,7 +266,7 @@ void ServerSaveActivity::AddAuthorInfo()
serverSaveInfo["username"] = Client::Ref().GetAuthUser().Username;
serverSaveInfo["title"] = save.GetName();
serverSaveInfo["description"] = save.GetDescription();
serverSaveInfo["published"] = save.GetPublished();
serverSaveInfo["published"] = (int)save.GetPublished();
serverSaveInfo["date"] = (Json::Value::UInt64)time(NULL);
Client::Ref().SaveAuthorInfo(&serverSaveInfo);
save.GetGameSave()->authors = serverSaveInfo;
Expand Down
3 changes: 3 additions & 0 deletions src/simulation/Snapshot.h
Expand Up @@ -3,6 +3,7 @@
#include <vector>

#include "Particle.h"
#include "json/json.h"

class Snapshot
{
Expand Down Expand Up @@ -30,6 +31,8 @@ class Snapshot
std::vector<int> WirelessData;
std::vector<playerst> stickmen;
std::vector<sign> signs;

Json::Value Authors;

Snapshot() :
AirPressure(),
Expand Down

0 comments on commit 6efedcd

Please sign in to comment.