Another enhancement for my HTTPManager which needed a lot of changes.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/uni@13307 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
unitraxx 2013-07-21 15:21:48 +00:00
parent dea145952b
commit 1cdad60b17
16 changed files with 236 additions and 263 deletions

View File

@ -68,7 +68,7 @@ namespace Online{
// ============================================================================
CurrentUser::CurrentUser(){
m_state = SIGNED_OUT;
m_state = US_SIGNED_OUT;
m_id = 0;
m_name = "";
m_token = "";
@ -82,8 +82,8 @@ namespace Online{
const irr::core::stringw &email,
bool terms)
{
assert(m_state == SIGNED_OUT || m_state == GUEST);
XMLRequest * request = new XMLRequest(Request::RT_SIGN_UP);
assert(m_state == US_SIGNED_OUT || m_state == US_GUEST);
XMLRequest * request = new XMLRequest();
request->setURL((std::string)UserConfigParams::m_server_multiplayer + "client-user.php");
request->setParameter("action",std::string("register"));
request->setParameter("username",username);
@ -94,10 +94,10 @@ namespace Online{
}
// ============================================================================
SignInRequest * CurrentUser::requestSavedSession()
CurrentUser::SignInRequest * CurrentUser::requestSavedSession()
{
SignInRequest * request = NULL;
if(m_state != SIGNED_IN && UserConfigParams::m_saved_session)
if(m_state != US_SIGNED_IN && UserConfigParams::m_saved_session)
{
request = new SignInRequest();
request->setURL((std::string)UserConfigParams::m_server_multiplayer + "client-user.php");
@ -105,16 +105,16 @@ namespace Online{
request->setParameter("userid", UserConfigParams::m_saved_user);
request->setParameter("token", UserConfigParams::m_saved_token.c_str());
HTTPManager::get()->addRequest(request);
m_state = SIGNING_IN;
m_state = US_SIGNING_IN;
}
return request;
}
SignInRequest * CurrentUser::requestSignIn( const irr::core::stringw &username,
CurrentUser::SignInRequest * CurrentUser::requestSignIn( const irr::core::stringw &username,
const irr::core::stringw &password,
bool save_session)
{
assert(m_state == SIGNED_OUT);
assert(m_state == US_SIGNED_OUT);
m_save_session = save_session;
SignInRequest * request = new SignInRequest();
request->setURL((std::string)UserConfigParams::m_server_multiplayer + "client-user.php");
@ -122,7 +122,7 @@ namespace Online{
request->setParameter("username",username);
request->setParameter("password",password);
HTTPManager::get()->addRequest(request);
m_state = SIGNING_IN;
m_state = US_SIGNING_IN;
return request;
}
@ -134,7 +134,7 @@ namespace Online{
int username_fetched = input->getResult()->get("username", &m_name);
int userid_fetched = input->getResult()->get("userid", &m_id);
assert(token_fetched && username_fetched && userid_fetched);
m_state = SIGNED_IN;
m_state = US_SIGNED_IN;
if(m_save_session)
{
UserConfigParams::m_saved_user = m_id;
@ -144,10 +144,10 @@ namespace Online{
}
}
else
m_state = SIGNED_OUT;
m_state = US_SIGNED_OUT;
}
void SignInRequest::callback()
void CurrentUser::SignInRequest::callback()
{
CurrentUser::acquire()->signIn(this);
CurrentUser::release();
@ -155,9 +155,9 @@ namespace Online{
// ============================================================================
ServerCreationRequest * CurrentUser::requestServerCreation(const irr::core::stringw &name, int max_players)
CurrentUser::ServerCreationRequest * CurrentUser::requestServerCreation(const irr::core::stringw &name, int max_players)
{
assert(m_state == SIGNED_IN);
assert(m_state == US_SIGNED_IN);
ServerCreationRequest * request = new ServerCreationRequest();
request->setURL((std::string)UserConfigParams::m_server_multiplayer + "client-user.php");
request->setParameter("action", std::string("create_server"));
@ -169,21 +169,21 @@ namespace Online{
return request;
}
void ServerCreationRequest::callback()
void CurrentUser::ServerCreationRequest::callback()
{
//FIXME
}
// ============================================================================
SignOutRequest * CurrentUser::requestSignOut(){
assert(m_state == SIGNED_IN || m_state == GUEST);
CurrentUser::SignOutRequest * CurrentUser::requestSignOut(){
assert(m_state == US_SIGNED_IN || m_state == US_GUEST);
SignOutRequest * request = new SignOutRequest();
request->setURL((std::string)UserConfigParams::m_server_multiplayer + "client-user.php");
request->setParameter("action",std::string("disconnect"));
request->setParameter("token",m_token);
request->setParameter("userid",m_id);
HTTPManager::get()->addRequest(request);
m_state = SIGNING_OUT;
m_state = US_SIGNING_OUT;
return request;
}
@ -196,13 +196,13 @@ namespace Online{
m_token = "";
m_name = "";
m_id = 0;
m_state = SIGNED_OUT;
m_state = US_SIGNED_OUT;
UserConfigParams::m_saved_user = 0;
UserConfigParams::m_saved_token = "";
UserConfigParams::m_saved_session = false;
}
void SignOutRequest::callback()
void CurrentUser::SignOutRequest::callback()
{
CurrentUser::acquire()->signOut(this);
CurrentUser::release();
@ -210,8 +210,8 @@ namespace Online{
// ============================================================================
ServerJoinRequest * CurrentUser::requestServerJoin(uint32_t server_id){
assert(m_state == SIGNED_IN || m_state == GUEST);
CurrentUser::ServerJoinRequest * CurrentUser::requestServerJoin(uint32_t server_id){
assert(m_state == US_SIGNED_IN || m_state == US_GUEST);
ServerJoinRequest * request = new ServerJoinRequest();
request->setURL((std::string)UserConfigParams::m_server_multiplayer + "address-management.php");
request->setParameter("action",std::string("request-connection"));
@ -221,7 +221,7 @@ namespace Online{
return request;
}
void ServerJoinRequest::callback()
void CurrentUser::ServerJoinRequest::callback()
{
//FIXME
}
@ -230,7 +230,7 @@ namespace Online{
irr::core::stringw CurrentUser::getUserName() const
{
if((m_state == SIGNED_IN ) || (m_state == GUEST))
if((m_state == US_SIGNED_IN ) || (m_state == US_GUEST))
return m_name;
else
return _("Currently not signed in");

View File

@ -29,32 +29,6 @@
namespace Online{
class SignInRequest : public XMLRequest
{
virtual void callback ();
public :
SignInRequest() : XMLRequest(RT_SIGN_IN) {}
};
class SignOutRequest : public XMLRequest
{
virtual void callback ();
public :
SignOutRequest() : XMLRequest(RT_SIGN_OUT) {}
};
class ServerCreationRequest : public XMLRequest {
virtual void callback ();
public :
ServerCreationRequest() : XMLRequest(RT_SERVER_JOIN) {}
};
class ServerJoinRequest : public XMLRequest {
virtual void callback ();
public :
ServerJoinRequest() : XMLRequest(RT_SERVER_JOIN) {}
};
// ============================================================================
/**
@ -66,13 +40,49 @@ namespace Online{
public:
enum UserState
{
SIGNED_OUT,
SIGNED_IN,
GUEST,
SIGNING_IN,
SIGNING_OUT
US_SIGNED_OUT,
US_SIGNED_IN,
US_GUEST,
US_SIGNING_IN,
US_SIGNING_OUT
};
enum RequestType
{
RT_SIGN_IN = 1,
RT_SIGN_OUT,
RT_SIGN_UP,
RT_SERVER_JOIN,
RT_SERVER_CREATION
};
class SignInRequest : public XMLRequest
{
virtual void callback ();
public:
SignInRequest() : XMLRequest(RT_SIGN_IN) {}
};
class SignOutRequest : public XMLRequest
{
virtual void callback ();
public:
SignOutRequest() : XMLRequest(RT_SIGN_OUT) {}
};
class ServerCreationRequest : public XMLRequest {
virtual void callback ();
public:
ServerCreationRequest() : XMLRequest(RT_SERVER_CREATION) {}
};
class ServerJoinRequest : public XMLRequest {
virtual void callback ();
public:
ServerJoinRequest() : XMLRequest(RT_SERVER_JOIN) {}
};
private:
std::string m_token;
bool m_save_session;
@ -110,9 +120,9 @@ namespace Online{
/** Returns the username if signed in. */
irr::core::stringw getUserName() const;
bool isSignedIn() const { return m_state == SIGNED_IN; }
bool isGuest() const { return m_state == GUEST; }
bool isSigningIn() const { return m_state == SIGNING_IN; }
bool isSignedIn() const { return m_state == US_SIGNED_IN; }
bool isGuest() const { return m_state == US_GUEST; }
bool isSigningIn() const { return m_state == US_SIGNING_IN; }
UserState getUserState() { return m_state; }
}; // class CurrentUser

View File

@ -74,11 +74,6 @@ namespace Online{
// ============================================================================
HTTPManager::~HTTPManager(){
curl_global_cleanup();
m_response_queue.lock();
//FIXME should all be deleted
m_response_queue.getData().clear();
m_response_queue.unlock();
//FIXME empty and delete request queue?
}
@ -129,9 +124,7 @@ namespace Online{
// a download, which mean we can get the mutex and ask the service
// thread here to cancel properly.
cancelAllDownloads();
QuitRequest * request = new QuitRequest();
addRequest(request);
addRequest(new Request(true, 9999, Request::RT_QUIT));
} // stopNetworkThread
@ -161,41 +154,6 @@ namespace Online{
m_request_queue.unlock();
} // insertRequest
// ----------------------------------------------------------------------------
/**
* \param request The pointer to the finished request to insert.
*/
void HTTPManager::addResponse(Request *request)
{
m_response_queue.lock();
if (m_response_queue.getData().count(request->getType()))
{
delete m_response_queue.getData()[request->getType()];
m_response_queue.getData().erase(request->getType());
}
m_response_queue.getData()[request->getType()] = request;
m_response_queue.unlock();
} // insertRequest
Request * HTTPManager::getResponse(Request::RequestType type)
{
Request * response = NULL;
m_response_queue.lock();
if (m_response_queue.getData().count(type))
response = m_response_queue.getData()[type];
m_response_queue.getData().erase(type);
m_response_queue.unlock();
return response;
}
XMLRequest * HTTPManager::getXMLResponse(Request::RequestType type)
{
Request * response = getResponse(type);
if(response != NULL)
return (XMLRequest *) response;
return NULL;
}
// ---------------------------------------------------------------------------
/** The actual main loop, which is started as a separate thread from the
* constructor. After testing for a new server, fetching news, the list
@ -210,7 +168,7 @@ namespace Online{
me->m_current_request = NULL;
me->m_request_queue.lock();
while( me->m_request_queue.getData().empty() || me->m_request_queue.getData().top()->getType() != Request::RT_QUIT )
while( me->m_request_queue.getData().empty() || me->m_request_queue.getData().top()->getType() != Request::RT_QUIT)
{
bool empty = me->m_request_queue.getData().empty();
// Wait in cond_wait for a request to arrive. The 'while' is necessary
@ -230,10 +188,6 @@ namespace Online{
delete me->m_current_request;
me->m_current_request = NULL;
}
else
{
me->addResponse(me->m_current_request);
}
me->m_request_queue.lock();
} // while

View File

@ -66,11 +66,6 @@ namespace Online{
>
> m_request_queue;
/** The list of pointers to all requests that are already handled but didn't need to be deleted. */
Synchronised< std::map < Online::Request::RequestType, Online::Request* > > m_response_queue;
void addResponse(Online::Request*);
static void *mainLoop(void *obj);
void startNetworkThread();
@ -91,9 +86,6 @@ namespace Online{
void addRequest(Online::Request *request);
void cancelAllDownloads();
void stopNetworkThread();
Request * getResponse(Online::Request::RequestType type);
/** Same as getResponse but with a cast to XMLRequest */
XMLRequest * getXMLResponse(Online::Request::RequestType type);
bool getAbort(){ return m_abort.getAtomic(); };

View File

@ -24,13 +24,6 @@ namespace Online
{
namespace Messages
{
irr::core::stringw loadingDots(bool spaces, float interval, int max_dots)
{
int nr_dots = int(floor(Time::getRealTime() * (1 / interval))) % (max_dots+1);
return irr::core::stringw((std::string(nr_dots,'.') + std::string(max_dots-nr_dots,' ')).c_str());
}
// ------------------------------------------------------------------------
irr::core::stringw signingIn()
{
return irr::core::stringw(_("Signing in")) + loadingDots();
@ -42,21 +35,35 @@ namespace Online
return irr::core::stringw(_("Signing out")) + loadingDots();
}
// ------------------------------------------------------------------------
irr::core::stringw signingUp()
{
return irr::core::stringw(_("Validating registration info")) + loadingDots();
}
// ------------------------------------------------------------------------
irr::core::stringw signedInAs(const irr::core::stringw & name)
{
return irr::core::stringw(_("Signed in as : ")) + name + ".";
}
// ------------------------------------------------------------------------
irr::core::stringw joiningServer()
{
return irr::core::stringw(_("Joining server")) + loadingDots();
}
// ------------------------------------------------------------------------
irr::core::stringw creatingServer()
{
return irr::core::stringw(_("Creating server")) + loadingDots();
}
// ------------------------------------------------------------------------
irr::core::stringw loadingDots(bool spaces, float interval, int max_dots)
{
int nr_dots = int(floor(Time::getRealTime() * (1 / interval))) % (max_dots+1);
return irr::core::stringw((std::string(nr_dots,'.') + std::string(max_dots-nr_dots,' ')).c_str());
}
} // namespace messages
} // namespace Online

View File

@ -29,6 +29,7 @@ namespace Online
irr::core::stringw loadingDots (bool spaces = true, float interval = 0.5f, int max_dots = 3);
irr::core::stringw signingIn ();
irr::core::stringw signingOut ();
irr::core::stringw signingUp ();
irr::core::stringw joiningServer ();
irr::core::stringw creatingServer ();
irr::core::stringw signedInAs (const irr::core::stringw & name);

View File

@ -28,8 +28,8 @@ namespace Online{
// =========================================================================================
Request::Request(RequestType type, int priority, bool manage_memory)
:m_type(type)
Request::Request(int type, bool manage_memory, int priority)
: m_type(type)
{
m_priority = priority;
m_manage_memory = manage_memory;
@ -55,18 +55,12 @@ namespace Online{
// =========================================================================================
QuitRequest::QuitRequest()
: Request(RT_QUIT,9999,true)
HTTPRequest::HTTPRequest(int type, bool manage_memory, int priority)
: Request(priority, manage_memory, type)
{
}
// =========================================================================================
HTTPRequest::HTTPRequest(RequestType type, const std::string & url)
: Request(type, 1,false)
{
m_url = url;
//Negative numbers are reserved for special requests ment for the HTTP Manager
assert(type >= 0);
m_url = "";
m_parameters = new Parameters;
m_progress.setAtomic(0);
}
@ -200,8 +194,8 @@ namespace Online{
// =========================================================================================
XMLRequest::XMLRequest(RequestType type, const std::string & url)
: HTTPRequest(type, url)
XMLRequest::XMLRequest(int type, bool manage_memory, int priority)
: HTTPRequest(priority, manage_memory, type)
{
m_info = "";
m_success = false;

View File

@ -32,18 +32,8 @@ namespace Online{
*/
class Request
{
public:
/** List of different types of requests which are used as key in the response map
*/
enum RequestType {
RT_QUIT,
RT_SIGN_IN,
RT_SIGN_OUT,
RT_SIGN_UP,
RT_SERVER_CREATION,
RT_SERVER_JOIN
};
private:
const int m_type;
protected:
/** The priority of this request. The higher the value the more
important this request is. */
@ -51,9 +41,6 @@ namespace Online{
/** Cancel this request if it is active. */
bool m_cancel;
const RequestType m_type;
/** True if the memory for this Request should be managed by
* http connector (i.e. this object is freed once the request
* is handled). Otherwise the memory is not freed, so it must
@ -63,12 +50,17 @@ namespace Online{
Synchronised<bool> m_done;
virtual void beforeOperation() {}
virtual void operation() = 0;
virtual void operation() {};
virtual void afterOperation();
public:
/** Negative numbers are reserved for requests that are special for the HTTP Manager*/
enum RequestType
{
RT_QUIT = -1
};
Request(RequestType type, int priority, bool manage_memory=true);
Request(int type, bool manage_memory, int priority);
virtual ~Request();
void execute();
@ -83,19 +75,16 @@ namespace Online{
/** Returns if this request is to be canceled. */
bool isCancelled() const { return m_cancel; }
// ------------------------------------------------------------------------
/** Specifies if the memory should be managed by http manager. */
void setManageMemory(bool m) { m_manage_memory = m; }
// ------------------------------------------------------------------------
/** Returns if the memory for this object should be managed by
* by network_http (i.e. freed once the request is handled). */
bool manageMemory() const { return m_manage_memory; }
bool isDone() const { return m_done.getAtomic(); }
RequestType getType() const { return m_type; }
virtual bool isAllowedToAdd() { return true; }
int getType() const { return m_type; }
/** This class is used by the priority queue to sort requests by priority.
*/
class Compare
@ -112,17 +101,6 @@ namespace Online{
// ========================================================================
class QuitRequest : public Request
{
public :
QuitRequest();
virtual void operation() OVERRIDE {}
};
// ========================================================================
class HTTPRequest : public Request
{
@ -153,7 +131,7 @@ namespace Online{
virtual void callback() {}
public :
HTTPRequest(RequestType type, const std::string & url = "");
HTTPRequest(int type = 0, bool manage_memory = false, int priority = 1);
virtual ~HTTPRequest();
void setParameter(const std::string & name, const std::string &value){
@ -191,7 +169,7 @@ namespace Online{
virtual void afterOperation() OVERRIDE;
public :
XMLRequest(RequestType type, const std::string & url = "");
XMLRequest(int type = 0, bool manage_memory = false, int priority = 1);
virtual XMLNode * getResult() const { return m_result; }
const irr::core::stringw & getInfo() const { return m_info; }

View File

@ -41,7 +41,7 @@ LoginDialog::LoginDialog(const Message message_type) :
{
m_self_destroy = false;
m_open_registration_dialog = false;
m_signing_in = false;
m_sign_in_request = NULL;
loadFromFile("online/login_dialog.stkgui");
m_message_widget = getWidget<LabelWidget>("message");
@ -96,6 +96,7 @@ LoginDialog::LoginDialog(const Message message_type) :
LoginDialog::~LoginDialog()
{
delete m_sign_in_request;
}
@ -114,9 +115,8 @@ void LoginDialog::login()
else
{
m_options_widget->setDeactivated();
Online::CurrentUser::acquire()->requestSignIn(username,password, m_remember_widget->getState());
m_sign_in_request = Online::CurrentUser::acquire()->requestSignIn(username,password, m_remember_widget->getState());
Online::CurrentUser::release();
m_signing_in = true;
}
}
@ -161,24 +161,36 @@ void LoginDialog::onEnterPressedInternal()
// -----------------------------------------------------------------------------
bool LoginDialog::onEscapePressed()
{
return false;
}
void LoginDialog::onUpdate(float dt)
{
XMLRequest * sign_in_request = HTTPManager::get()->getXMLResponse(Request::RT_SIGN_IN);
if(sign_in_request != NULL)
if(m_sign_in_request != NULL)
{
if(sign_in_request->isSuccess())
if(m_sign_in_request->isDone())
{
m_self_destroy = true;
if(m_sign_in_request->isSuccess())
{
m_self_destroy = true;
}
else
{
sfx_manager->quickSound( "anvil" );
m_info_widget->setErrorColor();
m_info_widget->setText(m_sign_in_request->getInfo(), false);
}
delete m_sign_in_request;
m_sign_in_request = NULL;
m_options_widget->setActivated();
}
else
{
sfx_manager->quickSound( "anvil" );
m_info_widget->setErrorColor();
m_info_widget->setText(sign_in_request->getInfo(), false);
m_info_widget->setDefaultColor();
m_info_widget->setText(Online::Messages::signingIn(), false);
}
delete sign_in_request;
m_signing_in = false;
m_options_widget->setActivated();
}
//If we want to open the registration dialog, we need to close this one first
m_open_registration_dialog && (m_self_destroy = true);
@ -191,10 +203,4 @@ void LoginDialog::onUpdate(float dt)
new RegistrationDialog();
return;
}
if (m_signing_in)
{
m_info_widget->setDefaultColor();
m_info_widget->setText(Online::Messages::signingIn(), false);
}
}

View File

@ -37,7 +37,7 @@ private:
bool m_self_destroy;
bool m_open_registration_dialog;
bool m_signing_in;
Online::CurrentUser::SignInRequest * m_sign_in_request;
GUIEngine::LabelWidget * m_message_widget;
GUIEngine::TextBoxWidget * m_username_widget;
GUIEngine::TextBoxWidget * m_password_widget;
@ -69,8 +69,10 @@ public:
~LoginDialog();
void onEnterPressedInternal();
GUIEngine::EventPropagation processEvent(const std::string& eventSource);
virtual bool onEscapePressed();
virtual void onUpdate(float dt);
//virtual void onTextUpdated();
};

View File

@ -25,6 +25,7 @@
#include "states_screens/state_manager.hpp"
#include "utils/translation.hpp"
#include "utils/string_utils.hpp"
#include "online/messages.hpp"
using namespace GUIEngine;
@ -38,6 +39,7 @@ RegistrationDialog::RegistrationDialog(const Phase phase) :
ModalDialog(0.8f,0.9f)
{
m_sign_up_request = NULL;
m_activation_request = NULL;
m_self_destroy = false;
m_show_registration_info = false;
m_show_registration_terms = false;
@ -59,6 +61,8 @@ RegistrationDialog::RegistrationDialog(const Phase phase) :
RegistrationDialog::~RegistrationDialog()
{
delete m_sign_up_request;
delete m_activation_request;
}
// -----------------------------------------------------------------------------
@ -190,8 +194,8 @@ bool RegistrationDialog::processTermsEvent(const std::string& eventSource){
if (eventSource == "next")
{
assert(getWidget<CheckBoxWidget>("accepted")->getState());
m_agreement = true;
m_sign_up_request = CurrentUser::acquire()->requestSignUp(m_username, m_password, m_password_confirm, m_email, m_agreement);
m_sign_up_request = CurrentUser::acquire()->requestSignUp(m_username, m_password, m_password_confirm, m_email, true);
CurrentUser::release();
return true;
}
else if (eventSource == "accepted")
@ -274,12 +278,42 @@ void RegistrationDialog::onEnterPressedInternal()
void RegistrationDialog::onUpdate(float dt)
{
/*
if(m_sign_up_request != NULL)
if (m_phase == Terms)
{
if(m_sign_up_request->isDone())
if(m_sign_up_request != NULL)
{
if(m_sign_up_request->isSuccess())
if(m_sign_up_request->isDone())
{
if(m_sign_up_request->isSuccess())
{
m_show_registration_activation = true;
}
else
{
sfx_manager->quickSound( "anvil" );
m_show_registration_info = true;
m_registration_error = m_sign_up_request->getInfo();
}
delete m_sign_up_request;
m_sign_up_request = NULL;
//FIXME m_options_widget->setActivated();
}
else
{
LabelWidget* label = getWidget<LabelWidget>("info");
assert(label != NULL);
label->setDefaultColor();
label->setText(Messages::signingUp(), false);
}
}
}
else if (m_phase == Activation)
{
/*
XMLRequest * sign_up_request = HTTPManager::get()->getXMLResponse(Request::RT_SIGN_UP);
if(sign_up_request != NULL)
{
if(sign_up_request->isSuccess())
{
m_show_registration_activation = true;
}
@ -287,18 +321,13 @@ void RegistrationDialog::onUpdate(float dt)
{
sfx_manager->quickSound( "anvil" );
m_show_registration_info = true;
m_registration_error = sign_up_request->getInfo();
}
m_options_widget->setActivated();
delete m_sign_in_request;
m_sign_in_request = NULL;
}
else
{
m_load_timer += dt;
m_info_widget->setDefaultColor();
m_info_widget->setText(Online::Messages::signingIn(m_load_timer), false);
}
}*/
delete sign_up_request;
m_signing_up = false;
//FIXME m_options_widget->setActivated();
}*/
}
// It's unsafe to delete from inside the event handler so we do it here
if (m_self_destroy)
ModalDialog::dismiss();

View File

@ -56,6 +56,7 @@ private:
bool m_show_registration_activation;
Online::XMLRequest * m_sign_up_request;
Online::XMLRequest * m_activation_request;
//Saved user input :
irr::core::stringw m_username;

View File

@ -46,7 +46,7 @@ ServerInfoDialog::ServerInfoDialog(Server * server) :
m_server = server;
m_self_destroy = false;
m_enter_lobby = false;
m_joining_server = false;
m_server_join_request = NULL;
loadFromFile("online/server_info_dialog.stkgui");
@ -69,13 +69,13 @@ ServerInfoDialog::ServerInfoDialog(Server * server) :
// -----------------------------------------------------------------------------
ServerInfoDialog::~ServerInfoDialog()
{
delete m_server_join_request;
}
// -----------------------------------------------------------------------------
void ServerInfoDialog::requestJoin()
{
Online::CurrentUser::acquire()->requestServerJoin(m_server->getServerId());
Online::CurrentUser::release();
m_joining_server = true;
}
// -----------------------------------------------------------------------------
@ -115,21 +115,28 @@ void ServerInfoDialog::onEnterPressedInternal()
void ServerInfoDialog::onUpdate(float dt)
{
XMLRequest * request = HTTPManager::get()->getXMLResponse(Request::RT_SERVER_JOIN);
if(request != NULL)
if(m_server_join_request != NULL)
{
if(request->isSuccess())
if(m_server_join_request->isDone())
{
m_self_destroy = true;
if(m_server_join_request->isSuccess())
{
m_self_destroy = true;
}
else
{
sfx_manager->quickSound( "anvil" );
m_info_widget->setErrorColor();
m_info_widget->setText(m_server_join_request->getInfo(), false);
}
delete m_server_join_request;
m_server_join_request = NULL;
}
else
{
sfx_manager->quickSound( "anvil" );
m_info_widget->setErrorColor();
m_info_widget->setText(request->getInfo(), false);
m_info_widget->setDefaultColor();
m_info_widget->setText(Online::Messages::joiningServer(), false);
}
delete request;
m_joining_server = false;
}
//If we want to open the registration dialog, we need to close this one first
@ -143,10 +150,4 @@ void ServerInfoDialog::onUpdate(float dt)
StateManager::get()->pushScreen(NetworkingLobby::getInstance());
return;
}
if (m_joining_server)
{
m_info_widget->setDefaultColor();
m_info_widget->setText(Online::Messages::joiningServer(), false);
}
}

View File

@ -40,7 +40,7 @@ private:
bool m_self_destroy;
bool m_enter_lobby;
bool m_joining_server;
Online::CurrentUser::ServerJoinRequest * m_server_join_request;
float m_load_timer;

View File

@ -51,8 +51,10 @@ DEFINE_SCREEN_SINGLETON( OnlineScreen );
OnlineScreen::OnlineScreen() : Screen("online/main.stkgui")
{
m_recorded_state = CurrentUser::SIGNED_OUT;
CurrentUser::acquire()->requestSavedSession();
m_recorded_state = CurrentUser::US_SIGNED_OUT;
CurrentUser::SignInRequest * request = CurrentUser::acquire()->requestSavedSession();
if(request != NULL)
m_requests.push_back(request);
CurrentUser::release();
} // OnlineScreen
@ -60,6 +62,7 @@ OnlineScreen::OnlineScreen() : Screen("online/main.stkgui")
OnlineScreen::~OnlineScreen()
{
m_requests.clearAndDeleteAll();
}
// ----------------------------------------------------------------------------
@ -108,24 +111,24 @@ void OnlineScreen::beforeAddingWidget()
m_bottom_menu_widget->setVisible(true);
m_top_menu_widget->setVisible(true);
hasStateChanged();
if (m_recorded_state == CurrentUser::SIGNED_IN)
if (m_recorded_state == CurrentUser::US_SIGNED_IN)
{
m_register_widget->setVisible(false);
m_sign_in_widget->setVisible(false);
}
else if (m_recorded_state == CurrentUser::SIGNED_OUT || m_recorded_state == CurrentUser::SIGNING_IN || m_recorded_state == CurrentUser::SIGNING_OUT)
else if (m_recorded_state == CurrentUser::US_SIGNED_OUT || m_recorded_state == CurrentUser::US_SIGNING_IN || m_recorded_state == CurrentUser::US_SIGNING_OUT)
{
m_quick_play_widget->setDeactivated();
m_find_server_widget->setDeactivated();
m_create_server_widget->setDeactivated();
m_sign_out_widget->setVisible(false);
if(m_recorded_state == CurrentUser::SIGNING_IN || m_recorded_state == CurrentUser::SIGNING_OUT)
if(m_recorded_state == CurrentUser::US_SIGNING_IN || m_recorded_state == CurrentUser::US_SIGNING_OUT)
{
m_register_widget->setDeactivated();
m_sign_in_widget->setDeactivated();
}
}
else if (m_recorded_state == CurrentUser::GUEST)
else if (m_recorded_state == CurrentUser::US_GUEST)
{
m_find_server_widget->setDeactivated();
m_create_server_widget->setDeactivated();
@ -150,47 +153,39 @@ void OnlineScreen::init()
// ----------------------------------------------------------------------------
void OnlineScreen::onUpdate(float delta, irr::video::IVideoDriver* driver)
{
if (hasStateChanged())
GUIEngine::reshowCurrentScreen();
if (m_recorded_state == CurrentUser::SIGNING_IN)
if (m_recorded_state == CurrentUser::US_SIGNING_IN)
{
m_online_status_widget->setText(Messages::signingIn(), false);
}
else if (m_recorded_state == CurrentUser::SIGNING_OUT)
else if (m_recorded_state == CurrentUser::US_SIGNING_OUT)
{
m_online_status_widget->setText(Messages::signingOut(), false);
}
XMLRequest * sign_in_request = HTTPManager::get()->getXMLResponse(Request::RT_SIGN_IN);
if(sign_in_request != NULL)
for(int i = m_requests.size()-1; i>=0; --i)
{
if(sign_in_request->isSuccess())
if(m_requests[i].isDone())
{
new MessageDialog(_("Signed in."));
if(m_requests[i].getType() == CurrentUser::RT_SIGN_IN)
{
if(m_requests[i].isSuccess())
{
new MessageDialog(_("Signed in."));
}
else
{
sfx_manager->quickSound( "anvil" );
new MessageDialog(m_requests[i].getInfo());
}
}else if(m_requests[i].getType() == CurrentUser::RT_SIGN_OUT)
{
new MessageDialog(_("Signed out successfully."));
}
m_requests.erase(i);
}
else
{
sfx_manager->quickSound( "anvil" );
new MessageDialog(sign_in_request->getInfo());
}
delete sign_in_request;
}
XMLRequest * sign_out_request = HTTPManager::get()->getXMLResponse(Request::RT_SIGN_OUT);
if(sign_out_request != NULL)
{
if(sign_out_request->isSuccess())
{
new MessageDialog(_("Signed out successfully."));
}
else
{
sfx_manager->quickSound( "anvil" );
new MessageDialog(sign_out_request->getInfo());
}
delete sign_out_request;
}
if (hasStateChanged())
GUIEngine::reshowCurrentScreen();
} // onUpdate
// ----------------------------------------------------------------------------
@ -272,7 +267,7 @@ void OnlineScreen::onDisabledItemClicked(const std::string& item)
// ----------------------------------------------------------------------------
void OnlineScreen::setInitialFocus()
{
if(m_recorded_state == CurrentUser::SIGNED_IN)
if(m_recorded_state == CurrentUser::US_SIGNED_IN)
m_top_menu_widget->setFocusForPlayer(PLAYER_ID_GAME_MASTER);
else
m_bottom_menu_widget->setFocusForPlayer(PLAYER_ID_GAME_MASTER);

View File

@ -23,6 +23,7 @@
#include "guiengine/widgets/ribbon_widget.hpp"
#include "guiengine/widgets/icon_button_widget.hpp"
#include "online/current_user.hpp"
#include "utils/ptr_vector.hpp"
namespace GUIEngine { class Widget; class ListWidget; }
@ -53,6 +54,8 @@ private:
Online::CurrentUser::UserState m_recorded_state;
PtrVector<Online::XMLRequest> m_requests;
/** \brief Checks if the recorded state differs from the actual state and sets it. */
bool hasStateChanged();
/** \brief Sets which widget has to be focused. Depends on the user state. */