summaryrefslogtreecommitdiff
path: root/src/client/Client.cpp
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-01-28 19:56:13 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-01-28 19:56:13 (GMT)
commit7c53ca7799832920066c23cfad2f1d7fa82233c7 (patch)
treea29fab25e584fb7f0d3705f13ac0a97abaae122a /src/client/Client.cpp
parent28d4aecb6c31ac1e450c1f073a0db13437d9d5d1 (diff)
downloadpowder-7c53ca7799832920066c23cfad2f1d7fa82233c7.zip
powder-7c53ca7799832920066c23cfad2f1d7fa82233c7.tar.gz
Voting, fix save browser
Diffstat (limited to 'src/client/Client.cpp')
-rw-r--r--src/client/Client.cpp81
1 files changed, 77 insertions, 4 deletions
diff --git a/src/client/Client.cpp b/src/client/Client.cpp
index 1d550ff..9b8b9c2 100644
--- a/src/client/Client.cpp
+++ b/src/client/Client.cpp
@@ -18,7 +18,8 @@
#include "cajun/writer.h"
#include "cajun/elements.h"
-Client::Client()
+Client::Client():
+ authUser(0, "")
{
int i = 0;
http_init(NULL);
@@ -40,6 +41,68 @@ Client::~Client()
http_done();
}
+void Client::SetAuthUser(User user)
+{
+ authUser = user;
+}
+
+User Client::GetAuthUser()
+{
+ return authUser;
+}
+
+RequestStatus Client::ExecVote(int saveID, int direction)
+{
+ lastError = "";
+ int dataStatus;
+ char * data;
+ int dataLength = 0;
+ std::stringstream idStream;
+ idStream << saveID;
+ std::string directionS;
+ if(direction==1)
+ {
+ directionS = "Up";
+ }
+ else
+ {
+ directionS = "Down";
+ }
+ std::stringstream userIDStream;
+ userIDStream << authUser.ID;
+ if(authUser.ID)
+ {
+ char * postNames[] = { "ID", "Action", NULL };
+ char * postDatas[] = { (char*)(idStream.str().c_str()), (char*)(directionS.c_str()) };
+ int postLengths[] = { idStream.str().length(), directionS.length() };
+ //std::cout << postNames[0] << " " << postDatas[0] << " " << postLengths[0] << std::endl;
+ data = http_multipart_post("http://" SERVER "/Vote.api", postNames, postDatas, postLengths, (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength);
+ }
+ else
+ {
+ lastError = "Not authenticated";
+ return RequestFailure;
+ }
+ std::cout << data << std::endl;
+ if(data && dataStatus == 200)
+ {
+ if(strncmp((const char *)data, "OK", 2)!=0)
+ {
+ free(data);
+ lastError = std::string((const char *)data);
+ return RequestFailure;
+ }
+ free(data);
+ return RequestOkay;
+ }
+ else if(data)
+ {
+ free(data);
+ }
+ lastError = http_ret_text(dataStatus);
+ return RequestFailure;
+}
+
unsigned char * Client::GetSaveData(int saveID, int saveDate, int & dataLength)
{
lastError = "";
@@ -94,7 +157,6 @@ LoginStatus Client::Login(string username, string password, User & user)
int postLengths[] = { username.length(), 32 };
data = http_multipart_post("http://" SERVER "/Login.json", postNames, postDatas, postLengths, NULL, NULL, NULL, &dataStatus, &dataLength);
//data = http_auth_get("http://" SERVER "/Login.json", (char*)username.c_str(), (char*)password.c_str(), NULL, &dataStatus, &dataLength);
- std::cout << data << std::endl;
if(dataStatus == 200 && data)
{
try
@@ -152,7 +214,16 @@ Save * Client::GetSave(int saveID, int saveDate)
char * data;
int dataStatus, dataLength;
//Save(int _id, int _votesUp, int _votesDown, string _userName, string _name, string description_, string date_, bool published_):
- data = http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength);
+ if(authUser.ID)
+ {
+ std::stringstream userIDStream;
+ userIDStream << authUser.ID;
+ data = http_auth_get((char *)urlStream.str().c_str(), (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength);
+ }
+ else
+ {
+ data = http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength);
+ }
if(dataStatus == 200 && data)
{
try
@@ -164,6 +235,7 @@ Save * Client::GetSave(int saveID, int saveDate)
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"];
@@ -174,6 +246,7 @@ Save * Client::GetSave(int saveID, int saveDate)
tempDate.Value(),
tempScoreUp.Value(),
tempScoreDown.Value(),
+ tempMyScore.Value(),
tempUsername.Value(),
tempName.Value(),
tempDescription.Value(),
@@ -240,7 +313,7 @@ std::vector<Save*> * Client::SearchSaves(int start, int count, string query, str
std::stringstream urlStream;
char * data;
int dataStatus, dataLength;
- urlStream << "http://" << SERVER << "/Browse.json?Start=" << start << "&Count=" << cout;
+ urlStream << "http://" << SERVER << "/Browse.json?Start=" << start << "&Count=" << count;
if(query.length() || sort.length())
{
urlStream << "&Search_Query=";