From 7c53ca7799832920066c23cfad2f1d7fa82233c7 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sat, 28 Jan 2012 19:56:13 +0000 Subject: Voting, fix save browser diff --git a/resources/document.ico b/resources/document.ico new file mode 100644 index 0000000..83c57e7 Binary files /dev/null and b/resources/document.ico differ diff --git a/resources/powder-res.rc b/resources/powder-res.rc new file mode 100644 index 0000000..46208e2 --- /dev/null +++ b/resources/powder-res.rc @@ -0,0 +1,4 @@ +#define IDI_ICON1 101 +#define IDI_ICON2 102 +IDI_ICON1 ICON DISCARDABLE "powder.ico" +IDI_ICON2 ICON DISCARDABLE "document.ico" diff --git a/resources/powder.ico b/resources/powder.ico new file mode 100644 index 0000000..cd3f6e0 Binary files /dev/null and b/resources/powder.ico differ 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 * 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 -{ +enum RequestStatus { + RequestOkay, RequestFailure +}; + +class Client: public Singleton { 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); } diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index f0c575d..1e41542 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -33,10 +33,6 @@ public: { if(cc->search->GetLoadedSave()) { - if(cc->gameModel->GetSave()) - { - delete cc->gameModel->GetSave(); - } cc->gameModel->SetSave(new Save(*(cc->search->GetLoadedSave()))); } } @@ -279,7 +275,8 @@ void GameController::OpenSaveWindow() void GameController::Vote(int direction) { - //TODO: Implement + if(gameModel->GetSave() && gameModel->GetUser().ID && gameModel->GetSave()->GetID() && gameModel->GetSave()->GetVote()==0) + gameModel->SetVote(direction); } void GameController::ChangeBrush() @@ -289,6 +286,7 @@ void GameController::ChangeBrush() void GameController::ClearSim() { + gameModel->SetSave(NULL); gameModel->ClearSimulation(); } diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 456d9c4..34cae1d 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -6,14 +6,15 @@ #include "interface/Point.h" #include "Brush.h" #include "EllipseBrush.h" +#include "client/Client.h" GameModel::GameModel(): activeTool(NULL), sim(NULL), ren(NULL), - currentSave(NULL), currentBrush(0), - currentUser(0, "") + currentUser(0, ""), + currentSave(NULL) { sim = new Simulation(); ren = new Renderer(ui::Engine::Ref().g, sim); @@ -66,6 +67,19 @@ GameModel::~GameModel() delete activeTool; } +void GameModel::SetVote(int direction) +{ + if(currentSave) + { + RequestStatus status = Client::Ref().ExecVote(currentSave->GetID(), direction); + if(status == RequestOkay) + { + currentSave->vote = direction; + notifySaveChanged(); + } + } +} + Brush * GameModel::GetBrush() { return brushList[currentBrush]; @@ -140,8 +154,13 @@ Save * GameModel::GetSave() void GameModel::SetSave(Save * newSave) { + if(currentSave) + delete currentSave; currentSave = newSave; - sim->Load(currentSave->GetData(), currentSave->GetDataLength()); + if(currentSave) + { + sim->Load(currentSave->GetData(), currentSave->GetDataLength()); + } notifySaveChanged(); } @@ -218,6 +237,7 @@ int GameModel::GetZoomFactor() void GameModel::SetUser(User user) { currentUser = user; + Client::Ref().SetAuthUser(user); notifyUserChanged(); } diff --git a/src/game/GameModel.h b/src/game/GameModel.h index 3c26bf3..39c7f4a 100644 --- a/src/game/GameModel.h +++ b/src/game/GameModel.h @@ -47,6 +47,7 @@ public: GameModel(); ~GameModel(); + void SetVote(int direction); Save * GetSave(); Brush * GetBrush(); void SetSave(Save * newSave); diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index 3b1cd26..9a2eb2d 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -327,6 +327,7 @@ void GameView::NotifyUserChanged(GameModel * sender) { loginButton->SetText(sender->GetUser().Username); } + NotifySaveChanged(sender); } @@ -339,26 +340,26 @@ void GameView::NotifySaveChanged(GameModel * sender) { if(sender->GetSave()) { - saveSimulationButton->SetText(sender->GetSave()->name); reloadButton->Enabled = true; - if(sender->GetSave()->GetID()) //Online saves have an ID, local saves have an ID of 0 and a filename - { - upVoteButton->Enabled = true; - downVoteButton->Enabled = true; - tagSimulationButton->Enabled = true; - } + upVoteButton->Enabled = (sender->GetSave()->GetID() && sender->GetUser().ID && sender->GetSave()->GetVote()==0); + if(sender->GetSave()->GetID() && sender->GetUser().ID && sender->GetSave()->GetVote()==1) + upVoteButton->SetBackgroundColour(ui::Colour(0, 200, 40)); else - { - upVoteButton->Enabled = false; - downVoteButton->Enabled = false; - tagSimulationButton->Enabled = false; - } + upVoteButton->SetBackgroundColour(ui::Colour(0, 0, 0)); + downVoteButton->Enabled = upVoteButton->Enabled; + if(sender->GetSave()->GetID() && sender->GetUser().ID && sender->GetSave()->GetVote()==-1) + downVoteButton->SetBackgroundColour(ui::Colour(200, 40, 40)); + else + downVoteButton->SetBackgroundColour(ui::Colour(0, 0, 0)); + tagSimulationButton->Enabled = (sender->GetSave()->GetID() && sender->GetUser().ID); } else { reloadButton->Enabled = false; upVoteButton->Enabled = false; + upVoteButton->SetBackgroundColour(ui::Colour(0, 0, 0)); downVoteButton->Enabled = false; + upVoteButton->SetBackgroundColour(ui::Colour(0, 0, 0)); tagSimulationButton->Enabled = false; } } diff --git a/src/interface/Button.cpp b/src/interface/Button.cpp index b4e32b2..fc3eda5 100644 --- a/src/interface/Button.cpp +++ b/src/interface/Button.cpp @@ -135,6 +135,7 @@ void Button::Draw(const Point& screenPos) } else { + g->fillrect(Position.X, Position.Y, Size.X, Size.Y, background.Red, background.Green, background.Blue, 180); g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 180, 180, 180, 255); g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, ButtonText, 180, 180, 180, 255); } diff --git a/src/interface/SaveButton.cpp b/src/interface/SaveButton.cpp index 38fb974..877dfda 100644 --- a/src/interface/SaveButton.cpp +++ b/src/interface/SaveButton.cpp @@ -148,7 +148,6 @@ void SaveButton::OnMouseLeave(int x, int y) void SaveButton::DoAction() { - std::cout << "Do action!" << std::endl; if(actionCallback) actionCallback->ActionCallback(this); } diff --git a/src/render/RenderView.cpp b/src/render/RenderView.cpp index 25978bc..1702d63 100644 --- a/src/render/RenderView.cpp +++ b/src/render/RenderView.cpp @@ -60,8 +60,10 @@ public: } virtual void ActionCallback(ui::Checkbox * sender) { - //if(sender->GetChecked()) + if(sender->GetChecked()) v->c->SetColourMode(colourMode); + else + v->c->SetColourMode(0); } }; diff --git a/src/search/Save.cpp b/src/search/Save.cpp index 5bcb0e9..47ac61a 100644 --- a/src/search/Save.cpp +++ b/src/search/Save.cpp @@ -11,7 +11,7 @@ Save::Save(Save & save) : userName(save.userName), name(save.name), Description(save.Description), date( save.date), Published(save.Published), id(save.id), votesUp( - save.votesUp), votesDown(save.votesDown), data(NULL) { + save.votesUp), votesDown(save.votesDown), data(NULL), vote(save.vote) { if (save.data) { std::cout << data << " " << save.data << std::endl; data = (unsigned char *) malloc(save.dataLength); @@ -24,14 +24,14 @@ Save::Save(int _id, int _date, int _votesUp, int _votesDown, string _userName, string _name) : id(_id), votesUp(_votesUp), votesDown(_votesDown), userName(_userName), name( _name), Description("No description provided"), date(_date), Published( - true), data(NULL) { + true), data(NULL), vote(0) { } -Save::Save(int _id, int date_, int _votesUp, int _votesDown, string _userName, +Save::Save(int _id, int date_, int _votesUp, int _votesDown, int _vote, string _userName, string _name, string description_, bool published_) : id(_id), votesUp(_votesUp), votesDown(_votesDown), userName(_userName), name( _name), Description(description_), date(date_), Published( - published_), data(NULL) { + published_), data(NULL), vote(_vote) { } Save::~Save() @@ -49,6 +49,15 @@ string Save::GetName() { return name; } +void Save::SetVote(int vote) +{ + this->vote = vote; +} +int Save::GetVote() +{ + return vote; +} + void Save::SetUserName(string userName) { this->userName = userName; } diff --git a/src/search/Save.h b/src/search/Save.h index 42cbb62..5c471e1 100644 --- a/src/search/Save.h +++ b/src/search/Save.h @@ -21,7 +21,7 @@ public: Save(int _id, int _date, int _votesUp, int _votesDown, string _userName, string _name); - Save(int _id, int date_, int _votesUp, int _votesDown, string _userName, string _name, string description_, bool published_); + Save(int _id, int date_, int _votesUp, int _votesDown, int _vote, string _userName, string _name, string description_, bool published_); ~Save(); @@ -30,6 +30,8 @@ public: string Description; + int vote; + bool Published; void SetName(string name); @@ -41,6 +43,9 @@ public: void SetID(int id); int GetID(); + void SetVote(int vote); + int GetVote(); + void SetVotesUp(int votesUp); int GetVotesUp(); diff --git a/src/search/SearchModel.cpp b/src/search/SearchModel.cpp index d9a4a1e..d740620 100644 --- a/src/search/SearchModel.cpp +++ b/src/search/SearchModel.cpp @@ -19,7 +19,7 @@ void SearchModel::UpdateSaveList(int pageNumber, std::string query) resultCount = 0; notifySaveListChanged(); notifyPageChanged(); - vector * tempSaveList = Client::Ref().SearchSaves((pageNumber-1)*12, 12, query, currentSort, resultCount); + vector * tempSaveList = Client::Ref().SearchSaves((pageNumber-1)*20, 20, query, currentSort, resultCount); saveList = *tempSaveList; delete tempSaveList; if(!saveList.size()) diff --git a/src/search/SearchView.cpp b/src/search/SearchView.cpp index 8e5360f..b1d9765 100644 --- a/src/search/SearchView.cpp +++ b/src/search/SearchView.cpp @@ -141,7 +141,7 @@ void SearchView::NotifyPageChanged(SearchModel * sender) void SearchView::NotifySaveListChanged(SearchModel * sender) { int i = 0; - int buttonWidth, buttonHeight, saveX = 0, saveY = 0, savesX = 4, savesY = 3, buttonPadding = 2; + int buttonWidth, buttonHeight, saveX = 0, saveY = 0, savesX = 5, savesY = 4, buttonPadding = 2; int buttonAreaWidth, buttonAreaHeight, buttonXOffset, buttonYOffset; vector saves = sender->GetSaveList(); -- cgit v0.9.2-21-gd62e