From 0139465337b0987f998ad164917e5050f10148dc Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 29 Jan 2017 11:40:20 +0100 Subject: [PATCH] [Friends] Faster update --- src/Components/Modules/Friends.cpp | 112 ++++++++++++++++------------- src/Components/Modules/Friends.hpp | 1 + src/Worker/Handlers/Friends.cpp | 56 +++++++++++---- src/Worker/Handlers/Friends.hpp | 1 + 4 files changed, 108 insertions(+), 62 deletions(-) diff --git a/src/Components/Modules/Friends.cpp b/src/Components/Modules/Friends.cpp index e6094454..a6d2aedf 100644 --- a/src/Components/Modules/Friends.cpp +++ b/src/Components/Modules/Friends.cpp @@ -8,48 +8,19 @@ namespace Components void Friends::UpdateUserInfo(SteamID user) { - std::lock_guard _(Friends::Mutex); - - auto i = std::find_if(Friends::FriendsList.begin(), Friends::FriendsList.end(), [user] (Friends::Friend entry) - { - return (entry.userId.Bits == user.Bits); - }); - - if(i != Friends::FriendsList.end()) - { Proto::IPC::Function function; - function.set_name("getPresence"); + function.set_name("getInfo"); *function.add_params() = Utils::String::VA("%llx", user.Bits); - std::string* key = function.add_params(); + *function.add_params() = "name"; + *function.add_params() = "state"; + *function.add_params() = "iw4x_name"; + *function.add_params() = "iw4x_status"; + *function.add_params() = "iw4x_rank"; + *function.add_params() = "iw4x_server"; - *key = "iw4x_status"; IPCHandler::SendWorker("friends", function.SerializeAsString()); - - *key = "iw4x_rank"; - IPCHandler::SendWorker("friends", function.SerializeAsString()); - - *key = "iw4x_server"; - IPCHandler::SendWorker("friends", function.SerializeAsString()); - } - - /*userInfo.online = Steam::Proxy::SteamFriends->GetFriendPersonaState(user) != 0; - userInfo.name = Steam::Proxy::SteamFriends->GetFriendPersonaName(user); - userInfo.statusName = Steam::Proxy::SteamFriends->GetFriendRichPresence(user, "iw4x_status"); - userInfo.prestige = Utils::Cryptography::Rand::GenerateInt() % (10 + 1); - userInfo.experience = Utils::Cryptography::Rand::GenerateInt() % (2516000 + 1);*/ - -/* qsort(Friends::FriendsList.data(), Friends::FriendsList.size(), sizeof(Friends::Friend), [](const void* first, const void* second) - { - const Friends::Friend* friend1 = static_cast(first); - const Friends::Friend* friend2 = static_cast(second); - - std::string name1 = Utils::String::ToLower(Colors::Strip(friend1->name)); - std::string name2 = Utils::String::ToLower(Colors::Strip(friend2->name)); - - return name1.compare(name2); - });*/ } void Friends::UpdateFriends() @@ -160,13 +131,24 @@ namespace Components if (entry == Friends::FriendsList.end()) return; - if(key == "iw4x_status") + if (key == "iw4x_status") { entry->statusName = value; } - else if(key == "iw4x_rank") + else if (key == "iw4x_server") { - if(value.size() == 4) + entry->server = value; + + // TODO: Query server here? + if (entry->server.getType() != Game::NA_BAD) + { + Node::AddNode(entry->server); + Network::SendCommand(entry->server, "getinfo", Utils::Cryptography::Rand::GenerateChallenge()); + } + } + else if (key == "iw4x_rank") + { + if (value.size() == 4) { int data = *reinterpret_cast(const_cast(value.data())); @@ -177,6 +159,44 @@ namespace Components } } + void Friends::InfoResponse(std::vector params) + { + if (params.size() >= 1) + { + std::lock_guard _(Friends::Mutex); + + SteamID id; + id.Bits = strtoull(params[0].data(), nullptr, 16); + + auto entry = std::find_if(Friends::FriendsList.begin(), Friends::FriendsList.end(), [id](Friends::Friend entry) + { + return (entry.userId.Bits == id.Bits); + }); + + if (entry == Friends::FriendsList.end()) return; + + for(unsigned int i = 1; i < params.size(); i += 2) + { + if ((i + 1) >= params.size()) break; + std::string key = params[i]; + std::string value = params[i + 1]; + + if(key == "name") + { + entry->name = value; + } + else if(key == "state") + { + entry->online = atoi(value.data()) != 0; + } + else + { + Friends::PresenceResponse({ Utils::String::VA("%llx", id.Bits), key, value }); + } + } + } + } + void Friends::FriendsResponse(std::vector params) { std::lock_guard _(Friends::Mutex); @@ -204,18 +224,9 @@ namespace Components Friends::FriendsList.push_back(entry); + Friends::UpdateUserInfo(id); + Proto::IPC::Function function; - function.set_name("getName"); - *function.add_params() = Utils::String::VA("%llx", id.Bits); - IPCHandler::SendWorker("friends", function.SerializeAsString()); - - function.Clear(); - function.set_name("getPresence"); - *function.add_params() = Utils::String::VA("%llx", id.Bits); - *function.add_params() = "iw4x_status"; - IPCHandler::SendWorker("friends", function.SerializeAsString()); - - function.Clear(); function.set_name("requestPresence"); *function.add_params() = Utils::String::VA("%llx", id.Bits); IPCHandler::SendWorker("friends", function.SerializeAsString()); @@ -263,6 +274,7 @@ namespace Components fInterface->map("friendsResponse", Friends::FriendsResponse); fInterface->map("nameResponse", Friends::NameResponse); fInterface->map("presenceResponse", Friends::PresenceResponse); + fInterface->map("infoResponse", Friends::InfoResponse); } Friends::~Friends() diff --git a/src/Components/Modules/Friends.hpp b/src/Components/Modules/Friends.hpp index 3e122300..ca377471 100644 --- a/src/Components/Modules/Friends.hpp +++ b/src/Components/Modules/Friends.hpp @@ -54,5 +54,6 @@ namespace Components static void FriendsResponse(std::vector params); static void NameResponse(std::vector params); static void PresenceResponse(std::vector params); + static void InfoResponse(std::vector params); }; } diff --git a/src/Worker/Handlers/Friends.cpp b/src/Worker/Handlers/Friends.cpp index f6020fdd..7c63bc10 100644 --- a/src/Worker/Handlers/Friends.cpp +++ b/src/Worker/Handlers/Friends.cpp @@ -93,18 +93,7 @@ namespace Handlers response.set_name("presenceResponse"); *response.add_params() = Utils::String::VA("%llX", id.Bits); *response.add_params() = params[1].data(); - - std::string* value = response.add_params(); - *value = Steam::Proxy::SteamFriends->GetFriendRichPresence(id, params[1].data()); - - if (params[1] == "iw4x_rank") - { - int experience = Utils::Cryptography::Rand::GenerateInt() % (2516000 + 1); - int prestige = Utils::Cryptography::Rand::GenerateInt() % (10 + 1); - - int data = (experience & 0xFFFFFF) | ((prestige << 24) & 0xFF); - *value = std::string(reinterpret_cast(&data), 4); - } + *response.add_params() = Steam::Proxy::SteamFriends->GetFriendRichPresence(id, params[1].data()); endpoint.send(this->getCommand(), response.SerializeAsString()); } @@ -121,6 +110,48 @@ namespace Handlers } } + void Friends::getInfo(Worker::Endpoint endpoint, std::vector params) + { + if (params.size() >= 1 && Steam::Proxy::SteamFriends) + { + SteamID id; + id.Bits = strtoull(params[0].data(), nullptr, 16); + + Proto::IPC::Function response; + response.set_name("infoResponse"); + *response.add_params() = Utils::String::VA("%llX", id.Bits); + + for(unsigned int i = 1; i < params.size(); ++i) + { + std::string key = params[i]; + *response.add_params() = key; + + if(key == "name") + { + *response.add_params() = Steam::Proxy::SteamFriends->GetFriendPersonaName(id); + } + else if(key == "state") + { + *response.add_params() = Utils::String::VA("%d", Steam::Proxy::SteamFriends->GetFriendPersonaState(id)); + } + else if (key == "iw4x_rank") // This is just a test + { + int experience = Utils::Cryptography::Rand::GenerateInt() % (2516000 + 1); + int prestige = Utils::Cryptography::Rand::GenerateInt() % (10 + 1); + + int data = (experience & 0xFFFFFF) | ((prestige << 24) & 0xFF); + *response.add_params() = std::string(reinterpret_cast(&data), 4); + } + else + { + *response.add_params() = Steam::Proxy::SteamFriends->GetFriendRichPresence(id, key.data()); + } + } + + endpoint.send(this->getCommand(), response.SerializeAsString()); + } + } + Friends::Friends() { using namespace std::placeholders; @@ -129,6 +160,7 @@ namespace Handlers this->addFunction("setPresence", std::bind(&Friends::setPresence, this, _1, _2)); this->addFunction("getPresence", std::bind(&Friends::getPresence, this, _1, _2)); this->addFunction("requestPresence", std::bind(&Friends::requestPresence, this, _1, _2)); + this->addFunction("getInfo", std::bind(&Friends::getInfo, this, _1, _2)); } Friends::~Friends() diff --git a/src/Worker/Handlers/Friends.hpp b/src/Worker/Handlers/Friends.hpp index 3a6e0435..0e15cdd9 100644 --- a/src/Worker/Handlers/Friends.hpp +++ b/src/Worker/Handlers/Friends.hpp @@ -22,5 +22,6 @@ namespace Handlers void setPresence(Worker::Endpoint endpoint, std::vector params); void getPresence(Worker::Endpoint endpoint, std::vector params); void requestPresence(Worker::Endpoint endpoint, std::vector params); + void getInfo(Worker::Endpoint endpoint, std::vector params); }; }