Skip to content

Commit

Permalink
feat(world): added PlayerDiagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Sep 15, 2024
1 parent 1f4ebc1 commit f4a0c91
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/serenity/src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { NpcRequest } from "./npc-request";
import { BlockActorData } from "./block-actor-data";
import { BookEdit } from "./book-edit";
import { RequestChunkRadius } from "./request-chunk-radius";
import { ServerboundDiagnostics } from "./serverbound-diagnostics";

const HANDLERS = [
RequestNetworkSettings,
Expand All @@ -45,7 +46,8 @@ const HANDLERS = [
NpcRequest,
BlockActorData,
BookEdit,
RequestChunkRadius
RequestChunkRadius,
ServerboundDiagnostics
];

export { HANDLERS };
Expand Down Expand Up @@ -73,3 +75,5 @@ export * from "./respawn";
export * from "./npc-request";
export * from "./block-actor-data";
export * from "./book-edit";
export * from "./request-chunk-radius";
export * from "./serverbound-diagnostics";
45 changes: 45 additions & 0 deletions packages/serenity/src/handlers/serverbound-diagnostics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
DisconnectReason,
ServerboundDiagnosticsPacket
} from "@serenityjs/protocol";

import { SerenityHandler } from "./serenity-handler";

import type { NetworkSession } from "@serenityjs/network";

class ServerboundDiagnostics extends SerenityHandler {
public static readonly packet = ServerboundDiagnosticsPacket.id;

public static handle(
packet: ServerboundDiagnosticsPacket,
session: NetworkSession
): void {
// Get the player from the session
// If there is no player, then disconnect the session.
const player = this.serenity.getPlayer(session);
if (!player)
return session.disconnect(
"Failed to connect due to an invalid player. Please try again.",
DisconnectReason.InvalidPlayer
);

// Get the diagnostics from the player
const diagnostics = player.diagnostics;

// Set the diagnostics enabled property to true
if (!diagnostics.enabled) diagnostics.enabled = true;

// Set the diagnostics properties
diagnostics.fps = packet.fps;
diagnostics.serverSimTickTime = packet.serverSimTickTime;
diagnostics.clientSimTickTime = packet.clientSimTickTime;
diagnostics.beginFrameTime = packet.beginFrameTime;
diagnostics.inputTime = packet.inputTime;
diagnostics.renderTime = packet.renderTime;
diagnostics.endFrameTime = packet.endFrameTime;
diagnostics.remainderTimePercent = packet.remainderTimePercent;
diagnostics.unaccountedTimePercent = packet.unaccountedTimePercent;
}
}

export { ServerboundDiagnostics };
53 changes: 53 additions & 0 deletions packages/world/src/player/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class PlayerDiagnostic {
/**
* Whether the diagnostic is enabled client-side.
*/
public enabled = false;

/**
* The client's frames per second.
*/
public fps = 0;

/**
* The server simulation tick time.
*/
public serverSimTickTime = 0;

/**
* The client simulation tick time.
*/
public clientSimTickTime = 0;

/**
* The time the frame began.
*/
public beginFrameTime = 0;

/**
* The time the input was received.
*/
public inputTime = 0;

/**
* The time the frame was rendered
*/
public renderTime = 0;

/**
* The time the frame ended.
*/
public endFrameTime = 0;

/**
* The remainder time percentage.
*/
public remainderTimePercent = 0;

/**
* The percentage of time that was unaccounted for in the frame.
*/
public unaccountedTimePercent = 0;
}

export { PlayerDiagnostic };
1 change: 1 addition & 0 deletions packages/world/src/player/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./player";
export * from "./device";
export * from "./status";
export * from "./options";
export * from "./diagnostics";
7 changes: 7 additions & 0 deletions packages/world/src/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { EntitySpawnedSignal, PlayerMissSwingSignal } from "../events";

import { PlayerStatus } from "./status";
import { Device } from "./device";
import { PlayerDiagnostic } from "./diagnostics";

import type { ItemStack } from "../item";
import type { PlayerOptions } from "./options";
Expand Down Expand Up @@ -84,6 +85,12 @@ class Player extends Entity {
*/
public readonly device: Device;

/**
* The player's diagnostics. This is used to track the player's network latency, and other diagnostic information.
* This is only available when the client has `Enable Client Diagnostics` enabled in the creator settings.
*/
public readonly diagnostics = new PlayerDiagnostic();

/**
* The current status of the player's connection.
*/
Expand Down

0 comments on commit f4a0c91

Please sign in to comment.