Show WAN server owner if it's localhost or friends'

This commit is contained in:
Benau 2018-03-23 13:41:07 +08:00
parent 61b6389764
commit dac7158ce0
5 changed files with 63 additions and 12 deletions

View File

@ -16,6 +16,10 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "network/server.hpp"
#include "config/player_manager.hpp"
#include "online/online_player_profile.hpp"
#include "online/online_profile.hpp"
#include "online/profile_manager.hpp"
#include "network/network_config.hpp"
#include "io/xml_node.hpp"
#include "utils/constants.hpp"
@ -57,6 +61,34 @@ Server::Server(const XMLNode& xml)
m_address.setPort(port);
xml.get("private_port", &m_private_port);
xml.get("password", &m_password_protected);
m_server_owner_name = "-";
// Display server owner name if he's your friend or localhost
Online::OnlineProfile* opp = PlayerManager::getCurrentPlayer()->getProfile();
// Check localhost owner
if (opp && opp->getID() == m_server_owner)
{
m_server_owner_name =
StringUtils::wideToUtf8(opp->getUserName());
}
else if (opp && opp->hasFetchedFriends())
{
// Check friend(s)
for (uint32_t user_id : opp->getFriends())
{
if (user_id == m_server_owner)
{
Online::OnlineProfile* friend_profile =
Online::ProfileManager::get()->getProfileByID(user_id);
if (friend_profile)
{
m_server_owner_name =
StringUtils::wideToUtf8(friend_profile->getUserName());
}
}
}
}
} // Server(const XML&)
// ----------------------------------------------------------------------------

View File

@ -76,6 +76,10 @@ protected:
bool m_password_protected;
/* WAN server only, show the owner name of server, can only be seen
* for localhost or if you are friend with the server owner. */
std::string m_server_owner_name;
public:
/** Initialises the object from an XML node. */
@ -118,6 +122,9 @@ public:
RaceManager::Difficulty getDifficulty() const { return m_difficulty; }
// ------------------------------------------------------------------------
bool isPasswordProtected() const { return m_password_protected; }
// ------------------------------------------------------------------------
const std::string& getServerOwnerName() const
{ return m_server_owner_name; }
}; // Server
#endif // HEADER_SERVER_HPP

View File

