Add IPC support to autoparty

This commit is contained in:
delimeats-ch 2021-04-01 10:52:15 -05:00
parent 3184d16d6c
commit 4236bd8e4f

View File

@ -52,6 +52,7 @@
#include "common.hpp"
#include "hack.hpp"
#include "ipc.hpp"
namespace hacks::tf2::autoparty
{
@ -75,6 +76,10 @@ static settings::Boolean autoparty_log{ "autoparty.log", "true" };
static settings::Boolean message_kicks{ "autoparty.message-kicks", "true" };
// Extra debugging information like locking/unlocking the party
static settings::Boolean autoparty_debug{ "autoparty.debug", "false" };
// Use the longest-running IPC members as party hosts
static settings::Boolean ipc_mode{ "autoparty.ipc-mode", "false" };
// How many IPC members to use as party hosts
static settings::Int ipc_count{ "autoparty.ipc-count", "0" };
// How often to run the autoparty routine, in seconds
static settings::Int timeout{ "autoparty.run-frequency", "60" };
// Only run the autoparty routine once every N seconds
@ -91,12 +96,48 @@ static std::vector<uint32> party_hosts = {};
if (*autoparty_debug) \
logging::Info("AutoParty (debug): " __VA_ARGS__)
// I can't think of a better way to do this right now
struct ipc_peer
{
uint32 friendid;
time_t ts_injected;
};
bool compare_ts(ipc_peer &a, ipc_peer &b)
{
return a.ts_injected < b.ts_injected;
}
// Re-populates party_hosts from the current or new configuration
void repopulate(std::string str)
{
// Empty previous values
party_hosts.clear();
// IPC autoparty mode
if (*ipc_mode)
{
if (!ipc::peer)
{
// uh oh
return;
}
auto &peer_data = ipc::peer->memory->peer_user_data;
std::vector<struct ipc_peer> sorted_peers = {};
for (int i = 0; i < cat_ipc::max_peers; i++)
{
ipc::user_data_s &data = peer_data[i];
struct ipc_peer peer = { .friendid = data.friendid, .ts_injected = data.ts_injected };
sorted_peers.push_back(peer);
}
std::sort(sorted_peers.begin(), sorted_peers.end(), compare_ts);
for (int i = 0; i < *ipc_count; i++)
{
party_hosts.push_back(sorted_peers[i].friendid);
}
return;
}
// Add Steam32 IDs to party_hosts
std::stringstream ss(str);
for (uint32 id; ss >> id;)
@ -202,6 +243,10 @@ void party_routine()
else
{
// We are in a party!
// Are we the *designated* leader of the current party?
if (is_host())
{
// And are we actually the leader?
// Get a list of party members, then check each one to determine the leader
std::vector<unsigned> members = client->GetPartySteamIDs();
uint32 leader_id = 0;
@ -215,11 +260,9 @@ void party_routine()
break;
}
}
// Are we the leader of the current party?
// If so, manage it
if (leader_id == g_ISteamUser->GetSteamID().GetAccountID())
{
// Great, let's manage it
// If a member is offline, just leave the party and allow new join requests
if (*auto_leave and client->GetNumMembers() > client->GetNumOnlineMembers())
{
@ -277,6 +320,14 @@ void party_routine()
unlock_party();
}
else
{
// We are in someone else's party as a leader!
// Leave the party and unlock our join request mode
hack::ExecuteCommand("tf_party_leave");
unlock_party();
}
}
else
{
// In a party, but not the leader
if (*auto_lock)
@ -297,6 +348,7 @@ void party_routine()
static InitRoutine init([]() {
host_list.installChangeCallback([](settings::VariableBase<std::string> &var, std::string after) { repopulate(after); });
ipc_mode.installChangeCallback([](settings::VariableBase<bool> &var, bool after) { repopulate(*host_list); });
EC::Register(EC::Paint, party_routine, "paint_autoparty", EC::average);
});
} // namespace hacks::tf2::autoparty