Skip to content

Commit 6efedcd

Browse files
committed
undo authors changes on ctrl+z, prevent excessive nesting
1 parent 5ee10d1 commit 6efedcd

File tree

8 files changed

+91
-43
lines changed

8 files changed

+91
-43
lines changed

src/client/Client.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,10 @@ class Client: public Singleton<Client> {
9797
void MergeStampAuthorInfo(Json::Value linksToAdd);
9898
void MergeAuthorInfo(Json::Value linksToAdd);
9999
void OverwriteAuthorInfo(Json::Value overwrite) { authors = overwrite; }
100+
Json::Value GetAuthorInfo() { return authors; }
100101
void SaveAuthorInfo(Json::Value *saveInto);
101102
void ClearAuthorInfo() { authors.clear(); }
102103
bool IsAuthorsEmpty() { return authors.size() == 0; }
103-
#if defined(DEBUG) || defined(SNAPSHOT)
104-
std::string GetAuthorString() { return authors.toStyledString(); }
105-
#endif
106104

107105
UpdateInfo GetUpdateInfo();
108106

src/client/GameSave.cpp

Lines changed: 78 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <sstream>
44
#include <cmath>
55
#include <vector>
6+
#include <set>
67
#include <bzlib.h>
78
#include "Config.h"
89
#include "Format.h"
@@ -1136,38 +1137,6 @@ void GameSave::readOPS(char * data, int dataLength)
11361137
free(partsSimIndex);
11371138
}
11381139

1139-
void GameSave::ConvertBsonToJson(bson_iterator *iter, Json::Value *j)
1140-
{
1141-
bson_iterator subiter;
1142-
bson_iterator_subiterator(iter, &subiter);
1143-
while (bson_iterator_next(&subiter))
1144-
{
1145-
std::string key = bson_iterator_key(&subiter);
1146-
if (bson_iterator_type(&subiter) == BSON_STRING)
1147-
(*j)[key] = bson_iterator_string(&subiter);
1148-
else if (bson_iterator_type(&subiter) == BSON_BOOL)
1149-
(*j)[key] = bson_iterator_bool(&subiter);
1150-
else if (bson_iterator_type(&subiter) == BSON_INT)
1151-
(*j)[key] = bson_iterator_int(&subiter);
1152-
else if (bson_iterator_type(&subiter) == BSON_LONG)
1153-
(*j)[key] = (Json::Value::Int64)bson_iterator_long(&subiter);
1154-
else if (bson_iterator_type(&subiter) == BSON_ARRAY)
1155-
{
1156-
bson_iterator arrayiter;
1157-
bson_iterator_subiterator(&subiter, &arrayiter);
1158-
while (bson_iterator_next(&arrayiter))
1159-
{
1160-
if (bson_iterator_type(&arrayiter) == BSON_OBJECT && !strcmp(bson_iterator_key(&arrayiter), "part"))
1161-
{
1162-
Json::Value tempPart;
1163-
ConvertBsonToJson(&arrayiter, &tempPart);
1164-
(*j)["links"].append(tempPart);
1165-
}
1166-
}
1167-
}
1168-
}
1169-
}
1170-
11711140
void GameSave::readPSv(char * data, int dataLength)
11721141
{
11731142
unsigned char * d = NULL, * c = (unsigned char *)data;
@@ -2377,8 +2346,68 @@ char * GameSave::serialiseOPS(unsigned int & dataLength)
23772346
return (char*)outputData;
23782347
}
23792348

