Files
DarkflameServer/dGame/dUtilities/DashboardNotify.cpp
Aaron Kimbrell 5a9a3e380b refactor: master packets as structs
Every MASTER service packet is now an LUBitStream struct (docs/PacketArchitecture.md,
PR 14) and the master server's switch is a dispatch map (PacketDispatcher), as are the
master handlers of the world, chat and dashboard servers.

- dNet/MasterPackets.h: RequestZoneTransfer, RequestZoneTransferResponse, ServerInfo,
  RequestSessionKey, SetSessionKey, SessionKeyResponse, NewSessionAlert, PlayerAdded /
  PlayerRemoved, CreatePrivateZone, RequestPrivateZone (passwords still cut to 50
  characters when read), WorldReady, WorldReadyInfo (WORLD_READY to the dashboard),
  PrepZone, Shutdown, ShutdownResponse, WorldShutDown (SHUTDOWN_RESPONSE to the
  dashboard), ShutdownUniverse, AffirmTransferRequest/Response, RequestServerList,
  ServerListResponse, DashboardShutdown, ConfigReload, InstanceShutdown. The Send*
  functions are gone; MasterPackets::SendToMaster(msg) and SendTo(sysAddr, msg) send a
  struct.
- The dashboard and instance migration structs (PlayerAction, DataChanged, Dashboard
  messages, MessageCapture, InstanceMigration) are LUBitStreams of the MASTER service now
  and moved to dNet/master/, included by MasterPackets.h. Their payloads are unchanged;
  master forwards them by re-serializing the struct instead of copying raw bytes.
- InstanceManager, ZoneInstanceManager, MigrationCoordinator, dServer (server info, zone
  transfer response), auth (SET_SESSION_KEY), the world (session keys, player added and
  removed, world ready, shutdown response, affirmations, prep zone, shutdown universe) and
  the dashboard (server list, instance shutdown, config reload, announcements, player
  actions, message capture) send and read structs.
- The login stamps are a `stamps` field of RequestZoneTransfer and
  RequestZoneTransferResponse (read leniently as before: a message without them reads as
  empty); master adds its stamps in the REQUEST_ZONE_TRANSFER handler and when it answers,
  as it did.
- InstanceManager::GetInstanceBySysAddr takes a const address.

Verified: tests/dGameTests/dNetTests/Legacy/MasterPacketsLegacy.h is a verbatim copy of
the old writers and readers; MasterPacketsTests requires identical bytes for a grid of
inputs, checks the old readers read what the structs write, round trips and truncation,
hand written golden packets, that zone transfers without stamps still read, that the dashboard/migration structs write what
"header + Serialize" wrote, and that the dispatcher drops truncated packets. No wire
bytes changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 22:30:50 -05:00

98 lines
3.2 KiB
C++

#include "DashboardNotify.h"
#include "MasterPackets.h"
#include <chrono>
#include <set>
#include "BitStreamUtils.h"
#include "master/DataChanged.h"
#include "Game.h"
#include "dServer.h"
#include "MessageType/Master.h"
#include "ServiceType.h"
#include "master/DashboardMessages.h"
#include "PlayerManager.h"
#include "Entity.h"
#include "Character.h"
#include "dZoneManager.h"
#include "GameMessages.h"
#include "ChatPackets.h"
#include "Amf3.h"
#include "GeneralUtils.h"
namespace {
constexpr auto SEND_INTERVAL = std::chrono::milliseconds(500);
constexpr auto POSITION_INTERVAL = std::chrono::seconds(1);
std::chrono::steady_clock::time_point g_LastPositions{};
bool g_HadPlayers = false;
std::set<std::pair<std::string, LWOOBJID>> g_Pending;
std::chrono::steady_clock::time_point g_LastSend{};
}
namespace DashboardNotify {
void Changed(const std::string& table, LWOOBJID id) {
if (g_Pending.size() >= DataChanged::MAX_ENTRIES) return; // a burst this big reaches the dashboard's own check anyway
g_Pending.emplace(table, id);
}
void Flush(bool force) {
if (g_Pending.empty() || !Game::server) return;
const auto now = std::chrono::steady_clock::now();
if (!force && now - g_LastSend < SEND_INTERVAL) return;
g_LastSend = now;
DataChanged message;
for (const auto& [table, id] : g_Pending) message.entries.push_back({ table, id });
g_Pending.clear();
MasterPackets::SendToMaster(message);
}
}
namespace DashboardNotify {
void SendPlayerPositions(uint32_t instanceId) {
if (!Game::server || !Game::zoneManager) return;
const auto now = std::chrono::steady_clock::now();
if (now - g_LastPositions < POSITION_INTERVAL) return;
g_LastPositions = now;
const auto& players = PlayerManager::GetAllPlayers();
// Once more after the last player leaves, so the dashboard clears this world
if (players.empty() && !g_HadPlayers) return;
g_HadPlayers = !players.empty();
PlayerPositions message;
const auto zone = Game::zoneManager->GetZoneID();
message.zoneId = zone.GetMapID();
message.instanceId = instanceId;
message.cloneId = zone.GetCloneID();
for (const auto* player : players) {
const auto* character = player ? player->GetCharacter() : nullptr;
if (!character) continue;
const auto position = player->GetPosition();
message.players.push_back({ character->GetID(), position.x, position.y, position.z });
}
MasterPackets::SendToMaster(message);
}
std::string ChatLine(const std::string& title, const std::string& message) {
if (title.empty()) return message;
const char last = title.back();
const bool punctuated = last == '!' || last == '?' || last == '.' || last == ':';
return title + (punctuated ? " " : ": ") + message;
}
void Announce(const std::string& title, const std::string& message) {
AMFArrayValue args;
args.Insert("title", title);
args.Insert("message", message);
GameMessages::SendUIMessageServerToAllClients("ToggleAnnounce", args);
// Also in chat, for anyone who closes the popup without reading it
const auto text = GeneralUtils::UTF8ToUTF16(ChatLine(title, message));
for (const auto* player : PlayerManager::GetAllPlayers()) {
if (player) ChatPackets::SendSystemMessage(player->GetSystemAddress(), text);
}
}
}