Files
DarkflameServer/dDashboardServer/routes/PlayerActions.h
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

51 lines
2.1 KiB
C++

#pragma once
#include <optional>
#include <chrono>
#include <cstdint>
#include <functional>
#include <string>
#include "json.hpp"
#include "master/PlayerAction.h"
/**
* Sends player actions (kick, refresh, rescue) through master to every world server and reports the outcome.
*
* Results arrive asynchronously, so HTTP handlers return a request id immediately. When the result comes in,
* the completion callback runs on the main thread, the outcome is pushed on the `action_result` WebSocket
* topic to the owner's connections only, and it can be polled with GET /api/actions/:id.
*/
namespace PlayerActions {
struct Outcome {
bool success{true};
std::string message{};
// Only returned by GetStatus to the account that started the work, never broadcast
nlohmann::json data = nullptr;
// For work started with Begin: how many players or characters it changed. Left out of the result when unset,
// rather than claiming 0 (player actions report what the worlds answered)
std::optional<uint32_t> affected{};
};
// Called with the aggregated result; returns the message shown to the moderator
using Completion = std::function<Outcome(const PlayerActionResult& result)>;
// Send an action to all worlds. Returns the request id. Only ownerAccountId (who asked for it) gets the outcome.
uint32_t Request(PlayerActionRequest request, uint32_t ownerAccountId, Completion onComplete);
// Feed a PLAYER_ACTION_RESULT packet from master
void HandleResult(const PlayerActionResult& result);
// Expire requests master never answered. Call once per tick.
void Update();
// Track some other asynchronous work (e.g. sending an email) with the same result reporting.
// Begin returns a request id; Finish publishes the outcome like a player action result.
uint32_t Begin(uint32_t ownerAccountId, std::chrono::seconds timeout = std::chrono::minutes(2));
void Finish(uint32_t requestId, const Outcome& outcome);
// Result of a finished request, or {"status": "pending"} / {"status": "unknown"}. Only the owner sees it; anyone else gets "unknown".
nlohmann::json GetStatus(uint32_t requestId, uint32_t requesterAccountId);
}