[GSoC Uni_] Writing and fetching from database (registering) works. Lots of debug code to be cleaned up.
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/uni@12913 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
parent
295a86017a
commit
fe47e50341
@ -24,6 +24,7 @@
|
||||
#include <assert.h>
|
||||
#include "online/http_connector.hpp"
|
||||
#include "config/user_config.hpp"
|
||||
#include "utils/string_utils.hpp"
|
||||
|
||||
static CurrentOnlineUser* user_singleton = NULL;
|
||||
|
||||
@ -48,8 +49,39 @@ CurrentOnlineUser::CurrentOnlineUser(){
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Register
|
||||
bool CurrentOnlineUser::signUp(const irr::core::stringw &username, const irr::core::stringw &password, irr::core::stringw &msg){
|
||||
assert(m_is_signed_in == false);
|
||||
HTTPConnector * connector = new HTTPConnector((std::string)UserConfigParams::m_server_multiplayer + "client-user.php");
|
||||
HTTPConnector::Parameters parameters;
|
||||
parameters["action"] = "register";
|
||||
parameters["user"] = username;
|
||||
parameters["password"] = password;
|
||||
const XMLNode * result = connector->getXMLFromPage(parameters);
|
||||
std::string rec_success;
|
||||
if(result->get("success", &rec_success))
|
||||
{
|
||||
if(rec_success == "yes")
|
||||
{
|
||||
msg = "Registered!";
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
msg = "Registering went wrong!";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
msg = "Registering went wrong!2";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CurrentOnlineUser::signIn(const std::string &username, const std::string &password)
|
||||
|
||||
// ============================================================================
|
||||
|
||||
bool CurrentOnlineUser::signIn(const irr::core::stringw &username, const irr::core::stringw &password, irr::core::stringw &msg)
|
||||
{
|
||||
assert(m_is_signed_in == false);
|
||||
HTTPConnector * connector = new HTTPConnector((std::string)UserConfigParams::m_server_multiplayer + "client-user.php");
|
||||
@ -58,12 +90,17 @@ bool CurrentOnlineUser::signIn(const std::string &username, const std::string &p
|
||||
parameters["user"] = username;
|
||||
parameters["password"] = password;
|
||||
const XMLNode * result = connector->getXMLFromPage(parameters);
|
||||
std::string token;
|
||||
bool succes = result->get("token", &token);
|
||||
if(succes)
|
||||
return false;
|
||||
std::string rec_token;
|
||||
irr::core::stringw rec_username;
|
||||
std::string rec_userid;
|
||||
|
||||
if(result->get("token", &rec_token) && result->get("username", &rec_username) && result->get("userid", &rec_userid))
|
||||
{
|
||||
m_user = new OnlineUser(username);
|
||||
m_token = token;
|
||||
long userid;
|
||||
StringUtils::fromString<long>(rec_userid, userid);
|
||||
m_user = new OnlineUser("");
|
||||
m_token = rec_token;
|
||||
m_is_signed_in = true;
|
||||
}
|
||||
else
|
||||
@ -75,7 +112,7 @@ bool CurrentOnlineUser::signIn(const std::string &username, const std::string &p
|
||||
|
||||
// ============================================================================
|
||||
|
||||
std::string CurrentOnlineUser::getUserName() const
|
||||
irr::core::stringw CurrentOnlineUser::getUserName() const
|
||||
{
|
||||
if(m_is_signed_in){
|
||||
assert(m_user != NULL);
|
||||
|
@ -21,6 +21,7 @@
|
||||
|
||||
#include "online/online_user.hpp"
|
||||
#include <string>
|
||||
#include <irrString.h>
|
||||
|
||||
|
||||
// ============================================================================
|
||||
@ -43,9 +44,10 @@ class CurrentOnlineUser
|
||||
// singleton
|
||||
static CurrentOnlineUser* get();
|
||||
static void deallocate();
|
||||
bool signIn(const std::string &username, const std::string &password);
|
||||
bool signIn(const irr::core::stringw &username, const irr::core::stringw &password, irr::core::stringw &msg);
|
||||
bool signUp(const irr::core::stringw &username, const irr::core::stringw &password, irr::core::stringw &msg);
|
||||
/** Returns the username if signed in. */
|
||||
std::string getUserName() const;
|
||||
irr::core::stringw getUserName() const;
|
||||
bool isSignedIn(){ return m_is_signed_in; }
|
||||
|
||||
}; // class CurrentOnlineUser
|
||||
|
@ -60,17 +60,17 @@ XMLNode * HTTPConnector::getXMLFromPage(Parameters & post_parameters)
|
||||
|
||||
std::string HTTPConnector::getPage(Parameters & post_parameters)
|
||||
{
|
||||
|
||||
Parameters::iterator iter;
|
||||
std::string postString;
|
||||
std::string postString = "";
|
||||
for (iter = post_parameters.begin(); iter != post_parameters.end(); ++iter)
|
||||
{
|
||||
if(iter != post_parameters.begin())
|
||||
postString.append("&");
|
||||
char * escaped = curl_easy_escape(this->curl , iter->first.c_str(), iter->first.size());
|
||||
postString.append(escaped);
|
||||
curl_free(escaped);
|
||||
postString.append(iter->first);
|
||||
postString.append("=");
|
||||
escaped = curl_easy_escape(this->curl , iter->second.c_str(), iter->second.size());
|
||||
core::stringc converted = core::stringc(iter->second.c_str());
|
||||
char * escaped = curl_easy_escape(this->curl , converted.c_str(), converted.size());
|
||||
postString.append(escaped);
|
||||
curl_free(escaped);
|
||||
}
|
||||
@ -80,7 +80,6 @@ std::string HTTPConnector::getPage(Parameters & post_parameters)
|
||||
curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
curl_easy_setopt(this->curl, CURLOPT_FILE, &readBuffer);
|
||||
res = curl_easy_perform(this->curl);
|
||||
|
||||
if(res != CURLE_OK)
|
||||
{
|
||||
Log::error("online/http_functions", "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
|
||||
|
@ -36,7 +36,7 @@ class HTTPConnector
|
||||
CURLcode res;
|
||||
|
||||
public:
|
||||
typedef std::map<std::string, std::string> Parameters;
|
||||
typedef std::map<std::string, core::stringw> Parameters;
|
||||
HTTPConnector(const std::string &url);
|
||||
~HTTPConnector();
|
||||
std::string getPage(Parameters & post_parameters);
|
||||
|
@ -24,7 +24,7 @@
|
||||
|
||||
|
||||
// ============================================================================
|
||||
OnlineUser::OnlineUser(const std::string &username)
|
||||
OnlineUser::OnlineUser(const irr::core::stringw &username)
|
||||
{
|
||||
m_username = username;
|
||||
} // OnlineUser
|
||||
|
@ -19,7 +19,7 @@
|
||||
#ifndef HEADER_ONLINE_USER_HPP
|
||||
#define HEADER_ONLINE_USER_HPP
|
||||
|
||||
#include <string>
|
||||
#include <irrString.h>
|
||||
|
||||
// ============================================================================
|
||||
|
||||
@ -33,17 +33,17 @@ class OnlineUser
|
||||
|
||||
protected:
|
||||
|
||||
std::string m_username;
|
||||
irr::core::stringw m_username;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
OnlineUser(const std::string &username);
|
||||
OnlineUser(const irr::core::stringw &username);
|
||||
|
||||
/** Returns the username. */
|
||||
std::string getUserName() const { return m_username; }
|
||||
irr::core::stringw getUserName() const { return m_username; }
|
||||
|
||||
}; // class OnlineUser
|
||||
|
||||
|
@ -59,6 +59,7 @@ LoginDialog::~LoginDialog()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
GUIEngine::EventPropagation LoginDialog::processEvent(const std::string& eventSource)
|
||||
@ -73,29 +74,21 @@ GUIEngine::EventPropagation LoginDialog::processEvent(const std::string& eventSo
|
||||
// ---- See if we can accept the input
|
||||
TextBoxWidget* textCtrl = getWidget<TextBoxWidget>("username");
|
||||
const stringw username = textCtrl->getText().trim();
|
||||
if (!StringUtils::notEmpty(username)){
|
||||
getWidget<LabelWidget>("errormsg")->setText(_("Username was empty"), false);
|
||||
sfx_manager->quickSound( "anvil" );
|
||||
return GUIEngine::EVENT_BLOCK;
|
||||
}
|
||||
textCtrl = getWidget<TextBoxWidget>("password");
|
||||
const stringw password = textCtrl->getText().trim();
|
||||
if (!StringUtils::notEmpty(password)){
|
||||
getWidget<LabelWidget>("errormsg")->setText(_("Password was empty"), false);
|
||||
sfx_manager->quickSound( "anvil" );
|
||||
return GUIEngine::EVENT_BLOCK;
|
||||
}
|
||||
;
|
||||
if(CurrentOnlineUser::get()->signIn(core::stringc(username.c_str()).c_str(), core::stringc(password.c_str()).c_str()))
|
||||
stringw msg = "";
|
||||
if(CurrentOnlineUser::get()->signUp(username,password,msg))
|
||||
{
|
||||
m_self_destroy = true;
|
||||
//m_self_destroy = true;
|
||||
m_self_destroy = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
getWidget<LabelWidget>("errormsg")->setText(_("Signing in went wrong."), false);
|
||||
|
||||
sfx_manager->quickSound( "anvil" );
|
||||
m_self_destroy = false;
|
||||
}
|
||||
getWidget<LabelWidget>("errormsg")->setText(msg, false);
|
||||
return GUIEngine::EVENT_BLOCK;
|
||||
}
|
||||
return GUIEngine::EVENT_LET;
|
||||
|
Loading…
x
Reference in New Issue
Block a user