[Friends] Faster update

This commit is contained in:
momo5502 2017-01-29 11:40:20 +01:00
parent adedc4088f
commit 0139465337
4 changed files with 108 additions and 62 deletions

View File

@ -7,49 +7,20 @@ namespace Components
std::vector<Friends::Friend> Friends::FriendsList; std::vector<Friends::Friend> Friends::FriendsList;
void Friends::UpdateUserInfo(SteamID user) void Friends::UpdateUserInfo(SteamID user)
{
std::lock_guard<std::recursive_mutex> _(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; Proto::IPC::Function function;
function.set_name("getPresence"); function.set_name("getInfo");
*function.add_params() = Utils::String::VA("%llx", user.Bits); *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()); 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<const Friends::Friend*>(first);
const Friends::Friend* friend2 = static_cast<const Friends::Friend*>(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() void Friends::UpdateFriends()
@ -164,6 +135,17 @@ namespace Components
{ {
entry->statusName = value; entry->statusName = value;
} }
else if (key == "iw4x_server")
{
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") else if (key == "iw4x_rank")
{ {
if (value.size() == 4) if (value.size() == 4)
@ -177,6 +159,44 @@ namespace Components
} }
} }
void Friends::InfoResponse(std::vector<std::string> params)
{
if (params.size() >= 1)
{
std::lock_guard<std::recursive_mutex> _(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<std::string> params) void Friends::FriendsResponse(std::vector<std::string> params)
{ {
std::lock_guard<std::recursive_mutex> _(Friends::Mutex); std::lock_guard<std::recursive_mutex> _(Friends::Mutex);
@ -204,18 +224,9 @@ namespace Components
Friends::FriendsList.push_back(entry); Friends::FriendsList.push_back(entry);
Friends::UpdateUserInfo(id);
Proto::IPC::Function function; 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.set_name("requestPresence");
*function.add_params() = Utils::String::VA("%llx", id.Bits); *function.add_params() = Utils::String::VA("%llx", id.Bits);
IPCHandler::SendWorker("friends", function.SerializeAsString()); IPCHandler::SendWorker("friends", function.SerializeAsString());
@ -263,6 +274,7 @@ namespace Components
fInterface->map("friendsResponse", Friends::FriendsResponse); fInterface->map("friendsResponse", Friends::FriendsResponse);
fInterface->map("nameResponse", Friends::NameResponse); fInterface->map("nameResponse", Friends::NameResponse);
fInterface->map("presenceResponse", Friends::PresenceResponse); fInterface->map("presenceResponse", Friends::PresenceResponse);
fInterface->map("infoResponse", Friends::InfoResponse);
} }
Friends::~Friends() Friends::~Friends()

View File

@ -54,5 +54,6 @@ namespace Components
static void FriendsResponse(std::vector<std::string> params); static void FriendsResponse(std::vector<std::string> params);
static void NameResponse(std::vector<std::string> params); static void NameResponse(std::vector<std::string> params);
static void PresenceResponse(std::vector<std::string> params); static void PresenceResponse(std::vector<std::string> params);
static void InfoResponse(std::vector<std::string> params);
}; };
} }

View File

@ -93,18 +93,7 @@ namespace Handlers
response.set_name("presenceResponse"); response.set_name("presenceResponse");
*response.add_params() = Utils::String::VA("%llX", id.Bits); *response.add_params() = Utils::String::VA("%llX", id.Bits);
*response.add_params() = params[1].data(); *response.add_params() = params[1].data();
*response.add_params() = Steam::Proxy::SteamFriends->GetFriendRichPresence(id, 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<char*>(&data), 4);
}
endpoint.send(this->getCommand(), response.SerializeAsString()); endpoint.send(this->getCommand(), response.SerializeAsString());
} }
@ -121,6 +110,48 @@ namespace Handlers
} }
} }
void Friends::getInfo(Worker::Endpoint endpoint, std::vector<std::string> 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<char*>(&data), 4);
}
else
{
*response.add_params() = Steam::Proxy::SteamFriends->GetFriendRichPresence(id, key.data());
}
}
endpoint.send(this->getCommand(), response.SerializeAsString());
}
}
Friends::Friends() Friends::Friends()
{ {
using namespace std::placeholders; using namespace std::placeholders;
@ -129,6 +160,7 @@ namespace Handlers
this->addFunction("setPresence", std::bind(&Friends::setPresence, this, _1, _2)); this->addFunction("setPresence", std::bind(&Friends::setPresence, this, _1, _2));
this->addFunction("getPresence", std::bind(&Friends::getPresence, 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("requestPresence", std::bind(&Friends::requestPresence, this, _1, _2));
this->addFunction("getInfo", std::bind(&Friends::getInfo, this, _1, _2));
} }
Friends::~Friends() Friends::~Friends()

View File

@ -22,5 +22,6 @@ namespace Handlers
void setPresence(Worker::Endpoint endpoint, std::vector<std::string> params); void setPresence(Worker::Endpoint endpoint, std::vector<std::string> params);
void getPresence(Worker::Endpoint endpoint, std::vector<std::string> params); void getPresence(Worker::Endpoint endpoint, std::vector<std::string> params);
void requestPresence(Worker::Endpoint endpoint, std::vector<std::string> params); void requestPresence(Worker::Endpoint endpoint, std::vector<std::string> params);
void getInfo(Worker::Endpoint endpoint, std::vector<std::string> params);
}; };
} }