2349+
void GameSave::ConvertBsonToJson(bson_iterator *iter, Json::Value *j)
2350+
{
2351+
bson_iterator subiter;
2352+
bson_iterator_subiterator(iter, &subiter);
2353+
while (bson_iterator_next(&subiter))
2354+
{
2355+
std::string key = bson_iterator_key(&subiter);
2356+
if (bson_iterator_type(&subiter) == BSON_STRING)
2357+
(*j)[key] = bson_iterator_string(&subiter);
2358+
else if (bson_iterator_type(&subiter) == BSON_BOOL)
2359+
(*j)[key] = bson_iterator_bool(&subiter);
2360+
else if (bson_iterator_type(&subiter) == BSON_INT)
2361+
(*j)[key] = bson_iterator_int(&subiter);
2362+
else if (bson_iterator_type(&subiter) == BSON_LONG)
2363+
(*j)[key] = (Json::Value::Int64)bson_iterator_long(&subiter);
2364+
else if (bson_iterator_type(&subiter) == BSON_ARRAY)
2365+
{
2366+
bson_iterator arrayiter;
2367+
bson_iterator_subiterator(&subiter, &arrayiter);
2368+
while (bson_iterator_next(&arrayiter))
2369+
{
2370+
if (bson_iterator_type(&arrayiter) == BSON_OBJECT && !strcmp(bson_iterator_key(&arrayiter), "part"))
2371+
{
2372+
Json::Value tempPart;
2373+
ConvertBsonToJson(&arrayiter, &tempPart);
2374+
(*j)["links"].append(tempPart);
2375+
}
2376+
else if (bson_iterator_type(&arrayiter) == BSON_INT && !strcmp(bson_iterator_key(&arrayiter), "saveID"))
2377+
{
2378+
(*j)["links"].append(bson_iterator_int(&arrayiter));
2379+
}
2380+
}
2381+
}
2382+
}
2383+
}
2384+
2385+
std::set<int> GetNestedSaveIDs(Json::Value j)
2386+
{
2387+
Json::Value::Members members = j.getMemberNames();
2388+
std::set<int> saveIDs = std::set<int>();
2389+
for (Json::Value::Members::iterator iter = members.begin(), end = members.end(); iter != end; ++iter)
2390+
{
2391+
std::string member = *iter;
2392+
if (member == "id")
2393+
saveIDs.insert(j[member].asInt());
2394+
else if (j[member].isArray())
2395+
{
2396+
for (Json::Value::ArrayIndex i = 0; i < j[member].size(); i++)
2397+
{
2398+
// only supports objects here because that is all we need
2399+
if (!j[member][i].isObject())
2400+
continue;
2401+
std::set<int> nestedSaveIDs = GetNestedSaveIDs(j[member][i]);
2402+
saveIDs.insert(nestedSaveIDs.begin(), nestedSaveIDs.end());
2403+
}
2404+
}
2405+
}
2406+
return saveIDs;
2407+
}
2408+
23802409
// converts a json object to bson
2381-
void GameSave::ConvertJsonToBson(bson *b, Json::Value j)
2410+
void GameSave::ConvertJsonToBson(bson *b, Json::Value j, int depth)
23822411
{
23832412
Json::Value::Members members = j.getMemberNames();
23842413
for (Json::Value::Members::iterator iter = members.begin(), end = members.end(); iter != end; ++iter)
@@ -2395,14 +2424,27 @@ void GameSave::ConvertJsonToBson(bson *b, Json::Value j)
23952424
else if (j[member].isArray())
23962425
{
23972426
bson_append_start_array(b, member.c_str());
2427+
std::set<int> saveIDs = std::set<int>();
23982428
for (Json::Value::ArrayIndex i = 0; i < j[member].size(); i++)
23992429
{
24002430
// only supports objects here because that is all we need
24012431
if (!j[member][i].isObject())
24022432
continue;
2403-
bson_append_start_object(b, "part");
2404-
ConvertJsonToBson(b, j[member][i]);
2405-
bson_append_finish_object(b);
2433+
if (depth > 4)
2434+
{
2435+
std::set<int> nestedSaveIDs = GetNestedSaveIDs(j[member][i]);
2436+
saveIDs.insert(nestedSaveIDs.begin(), nestedSaveIDs.end());
2437+
}
2438+
else
2439+
{
2440+
bson_append_start_object(b, "part");
2441+
ConvertJsonToBson(b, j[member][i], depth+1);
2442+
bson_append_finish_object(b);
2443+
}
2444+
}
2445+
for (std::set<int>::iterator iter = saveIDs.begin(), end = saveIDs.end(); iter != end; ++iter)
2446+
{
2447+
bson_append_int(b, "saveID", *iter);
24062448
}
24072449
bson_append_finish_array(b);
24082450
}

src/client/GameSave.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class GameSave
116116
void readOPS(char * data, int dataLength);
117117
void readPSv(char * data, int dataLength);
118118
char * serialiseOPS(unsigned int & dataSize);
119-
void ConvertJsonToBson(bson *b, Json::Value j);
119+
void ConvertJsonToBson(bson *b, Json::Value j, int depth = 0);
120120
void ConvertBsonToJson(bson_iterator *b, Json::Value *j);
121121
};
122122

