@caperjs/plugin-colyseus 0.1.0

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/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { ColyseusPlugin } from './ColyseusPlugin';
2
+ export default ColyseusPlugin;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,eAAe,cAAc,CAAC"}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@caperjs/plugin-colyseus",
3
+ "version": "0.1.0",
4
+ "description": "Colyseus",
5
+ "files": [
6
+ "lib",
7
+ "src"
8
+ ],
9
+ "main": "lib/caper-plugin-colyseus.mjs",
10
+ "types": "lib/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "default": "./lib/caper-plugin-colyseus.mjs",
14
+ "types": "./lib/index.d.ts"
15
+ }
16
+ },
17
+ "sideEffects": false,
18
+ "keywords": [
19
+ "colyseus",
20
+ "colyseus.js",
21
+ "@caperjs/core",
22
+ "caper"
23
+ ],
24
+ "author": "",
25
+ "license": "MIT",
26
+ "devDependencies": {
27
+ "vite": "^8.0.7",
28
+ "vite-plugin-dts": "^4.5.3",
29
+ "@caperjs/core": "0.1.0"
30
+ },
31
+ "peerDependencies": {
32
+ "@caperjs/core": "0.1.0"
33
+ },
34
+ "dependencies": {
35
+ "@colyseus/schema": "^2.0.34",
36
+ "colyseus.js": "^0.15.24"
37
+ },
38
+ "scripts": {
39
+ "build": "vite build",
40
+ "test": "echo \"Error: no test specified\" && exit 1"
41
+ }
42
+ }
@@ -0,0 +1,84 @@
1
+ import { Schema } from '@colyseus/schema';
2
+ import * as Colyseus from 'colyseus.js';
3
+ import { Room } from 'colyseus.js';
4
+ import type { IApplication, IPlugin } from '@caperjs/core';
5
+ import { Logger, Plugin } from '@caperjs/core';
6
+
7
+ export type SchemaConstructor<T = Schema> = new (...args: any[]) => T;
8
+
9
+ export type ColyseusPluginOptions = {
10
+ port: string;
11
+ };
12
+
13
+ export interface IColyseusPlugin extends IPlugin<ColyseusPluginOptions> {
14
+ readonly client: Colyseus.Client;
15
+
16
+ initialize(options?: Partial<ColyseusPluginOptions>, app?: IApplication): void;
17
+
18
+ joinOrCreate<T>(
19
+ roomName: string,
20
+ options?: Colyseus.JoinOptions,
21
+ rootSchema?: SchemaConstructor<T>,
22
+ ): Promise<Room<T>>;
23
+
24
+ create<T>(roomName: string, options?: Colyseus.JoinOptions, rootSchema?: SchemaConstructor<T>): Promise<Room<T>>;
25
+
26
+ join<T>(roomName: string, options?: Colyseus.JoinOptions, rootSchema?: SchemaConstructor<T>): Promise<Room<T>>;
27
+
28
+ joinById<T>(roomId: string, options?: Colyseus.JoinOptions, rootSchema?: SchemaConstructor<T>): Promise<Room<T>>;
29
+ }
30
+
31
+ export class ColyseusPlugin extends Plugin<ColyseusPluginOptions> implements IColyseusPlugin {
32
+ private _client: Colyseus.Client;
33
+
34
+ get client(): Colyseus.Client {
35
+ return this._client;
36
+ }
37
+
38
+ async initialize(options?: Partial<ColyseusPluginOptions>, app?: IApplication) {
39
+ if (!app) {
40
+ throw new Error('Application instance is required for ColyseusPlugin initialization.');
41
+ }
42
+ this._options = {
43
+ port: app.env.VITE_COLYSEUS_PORT || app.env.COLYSEUS_PORT || '2567',
44
+ ...(options || {}),
45
+ };
46
+ Logger.log(`Colyseus plugin initialized`);
47
+
48
+ if (!this._client) {
49
+ this._client = new Colyseus.Client(`ws://localhost:${this.options.port}`);
50
+ }
51
+ }
52
+
53
+ async joinOrCreate<T = any>(
54
+ roomName: string,
55
+ options?: Colyseus.JoinOptions,
56
+ rootSchema?: SchemaConstructor<T>,
57
+ ): Promise<Room<T>> {
58
+ return this._client.joinOrCreate<T>(roomName, options, rootSchema);
59
+ }
60
+
61
+ async join<T = any>(
62
+ roomName: string,
63
+ options?: Colyseus.JoinOptions,
64
+ rootSchema?: SchemaConstructor<T>,
65
+ ): Promise<Room<T>> {
66
+ return this._client.join<T>(roomName, options, rootSchema);
67
+ }
68
+
69
+ async joinById<T = any>(
70
+ roomId: string,
71
+ options?: Colyseus.JoinOptions,
72
+ rootSchema?: SchemaConstructor<T>,
73
+ ): Promise<Room<T>> {
74
+ return this._client.joinById<T>(roomId, options, rootSchema);
75
+ }
76
+
77
+ async create<T = any>(
78
+ roomName: string,
79
+ options?: Colyseus.JoinOptions,
80
+ rootSchema?: SchemaConstructor<T>,
81
+ ): Promise<Room<T>> {
82
+ return this._client.joinById<T>(roomName, options, rootSchema);
83
+ }
84
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { ColyseusPlugin } from './ColyseusPlugin';
2
+
3
+ export default ColyseusPlugin;