diff --git a/src/online/server.cpp b/src/online/server.cpp index a72da154f..d8dd16f50 100644 --- a/src/online/server.cpp +++ b/src/online/server.cpp @@ -26,65 +26,76 @@ namespace Online { - Server::SortOrder Server::m_sort_order = Server::SO_NAME; +Server::SortOrder Server::m_sort_order = Server::SO_NAME; - Server::Server(const XMLNode & xml, bool is_lan) +/** Constructor based on XML data received from the stk server. + * \param xml The data for one server as received as part of the + * get-all stk-server request. + * \param is_lan If this is a lan only server. + */ +Server::Server(const XMLNode & xml, bool is_lan) +{ + assert(xml.getName() == "server"); + + m_name = ""; + m_satisfaction_score = 0; + m_server_id = 0; + m_current_players = 0; + m_max_players = 0; + m_is_lan = is_lan; + + xml.get("name", &m_lower_case_name); + m_name = StringUtils::xmlDecode(m_lower_case_name); + m_lower_case_name = StringUtils::toLowerCase(m_lower_case_name); + + xml.get("id", &m_server_id); + xml.get("hostid", &m_host_id); + xml.get("max_players", &m_max_players); + xml.get("current_players", &m_current_players); + +} // Server(const XML&) + +// ---------------------------------------------------------------------------- +/** Manual server creation, based on data received from a LAN server discovery + * (see ServersManager::getLANRefresh). + * \param name Name of the server. + * \param is_lan If this is a lan-only server. + * \param max_players Maximum number of players allowed on this server. + * \param current_players The currently connected number of players. + */ +Server::Server(const core::stringw &name, bool is_lan, int max_players, + int current_players) +{ + m_name = name; + m_satisfaction_score = 0; + m_server_id = 0; + m_current_players = current_players; + m_max_players = max_players; + m_is_lan = is_lan; +} // server(name, ...) + +// ---------------------------------------------------------------------------- +/** \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 list = StringUtils::split(words, ' ', false); + + for (unsigned int i = 0; i < list.size(); i++) { - assert(xml.getName() == "server"); + list[i].make_lower(); - m_name = ""; - m_satisfaction_score = 0; - m_server_id = 0; - m_current_players = 0; - m_max_players = 0; - m_is_lan = is_lan; - - xml.get("name", &m_lower_case_name); - m_name = StringUtils::xmlDecode(m_lower_case_name); - m_lower_case_name = StringUtils::toLowerCase(m_lower_case_name); - - xml.get("id", &m_server_id); - xml.get("hostid", &m_host_id); - xml.get("max_players", &m_max_players); - xml.get("current_players", &m_current_players); - - } // Server(const XML&) - - // ---------------------------------------------------------------------------- - Server::Server(const core::stringw &name, bool is_lan, int max_players, - int current_players) - { - m_name = name; - m_satisfaction_score = 0; - m_server_id = 0; - m_current_players = current_players; - m_max_players = max_players; - m_is_lan = is_lan; - } // server(name, ...) - - // ---------------------------------------------------------------------------- - /** - * \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 list = StringUtils::split(words, ' ', false); - - for (unsigned int i = 0; i < list.size(); i++) + if ((core::stringw(m_name).make_lower()).find(list[i].c_str()) != -1) { - list[i].make_lower(); - - if ((core::stringw(m_name).make_lower()).find(list[i].c_str()) != -1) - { - return true; - } + return true; } + } - return false; - } // filterByWords + return false; +} // filterByWords } // namespace Online diff --git a/src/online/server.hpp b/src/online/server.hpp index b1210e8d0..448f0cf35 100644 --- a/src/online/server.hpp +++ b/src/online/server.hpp @@ -33,98 +33,96 @@ class XMLNode; namespace Online { - /** - * \ingroup online - */ - class Server +/** + * \ingroup online + */ +class Server +{ +public: + + /** Set the sort order used in the comparison function. */ + enum SortOrder { - public: + SO_SCORE = 1, // Sorted on satisfaction score + SO_NAME = 2, // Sorted alphabetically by name + SO_PLAYERS = 4 + }; - /** Set the sort order used in the comparison function. */ - enum SortOrder - { - SO_SCORE = 1, // Sorted on satisfaction score - SO_NAME = 2, // Sorted alphabetically by name - SO_PLAYERS = 4 - }; +protected: + /** The server name to be displayed. */ + irr::core::stringw m_name; - protected: - /** The server name to be displayed. */ - irr::core::stringw m_name; - std::string m_lower_case_name; // Used for comparison + /** Name in lower case for comparisons. */ + std::string m_lower_case_name; - uint32_t m_server_id; - uint32_t m_host_id; + uint32_t m_server_id; + uint32_t m_host_id; - /** The maximum number of players that the server supports */ - int m_max_players; + /** The maximum number of players that the server supports */ + int m_max_players; - /** The number of players currently on the server */ - int m_current_players; + /** The number of players currently on the server */ + int m_current_players; - /** The score/rating given */ - float m_satisfaction_score; + /** The score/rating given */ + float m_satisfaction_score; - /** True if this server is on the LAN, false otherwise. */ - bool m_is_lan; + /** True if this server is on the LAN, false otherwise. */ + bool m_is_lan; - /** The sort order to be used in the comparison. */ - static SortOrder m_sort_order; + /** The sort order to be used in the comparison. */ + static SortOrder m_sort_order; - Server() {} - - public: +public: /** Initialises the object from an XML node. */ Server(const XMLNode &xml, bool is_lan); Server(const core::stringw &name, bool is_lan, int max_players, int current_players); - // ------------------------------------------------------------------------ - /** 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; } + bool filterByWords(const irr::core::stringw words) const; + // ------------------------------------------------------------------------ + /** 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 uint32_t getHostId() const { return m_host_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 + // ------------------------------------------------------------------------ + /** Returns the name of the server. */ + const irr::core::stringw& getName() const { return m_name; } + // ------------------------------------------------------------------------ + /** Returns the ID of this server. */ + const uint32_t getServerId() const { return m_server_id; } + // ------------------------------------------------------------------------ + /** Returns the unique host id of this server. */ + const uint32_t getHostId() const { return m_host_id; } + // ------------------------------------------------------------------------ + /** Returns the maximum number of players allowed on this server. */ + const int getMaxPlayers() const { return m_max_players; } + // ------------------------------------------------------------------------ + /** Returns the number of currently connected players. */ + const int getCurrentPlayers() const { return m_current_players; } + // ------------------------------------------------------------------------ + /** 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 + { + switch (m_sort_order) { - switch(m_sort_order) - { - case SO_SCORE: - return m_satisfaction_score < server.getScore(); - break; - case SO_NAME: - // m_id is the lower case name - return m_lower_case_name < server.m_lower_case_name; - break; - case SO_PLAYERS: - return m_current_players < server.getCurrentPlayers(); - break; - } // switch + case SO_SCORE: + return m_satisfaction_score < server.m_satisfaction_score; + break; + case SO_NAME: + // m_id is the lower case name + return m_lower_case_name < server.m_lower_case_name; + break; + case SO_PLAYERS: + return m_current_players < server.m_current_players; + break; + } // switch - return true; - } // operator< + return true; + } // operator< - }; // Server +}; // Server } // namespace Online #endif // HEADER_SERVER_HPP