diff --git a/src/Controller.h b/src/Controller.h new file mode 100644 index 000000000..5d18f36dc --- /dev/null +++ b/src/Controller.h @@ -0,0 +1,19 @@ +/* + * Controller.h + * + * Created on: Jan 25, 2012 + * Author: Simon + */ + +#ifndef CONTROLLER_H_ +#define CONTROLLER_H_ + +class ControllerCallback +{ +public: + ControllerCallback() {} + virtual void ControllerExit() {} + virtual ~ControllerCallback() {} +}; + +#endif /* CONTROLLER_H_ */ diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 0070e65af..b6fe2a41e 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -40,35 +40,31 @@ Client::~Client() http_done(); } -LoginStatus Client::Login(string username, string password) +LoginStatus Client::Login(string username, string password, User & user) { lastError = ""; std::stringstream urlStream; std::stringstream hashStream; - unsigned char cHashData[16]; + char passwordHash[33]; + char totalHash[33]; - //Build hash of username + password - struct md5_context md5; - md5_init(&md5); - md5_update(&md5, (unsigned char *)username.c_str(), username.length()); - md5_update(&md5, (unsigned char *)"-", 1); - md5_update(&md5, (unsigned char *)password.c_str(), password.length()); - md5_final(cHashData, &md5); - //Make sure hash is Hex - //char * cHashHex = (char *)malloc(33); - for(int i = 0; i < 16; i++) - { - hashStream << hexChars[cHashData[i]>>4] << hexChars[cHashData[i]&15]; - //cHashHex[i*2] = hex[cHashData[i]>>4]; - //cHashHex[i*2+1] = hex[cHashData[i]&15]; - } - //cHashHex[32] = 0; + user.ID = 0; + user.Username = ""; + user.SessionID = ""; + user.SessionKey = ""; + + //Doop + md5_ascii(passwordHash, (const unsigned char *)password.c_str(), password.length()); + passwordHash[32] = 0; + hashStream << username << "-" << passwordHash; + md5_ascii(totalHash, (const unsigned char *)(hashStream.str().c_str()), hashStream.str().length()); + totalHash[32] = 0; char * data; int dataStatus, dataLength; char * postNames[] = { "Username", "Hash", NULL }; - char * postDatas[] = { (char*)username.c_str(), (char*)hashStream.str().c_str() }; - int postLengths[] = { username.length(), hashStream.str().length() }; + char * postDatas[] = { (char*)username.c_str(), totalHash }; + 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; @@ -84,6 +80,13 @@ LoginStatus Client::Login(string username, string password) free(data); if(tempStatus.Value() == 1) { + json::Number userIDTemp = objDocument["UserID"]; + json::String sessionIDTemp = objDocument["SessionID"]; + json::String sessionKeyTemp = objDocument["SessionKey"]; + user.Username = username; + user.ID = userIDTemp.Value(); + user.SessionID = sessionIDTemp.Value(); + user.SessionKey = sessionKeyTemp.Value(); return LoginOkay; } else @@ -99,6 +102,10 @@ LoginStatus Client::Login(string username, string password) return LoginError; } } + else + { + lastError = http_ret_text(dataStatus); + } if(data) { free(data); diff --git a/src/client/Client.h b/src/client/Client.h index ef6a35eab..15ab03a9a 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -9,6 +9,7 @@ #include "search/Thumbnail.h" #include "search/Save.h" #include "Singleton.h" +#include "User.h" enum LoginStatus { @@ -28,7 +29,7 @@ private: public: Client(); ~Client(); - LoginStatus Login(string username, string password); + LoginStatus Login(string username, string password, User & user); void ClearThumbnailRequests(); std::vector * SearchSaves(int start, int count, string query, string sort, int & resultCount); Thumbnail * GetPreview(int saveID, int saveDate); diff --git a/src/client/MD5.cpp b/src/client/MD5.cpp index 9cb398c53..d921bfa77 100644 --- a/src/client/MD5.cpp +++ b/src/client/MD5.cpp @@ -208,7 +208,7 @@ void md5_transform(unsigned buf[4], const unsigned char inraw[64]) buf[3] += d; } -static char hex[] = "0123456789abcdef"; +static char hexChars[] = "0123456789abcdef"; void md5_ascii(char *result, unsigned char const *buf, unsigned len) { struct md5_context md5; @@ -224,8 +224,8 @@ void md5_ascii(char *result, unsigned char const *buf, unsigned len) for (i=0; i<16; i++) { - result[i*2] = hex[(hash[i]>>4)&0xF]; - result[i*2+1] = hex[hash[i]&0x0F]; + result[i*2] = hexChars[(hash[i]>>4)&0xF]; + result[i*2+1] = hexChars[hash[i]&0x0F]; } result[32] = 0; } diff --git a/src/client/User.h b/src/client/User.h new file mode 100644 index 000000000..9dd231c92 --- /dev/null +++ b/src/client/User.h @@ -0,0 +1,29 @@ +/* + * User.h + * + * Created on: Jan 25, 2012 + * Author: Simon + */ + +#ifndef USER_H_ +#define USER_H_ + +#include + +class User +{ +public: + int ID; + std::string Username; + std::string SessionID; + std::string SessionKey; + User(int id, std::string username): + ID(id), + Username(username) + { + + } +}; + + +#endif /* USER_H_ */ diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index b00c802f6..d7c9633c8 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -11,9 +11,23 @@ using namespace std; +class GameController::LoginCallback: public ControllerCallback +{ + GameController * cc; +public: + LoginCallback(GameController * cc_) { cc = cc_; } + virtual void ControllerExit() + { + cc->gameModel->SetUser(cc->loginWindow->GetUser()); + delete cc->loginWindow; + cc->loginWindow = NULL; + } +}; + GameController::GameController(): search(NULL), - renderOptions(NULL) + renderOptions(NULL), + loginWindow(NULL) { gameView = new GameView(); gameModel = new GameModel(); @@ -135,7 +149,7 @@ void GameController::OpenSearch() void GameController::OpenLogin() { - loginWindow = new LoginController(); + loginWindow = new LoginController(new LoginCallback(this)); ui::Engine::Ref().ShowWindow(loginWindow->GetView()); } diff --git a/src/game/GameController.h b/src/game/GameController.h index 67afdc4b2..157df99e6 100644 --- a/src/game/GameController.h +++ b/src/game/GameController.h @@ -25,6 +25,7 @@ private: RenderController * renderOptions; LoginController * loginWindow; public: + class LoginCallback; GameController(); ~GameController(); GameView * GetView(); diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 3271f3a39..8062c64c2 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -11,7 +11,8 @@ GameModel::GameModel(): sim(NULL), ren(NULL), currentSave(NULL), - currentBrush(new Brush(ui::Point(4, 4))) + currentBrush(new Brush(ui::Point(4, 4))), + currentUser(0, "") { sim = new Simulation(); ren = new Renderer(ui::Engine::Ref().g, sim); @@ -132,6 +133,17 @@ Renderer * GameModel::GetRenderer() return ren; } +User GameModel::GetUser() +{ + return currentUser; +} + +void GameModel::SetUser(User user) +{ + currentUser = user; + notifyUserChanged(); +} + void GameModel::SetPaused(bool pauseState) { sim->sys_pause = pauseState?1:0; @@ -211,3 +223,11 @@ void GameModel::notifyActiveToolChanged() observers[i]->NotifyActiveToolChanged(this); } } + +void GameModel::notifyUserChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyUserChanged(this); + } +} diff --git a/src/game/GameModel.h b/src/game/GameModel.h index 14319e759..b8f52175b 100644 --- a/src/game/GameModel.h +++ b/src/game/GameModel.h @@ -7,6 +7,7 @@ #include "Renderer.h" #include "GameView.h" #include "Brush.h" +#include "client/User.h" #include "Tool.h" #include "Menu.h" @@ -29,6 +30,7 @@ private: Simulation * sim; Renderer * ren; Tool * activeTool; + User currentUser; void notifyRendererChanged(); void notifySimulationChanged(); void notifyPausedChanged(); @@ -37,6 +39,7 @@ private: void notifyMenuListChanged(); void notifyToolListChanged(); void notifyActiveToolChanged(); + void notifyUserChanged(); public: GameModel(); ~GameModel(); @@ -53,7 +56,8 @@ public: vector GetToolList(); void SetActiveMenu(Menu * menu); Menu * GetActiveMenu(); - + User GetUser(); + void SetUser(User user); Simulation * GetSimulation(); Renderer * GetRenderer(); }; diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index d45275621..70f6663f6 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -305,6 +305,18 @@ void GameView::NotifySimulationChanged(GameModel * sender) { } +void GameView::NotifyUserChanged(GameModel * sender) +{ + if(!sender->GetUser().ID) + { + loginButton->SetText("Login"); + } + else + { + loginButton->SetText(sender->GetUser().Username); + } +} + void GameView::NotifyPausedChanged(GameModel * sender) { diff --git a/src/game/GameView.h b/src/game/GameView.h index ef355365f..f0c3e8256 100644 --- a/src/game/GameView.h +++ b/src/game/GameView.h @@ -48,6 +48,7 @@ public: void NotifyMenuListChanged(GameModel * sender); void NotifyToolListChanged(GameModel * sender); void NotifyActiveToolChanged(GameModel * sender); + void NotifyUserChanged(GameModel * sender); virtual void OnMouseMove(int x, int y, int dx, int dy); virtual void OnMouseDown(int x, int y, unsigned button); virtual void OnMouseUp(int x, int y, unsigned button); diff --git a/src/interface/Engine.cpp b/src/interface/Engine.cpp index 9778a5d94..a8220794e 100644 --- a/src/interface/Engine.cpp +++ b/src/interface/Engine.cpp @@ -18,7 +18,7 @@ Engine::Engine(): mousey_(0), mousexp_(0), mouseyp_(0), - FpsLimit(0.0f), + FpsLimit(60.0f), windows(stack()), lastBuffer(NULL), prevBuffers(stack()), diff --git a/src/login/LoginController.cpp b/src/login/LoginController.cpp index 64c822146..5a81a55f1 100644 --- a/src/login/LoginController.cpp +++ b/src/login/LoginController.cpp @@ -6,8 +6,9 @@ */ #include "LoginController.h" +#include "client/User.h" -LoginController::LoginController() { +LoginController::LoginController(ControllerCallback * callback) { // TODO Auto-generated constructor stub loginView = new LoginView(); loginModel = new LoginModel(); @@ -15,6 +16,8 @@ LoginController::LoginController() { loginView->AttachController(this); loginModel->AddObserver(loginView); + this->callback = callback; + } void LoginController::Login(string username, string password) @@ -22,6 +25,11 @@ void LoginController::Login(string username, string password) loginModel->Login(username, password); } +User LoginController::GetUser() +{ + return loginModel->GetUser(); +} + void LoginController::Exit() { if(ui::Engine::Ref().GetWindow() == loginView) @@ -29,6 +37,8 @@ void LoginController::Exit() ui::Engine::Ref().CloseWindow(); loginView = NULL; } + if(callback) + callback->ControllerExit(); } LoginController::~LoginController() { diff --git a/src/login/LoginController.h b/src/login/LoginController.h index 2df28ab9f..eb15d649c 100644 --- a/src/login/LoginController.h +++ b/src/login/LoginController.h @@ -11,6 +11,8 @@ #include #include "LoginView.h" #include "LoginModel.h" +#include "Controller.h" +#include "client/User.h" using namespace std; @@ -19,12 +21,13 @@ class LoginModel; class LoginController { LoginView * loginView; LoginModel * loginModel; + ControllerCallback * callback; public: - LoginController(); + LoginController(ControllerCallback * callback = NULL); void Login(string username, string password); void Exit(); LoginView * GetView() { return loginView; } - + User GetUser(); virtual ~LoginController(); }; diff --git a/src/login/LoginModel.cpp b/src/login/LoginModel.cpp index 185df683b..30156131c 100644 --- a/src/login/LoginModel.cpp +++ b/src/login/LoginModel.cpp @@ -7,7 +7,9 @@ #include "LoginModel.h" -LoginModel::LoginModel() { +LoginModel::LoginModel(): + currentUser(0, "") +{ // TODO Auto-generated constructor stub } @@ -17,7 +19,7 @@ void LoginModel::Login(string username, string password) statusText = "Logging in..."; loginStatus = false; notifyStatusChanged(); - LoginStatus status = Client::Ref().Login(username, password); + LoginStatus status = Client::Ref().Login(username, password, currentUser); switch(status) { case LoginOkay: @@ -41,6 +43,11 @@ string LoginModel::GetStatusText() return statusText; } +User LoginModel::GetUser() +{ + return currentUser; +} + bool LoginModel::GetStatus() { return loginStatus; diff --git a/src/login/LoginModel.h b/src/login/LoginModel.h index cd10a5db5..121d78e29 100644 --- a/src/login/LoginModel.h +++ b/src/login/LoginModel.h @@ -21,12 +21,14 @@ class LoginModel { string statusText; bool loginStatus; void notifyStatusChanged(); + User currentUser; public: LoginModel(); void Login(string username, string password); void AddObserver(LoginView * observer); string GetStatusText(); bool GetStatus(); + User GetUser(); virtual ~LoginModel(); };