[Friends] Better sorting and notify if friends come online

This commit is contained in:
momo5502 2017-02-12 13:07:14 +01:00
parent 358d039244
commit 3c07d1addf

View File

@ -88,7 +88,9 @@ namespace Components
std::string server = Steam::Proxy::SteamFriends->GetFriendRichPresence(user, "iw4x_server"); std::string server = Steam::Proxy::SteamFriends->GetFriendRichPresence(user, "iw4x_server");
Network::Address oldAddress = entry->server; Network::Address oldAddress = entry->server;
bool gotOnline = Friends::IsOnline(entry->lastTime);
entry->lastTime = static_cast<unsigned int>(atoi(Steam::Proxy::SteamFriends->GetFriendRichPresence(user, "iw4x_playing"))); entry->lastTime = static_cast<unsigned int>(atoi(Steam::Proxy::SteamFriends->GetFriendRichPresence(user, "iw4x_playing")));
gotOnline = !gotOnline && Friends::IsOnline(entry->lastTime);
if (server.empty()) if (server.empty())
{ {
@ -110,6 +112,11 @@ namespace Components
} }
Friends::SortList(); Friends::SortList();
if(Dvar::Var("cl_notifyFriendState").get<bool>() && gotOnline)
{
Toast::Show("cardicon_weed", "Friends", Utils::String::VA("%s is playing IW4x", entry->name.data()), 3000);
}
} }
void Friends::UpdateState(bool force) void Friends::UpdateState(bool force)
@ -218,14 +225,13 @@ namespace Components
std::lock_guard<std::recursive_mutex> _(Friends::Mutex); std::lock_guard<std::recursive_mutex> _(Friends::Mutex);
if (!Steam::Proxy::SteamFriends) return; if (!Steam::Proxy::SteamFriends) return;
auto oldFriends = Friends::FriendsList;
Friends::FriendsList.clear();
int count = Steam::Proxy::SteamFriends->GetFriendCount(4); int count = Steam::Proxy::SteamFriends->GetFriendCount(4);
Proto::Friends::List list; Proto::Friends::List list;
list.ParseFromString(Utils::IO::ReadFile("players/friends.dat")); list.ParseFromString(Utils::IO::ReadFile("players/friends.dat"));
std::vector<Friends::Friend> steamFriends;
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
{ {
SteamID id = Steam::Proxy::SteamFriends->GetFriendByIndex(i, 4); SteamID id = Steam::Proxy::SteamFriends->GetFriendByIndex(i, 4);
@ -251,20 +257,40 @@ namespace Components
} }
} }
auto oldEntry = std::find_if(oldFriends.begin(), oldFriends.end(), [id](Friends::Friend entry) auto oldEntry = std::find_if(Friends::FriendsList.begin(), Friends::FriendsList.end(), [id](Friends::Friend entry)
{ {
return (entry.userId.Bits == id.Bits); return (entry.userId.Bits == id.Bits);
}); });
if (oldEntry != oldFriends.end()) entry = *oldEntry; if (oldEntry != Friends::FriendsList.end()) entry = *oldEntry;
else Friends::FriendsList.push_back(entry);
Friends::FriendsList.push_back(entry); steamFriends.push_back(entry);
}
for(auto i = Friends::FriendsList.begin(); i != Friends::FriendsList.end();)
{
SteamID id = i->userId;
auto oldEntry = std::find_if(steamFriends.begin(), steamFriends.end(), [id](Friends::Friend entry)
{
return (entry.userId.Bits == id.Bits);
});
if(oldEntry == steamFriends.end())
{
i = Friends::FriendsList.erase(i);
}
else
{
*i = *oldEntry;
++i;
Friends::UpdateUserInfo(id); Friends::UpdateUserInfo(id);
Steam::Proxy::SteamFriends->RequestFriendRichPresence(id); Steam::Proxy::SteamFriends->RequestFriendRichPresence(id);
} }
} }
}
unsigned int Friends::GetFriendCount() unsigned int Friends::GetFriendCount()
{ {
@ -391,6 +417,7 @@ namespace Components
{ {
if (Dedicated::IsEnabled() ||ZoneBuilder::IsEnabled()) return; if (Dedicated::IsEnabled() ||ZoneBuilder::IsEnabled()) return;
Dvar::Register<bool>("cl_anonymous", false, Game::DVAR_FLAG_SAVED, ""); Dvar::Register<bool>("cl_anonymous", false, Game::DVAR_FLAG_SAVED, "");
Dvar::Register<bool>("cl_notifyFriendState", false, Game::DVAR_FLAG_SAVED, ""); // False by default, might set default to true and add that to the options!
// Callback to update user information // Callback to update user information
Steam::Proxy::RegisterCallback(336, [](void* data) Steam::Proxy::RegisterCallback(336, [](void* data)
@ -464,7 +491,7 @@ namespace Components
} }
} }
if(sortInterval.elapsed(3s)) if(sortInterval.elapsed(1s))
{ {
sortInterval.update(); sortInterval.update();