src/gui/game/GameController.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,14 @@ void GameController::HistoryRestore()
242242
if (historyPosition == history.size())
243243
{
244244
Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot();
245+
if (newSnap)
246+
newSnap->Authors = Client::Ref().GetAuthorInfo();
245247
delete gameModel->GetRedoHistory();
246248
gameModel->SetRedoHistory(newSnap);
247249
}
248250
Snapshot * snap = history[newHistoryPosition];
249251
gameModel->GetSimulation()->Restore(*snap);
252+
Client::Ref().OverwriteAuthorInfo(snap->Authors);
250253
gameModel->SetHistory(history);
251254
gameModel->SetHistoryPosition(newHistoryPosition);
252255
}
@@ -258,6 +261,7 @@ void GameController::HistorySnapshot()
258261
Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot();
259262
if (newSnap)
260263
{
264+
newSnap->Authors = Client::Ref().GetAuthorInfo();
261265
while (historyPosition < history.size())
262266
{
263267
Snapshot * snap = history.back();
@@ -295,6 +299,7 @@ void GameController::HistoryForward()
295299
if (!snap)
296300
return;
297301
gameModel->GetSimulation()->Restore(*snap);
302+
Client::Ref().OverwriteAuthorInfo(snap->Authors);
298303
gameModel->SetHistoryPosition(newHistoryPosition);
299304
}
300305

src/gui/game/GameModel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ void GameModel::SetSave(SaveInfo * newSave)
659659
saveData->authors["username"] = newSave->userName;
660660
saveData->authors["title"] = newSave->name;
661661
saveData->authors["description"] = newSave->Description;
662-
saveData->authors["published"] = newSave->Published;
662+
saveData->authors["published"] = (int)newSave->Published;
663663
saveData->authors["date"] = newSave->updatedDate;
664664
}
665665
// This save was probably just created, and we didn't know the ID when creating it

src/gui/game/GameView.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,7 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
14921492
case 'a':
14931493
if (ctrl)
14941494
{
1495-
std::string authorString = Client::Ref().GetAuthorString();
1495+
std::string authorString = Client::Ref().GetAuthorInfo().toStyledString();
14961496
new InformationMessage("Save authorship info", authorString, true);
14971497
}
14981498
break;

src/gui/save/ServerSaveActivity.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ void ServerSaveActivity::AddAuthorInfo()
266266
serverSaveInfo["username"] = Client::Ref().GetAuthUser().Username;
267267
serverSaveInfo["title"] = save.GetName();
268268
serverSaveInfo["description"] = save.GetDescription();
269-
serverSaveInfo["published"] = save.GetPublished();
269+
serverSaveInfo["published"] = (int)save.GetPublished();
270270
serverSaveInfo["date"] = (Json::Value::UInt64)time(NULL);
271271
Client::Ref().SaveAuthorInfo(&serverSaveInfo);
272272
save.GetGameSave()->authors = serverSaveInfo;

src/simulation/Snapshot.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <vector>
44

55
#include "Particle.h"
6+
#include "json/json.h"
67

78
class Snapshot
89
{
@@ -30,6 +31,8 @@ class Snapshot
3031
std::vector<int> WirelessData;
3132
std::vector<playerst> stickmen;
3233
std::vector<sign> signs;
34+
35+
Json::Value Authors;
3336

3437
Snapshot() :
3538
AirPressure(),

0 commit comments

Comments
 (0)