Added Server and ServersManager to the online namespace and edited everything that uses those.
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/uni@13279 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
parent
8ec5adeec0
commit
ae6bc57b95
@ -24,49 +24,51 @@
|
|||||||
#include "utils/constants.hpp"
|
#include "utils/constants.hpp"
|
||||||
#include "utils/string_utils.hpp"
|
#include "utils/string_utils.hpp"
|
||||||
|
|
||||||
Server::SortOrder Server::m_sort_order=Server::SO_NAME; //FIXME change to some other default
|
namespace online{
|
||||||
|
Server::SortOrder Server::m_sort_order=Server::SO_NAME; //FIXME change to some other default
|
||||||
|
|
||||||
Server::Server(const XMLNode & xml)
|
Server::Server(const XMLNode & xml)
|
||||||
{
|
|
||||||
assert(xml.getName() == "server");
|
|
||||||
m_name = "";
|
|
||||||
m_satisfaction_score = 0;
|
|
||||||
m_server_id = 0;
|
|
||||||
m_current_players = 0;
|
|
||||||
m_max_players = 0;
|
|
||||||
|
|
||||||
xml.get("name", &m_lower_case_name);
|
|
||||||
m_name = StringUtils::decodeFromHtmlEntities(m_lower_case_name);
|
|
||||||
m_lower_case_name = StringUtils::toLowerCase(m_lower_case_name);
|
|
||||||
|
|
||||||
xml.get("id", &m_server_id);
|
|
||||||
xml.get("max_players", &m_max_players);
|
|
||||||
xml.get("current_players", &m_current_players);
|
|
||||||
|
|
||||||
}; // Server(const XML&)
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
* \brief Filter the add-on with a list of words.
|
|
||||||
* \param words A list of words separated by ' '.
|
|
||||||
* \return true if the add-on contains one of the words, otherwise false.
|
|
||||||
*/
|
|
||||||
bool Server::filterByWords(const core::stringw words) const
|
|
||||||
{
|
|
||||||
if (words == NULL || words.empty())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
std::vector<core::stringw> list = StringUtils::split(words, ' ', false);
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < list.size(); i++)
|
|
||||||
{
|
{
|
||||||
list[i].make_lower();
|
assert(xml.getName() == "server");
|
||||||
|
m_name = "";
|
||||||
if ((core::stringw(m_name).make_lower()).find(list[i].c_str()) != -1)
|
m_satisfaction_score = 0;
|
||||||
{
|
m_server_id = 0;
|
||||||
|
m_current_players = 0;
|
||||||
|
m_max_players = 0;
|
||||||
|
|
||||||
|
xml.get("name", &m_lower_case_name);
|
||||||
|
m_name = StringUtils::decodeFromHtmlEntities(m_lower_case_name);
|
||||||
|
m_lower_case_name = StringUtils::toLowerCase(m_lower_case_name);
|
||||||
|
|
||||||
|
xml.get("id", &m_server_id);
|
||||||
|
xml.get("max_players", &m_max_players);
|
||||||
|
xml.get("current_players", &m_current_players);
|
||||||
|
|
||||||
|
}; // Server(const XML&)
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* \brief Filter the add-on with a list of words.
|
||||||
|
* \param words A list of words separated by ' '.
|
||||||
|
* \return true if the add-on contains one of the words, otherwise false.
|
||||||
|
*/
|
||||||
|
bool Server::filterByWords(const core::stringw words) const
|
||||||
|
{
|
||||||
|
if (words == NULL || words.empty())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
std::vector<core::stringw> list = StringUtils::split(words, ' ', false);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < list.size(); i++)
|
||||||
|
{
|
||||||
|
list[i].make_lower();
|
||||||
|
|
||||||
|
if ((core::stringw(m_name).make_lower()).find(list[i].c_str()) != -1)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return false;
|
||||||
return false;
|
} // filterByWords
|
||||||
} // filterByWords
|
} // namespace online
|
||||||
|
@ -31,104 +31,105 @@
|
|||||||
|
|
||||||
class XMLNode;
|
class XMLNode;
|
||||||
|
|
||||||
/**
|
namespace online{
|
||||||
* \ingroup online
|
/**
|
||||||
*/
|
* \ingroup online
|
||||||
class Server
|
*/
|
||||||
{
|
class Server
|
||||||
public:
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
/** Set the sort order used in the comparison function. */
|
/** Set the sort order used in the comparison function. */
|
||||||
enum SortOrder { SO_SCORE = 1, // Sorted on satisfaction score
|
enum SortOrder { SO_SCORE = 1, // Sorted on satisfaction score
|
||||||
SO_NAME = 2, // Sorted alphabetically by name
|
SO_NAME = 2, // Sorted alphabetically by name
|
||||||
SO_PLAYERS = 4
|
SO_PLAYERS = 4
|
||||||
};
|
};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/** The name to be displayed. */
|
/** The name to be displayed. */
|
||||||
irr::core::stringw m_name;
|
irr::core::stringw m_name;
|
||||||
std::string m_lower_case_name; //Used for comparison
|
std::string m_lower_case_name; //Used for comparison
|
||||||
|
|
||||||
uint32_t m_server_id;
|
uint32_t m_server_id;
|
||||||
|
|
||||||
int m_max_players;
|
int m_max_players;
|
||||||
|
|
||||||
int m_current_players;
|
int m_current_players;
|
||||||
|
|
||||||
float m_satisfaction_score;
|
float m_satisfaction_score;
|
||||||
|
|
||||||
/** The sort order to be used in the comparison. */
|
/** The sort order to be used in the comparison. */
|
||||||
static SortOrder m_sort_order;
|
static SortOrder m_sort_order;
|
||||||
|
|
||||||
Server() {};
|
Server() {};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/** Initialises the object from an XML node. */
|
|
||||||
Server(const XMLNode & xml);
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
/** Sets the sort order used in the comparison function. It is static, so
|
|
||||||
* that each instance can access the sort order. */
|
|
||||||
static void setSortOrder(SortOrder so) { m_sort_order = so; }
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
/** Returns the name of the server. */
|
|
||||||
const irr::core::stringw& getName() const { return m_name; }
|
|
||||||
const std::string & getLowerCaseName() const { return m_lower_case_name; }
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
const float getScore() const { return m_satisfaction_score; }
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
/** Returns the ID of this server. */
|
|
||||||
const uint32_t getServerId() const { return m_server_id; }
|
|
||||||
const int getMaxPlayers() const { return m_max_players; }
|
|
||||||
const int getCurrentPlayers() const { return m_current_players; }
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
bool filterByWords(const irr::core::stringw words) const;
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/** Compares two servers according to the sort order currently defined.
|
/** Initialises the object from an XML node. */
|
||||||
* \param a The addon to compare this addon to.
|
Server(const XMLNode & xml);
|
||||||
*/
|
// ------------------------------------------------------------------------
|
||||||
bool operator<(const Server &server) const
|
/** Sets the sort order used in the comparison function. It is static, so
|
||||||
{
|
* that each instance can access the sort order. */
|
||||||
switch(m_sort_order)
|
static void setSortOrder(SortOrder so) { m_sort_order = so; }
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
/** Returns the name of the server. */
|
||||||
|
const irr::core::stringw& getName() const { return m_name; }
|
||||||
|
const std::string & getLowerCaseName() const { return m_lower_case_name; }
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
const float getScore() const { return m_satisfaction_score; }
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
/** Returns the ID of this server. */
|
||||||
|
const uint32_t getServerId() const { return m_server_id; }
|
||||||
|
const int getMaxPlayers() const { return m_max_players; }
|
||||||
|
const int getCurrentPlayers() const { return m_current_players; }
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
bool filterByWords(const irr::core::stringw words) const;
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Compares two servers according to the sort order currently defined.
|
||||||
|
* \param a The addon to compare this addon to.
|
||||||
|
*/
|
||||||
|
bool operator<(const Server &server) const
|
||||||
{
|
{
|
||||||
case SO_SCORE:
|
switch(m_sort_order)
|
||||||
return m_satisfaction_score < server.getScore();
|
{
|
||||||
break;
|
case SO_SCORE:
|
||||||
case SO_NAME:
|
return m_satisfaction_score < server.getScore();
|
||||||
// m_id is the lower case name
|
break;
|
||||||
return m_name < server.getName();
|
case SO_NAME:
|
||||||
break;
|
// m_id is the lower case name
|
||||||
case SO_PLAYERS:
|
return m_name < server.getName();
|
||||||
return m_current_players < server.getCurrentPlayers();
|
break;
|
||||||
break;
|
case SO_PLAYERS:
|
||||||
} // switch
|
return m_current_players < server.getCurrentPlayers();
|
||||||
return true;
|
break;
|
||||||
} // operator<
|
} // switch
|
||||||
|
return true;
|
||||||
|
} // operator<
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
/** Compares two addons according to the sort order currently defined.
|
/** Compares two addons according to the sort order currently defined.
|
||||||
* Comparison is done for sorting in descending order.
|
* Comparison is done for sorting in descending order.
|
||||||
* \param a The addon to compare this addon to.
|
* \param a The addon to compare this addon to.
|
||||||
*/
|
*/
|
||||||
bool operator>(const Server &server) const
|
bool operator>(const Server &server) const
|
||||||
{
|
|
||||||
switch(m_sort_order)
|
|
||||||
{
|
{
|
||||||
case SO_SCORE:
|
switch(m_sort_order)
|
||||||
return m_satisfaction_score > server.getScore();
|
{
|
||||||
break;
|
case SO_SCORE:
|
||||||
case SO_NAME:
|
return m_satisfaction_score > server.getScore();
|
||||||
return m_lower_case_name > server.getLowerCaseName();
|
break;
|
||||||
break;
|
case SO_NAME:
|
||||||
case SO_PLAYERS:
|
return m_lower_case_name > server.getLowerCaseName();
|
||||||
return m_current_players > server.getCurrentPlayers();
|
break;
|
||||||
break;
|
case SO_PLAYERS:
|
||||||
} // switch
|
return m_current_players > server.getCurrentPlayers();
|
||||||
return true;
|
break;
|
||||||
} // operator>
|
} // switch
|
||||||
|
return true;
|
||||||
}; // Server
|
} // operator>
|
||||||
|
|
||||||
|
}; // Server
|
||||||
|
} // namespace online
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -26,54 +26,57 @@
|
|||||||
#include "config/user_config.hpp"
|
#include "config/user_config.hpp"
|
||||||
#include "utils/translation.hpp"
|
#include "utils/translation.hpp"
|
||||||
|
|
||||||
static ServersManager* user_singleton = NULL;
|
namespace online{
|
||||||
|
|
||||||
ServersManager* ServersManager::get()
|
static ServersManager* user_singleton = NULL;
|
||||||
{
|
|
||||||
if (user_singleton == NULL)
|
|
||||||
user_singleton = new ServersManager();
|
|
||||||
return user_singleton;
|
|
||||||
} // get
|
|
||||||
|
|
||||||
void ServersManager::deallocate()
|
ServersManager* ServersManager::get()
|
||||||
{
|
|
||||||
delete user_singleton;
|
|
||||||
user_singleton = NULL;
|
|
||||||
} // deallocate
|
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
ServersManager::ServersManager(){
|
|
||||||
m_servers = new PtrVector<Server>;
|
|
||||||
refresh();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
void ServersManager::refresh()
|
|
||||||
{
|
|
||||||
HTTPConnector * connector = new HTTPConnector((std::string)UserConfigParams::m_server_multiplayer + "client-user.php");
|
|
||||||
connector->setParameter("action",std::string("get_server_list"));
|
|
||||||
|
|
||||||
const XMLNode * result = connector->getXMLFromPage();
|
|
||||||
std::string rec_success = "";
|
|
||||||
if(result->get("success", &rec_success))
|
|
||||||
{
|
{
|
||||||
if (rec_success =="yes")
|
if (user_singleton == NULL)
|
||||||
|
user_singleton = new ServersManager();
|
||||||
|
return user_singleton;
|
||||||
|
} // get
|
||||||
|
|
||||||
|
void ServersManager::deallocate()
|
||||||
|
{
|
||||||
|
delete user_singleton;
|
||||||
|
user_singleton = NULL;
|
||||||
|
} // deallocate
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
ServersManager::ServersManager(){
|
||||||
|
m_servers = new PtrVector<Server>;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
void ServersManager::refresh()
|
||||||
|
{
|
||||||
|
HTTPConnector * connector = new HTTPConnector((std::string)UserConfigParams::m_server_multiplayer + "client-user.php");
|
||||||
|
connector->setParameter("action",std::string("get_server_list"));
|
||||||
|
|
||||||
|
const XMLNode * result = connector->getXMLFromPage();
|
||||||
|
std::string rec_success = "";
|
||||||
|
if(result->get("success", &rec_success))
|
||||||
{
|
{
|
||||||
const XMLNode * servers_xml = result->getNode("servers");
|
if (rec_success =="yes")
|
||||||
m_servers->clearAndDeleteAll();
|
|
||||||
for (unsigned int i = 0; i < servers_xml->getNumNodes(); i++)
|
|
||||||
{
|
{
|
||||||
m_servers->push_back(new Server(*servers_xml->getNode(i)));
|
const XMLNode * servers_xml = result->getNode("servers");
|
||||||
|
m_servers->clearAndDeleteAll();
|
||||||
|
for (unsigned int i = 0; i < servers_xml->getNumNodes(); i++)
|
||||||
|
{
|
||||||
|
m_servers->push_back(new Server(*servers_xml->getNode(i)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//FIXME error message
|
||||||
}
|
}
|
||||||
//FIXME error message
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
Server * ServersManager::getQuickPlay()
|
Server * ServersManager::getQuickPlay()
|
||||||
{
|
{
|
||||||
if(m_servers->size() > 0)
|
if(m_servers->size() > 0)
|
||||||
return m_servers->get(0);
|
return m_servers->get(0);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
} // namespace online
|
||||||
|
@ -23,35 +23,39 @@
|
|||||||
#include "online/server.hpp"
|
#include "online/server.hpp"
|
||||||
|
|
||||||
|
|
||||||
// ============================================================================
|
namespace online {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief
|
* \brief
|
||||||
* \ingroup online
|
* \ingroup online
|
||||||
*/
|
*/
|
||||||
class ServersManager
|
class ServersManager
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
ServersManager();
|
ServersManager();
|
||||||
PtrVector<Server> * m_servers;
|
PtrVector<Server> * m_servers;
|
||||||
bool m_not_fetched;
|
bool m_not_fetched;
|
||||||
Server * m_joined_server;
|
Server * m_joined_server;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// singleton
|
// singleton
|
||||||
static ServersManager* get();
|
static ServersManager* get();
|
||||||
static void deallocate();
|
static void deallocate();
|
||||||
|
|
||||||
|
void refresh();
|
||||||
|
PtrVector<Server> * getServers () const { return m_servers; };
|
||||||
|
int getNumServers () const { return m_servers->size(); };
|
||||||
|
Server * getServer (int index) const { return m_servers->get(index);};
|
||||||
|
void sort(bool sort_desc) { m_servers->insertionSort(0, sort_desc); };
|
||||||
|
void setJoinedServer(Server * server){ m_joined_server = server;};
|
||||||
|
Server * getJoinedServer(){ return m_joined_server;};
|
||||||
|
//Returns the best server to join
|
||||||
|
Server * getQuickPlay();
|
||||||
|
}; // class ServersManager
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace online
|
||||||
|
|
||||||
void refresh();
|
|
||||||
PtrVector<Server> * getServers () const { return m_servers; };
|
|
||||||
int getNumServers () const { return m_servers->size(); };
|
|
||||||
Server * getServer (int index) const { return m_servers->get(index);};
|
|
||||||
void sort(bool sort_desc) { m_servers->insertionSort(0, sort_desc); };
|
|
||||||
void setJoinedServer(Server * server){ m_joined_server = server;};
|
|
||||||
Server * getJoinedServer(){ return m_joined_server;};
|
|
||||||
//Returns the best server to join
|
|
||||||
Server * getQuickPlay();
|
|
||||||
}; // class ServersManager
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
using namespace GUIEngine;
|
using namespace GUIEngine;
|
||||||
using namespace irr;
|
using namespace irr;
|
||||||
using namespace irr::gui;
|
using namespace irr::gui;
|
||||||
|
using namespace online;
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -26,6 +26,8 @@
|
|||||||
#include "guiengine/widgets/ribbon_widget.hpp"
|
#include "guiengine/widgets/ribbon_widget.hpp"
|
||||||
#include "guiengine/widgets/label_widget.hpp"
|
#include "guiengine/widgets/label_widget.hpp"
|
||||||
#include "online/server.hpp"
|
#include "online/server.hpp"
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Dialog that allows a user to sign in
|
* \brief Dialog that allows a user to sign in
|
||||||
* \ingroup states_screens
|
* \ingroup states_screens
|
||||||
@ -38,7 +40,7 @@ private:
|
|||||||
bool m_self_destroy;
|
bool m_self_destroy;
|
||||||
bool m_enter_lobby;
|
bool m_enter_lobby;
|
||||||
|
|
||||||
Server * m_server;
|
online::Server * m_server;
|
||||||
|
|
||||||
GUIEngine::LabelWidget * m_name_widget;
|
GUIEngine::LabelWidget * m_name_widget;
|
||||||
GUIEngine::LabelWidget * m_info_widget;
|
GUIEngine::LabelWidget * m_info_widget;
|
||||||
@ -50,7 +52,7 @@ private:
|
|||||||
void requestJoin();
|
void requestJoin();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ServerInfoDialog(Server * server);
|
ServerInfoDialog(online::Server * server);
|
||||||
~ServerInfoDialog();
|
~ServerInfoDialog();
|
||||||
|
|
||||||
void onEnterPressedInternal();
|
void onEnterPressedInternal();
|
||||||
|
@ -37,7 +37,7 @@
|
|||||||
#include "utils/translation.hpp"
|
#include "utils/translation.hpp"
|
||||||
#include "online/servers_manager.hpp"
|
#include "online/servers_manager.hpp"
|
||||||
|
|
||||||
|
using namespace online;
|
||||||
using namespace GUIEngine;
|
using namespace GUIEngine;
|
||||||
|
|
||||||
DEFINE_SCREEN_SINGLETON( NetworkingLobby );
|
DEFINE_SCREEN_SINGLETON( NetworkingLobby );
|
||||||
|
@ -36,7 +36,7 @@ class NetworkingLobby : public GUIEngine::Screen,
|
|||||||
private:
|
private:
|
||||||
friend class GUIEngine::ScreenSingleton<NetworkingLobby>;
|
friend class GUIEngine::ScreenSingleton<NetworkingLobby>;
|
||||||
|
|
||||||
Server * m_server;
|
online::Server * m_server;
|
||||||
|
|
||||||
NetworkingLobby();
|
NetworkingLobby();
|
||||||
|
|
||||||
|
@ -44,6 +44,7 @@
|
|||||||
|
|
||||||
|
|
||||||
using namespace GUIEngine;
|
using namespace GUIEngine;
|
||||||
|
using namespace online;
|
||||||
|
|
||||||
DEFINE_SCREEN_SINGLETON( OnlineScreen );
|
DEFINE_SCREEN_SINGLETON( OnlineScreen );
|
||||||
|
|
||||||
|
@ -29,6 +29,8 @@
|
|||||||
#include "utils/string_utils.hpp"
|
#include "utils/string_utils.hpp"
|
||||||
#include "online/servers_manager.hpp"
|
#include "online/servers_manager.hpp"
|
||||||
|
|
||||||
|
using namespace online;
|
||||||
|
|
||||||
DEFINE_SCREEN_SINGLETON( ServerSelection );
|
DEFINE_SCREEN_SINGLETON( ServerSelection );
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
Loading…
x
Reference in New Issue
Block a user