@hytopia.com/server-protocol 1.0.1 → 1.0.3

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/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # HYTOPIA Server Protcol
2
+
3
+ This repo contains all of the packet and payload schemas defined by the HYTOPIA server's protocol.
package/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ import * as inboundPackets from './packets/inbound';
2
+ import * as outboundPackets from './packets/outbound';
3
+
4
+
5
+ export { inboundPackets, outboundPackets };
6
+ export * from './schemas';
7
+ export * from './packets/PacketCore';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hytopia.com/server-protocol",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -17,7 +17,7 @@ import type { JSONSchemaType, ValidateFunction } from 'ajv';
17
17
  * in encoded format.
18
18
  */
19
19
 
20
- export enum PacketType {
20
+ export enum PacketIds {
21
21
  // Standard Inbound Packet Types: 0 - 31 range
22
22
  HEARTBEAT = 0,
23
23
  INPUT = 1,
@@ -40,28 +40,37 @@ export enum PacketType {
40
40
  * Generators for packet definitions and validators.
41
41
  */
42
42
 
43
- export interface IPacketDefinition<TId extends PacketType, TPayload> {
43
+ export interface IPacketDefinition<TId extends PacketIds, TSchema> {
44
44
  id: TId;
45
- schema: JSONSchemaType<TPayload>;
46
- validate: ValidateFunction<TPayload>;
45
+ schema: JSONSchemaType<TSchema>;
46
+ validate: ValidateFunction<TSchema>;
47
47
  }
48
48
 
49
- export interface IPacket<TId extends PacketType, TPayload> {
49
+ export interface IPacket<TId extends PacketIds, TSchema> {
50
50
  id: TId;
51
- payload: TPayload;
51
+ payload: TSchema;
52
52
  }
53
53
 
54
- export const definePacket = <TId extends PacketType, TPayload>(
54
+ export type AnyPacket = IPacket<PacketIds, unknown>;
55
+
56
+ export type AnyPacketDefinition = IPacketDefinition<number, unknown>;
57
+
58
+ export const createPacket = <TId extends number, TSchema>(
59
+ packetDef: IPacketDefinition<TId, TSchema>,
60
+ payload: TSchema,
61
+ ): IPacket<TId, TSchema> => {
62
+ if (!packetDef.validate(payload)) {
63
+ throw new Error(`createPacket(): Invalid payload for packet with id ${packetDef.id}, error: ${Ajv.instance.errorsText(packetDef.validate.errors)}`);
64
+ }
65
+
66
+ return { id: packetDef.id, payload };
67
+ }
68
+
69
+ export const definePacket = <TId extends PacketIds, TSchema>(
55
70
  id: TId,
56
- schema: JSONSchemaType<TPayload>,
57
- ): IPacketDefinition<TId, TPayload> => ({
71
+ schema: JSONSchemaType<TSchema>,
72
+ ): IPacketDefinition<TId, TSchema> => ({
58
73
  id,
59
74
  schema,
60
75
  validate: Ajv.instance.compile(schema),
61
76
  });
62
-
63
- // Represents any packet type
64
- export type AnyPacket = IPacket<PacketType, unknown>;
65
-
66
- // Represents any packet definition type
67
- export type AnyPacketDefinition = IPacketDefinition<number, unknown>;
@@ -1,11 +1,11 @@
1
- import { definePacket, PacketType } from '../PacketTypes';
2
- import type { IPacket } from '../PacketTypes';
1
+ import { definePacket, PacketIds } from '../PacketCore';
2
+ import type { IPacket } from '../PacketCore';
3
3
  import { heartbeatSchema } from '../../schemas/Heartbeat';
4
4
  import type { Heartbeat } from '../../schemas/Heartbeat';
5
5
 
6
- export type HeartbeatPacket = IPacket<typeof PacketType.HEARTBEAT, Heartbeat>;
6
+ export type HeartbeatPacket = IPacket<typeof PacketIds.HEARTBEAT, Heartbeat>;
7
7
 
8
8
  export const heartbeatPacketDefinition = definePacket(
9
- PacketType.HEARTBEAT,
9
+ PacketIds.HEARTBEAT,
10
10
  heartbeatSchema
11
11
  );
@@ -1,11 +1,11 @@
1
- import { definePacket, PacketType } from '../PacketTypes';
2
- import type { IPacket } from '../PacketTypes';
1
+ import { definePacket, PacketIds } from '../PacketCore';
2
+ import type { IPacket } from '../PacketCore';
3
3
  import { inputSchema } from '../../schemas/Input';
4
4
  import type { Input } from '../../schemas/Input';
5
5
 
6
- export type InputPacket = IPacket<typeof PacketType.INPUT, Input>;
6
+ export type InputPacket = IPacket<typeof PacketIds.INPUT, Input>;
7
7
 
8
8
  export const inputPacketDefinition = definePacket(
9
- PacketType.INPUT,
9
+ PacketIds.INPUT,
10
10
  inputSchema
11
11
  );
@@ -0,0 +1,2 @@
1
+ export * from './Heartbeat';
2
+ export * from './Input';
@@ -1,11 +1,11 @@
1
- import { definePacket, PacketType } from '../PacketTypes';
2
- import type { IPacket } from '../PacketTypes';
1
+ import { definePacket, PacketIds } from '../PacketCore';
2
+ import type { IPacket } from '../PacketCore';
3
3
  import { blockSchema } from '../../schemas/Block';
4
4
  import type { Block } from '../../schemas/Block';
5
5
 
6
- export type BlockPacket = IPacket<typeof PacketType.BLOCK, Block>;
6
+ export type BlockPacket = IPacket<typeof PacketIds.BLOCK, Block>;
7
7
 
8
8
  export const blockPacketDefinition = definePacket(
9
- PacketType.BLOCK,
9
+ PacketIds.BLOCK,
10
10
  blockSchema,
11
11
  );
@@ -1,11 +1,11 @@
1
- import { definePacket, PacketType } from '../PacketTypes';
2
- import type { IPacket } from '../PacketTypes';
1
+ import { definePacket, PacketIds } from '../PacketCore';
2
+ import type { IPacket } from '../PacketCore';
3
3
  import { blockRegistrySchema } from '../../schemas/BlockRegistry';
4
4
  import type { BlockRegistry } from '../../schemas/BlockRegistry';
5
5
 
6
- export type BlockRegistryPacket = IPacket<typeof PacketType.BLOCK_REGISTRY, BlockRegistry>;
6
+ export type BlockRegistryPacket = IPacket<typeof PacketIds.BLOCK_REGISTRY, BlockRegistry>;
7
7
 
8
8
  export const blockRegistryPacketDefinition = definePacket(
9
- PacketType.BLOCK_REGISTRY,
9
+ PacketIds.BLOCK_REGISTRY,
10
10
  blockRegistrySchema,
11
11
  );
@@ -1,11 +1,11 @@
1
- import { definePacket, PacketType } from '../PacketTypes';
2
- import type { IPacket } from '../PacketTypes';
1
+ import { definePacket, PacketIds } from '../PacketCore';
2
+ import type { IPacket } from '../PacketCore';
3
3
  import { blockTypeSchema } from '../../schemas/BlockType';
4
4
  import type { BlockType } from '../../schemas/BlockType';
5
5
 
6
- export type BlockTypePacket = IPacket<typeof PacketType.BLOCK_TYPE, BlockType>;
6
+ export type BlockTypePacket = IPacket<typeof PacketIds.BLOCK_TYPE, BlockType>;
7
7
 
8
8
  export const blockTypePacketDefinition = definePacket(
9
- PacketType.BLOCK_TYPE,
9
+ PacketIds.BLOCK_TYPE,
10
10
  blockTypeSchema,
11
11
  );
@@ -1,11 +1,11 @@
1
- import { definePacket, PacketType } from '../PacketTypes';
2
- import type { IPacket } from '../PacketTypes';
1
+ import { definePacket, PacketIds } from '../PacketCore';
2
+ import type { IPacket } from '../PacketCore';
3
3
  import { chunkSchema } from '../../schemas/Chunk';
4
4
  import type { Chunk } from '../../schemas/Chunk';
5
5
 
6
- export type ChunkPacket = IPacket<typeof PacketType.CHUNK, Chunk>;
6
+ export type ChunkPacket = IPacket<typeof PacketIds.CHUNK, Chunk>;
7
7
 
8
8
  export const chunkPacketDefinition = definePacket(
9
- PacketType.CHUNK,
9
+ PacketIds.CHUNK,
10
10
  chunkSchema,
11
11
  );
@@ -1,11 +1,11 @@
1
- import { definePacket, PacketType } from '../PacketTypes';
2
- import type { IPacket } from '../PacketTypes';
1
+ import { definePacket, PacketIds } from '../PacketCore';
2
+ import type { IPacket } from '../PacketCore';
3
3
  import { entitySchema } from '../../schemas/Entity';
4
4
  import type { Entity } from '../../schemas/Entity';
5
5
 
6
- export type EntityPacket = IPacket<typeof PacketType.ENTITY, Entity>;
6
+ export type EntityPacket = IPacket<typeof PacketIds.ENTITY, Entity>;
7
7
 
8
8
  export const entityPacketDefinition = definePacket(
9
- PacketType.ENTITY,
9
+ PacketIds.ENTITY,
10
10
  entitySchema,
11
11
  );
@@ -1,11 +1,11 @@
1
- import { definePacket, PacketType } from '../PacketTypes';
2
- import type { IPacket } from '../PacketTypes';
1
+ import { definePacket, PacketIds } from '../PacketCore';
2
+ import type { IPacket } from '../PacketCore';
3
3
  import { physicsDebugRenderSchema } from '../../schemas/PhysicsDebugRender';
4
4
  import type { PhysicsDebugRender } from '../../schemas/PhysicsDebugRender';
5
5
 
6
- export type PhysicsDebugRenderPacket = IPacket<typeof PacketType.PHYSICS_DEBUG_RENDER, PhysicsDebugRender>;
6
+ export type PhysicsDebugRenderPacket = IPacket<typeof PacketIds.PHYSICS_DEBUG_RENDER, PhysicsDebugRender>;
7
7
 
8
8
  export const physicsDebugRenderPacketDefinition = definePacket(
9
- PacketType.PHYSICS_DEBUG_RENDER,
9
+ PacketIds.PHYSICS_DEBUG_RENDER,
10
10
  physicsDebugRenderSchema,
11
11
  );
@@ -0,0 +1,6 @@
1
+ export * from './Block';
2
+ export * from './BlockRegistry';
3
+ export * from './BlockType';
4
+ export * from './Chunk';
5
+ export * from './Entity';
6
+ export * from './PhysicsDebugRender';
@@ -0,0 +1,16 @@
1
+ export * from './Block';
2
+ export * from './BlockRegistry';
3
+ export * from './BlockType';
4
+ export * from './Chunk';
5
+ export * from './Collider';
6
+ export * from './ColliderDesc';
7
+ export * from './Entity';
8
+ export * from './Heartbeat';
9
+ export * from './Input';
10
+ export * from './PhysicsDebugRender';
11
+ export * from './Quaternion';
12
+ export * from './RigidBody';
13
+ export * from './RigidBodyDesc';
14
+ export * from './Shape';
15
+ export * from './Vector';
16
+ export * from './VectorBoolean';
package/packets/index.ts DELETED
@@ -1,26 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
-
4
- const inboundPackets: Record<string, any> = {};
5
- const outboundPackets: Record<string, any> = {};
6
-
7
- const importPackets = async () => {
8
- for (const directory of ['inbound', 'outbound']) {
9
- const directoryPath = path.join(__dirname, directory);
10
- const files = fs.readdirSync(directoryPath);
11
-
12
- for (const file of files) {
13
- if (file.endsWith('.ts')) {
14
- const name = path.basename(file, '.ts');
15
- const module = await import(`./${directory}/${name}`);
16
- const typePackets = directory === 'inbound' ? inboundPackets : outboundPackets;
17
- const lcfName = name.charAt(0).toLowerCase() + name.slice(1);
18
- typePackets[`${lcfName}PacketDefinition`] = module[`${lcfName}PacketDefinition`];
19
- }
20
- }
21
- }
22
- };
23
-
24
- await importPackets();
25
-
26
- export default { inboundPackets, outboundPackets };