summaryrefslogtreecommitdiff
path: root/src/client
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
parent28d4aecb6c31ac1e450c1f073a0db13437d9d5d1 (diff)
downloadpowder-7c53ca7799832920066c23cfad2f1d7fa82233c7.zip
powder-7c53ca7799832920066c23cfad2f1d7fa82233c7.tar.gz
Voting, fix save browser
Diffstat (limited to 'src/client')
-rw-r--r--src/client/Client.cpp81
-rw-r--r--src/client/Client.h24
-rw-r--r--src/client/HTTP.cpp1
3 files changed, 97 insertions, 9 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=";
diff --git a/src/client/Client.h b/src/client/Client.h
index 1b3bb02..89c4e64 100644
--- a/src/client/Client.h
+++ b/src/client/Client.h
@@ -11,15 +11,22 @@
#include "Singleton.h"
#include "User.h"
-enum LoginStatus
-{
+enum LoginStatus {
LoginOkay, LoginError
};
-class Client: public Singleton<Client>
-{
+enum RequestStatus {
+ RequestOkay, RequestFailure
+};
+
+class Client: public Singleton<Client> {
private:
std::string lastError;
+
+ //Auth session
+ User authUser;
+
+ //Thumbnail retreival
int thumbnailCacheNextID;
Thumbnail * thumbnailCache[THUMB_CACHE_SIZE];
void * activeThumbRequests[IMGCONNS];
@@ -29,6 +36,9 @@ private:
public:
Client();
~Client();
+
+ RequestStatus ExecVote(int saveID, int direction);
+
unsigned char * GetSaveData(int saveID, int saveDate, int & dataLength);
LoginStatus Login(string username, string password, User & user);
void ClearThumbnailRequests();
@@ -36,7 +46,11 @@ public:
Thumbnail * GetPreview(int saveID, int saveDate);
Thumbnail * GetThumbnail(int saveID, int saveDate);
Save * GetSave(int saveID, int saveDate);
- std::string GetLastError() { return lastError; }
+ void SetAuthUser(User user);
+ User GetAuthUser();
+ std::string GetLastError() {
+ return lastError;
+ }
};
#endif // CLIENT_H
diff --git a/src/client/HTTP.cpp b/src/client/HTTP.cpp
index a94ac73..b4f4777 100644
--- a/src/client/HTTP.cpp
+++ b/src/client/HTTP.cpp
@@ -731,6 +731,7 @@ char *http_auth_get(char *uri, char *user, char *pass, char *session_id, int *re
*len = 0;
return NULL;
}
+ http_auth_headers(ctx, user, pass, session_id);
return http_async_req_stop(ctx, ret, len);
}