Voting, fix save browser

This commit is contained in:
Simon Robertshaw 2012-01-28 19:56:13 +00:00
parent 28d4aecb6c
commit 7c53ca7799
17 changed files with 166 additions and 38 deletions

BIN
resources/document.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

4
resources/powder-res.rc Normal file
View File

@ -0,0 +1,4 @@
#define IDI_ICON1 101
#define IDI_ICON2 102
IDI_ICON1 ICON DISCARDABLE "powder.ico"
IDI_ICON2 ICON DISCARDABLE "document.ico"

BIN
resources/powder.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

View File

@ -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_):
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=";

View File

@ -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

View File

@ -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);
}

View File

@ -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();
}

View File

@ -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;
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();
}

View File

@ -47,6 +47,7 @@ public:
GameModel();
~GameModel();
void SetVote(int direction);
Save * GetSave();
Brush * GetBrush();
void SetSave(Save * newSave);

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}
};

View File

@ -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;
}

View File

@ -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();

View File

@ -19,7 +19,7 @@ void SearchModel::UpdateSaveList(int pageNumber, std::string query)
resultCount = 0;
notifySaveListChanged();
notifyPageChanged();
vector<Save*> * tempSaveList = Client::Ref().SearchSaves((pageNumber-1)*12, 12, query, currentSort, resultCount);
vector<Save*> * tempSaveList = Client::Ref().SearchSaves((pageNumber-1)*20, 20, query, currentSort, resultCount);
saveList = *tempSaveList;
delete tempSaveList;
if(!saveList.size())

View File

@ -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<Save*> saves = sender->GetSaveList();