diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 9d819e645..c446c14e3 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -1735,6 +1735,80 @@ SaveInfo * Client::GetSave(int saveID, int saveDate) return NULL; } +RequestBroker::Request * Client::GetSaveAsync(int saveID, int saveDate) +{ + std::stringstream urlStream; + urlStream << "http://" << SERVER << "/Browse/View.json?ID=" << saveID; + if(saveDate) + { + urlStream << "&Date=" << saveDate; + } + + class SaveInfoParser: public APIResultParser + { + virtual void * ProcessResponse(unsigned char * data, int dataLength) + { + try + { + std::istringstream dataStream((char*)data); + json::Object objDocument; + json::Reader::Read(objDocument, dataStream); + + json::Number tempID = objDocument["ID"]; + json::Number tempScoreUp = objDocument["ScoreUp"]; + json::Number tempScoreDown = objDocument["ScoreDown"]; + json::Number tempMyScore = objDocument["ScoreMine"]; + json::String tempUsername = objDocument["Username"]; + json::String tempName = objDocument["Name"]; + json::String tempDescription = objDocument["Description"]; + json::Number tempDate = objDocument["Date"]; + json::Boolean tempPublished = objDocument["Published"]; + json::Boolean tempFavourite = objDocument["Favourite"]; + json::Number tempComments = objDocument["Comments"]; + json::Number tempViews = objDocument["Views"]; + json::Number tempVersion = objDocument["Version"]; + + json::Array tagsArray = objDocument["Tags"]; + std::vector tempTags; + + for(int j = 0; j < tagsArray.Size(); j++) + { + json::String tempTag = tagsArray[j]; + tempTags.push_back(tempTag.Value()); + } + + SaveInfo * tempSave = new SaveInfo( + tempID.Value(), + tempDate.Value(), + tempScoreUp.Value(), + tempScoreDown.Value(), + tempMyScore.Value(), + tempUsername.Value(), + tempName.Value(), + tempDescription.Value(), + tempPublished.Value(), + tempTags + ); + tempSave->Comments = tempComments.Value(); + tempSave->Favourite = tempFavourite.Value(); + tempSave->Views = tempViews.Value(); + tempSave->Version = tempVersion.Value(); + return tempSave; + } + catch (json::Exception &e) + { + return NULL; + } + } + virtual void Cleanup(void * objectPtr) + { + delete (SaveInfo*)objectPtr; + } + virtual ~SaveInfoParser() { } + }; + return new APIRequest(urlStream.str(), new SaveInfoParser()); +} + Thumbnail * Client::GetPreview(int saveID, int saveDate) { std::stringstream urlStream; @@ -1777,6 +1851,54 @@ Thumbnail * Client::GetPreview(int saveID, int saveDate) return new Thumbnail(saveID, saveDate, (pixel *)malloc((128*128) * PIXELSIZE), ui::Point(128, 128)); } +RequestBroker::Request * Client::GetCommentsAsync(int saveID, int start, int count) +{ + class CommentsParser: public APIResultParser + { + virtual void * ProcessResponse(unsigned char * data, int dataLength) + { + std::vector * commentArray = new std::vector(); + try + { + std::istringstream dataStream((char*)data); + json::Array commentsArray; + json::Reader::Read(commentsArray, dataStream); + + for(int j = 0; j < commentsArray.Size(); j++) + { + json::Number tempUserID = commentsArray[j]["UserID"]; + json::String tempUsername = commentsArray[j]["Username"]; + json::String tempFormattedUsername = commentsArray[j]["FormattedUsername"]; + json::String tempComment = commentsArray[j]["Text"]; + commentArray->push_back( + new SaveComment( + tempUserID.Value(), + tempUsername.Value(), + tempFormattedUsername.Value(), + tempComment.Value() + ) + ); + } + return commentArray; + } + catch (json::Exception &e) + { + delete commentArray; + return NULL; + } + } + virtual void Cleanup(void * objectPtr) + { + delete (std::vector*)objectPtr; + } + virtual ~CommentsParser() { } + }; + + std::stringstream urlStream; + urlStream << "http://" << SERVER << "/Browse/Comments.json?ID=" << saveID << "&Start=" << start << "&Count=" << count; + return new APIRequest(urlStream.str(), new CommentsParser()); +} + std::vector * Client::GetComments(int saveID, int start, int count) { lastError = ""; diff --git a/src/client/Client.h b/src/client/Client.h index 24f5782ea..659f615c5 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -144,10 +144,16 @@ public: void ClearThumbnailRequests(); std::vector * SearchSaves(int start, int count, std::string query, std::string sort, std::string category, int & resultCount); std::vector > * GetTags(int start, int count, std::string query, int & resultCount); + std::vector * GetComments(int saveID, int start, int count); + RequestBroker::Request * GetCommentsAsync(int saveID, int start, int count); + Thumbnail * GetPreview(int saveID, int saveDate); Thumbnail * GetThumbnail(int saveID, int saveDate); + SaveInfo * GetSave(int saveID, int saveDate); + RequestBroker::Request * GetSaveAsync(int saveID, int saveDate); + RequestStatus DeleteSave(int saveID); RequestStatus ReportSave(int saveID, std::string message); RequestStatus UnpublishSave(int saveID);