diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2013-07-28 09:30:01 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2013-07-28 09:30:01 (GMT) |
| commit | ddd51aed3d354fc81ec764fb72aa76f56884a840 (patch) | |
| tree | e272254e2299c02b6c5019e8f22dd0b44e8f4b5f /src/client/Client.cpp | |
| parent | 7a847e7a5559c21df3b6357d6620e3581292e71d (diff) | |
| download | powder-ddd51aed3d354fc81ec764fb72aa76f56884a840.zip powder-ddd51aed3d354fc81ec764fb72aa76f56884a840.tar.gz | |
Async Request methods for SaveInfo and SaveData
Diffstat (limited to 'src/client/Client.cpp')
| -rw-r--r-- | src/client/Client.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 9d819e6..c446c14 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<std::string> 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<SaveComment*> * commentArray = new std::vector<SaveComment*>(); + 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<SaveComment*>*)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<SaveComment*> * Client::GetComments(int saveID, int start, int count) { lastError = ""; |