@ -65,7 +65,7 @@ OnlineProfile::OnlineProfile(const uint32_t & userid,
m_id = userid;
m_is_current_user = is_current_user;
m_username = username;
m_has_fetched_friends = false;
m_has_fetched_friends.store(false);
m_has_fetched_achievements = false;
m_relation_info = NULL;
m_is_friend = false;
@ -85,7 +85,7 @@ OnlineProfile::OnlineProfile(const XMLNode * xml, ConstructorType type)
m_relation_info = NULL;
m_is_friend = false;
m_cache_bit = true;
m_has_fetched_friends = false;
m_has_fetched_friends.store(false);
m_has_fetched_achievements = false;
if (type == C_RELATION_INFO)
{
@ -184,7 +184,7 @@ void OnlineProfile::storeAchievements(const XMLNode * input)
void OnlineProfile::fetchFriends()
{
assert(PlayerManager::isCurrentLoggedIn());
if (m_has_fetched_friends)
if (m_has_fetched_friends.load())
return;
m_state = State(m_state | S_FETCHING_FRIENDS);
@ -238,8 +238,8 @@ void OnlineProfile::storeFriends(const XMLNode * input)
ProfileManager::get()->addToCache(profile);
}
} // for i in nodes
m_has_fetched_friends = true;
m_state = State(m_state & ~S_FETCHING_FRIENDS);
m_has_fetched_friends.store(true);
} // storeFriends
// ----------------------------------------------------------------------------
@ -248,7 +248,7 @@ void OnlineProfile::storeFriends(const XMLNode * input)
*/
void OnlineProfile::removeFriend(const uint32_t id)
{
assert(m_has_fetched_friends);
assert(m_has_fetched_friends.load());
IDList::iterator iter;
for (iter = m_friends.begin(); iter != m_friends.end();)
{
@ -270,7 +270,7 @@ void OnlineProfile::removeFriend(const uint32_t id)
*/
void OnlineProfile::addFriend(const uint32_t id)
{
assert(m_has_fetched_friends);
assert(m_has_fetched_friends.load());
// find if friend id is is already in the user list
for (unsigned int i = 0; i < m_friends.size(); i++)
@ -296,7 +296,7 @@ void OnlineProfile::deleteRelationalInfo()
*/
const OnlineProfile::IDList& OnlineProfile::getFriends()
{
assert(m_has_fetched_friends &&
assert(m_has_fetched_friends.load() &&
(m_state & S_FETCHING_FRIENDS) == 0);
return m_friends;
} // getFriends
@ -322,7 +322,8 @@ void OnlineProfile::merge(OnlineProfile *profile)
assert(profile != NULL);
// profile has fetched friends, use that instead
if (!m_has_fetched_friends && profile->m_has_fetched_friends)
if (!m_has_fetched_friends.load() &&
profile->m_has_fetched_friends.load())
m_friends = profile->m_friends;
// profile has fetched achievements, use that instead

View File

@ -24,6 +24,7 @@
#include "utils/types.hpp"
#include "utils/ptr_vector.hpp"
#include <atomic>
#include <irrString.h>
#include <string>
@ -91,7 +92,7 @@ private:
/** Whether or not the user of this profile, is a friend of the current user */
bool m_is_friend;
bool m_has_fetched_friends;
std::atomic_bool m_has_fetched_friends;
/** List of user id's that are friends with the user of this profile.
* In case this profile is of the current user, this list also contains
@ -123,15 +124,15 @@ public:
// ------------------------------------------------------------------------
/** Returns true if the achievements for this profile have been fetched. */
bool hasFetchedAchievements() const { return m_has_fetched_achievements; }
bool hasFetchedAchievements() const { return m_has_fetched_achievements; }
// ------------------------------------------------------------------------
/** Unsets the flag that all friends of this profile are in cache. Used
* when a profile is pushed out of cache. */
void unsetHasFetchedFriends() { m_has_fetched_friends = false; }
void unsetHasFetchedFriends() { m_has_fetched_friends.store(false); }
// ------------------------------------------------------------------------
/** Returns true if the friend list for this profile has been fetched. */
bool hasFetchedFriends() const { return m_has_fetched_friends; }
bool hasFetchedFriends() const { return m_has_fetched_friends.load(); }
// ------------------------------------------------------------------------
/** True if the profile has fetched friends. */

View File

@ -104,6 +104,8 @@ void ServerSelection::beforeAddingWidget()
m_server_list_widget->addColumn( _("Players"), 1);
m_server_list_widget->addColumn(_("Difficulty"), 1);
m_server_list_widget->addColumn(_("Game mode"), 1);
if (NetworkConfig::get()->isWAN())
m_server_list_widget->addColumn(_("Server owner"), 1);
} // beforeAddingWidget
// ----------------------------------------------------------------------------
@ -145,6 +147,9 @@ void ServerSelection::loadList(unsigned sort_case)
case 3:
return c->getRaceMinorMode() > d->getRaceMinorMode();
break;
case 4:
return c->getServerOwnerName() > d->getServerOwnerName();
break;
} // switch
assert(false);
return false;
@ -166,6 +171,11 @@ void ServerSelection::loadList(unsigned sort_case)
core::stringw mode = RaceManager::getNameOf(server->getRaceMinorMode());
row.push_back(GUIEngine::ListWidget::ListCell(mode, -1, 1, true));
if (NetworkConfig::get()->isWAN())
{
row.push_back(GUIEngine::ListWidget::ListCell(StringUtils::
utf8ToWide(server->getServerOwnerName()), -1, 1, true));
}
m_server_list_widget->addItem("server", row);
}
} // loadList