Files
Shiip-of-Hakinian-Espanol/soh/soh/Network/Anchor/Packets/AllClientState.cpp
Garrett Cox 31ee15dcc8 WIP
2024-12-16 09:18:29 -06:00

75 lines
2.8 KiB
C++

#ifdef ENABLE_REMOTE_CONTROL
#include "soh/Network/Anchor/Anchor.h"
#include "soh/Network/Anchor/JsonConversions.hpp"
#include <nlohmann/json.hpp>
#include <libultraship/libultraship.h>
#include "soh/OTRGlobals.h"
#include "soh/Notification/Notification.h"
/**
* ALL_CLIENT_STATE
*
* Contains a list of all clients and their CLIENT_STATE currently connected to the server
*
* The server itself sends this packet to all clients when a client connects or disconnects
*/
void Anchor::HandlePacket_AllClientState(nlohmann::json payload) {
std::vector<AnchorClient> newClients = payload["state"].get<std::vector<AnchorClient>>();
// add new clients
for (auto& client : newClients) {
if (client.self) {
ownClientId = client.clientId;
CVarSetInteger(CVAR_REMOTE_ANCHOR("LastClientId"), ownClientId);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame();
clients[client.clientId].self = true;
} else {
clients[client.clientId].self = false;
if (clients.contains(client.clientId)) {
if (clients[client.clientId].online != client.online) {
Notification::Emit({
.prefix = client.name,
.message = client.online ? "Connected" : "Disconnected",
});
}
} else if (client.online) {
Notification::Emit({
.prefix = client.name,
.message = "Connected",
});
}
}
clients[client.clientId].clientId = client.clientId;
clients[client.clientId].name = client.name;
clients[client.clientId].color = client.color;
clients[client.clientId].clientVersion = client.clientVersion;
clients[client.clientId].teamId = client.teamId;
clients[client.clientId].online = client.online;
clients[client.clientId].seed = client.seed;
clients[client.clientId].isSaveLoaded = client.isSaveLoaded;
clients[client.clientId].isGameComplete = client.isGameComplete;
clients[client.clientId].sceneNum = client.sceneNum;
clients[client.clientId].entranceIndex = client.entranceIndex;
}
// remove clients that are no longer in the list
std::vector<uint32_t> clientsToRemove;
for (auto& [clientId, client] : clients) {
if (std::find_if(newClients.begin(), newClients.end(),
[clientId](AnchorClient& c) { return c.clientId == clientId; }) == newClients.end()) {
clientsToRemove.push_back(clientId);
}
}
// (seperate loop to avoid iterator invalidation)
for (auto& clientId : clientsToRemove) {
clients.erase(clientId);
}
RefreshClientActors();
}
#endif // ENABLE_REMOTE_CONTROL