@hytopia.com/server-protocol 1.0.26 → 1.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hytopia.com/server-protocol",
3
- "version": "1.0.26",
3
+ "version": "1.0.28",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -49,9 +49,12 @@ export interface IPacketDefinition<TId extends PacketId, TSchema> {
49
49
  validate: ValidateFunction<TSchema>;
50
50
  }
51
51
 
52
+ export type WorldTick = number;
53
+
52
54
  export type IPacket<TId extends PacketId, TSchema> = [
53
55
  TId, // packet id
54
- TSchema // packet data
56
+ TSchema, // packet data
57
+ WorldTick?, // world tick
55
58
  ];
56
59
 
57
60
  export type AnyPacket = IPacket<PacketId, unknown>;
@@ -64,15 +67,22 @@ export interface Serializable {
64
67
  serialize(): AnySchema;
65
68
  }
66
69
 
67
- export function createPacket<TId extends number, TSchema>(
70
+ export function createPacket<TId extends PacketId, TSchema>(
68
71
  packetDef: IPacketDefinition<TId, TSchema>,
69
72
  data: TSchema,
73
+ worldTick?: WorldTick,
70
74
  ): IPacket<TId, TSchema> {
71
75
  if (!packetDef.validate(data)) {
72
- throw new Error(`createPacket(): Invalid payload for packet with id ${packetDef.id}, error: ${Ajv.instance.errorsText(packetDef.validate.errors)}`);
76
+ throw new Error(`Invalid payload for packet with id ${packetDef.id}. Error: ${Ajv.instance.errorsText(packetDef.validate.errors)}`);
77
+ }
78
+
79
+ const packet: IPacket<TId, TSchema> = [packetDef.id, data];
80
+
81
+ if (typeof worldTick === 'number') {
82
+ packet.push(worldTick);
73
83
  }
74
84
 
75
- return [ packetDef.id, data ];
85
+ return packet;
76
86
  }
77
87
 
78
88
  export function definePacket<TId extends PacketId, TSchema>(
@@ -25,7 +25,8 @@ export function isValidPacket(packet: unknown): packet is AnyPacket {
25
25
  typeof packet !== 'object' ||
26
26
  packet === null ||
27
27
  typeof packet[0] !== 'number' || packet[0] < 0 ||
28
- packet[1] === undefined
28
+ packet[1] === undefined ||
29
+ (packet[2] !== undefined && (typeof packet[2] !== 'number' || packet[2] < 0))
29
30
  ) {
30
31
  return false;
31
32
  }
@@ -2,8 +2,9 @@ import { definePacket, PacketId } from '../PacketCore';
2
2
  import type { IPacket } from '../PacketCore';
3
3
  import { blockSchema } from '../../schemas/Block';
4
4
  import type { BlockSchema } from '../../schemas/Block';
5
+ import type { WorldTick } from '../PacketCore';
5
6
 
6
- export type BlockPacket = IPacket<typeof PacketId.BLOCK, BlockSchema>;
7
+ export type BlockPacket = IPacket<typeof PacketId.BLOCK, BlockSchema> & [WorldTick];
7
8
 
8
9
  export const blockPacketDefinition = definePacket(
9
10
  PacketId.BLOCK,
@@ -2,8 +2,9 @@ import { definePacket, PacketId } from '../PacketCore';
2
2
  import type { IPacket } from '../PacketCore';
3
3
  import { blockTypeSchema } from '../../schemas/BlockType';
4
4
  import type { BlockTypeSchema } from '../../schemas/BlockType';
5
+ import type { WorldTick } from '../PacketCore';
5
6
 
6
- export type BlockTypePacket = IPacket<typeof PacketId.BLOCK_TYPE, BlockTypeSchema>;
7
+ export type BlockTypePacket = IPacket<typeof PacketId.BLOCK_TYPE, BlockTypeSchema> & [WorldTick];
7
8
 
8
9
  export const blockTypePacketDefinition = definePacket(
9
10
  PacketId.BLOCK_TYPE,
@@ -2,8 +2,9 @@ import { definePacket, PacketId } from '../PacketCore';
2
2
  import type { IPacket } from '../PacketCore';
3
3
  import { blockTypeRegistrySchema } from '../../schemas/BlockTypeRegistry';
4
4
  import type { BlockTypeRegistrySchema } from '../../schemas/BlockTypeRegistry';
5
+ import type { WorldTick } from '../PacketCore';
5
6
 
6
- export type BlockTypeRegistryPacket = IPacket<typeof PacketId.BLOCK_TYPE_REGISTRY, BlockTypeRegistrySchema>;
7
+ export type BlockTypeRegistryPacket = IPacket<typeof PacketId.BLOCK_TYPE_REGISTRY, BlockTypeRegistrySchema> & [WorldTick];
7
8
 
8
9
  export const blockTypeRegistryPacketDefinition = definePacket(
9
10
  PacketId.BLOCK_TYPE_REGISTRY,
@@ -2,8 +2,9 @@ import { definePacket, PacketId } from '../PacketCore';
2
2
  import type { IPacket } from '../PacketCore';
3
3
  import { chunkSchema } from '../../schemas/Chunk';
4
4
  import type { ChunkSchema } from '../../schemas/Chunk';
5
+ import type { WorldTick } from '../PacketCore';
5
6
 
6
- export type ChunkPacket = IPacket<typeof PacketId.CHUNK, ChunkSchema>;
7
+ export type ChunkPacket = IPacket<typeof PacketId.CHUNK, ChunkSchema> & [WorldTick];
7
8
 
8
9
  export const chunkPacketDefinition = definePacket(
9
10
  PacketId.CHUNK,
@@ -2,8 +2,9 @@ import { definePacket, PacketId } from '../PacketCore';
2
2
  import type { IPacket } from '../PacketCore';
3
3
  import { entitySchema } from '../../schemas/Entity';
4
4
  import type { EntitySchema } from '../../schemas/Entity';
5
+ import type { WorldTick } from '../PacketCore';
5
6
 
6
- export type EntityPacket = IPacket<typeof PacketId.ENTITY, EntitySchema>;
7
+ export type EntityPacket = IPacket<typeof PacketId.ENTITY, EntitySchema> & [WorldTick];
7
8
 
8
9
  export const entityPacketDefinition = definePacket(
9
10
  PacketId.ENTITY,
@@ -2,8 +2,9 @@ import { definePacket, PacketId } from '../PacketCore';
2
2
  import type { IPacket } from '../PacketCore';
3
3
  import { physicsDebugRenderSchema } from '../../schemas/PhysicsDebugRender';
4
4
  import type { PhysicsDebugRenderSchema } from '../../schemas/PhysicsDebugRender';
5
+ import type { WorldTick } from '../PacketCore';
5
6
 
6
- export type PhysicsDebugRenderPacket = IPacket<typeof PacketId.PHYSICS_DEBUG_RENDER, PhysicsDebugRenderSchema>;
7
+ export type PhysicsDebugRenderPacket = IPacket<typeof PacketId.PHYSICS_DEBUG_RENDER, PhysicsDebugRenderSchema> & [WorldTick];
7
8
 
8
9
  export const physicsDebugRenderPacketDefinition = definePacket(
9
10
  PacketId.PHYSICS_DEBUG_RENDER,
@@ -2,8 +2,9 @@ import { definePacket, PacketId } from '../PacketCore';
2
2
  import type { IPacket } from '../PacketCore';
3
3
  import { worldSchema } from '../../schemas/World';
4
4
  import type { WorldSchema } from '../../schemas/World';
5
+ import type { WorldTick } from '../PacketCore';
5
6
 
6
- export type WorldPacket = IPacket<typeof PacketId.WORLD, WorldSchema>;
7
+ export type WorldPacket = IPacket<typeof PacketId.WORLD, WorldSchema> & [WorldTick];
7
8
 
8
9
  export const worldPacketDefinition = definePacket(
9
10
  PacketId.WORLD,
package/schemas/World.ts CHANGED
@@ -4,6 +4,7 @@ export type WorldSchema = {
4
4
  i: number; // id
5
5
  f?: boolean; // focused
6
6
  n?: string; // name
7
+ s?: string; // skyboxUri
7
8
  };
8
9
 
9
10
  export const worldSchema: JSONSchemaType<WorldSchema> = {
@@ -11,7 +12,8 @@ export const worldSchema: JSONSchemaType<WorldSchema> = {
11
12
  properties: {
12
13
  i: { type: 'number' },
13
14
  f: { type: 'boolean', nullable: true },
14
- n: { type: 'string', nullable: true }
15
+ n: { type: 'string', nullable: true },
16
+ s: { type: 'string', nullable: true }
15
17
  },
16
18
  required: [ 'i' ],
17
19
  additionalProperties: false,