@hytopia.com/server-protocol 1.0.29 → 1.0.31

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.29",
3
+ "version": "1.0.31",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -30,7 +30,8 @@ export enum PacketId {
30
30
  BLOCK_TYPE = 34,
31
31
  CHUNK = 35,
32
32
  ENTITY = 36,
33
- WORLD = 37,
33
+ SIMULATION = 37,
34
+ WORLD = 38,
34
35
 
35
36
  // Debug Inbound Packet Types: 128 - 191 range
36
37
  // NONE atm, start at 128
@@ -51,9 +52,11 @@ export interface IPacketDefinition<TId extends PacketId, TSchema> {
51
52
 
52
53
  export type WorldTick = number;
53
54
 
54
- export type IPacket<TId extends PacketId, TSchema, TRequireWorldTick extends boolean = false> = TRequireWorldTick extends true
55
- ? [TId, TSchema, WorldTick]
56
- : [TId, TSchema];
55
+ export type IPacket<TId extends PacketId, TSchema> = [
56
+ TId, // packet id
57
+ TSchema, // packet data
58
+ WorldTick?, // world tick
59
+ ];
57
60
 
58
61
  export type AnyPacket = IPacket<PacketId, unknown>;
59
62
 
@@ -65,23 +68,25 @@ export interface Serializable {
65
68
  serialize(): AnySchema;
66
69
  }
67
70
 
68
- export function createPacket<TId extends PacketId, TSchema, TRequireWorldTick extends boolean = false>(
71
+ export function createPacket<TId extends PacketId, TSchema>(
69
72
  packetDef: IPacketDefinition<TId, TSchema>,
70
73
  data: TSchema,
71
- worldTick?: TRequireWorldTick extends true ? WorldTick : WorldTick | undefined
72
- ): IPacket<TId, TSchema, TRequireWorldTick> {
74
+ worldTick?: WorldTick,
75
+ ): IPacket<TId, TSchema> {
73
76
  if (!packetDef.validate(data)) {
74
77
  throw new Error(`Invalid payload for packet with id ${packetDef.id}. Error: ${Ajv.instance.errorsText(packetDef.validate.errors)}`);
75
78
  }
76
79
 
77
- if (worldTick === undefined) {
78
- return [packetDef.id, data] as IPacket<TId, TSchema, TRequireWorldTick>;
80
+ const packet: IPacket<TId, TSchema> = [packetDef.id, data];
81
+
82
+ if (typeof worldTick === 'number') {
83
+ packet.push(worldTick);
79
84
  }
80
85
 
81
- return [packetDef.id, data, worldTick] as IPacket<TId, TSchema, TRequireWorldTick>;
86
+ return packet;
82
87
  }
83
88
 
84
- export function definePacket<TId extends PacketId, TSchema, TRequireWorldTick extends boolean = false>(
89
+ export function definePacket<TId extends PacketId, TSchema>(
85
90
  id: TId,
86
91
  schema: JSONSchemaType<TSchema>,
87
92
  ): IPacketDefinition<TId, TSchema> {
@@ -22,11 +22,11 @@ for (const packet of Object.values(allPackets)) {
22
22
 
23
23
  export function isValidPacket(packet: unknown): packet is AnyPacket {
24
24
  if (
25
- !Array.isArray(packet) ||
26
- packet.length < 2 || packet.length > 3 ||
25
+ typeof packet !== 'object' ||
26
+ packet === null ||
27
27
  typeof packet[0] !== 'number' || packet[0] < 0 ||
28
28
  packet[1] === undefined ||
29
- (packet.length === 3 && typeof packet[2] !== 'number')
29
+ (packet[2] !== undefined && (typeof packet[2] !== 'number' || packet[2] < 0))
30
30
  ) {
31
31
  return false;
32
32
  }
@@ -2,10 +2,11 @@ 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, true>;
7
+ export type BlockPacket = IPacket<typeof PacketId.BLOCK, BlockSchema> & [WorldTick];
7
8
 
8
- export const blockPacketDefinition = definePacket<typeof PacketId.BLOCK, BlockSchema, true>(
9
+ export const blockPacketDefinition = definePacket(
9
10
  PacketId.BLOCK,
10
11
  blockSchema,
11
12
  );
@@ -2,10 +2,11 @@ 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, true>;
7
+ export type BlockTypePacket = IPacket<typeof PacketId.BLOCK_TYPE, BlockTypeSchema> & [WorldTick];
7
8
 
8
- export const blockTypePacketDefinition = definePacket<typeof PacketId.BLOCK_TYPE, BlockTypeSchema, true>(
9
+ export const blockTypePacketDefinition = definePacket(
9
10
  PacketId.BLOCK_TYPE,
10
11
  blockTypeSchema,
11
12
  );
@@ -4,9 +4,9 @@ import { blockTypeRegistrySchema } from '../../schemas/BlockTypeRegistry';
4
4
  import type { BlockTypeRegistrySchema } from '../../schemas/BlockTypeRegistry';
5
5
  import type { WorldTick } from '../PacketCore';
6
6
 
7
- export type BlockTypeRegistryPacket = IPacket<typeof PacketId.BLOCK_TYPE_REGISTRY, BlockTypeRegistrySchema, true>;
7
+ export type BlockTypeRegistryPacket = IPacket<typeof PacketId.BLOCK_TYPE_REGISTRY, BlockTypeRegistrySchema> & [WorldTick];
8
8
 
9
- export const blockTypeRegistryPacketDefinition = definePacket<typeof PacketId.BLOCK_TYPE_REGISTRY, BlockTypeRegistrySchema, true>(
9
+ export const blockTypeRegistryPacketDefinition = definePacket(
10
10
  PacketId.BLOCK_TYPE_REGISTRY,
11
11
  blockTypeRegistrySchema,
12
12
  );
@@ -2,10 +2,11 @@ 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, true>;
7
+ export type ChunkPacket = IPacket<typeof PacketId.CHUNK, ChunkSchema> & [WorldTick];
7
8
 
8
- export const chunkPacketDefinition = definePacket<typeof PacketId.CHUNK, ChunkSchema, true>(
9
+ export const chunkPacketDefinition = definePacket(
9
10
  PacketId.CHUNK,
10
11
  chunkSchema,
11
12
  );
@@ -2,10 +2,11 @@ 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, true>;
7
+ export type EntityPacket = IPacket<typeof PacketId.ENTITY, EntitySchema> & [WorldTick];
7
8
 
8
- export const entityPacketDefinition = definePacket<typeof PacketId.ENTITY, EntitySchema, true>(
9
+ export const entityPacketDefinition = definePacket(
9
10
  PacketId.ENTITY,
10
11
  entitySchema,
11
12
  );
@@ -2,10 +2,11 @@ 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, true>;
7
+ export type PhysicsDebugRenderPacket = IPacket<typeof PacketId.PHYSICS_DEBUG_RENDER, PhysicsDebugRenderSchema> & [WorldTick];
7
8
 
8
- export const physicsDebugRenderPacketDefinition = definePacket<typeof PacketId.PHYSICS_DEBUG_RENDER, PhysicsDebugRenderSchema, true>(
9
+ export const physicsDebugRenderPacketDefinition = definePacket(
9
10
  PacketId.PHYSICS_DEBUG_RENDER,
10
11
  physicsDebugRenderSchema,
11
12
  );
@@ -0,0 +1,12 @@
1
+ import { definePacket, PacketId } from '../PacketCore';
2
+ import type { IPacket } from '../PacketCore';
3
+ import { simulationSchema } from '../../schemas/Simulation';
4
+ import type { SimulationSchema } from '../../schemas/Simulation';
5
+ import type { WorldTick } from '../PacketCore';
6
+
7
+ export type SimulationPacket = IPacket<typeof PacketId.SIMULATION, SimulationSchema> & [WorldTick];
8
+
9
+ export const simulationPacketDefinition = definePacket(
10
+ PacketId.SIMULATION,
11
+ simulationSchema,
12
+ );
@@ -2,10 +2,11 @@ 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, true>;
7
+ export type WorldPacket = IPacket<typeof PacketId.WORLD, WorldSchema> & [WorldTick];
7
8
 
8
- export const worldPacketDefinition = definePacket<typeof PacketId.WORLD, WorldSchema, true>(
9
+ export const worldPacketDefinition = definePacket(
9
10
  PacketId.WORLD,
10
11
  worldSchema,
11
12
  );
@@ -4,4 +4,5 @@ export * from './BlockType';
4
4
  export * from './Chunk';
5
5
  export * from './Entity';
6
6
  export * from './PhysicsDebugRender';
7
+ export * from './Simulation';
7
8
  export * from './World';
@@ -0,0 +1,20 @@
1
+ import { JSONSchemaType } from 'ajv';
2
+ import { vectorSchema } from './Vector';
3
+ import type { VectorSchema } from './Vector';
4
+
5
+ export type SimulationSchema = {
6
+ wi: number; // world id
7
+ t?: number; // tickRate (ticks per second)
8
+ g?: VectorSchema; // gravity
9
+ };
10
+
11
+ export const simulationSchema: JSONSchemaType<SimulationSchema> = {
12
+ type: 'object',
13
+ properties: {
14
+ wi: { type: 'number' },
15
+ t: { type: 'number', nullable: true },
16
+ g: { ...vectorSchema, nullable: true },
17
+ },
18
+ required: [ 'wi' ],
19
+ additionalProperties: false,
20
+ }