More profile info progress

This commit is contained in:
momo5502 2023-04-07 09:01:36 +02:00
parent c569ed5d8d
commit 26a56b0602
5 changed files with 101 additions and 12 deletions

View File

@ -141,8 +141,7 @@ namespace auth
const utils::info_string info_string(params[1]); const utils::info_string info_string(params[1]);
const auto xuid = strtoull(info_string.get("xuid").data(), nullptr, 10); const auto xuid = strtoull(info_string.get("xuid").data(), nullptr, 10);
profile_infos::add_profile_info(xuid, std::move(info)); profile_infos::add_and_distribute_profile_info(target, xuid, info);
profile_infos::distribute_profile_infos();
game::SV_DirectConnect(target); game::SV_DirectConnect(target);
} }

View File

@ -6,6 +6,7 @@
#include "network.hpp" #include "network.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
#include "workshop.hpp" #include "workshop.hpp"
#include "profile_infos.hpp"
#include <utils/hook.hpp> #include <utils/hook.hpp>
#include <utils/string.hpp> #include <utils/string.hpp>
@ -211,6 +212,7 @@ namespace party
connect_host = target; connect_host = target;
} }
profile_infos::clear_profile_infos();
query_server(connect_host, handle_connect_query_response); query_server(connect_host, handle_connect_query_response);
} }
@ -314,6 +316,11 @@ namespace party
return *reinterpret_cast<game::netadr_t*>(address); return *reinterpret_cast<game::netadr_t*>(address);
} }
bool is_host(const game::netadr_t& addr)
{
return get_connected_server() == addr || connect_host == addr;
}
struct component final : client_component struct component final : client_component
{ {
void post_unpack() override void post_unpack() override

View File

@ -9,4 +9,6 @@ namespace party
void query_server(const game::netadr_t& host, query_callback callback); void query_server(const game::netadr_t& host, query_callback callback);
game::netadr_t get_connected_server(); game::netadr_t get_connected_server();
bool is_host(const game::netadr_t& addr);
} }

View File

@ -12,12 +12,14 @@
#include "../steam/steam.hpp" #include "../steam/steam.hpp"
#include <utils/io.hpp> #include <utils/io.hpp>
#include "game/utils.hpp"
namespace profile_infos namespace profile_infos
{ {
namespace namespace
{ {
using profile_map = std::unordered_map<uint64_t, profile_info>; using profile_map = std::unordered_map<uint64_t, profile_info>;
utils::concurrency::container<profile_map> profile_mapping; utils::concurrency::container<profile_map> profile_mapping{};
std::optional<profile_info> load_profile_info() std::optional<profile_info> load_profile_info()
{ {
@ -40,6 +42,49 @@ namespace profile_infos
return {std::move(info)}; return {std::move(info)};
} }
int get_max_client_count()
{
return game::get_dvar_int("com_maxclients");
}
void send_profile_info(const game::netadr_t& address, const std::string& buffer)
{
network::send(address, "profileInfo", buffer);
}
template <typename T>
void distribute_profile_info(T* client_states, const std::string& buffer)
{
if (!client_states)
{
return;
}
for (int i = 0; i < get_max_client_count(); ++i)
{
if (client_states[i].client_state > 0)
{
send_profile_info(client_states[i].address, buffer);
}
}
}
void distribute_profile_info(const uint64_t user_id, const profile_info& info)
{
utils::byte_buffer buffer{};
buffer.write(user_id);
info.serialize(buffer);
if (game::is_server())
{
distribute_profile_info(*game::svs_clients, buffer.get_buffer());
}
else
{
distribute_profile_info(*game::svs_clients_cl, buffer.get_buffer());
}
}
} }
profile_info::profile_info(utils::byte_buffer& buffer) profile_info::profile_info(utils::byte_buffer& buffer)
@ -54,26 +99,61 @@ namespace profile_infos
buffer.write_string(this->ddl); buffer.write_string(this->ddl);
} }
void add_profile_info(const uint64_t user_id, profile_info info) void add_profile_info(const uint64_t user_id, const profile_info& info)
{ {
if (user_id == steam::SteamUser()->GetSteamID().bits) if (user_id == steam::SteamUser()->GetSteamID().bits)
{ {
return; return;
} }
printf("Adding profile info: %llX\n", user_id);
profile_mapping.access([&](profile_map& profiles) profile_mapping.access([&](profile_map& profiles)
{ {
profiles[user_id] = std::move(info); profiles[user_id] = info;
}); });
} }
void distribute_profile_infos() void distribute_profile_info_to_user(const game::netadr_t& addr, const uint64_t user_id, const profile_info& info)
{ {
// TODO utils::byte_buffer buffer{};
buffer.write(user_id);
info.serialize(buffer);
send_profile_info(addr, buffer.get_buffer());
} }
std::optional<profile_info> get_profile_info(uint64_t user_id) void distribute_profile_infos_to_user(const game::netadr_t& addr)
{ {
profile_mapping.access([&](const profile_map& profiles)
{
for (const auto& entry : profiles)
{
distribute_profile_info_to_user(addr, entry.first, entry.second);
}
});
}
void add_and_distribute_profile_info(const game::netadr_t& addr, const uint64_t user_id, const profile_info& info)
{
distribute_profile_infos_to_user(addr);
add_profile_info(user_id, info);
distribute_profile_info(user_id, info);
}
void clear_profile_infos()
{
profile_mapping.access([&](profile_map& profiles)
{
profiles = {};
});
}
std::optional<profile_info> get_profile_info(const uint64_t user_id)
{
printf("Requesting profile info: %llX\n", user_id);
if (user_id == steam::SteamUser()->GetSteamID().bits) if (user_id == steam::SteamUser()->GetSteamID().bits)
{ {
return load_profile_info(); return load_profile_info();
@ -110,7 +190,7 @@ namespace profile_infos
{ {
network::on("profileInfo", [](const game::netadr_t& server, const network::data_view& data) network::on("profileInfo", [](const game::netadr_t& server, const network::data_view& data)
{ {
if (party::get_connected_server() != server) if (!party::is_host(server))
{ {
return; return;
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <game/game.hpp>
#include <utils/byte_buffer.hpp> #include <utils/byte_buffer.hpp>
namespace profile_infos namespace profile_infos
@ -14,9 +15,9 @@ namespace profile_infos
void serialize(utils::byte_buffer& buffer) const; void serialize(utils::byte_buffer& buffer) const;
}; };
void add_profile_info(uint64_t user_id, profile_info info); void add_profile_info(uint64_t user_id, const profile_info& info);
void add_and_distribute_profile_info(const game::netadr_t& addr, uint64_t user_id, const profile_info& info);
void distribute_profile_infos(); void clear_profile_infos();
std::optional<profile_info> get_profile_info(uint64_t user_id); std::optional<profile_info> get_profile_info(uint64_t user_id);
void update_profile_info(const profile_info& info); void update_profile_info(const profile_info& info);