Some additions to some stuff. Mostly the profile manager, where I've added a separate queue for persistent profiles. (I.e. the current user and his friends.) We should definitely put a max on the number of friends. (Pretty low, and remove the limit for premium ;) )

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/uni@13515 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
unitraxx 2013-08-19 19:31:50 +00:00
parent f43c5c915f
commit d40dd67e96
9 changed files with 102 additions and 39 deletions

View File

@ -46,7 +46,7 @@ namespace GUIEngine
/**Singleton */
static DialogQueue * get();
static void deallocate();
void pushDialog(ModalDialog * dialog, bool closes_any_dialog);
void pushDialog(ModalDialog * dialog, bool closes_any_dialog = false);
void update();
};

View File

@ -51,6 +51,8 @@ ModalDialog::ModalDialog(const float percentWidth, const float percentHeight, bo
{
m_dialog_location = location;
m_init = false;
m_percent_width = percentWidth;
m_percent_height = percentHeight;
if(do_init)
doInit();
}

View File

@ -138,7 +138,7 @@ namespace Online{
int username_fetched = input->get("username", &username);
uint32_t userid(0);
int userid_fetched = input->get("userid", &userid);
m_profile = new Profile(userid, username, false, true);
m_profile = new Profile(userid, username, true);
assert(token_fetched && username_fetched && userid_fetched);
m_state = US_SIGNED_IN;
if(getSaveSession())
@ -147,7 +147,7 @@ namespace Online{
UserConfigParams::m_saved_token = getToken();
UserConfigParams::m_saved_session = true;
}
ProfileManager::get()->addToCache(m_profile);
ProfileManager::get()->addPersistent(m_profile);
m_profile->fetchFriends();
HTTPManager::get()->startPolling();
}

View File

@ -48,23 +48,22 @@ namespace Online{
// ============================================================================
Profile::Profile( const uint32_t & userid,
const irr::core::stringw & username,
bool auto_delete,
bool is_current_user)
{
setState (S_READY);
m_auto_delete = auto_delete;
m_state = S_READY;
m_cache_bit = true;
m_id = userid;
m_is_current_user = is_current_user;
m_username = username;
m_has_fetched_friends = false;
m_relation_info = NULL;
m_is_friend = false;
}
Profile::Profile(const XMLNode * xml, ConstructorType type, bool auto_delete)
Profile::Profile(const XMLNode * xml, ConstructorType type)
{
m_relation_info = NULL;
m_auto_delete = auto_delete;
m_is_friend = false;
if(type == C_RELATION_INFO){
std::string is_online_string("");
xml->get("online", &is_online_string);
@ -81,6 +80,10 @@ namespace Online{
xml->get("is_asker", &is_asker_string);
is_asker = is_asker_string == "yes";
}
else
{
m_is_friend = true;
}
m_relation_info = new RelationInfo(date, is_online, is_pending, is_asker);
xml = xml->getNode("user");
}
@ -90,7 +93,7 @@ namespace Online{
m_cache_bit = true;
m_has_fetched_friends = false;
m_is_current_user = (m_id == CurrentUser::get()->getID());
setState (S_READY);
m_state = S_READY;
}
// ============================================================================
Profile::~Profile()
@ -104,7 +107,7 @@ namespace Online{
assert(CurrentUser::get()->isRegisteredUser());
if(m_has_fetched_friends)
return;
setState (S_FETCHING);
m_state = S_FETCHING;
requestFriendsList();
}
// ============================================================================
@ -118,14 +121,19 @@ namespace Online{
{
Profile * profile;
if(m_is_current_user)
{
profile = new Profile(friends_xml->getNode(i) , C_RELATION_INFO);
ProfileManager::get()->addPersistent(profile);
}
else
{
profile = new Profile(friends_xml->getNode(i)->getNode("user"), C_DEFAULT);
ProfileManager::get()->addToCache(profile);
}
m_friends.push_back(profile->getID());
ProfileManager::get()->addToCache(profile);
}
m_has_fetched_friends = true;
Profile::setState (Profile::S_READY);
m_state = S_READY;
}
@ -154,7 +162,7 @@ namespace Online{
const std::vector<uint32_t> & Profile::getFriends()
{
assert (m_has_fetched_friends && getState() == S_READY);
assert (m_has_fetched_friends && m_state == S_READY);
return m_friends;
}
// ============================================================================

View File

@ -76,20 +76,17 @@ namespace Online{
S_READY
};
Synchronised<State> m_state;
State m_state;
bool m_is_current_user;
uint32_t m_id;
irr::core::stringw m_username;
RelationInfo * m_relation_info;
bool m_is_friend;
bool m_has_fetched_friends;
std::vector<uint32_t> m_friends;
bool m_cache_bit;
bool m_auto_delete;
void setState(State state) { m_state.setAtomic(state); }
const State getState() const { return m_state.getAtomic(); }
void requestFriendsList();
void friendsListCallback(const XMLNode * input);
@ -97,25 +94,23 @@ namespace Online{
public:
Profile( const uint32_t & userid,
const irr::core::stringw & username,
bool auto_delete = true,
bool is_current_user = false);
Profile( const XMLNode * xml,
ConstructorType type = C_DEFAULT,
bool auto_delete = true);
ConstructorType type = C_DEFAULT);
~Profile();
void fetchFriends();
const std::vector<uint32_t> & getFriends();
bool isFetching() const { return getState() == S_FETCHING; }
bool isReady() const { return getState() == S_READY; }
bool isFetching() const { return m_state == S_FETCHING; }
bool isReady() const { return m_state == S_READY; }
bool isCurrentUser() const { return m_is_current_user; }
bool isFriend() const { return m_is_friend; }
RelationInfo * getRelationInfo() { return m_relation_info; }
void setCacheBit() { m_cache_bit = true; }
void unsetCacheBit() { m_cache_bit = false; }
bool getCacheBit() const { return m_cache_bit; }
bool canAutoDelete() const { return m_auto_delete; }
uint32_t getID() const { return m_id; }
const irr::core::stringw & getUserName() const { return m_username; }

View File

@ -51,6 +51,18 @@ namespace Online{
assert(m_max_cache_size > 1);
}
// ============================================================================
ProfileManager::~ProfileManager()
{
ProfilesMap::iterator it;
for ( it = m_profiles_persistent.begin(); it != m_profiles_persistent.end(); ++it ) {
delete it->second;
}
for ( it = m_profiles_cache.begin(); it != m_profiles_persistent.end(); ++it ) {
delete it->second;
}
}
// ============================================================================
void ProfileManager::iterateCache(Profile * profile)
@ -84,10 +96,13 @@ namespace Online{
ProfilesMap::iterator iter;
for (iter = m_profiles_cache.begin(); iter != m_profiles_cache.end();)
{
if (!iter->second->getCacheBit() && iter->second->canAutoDelete())
if (!iter->second->getCacheBit())
{
m_profiles_cache.erase(iter++);
continue;
ProfilesMap::iterator toErase = iter;
++iter;
delete toErase->second;
m_profiles_cache.erase(toErase);
break;
}
else
++iter;
@ -100,9 +115,30 @@ namespace Online{
// ============================================================================
void ProfileManager::addPersistent(Profile * profile)
{
assert (!inPersistent(profile->getID()));
m_profiles_persistent[profile->getID()] = profile;
}
// ============================================================================
void ProfileManager::removePersistent(const uint32_t id)
{
assert (inPersistent(id));
delete m_profiles_persistent[id];
m_profiles_persistent.erase(id);
}
// ============================================================================
void ProfileManager::addToCache(Profile * profile)
{
if(cacheHit(profile->getID()))
if(inPersistent(profile->getID()))
{
//FIXME should do updating of values
}
else if(cacheHit(profile->getID()))
{
//FIXME should do updating of values
delete profile;
@ -115,6 +151,16 @@ namespace Online{
// ============================================================================
bool ProfileManager::inPersistent(const uint32_t id)
{
if (m_profiles_persistent.find(id) != m_profiles_persistent.end())
{
return true;
}
return false;
}
// ============================================================================
bool ProfileManager::cacheHit(const uint32_t id)
{
@ -129,18 +175,15 @@ namespace Online{
// ============================================================================
void ProfileManager::setVisiting(const uint32_t id)
{
if(cacheHit(id))
{
m_currently_visiting = m_profiles_cache[id];
}
else
m_currently_visiting = NULL;
m_currently_visiting = getProfileByID(id);
}
// ============================================================================
Profile * ProfileManager::getProfileByID(const uint32_t id)
{
if(inPersistent(id))
return m_profiles_persistent[id];
if(cacheHit(id))
return m_profiles_cache[id];
return NULL;

View File

@ -40,10 +40,12 @@ namespace Online{
private:
ProfileManager ();
~ProfileManager ();
typedef std::map<uint32_t, Profile*> ProfilesMap;
ProfilesMap m_profiles_cache;
ProfilesMap m_profiles_persistent; // current user and friends
Profile * m_currently_visiting;
static const unsigned int m_max_cache_size = 20;
@ -56,8 +58,11 @@ namespace Online{
static void deallocate();
void addToCache(Profile * profile);
void addPersistent(Profile * profile);
void removePersistent(const uint32_t id);
void setVisiting(const uint32_t id);
bool cacheHit(const uint32_t id);
bool inPersistent(const uint32_t id);
Profile * getVisitingProfile() {return m_currently_visiting;}
Profile * getProfileByID(const uint32_t id);

View File

@ -41,14 +41,14 @@ using namespace Online;
// -----------------------------------------------------------------------------
UserInfoDialog::UserInfoDialog(uint32_t visiting_id)
: ModalDialog(0.8f,0.8f), m_visiting_id(visiting_id)
UserInfoDialog::UserInfoDialog(uint32_t showing_id)
: ModalDialog(0.8f,0.8f), m_showing_id(showing_id)
{
m_self_destroy = false;
m_enter_profile = false;
loadFromFile("online/user_info_dialog.stkgui");
m_profile = ProfileManager::get()->getProfileByID(visiting_id);
m_profile = ProfileManager::get()->getProfileByID(showing_id);
m_name_widget = getWidget<LabelWidget>("name");
assert(m_name_widget != NULL);
m_name_widget->setText(m_profile->getUserName(),false);
@ -70,6 +70,14 @@ UserInfoDialog::UserInfoDialog(uint32_t visiting_id)
}
void UserInfoDialog::beforeAddingWidgets()
{
/*m_accept_widget->setVisible(false);
m_decline_widget->setVisible(false);*/
}
// -----------------------------------------------------------------------------
UserInfoDialog::~UserInfoDialog()
{

View File

@ -40,7 +40,7 @@ private:
bool m_self_destroy;
bool m_enter_profile;
const uint32_t m_visiting_id;
const uint32_t m_showing_id;
Online::Profile * m_profile;
GUIEngine::LabelWidget * m_name_widget;
@ -56,9 +56,11 @@ private:
void requestJoin();
public:
UserInfoDialog(uint32_t visiting_id);
UserInfoDialog(uint32_t showing_id);
~UserInfoDialog();
virtual void beforeAddingWidgets();
void onEnterPressedInternal();
GUIEngine::EventPropagation processEvent(const std::string& eventSource);