@momo2555/koppeliajs 0.0.162 → 0.0.164

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.
@@ -1,6 +1,10 @@
1
1
  import { Console } from "./console.js";
2
2
  import { Message, MessageType, PeerType } from "./message.js";
3
3
  import { Resident } from "./resident.js";
4
+ /**
5
+ * Represents a physical or logical device connected to the console.
6
+ * Handles device-specific commands like LEDs, vibrations, and hardware module subscriptions.
7
+ */
4
8
  export class Device {
5
9
  _address;
6
10
  _color;
@@ -8,6 +12,8 @@ export class Device {
8
12
  _console;
9
13
  _attachedEvents;
10
14
  _resident;
15
+ _callbackIds;
16
+ _eventsIds;
11
17
  isAttachedToResident;
12
18
  constructor(console, address = "") {
13
19
  this._address = address;
@@ -15,22 +21,21 @@ export class Device {
15
21
  this._name = "";
16
22
  this._console = console;
17
23
  this._attachedEvents = [];
24
+ this._callbackIds = [];
25
+ this._eventsIds = [];
18
26
  this.isAttachedToResident = false;
19
27
  }
20
- // public set color(color: Color) {
21
- // this._color = color;
22
- // // TODO : send a request to change the color
23
- // }
24
28
  get color() {
25
29
  return this._color;
26
30
  }
27
- // public set name(name: string) {
28
- // this._name = name;
29
- // // TODO : send a request to change the name
30
- // }
31
31
  get name() {
32
32
  return this._name;
33
33
  }
34
+ /**
35
+ * Subscribes to a specific hardware event for this device.
36
+ * @param eventName The name of the event to listen to.
37
+ * @param callback Function to execute when the event is triggered.
38
+ */
34
39
  onEvent(eventName, callback) {
35
40
  this._attachEvent(eventName);
36
41
  let consoleEvent = (device, from_addr, event) => {
@@ -38,35 +43,57 @@ export class Device {
38
43
  callback();
39
44
  }
40
45
  };
41
- this._console.onDeviceEvent(consoleEvent);
46
+ return this._console.onDeviceEvent(consoleEvent);
42
47
  }
48
+ /**
49
+ * Enables the cursor module and listens for coordinate updates.
50
+ * @param callback Function to execute with the incoming (x, y) coordinates.
51
+ */
43
52
  onCursor(callback) {
44
53
  this._enableModule("cursor");
45
54
  this._attachEvent("cursor");
46
- this._console.onRequest((request, params, form, address) => {
55
+ let callbackId = this._console.onRequest((request, params, form, address) => {
47
56
  if (request == "cursor" && address == this._address) {
48
57
  callback(params.x, params.y);
49
58
  }
50
59
  });
60
+ this._callbackIds.push(callbackId);
61
+ return callbackId;
51
62
  }
63
+ /**
64
+ * Enables the biking module and listens for speed updates.
65
+ * @param callback Function to execute with the incoming speed data.
66
+ */
52
67
  onBiking(callback) {
53
68
  this._enableModule("biking");
54
69
  this._attachEvent("biking");
55
- this._console.onRequest((request, params, form, address) => {
70
+ let callbackId = this._console.onRequest((request, params, form, address) => {
56
71
  if (request == "biking" && address == this._address) {
57
72
  callback(params.speed);
58
73
  }
59
74
  });
75
+ this._callbackIds.push(callbackId);
76
+ return callbackId;
60
77
  }
78
+ /**
79
+ * Enables the vertical detector module and listens for orientation updates.
80
+ * @param callback Function to execute with the boolean vertical state.
81
+ */
61
82
  onVerticalDetector(callback) {
62
83
  this._enableModule("vDetct");
63
84
  this._attachEvent("verticalDetector");
64
- this._console.onRequest((request, params, form, address) => {
85
+ let callbackId = this._console.onRequest((request, params, form, address) => {
65
86
  if (request == "verticalDetector" && address == this._address) {
66
87
  callback(params.value);
67
88
  }
68
89
  });
90
+ this._callbackIds.push(callbackId);
91
+ return callbackId;
69
92
  }
93
+ /**
94
+ * Sends a request to the device to attach a specific event listener on the hardware side.
95
+ * @param event The event name to attach.
96
+ */
70
97
  _attachEvent(event) {
71
98
  if (!(event in this._attachedEvents)) {
72
99
  let request = new Message();
@@ -77,13 +104,21 @@ export class Device {
77
104
  this._attachedEvents.push(event);
78
105
  }
79
106
  }
80
- _enableModule(moduleNmae) {
107
+ /**
108
+ * Sends a request to the device to enable a specific hardware module.
109
+ * @param moduleName The name of the module to enable (Note: retains original code spelling).
110
+ */
111
+ _enableModule(moduleName) {
81
112
  let request = new Message();
82
- request.addParam("module", moduleNmae);
113
+ request.addParam("module", moduleName);
83
114
  request.setDestination(PeerType.DEVICE, this._address);
84
115
  request.setRequest("enableModule");
85
116
  this._console.sendMessage(request);
86
117
  }
118
+ /**
119
+ * Changes the current LED color of the device.
120
+ * @param color The RGB color configuration to apply.
121
+ */
87
122
  setColor(color) {
88
123
  this._color = color;
89
124
  let request = new Message();
@@ -92,6 +127,11 @@ export class Device {
92
127
  request.setRequest("setColor");
93
128
  this._console.sendMessage(request);
94
129
  }
130
+ /**
131
+ * Sends a predefined sequence of colors to the device.
132
+ * @param sequence Array of color sequence identifiers or configurations.
133
+ * @param reset Whether to interrupt the current sequence before starting the new one.
134
+ */
95
135
  setColorSequence(sequence, reset = false) {
96
136
  let request = new Message();
97
137
  request.setDestination(PeerType.DEVICE, this._address);
@@ -101,11 +141,11 @@ export class Device {
101
141
  this._console.sendMessage(request);
102
142
  }
103
143
  /**
104
- * Vibrate the device (use the motor vibrator)
105
- * @param time
106
- * @param blink
107
- * @param blinkOff
108
- * @param blinkCount
144
+ * Triggers the device's vibration motor.
145
+ * @param time Total vibration time in milliseconds.
146
+ * @param blink Enables pulsating/intermittent vibration.
147
+ * @param blinkOff The duration of the pause between pulses in milliseconds.
148
+ * @param blinkCount The number of pulsing cycles to execute.
109
149
  */
110
150
  vibrate(time, blink = false, blinkOff = 0, blinkCount = 0) {
111
151
  let vibration = {
@@ -123,9 +163,29 @@ export class Device {
123
163
  request.setRequest("vibrate");
124
164
  this._console.sendMessage(request);
125
165
  }
166
+ clearCallback(callbackId) {
167
+ if (this._callbackIds.includes(callbackId)) {
168
+ this._console.unsubscribeCallback(callbackId);
169
+ }
170
+ }
171
+ clearAllCallbacks() {
172
+ for (let callbackId of this._callbackIds) {
173
+ this.clearCallback(callbackId);
174
+ }
175
+ }
176
+ clearEvent(eventId) {
177
+ if (this._eventsIds.includes(eventId)) {
178
+ this._console.unsubscribeCallback(eventId);
179
+ }
180
+ }
181
+ clearAllEvents() {
182
+ for (let eventId of this._eventsIds) {
183
+ this.clearCallback(eventId);
184
+ }
185
+ }
126
186
  /**
127
- * Convert this device object to a json object
128
- * @returns
187
+ * Serializes the Device object into a generic dictionary.
188
+ * @returns A plain object representing the device's current state.
129
189
  */
130
190
  toObject() {
131
191
  return {
@@ -135,8 +195,8 @@ export class Device {
135
195
  };
136
196
  }
137
197
  /**
138
- * set the device object from a json object
139
- * @param object
198
+ * Hydrates the Device instance using properties from a provided object.
199
+ * @param object The plain object containing device properties.
140
200
  */
141
201
  fromObject(object) {
142
202
  if (object.address !== undefined) {
@@ -5,6 +5,11 @@ import { Play } from "./play.js";
5
5
  import { Resident } from "./resident.js";
6
6
  import { type OptionChangedCallback } from "./option.js";
7
7
  import { Song } from "./song.js";
8
+ /**
9
+ * The main Koppelia framework entry point.
10
+ * Implements a Singleton pattern to provide global access to the console, state, stages,
11
+ * devices, and game synchronization features across the Svelte application.
12
+ */
8
13
  export declare class Koppelia {
9
14
  private _console;
10
15
  private _state;
@@ -12,114 +17,233 @@ export declare class Koppelia {
12
17
  private static _instance;
13
18
  private _option;
14
19
  private _callbacks;
15
- constructor();
20
+ private constructor();
21
+ /**
22
+ * Retrieves the singleton instance of the Koppelia class.
23
+ * Instantiates it if it does not yet exist.
24
+ */
16
25
  static get instance(): Koppelia;
26
+ /**
27
+ * Retrieves the global Svelte writable store representing the synchronized game state.
28
+ */
17
29
  get state(): Writable<AnyState>;
30
+ /**
31
+ * Merges a partial update into the current global state and broadcasts the change.
32
+ * @param stateUpdate A dictionary containing the keys/values to update.
33
+ */
18
34
  updateState(stateUpdate: AnyState): void;
35
+ /**
36
+ * Completely overwrites the global state with a new state object.
37
+ * @param newState The new state object to apply.
38
+ */
19
39
  setState(newState: AnyState): void;
40
+ /**
41
+ * Checks if the underlying WebSocket console connection is fully established.
42
+ */
20
43
  get ready(): boolean;
44
+ /**
45
+ * Enables or disables debug mode for extended console logging.
46
+ * @param enable True to enable debug logs, false to disable.
47
+ */
21
48
  setDebugMode(enable: boolean): void;
22
49
  /**
23
- * Add a callback that will be called when the connection to the console is entirely ready
24
- *
25
- * @param callback
50
+ * Registers a callback to execute when the console connection is fully ready.
51
+ * @param callback The function to execute.
26
52
  */
27
53
  onReady(callback: () => void): void;
28
54
  /**
29
- * Init the default state of the game and the list of all stages
30
- *
31
- * @param defaultState Default state that be initialized
32
- * @param stages List of all stages
55
+ * Initializes the default state and the routing stages of the game.
56
+ * Specifically executes for "monitor" peers to ensure the primary game view sets the rules.
57
+ * @param defaultState The initial state structure.
58
+ * @param stages An array of valid stage names for application routing.
33
59
  */
34
60
  init(defaultState: AnyState, stages: string[]): void;
35
61
  /**
36
- * Go to a stage, the stage must be in the stage list
37
- * Before changing the staging all callbacks will be destroyed
38
- * If the stage list is empty the console will return an error
39
- * @param stageName
62
+ * Requests a transition to a specific stage (view) across the network.
63
+ * Note: All active console event listeners will be destroyed before transition.
64
+ * @param stageName The target stage to navigate to.
40
65
  */
41
66
  goto(stageName: string): void;
67
+ getCurrentStage(): string;
68
+ /**
69
+ * Normalizes a media URL to ensure cross-client compatibility.
70
+ * @param mediaUrl The raw media URL.
71
+ * @returns The corrected URL.
72
+ */
42
73
  fixMediaUrl(mediaUrl: string): string;
74
+ /**
75
+ * Constructs the full URL for a given relative media path.
76
+ * @param path The relative media path.
77
+ * @returns The full URL string.
78
+ */
43
79
  getMediaLink(path: string): string;
44
80
  /**
45
- * Get the list of devices in a callback
46
- * @param callback
81
+ * Asynchronously fetches the list of available connected devices from the master peer.
82
+ * @returns A promise resolving to an array of instantiated Device objects.
47
83
  */
48
84
  getDevices(): Promise<Device[]>;
85
+ private _onDeviceConnNotification;
86
+ onDeviceConnectedNotification(callback: (device: Device) => void): string;
87
+ onDeviceDisconnectedNotification(callback: (device: Device) => void): string;
88
+ unsubDeviceConnectionNotification(callbackId: string): void;
49
89
  /**
50
- * Get the game ID of the current game
51
- * @returns the game id as a string
90
+ * Retrieves the unique identifier of the currently loaded game.
91
+ * @returns The game ID string provided by public environment variables.
52
92
  */
53
93
  getGameId(): string;
54
94
  /**
55
- * Get the list of plays, the plays returned by this function doesn't include the raw
56
- * You have to call a function in from the play object to download all the raw files
57
- * @param count limit of plays to get
58
- * @param index index from which to start fetching the plays
59
- * @param orderBy order by date or name
60
- * @returns the List of plays an array of objects of type Play,
95
+ * Asynchronously fetches a paginated list of Play sessions.
96
+ * Note: This only fetches metadata. Specific Play content must be downloaded via Play methods.
97
+ * @param count Maximum number of plays to retrieve (default: 10).
98
+ * @param index The starting offset index (default: 0).
99
+ * @param orderBy Sorting criteria, e.g., "date" or "name" (default: "date").
100
+ * @returns A promise resolving to an array of Play instances.
61
101
  */
62
102
  getPlays(count?: number, index?: number, orderBy?: string): Promise<Play[]>;
103
+ /**
104
+ * Asynchronously fetches the list of registered residents/players.
105
+ * @returns A promise resolving to an array of Resident instances.
106
+ */
63
107
  getResidents(): Promise<Resident[]>;
108
+ /**
109
+ * Asynchronously fetches a specific song by its unique ID.
110
+ * @param songId The ID of the song to retrieve.
111
+ * @returns A promise resolving to the corresponding Song instance.
112
+ */
64
113
  getSongById(songId: string): Promise<Song>;
114
+ /**
115
+ * Asynchronously fetches a dictionary of all songs associated with the currently active play.
116
+ * @returns A promise resolving to a map of song IDs to Song instances.
117
+ */
65
118
  getCurrentPlaySongs(): Promise<{
66
119
  [key: string]: Song;
67
120
  }>;
68
121
  /**
69
- * Get the current play that has been set
70
- * @returns the current play
122
+ * Asynchronously retrieves the currently active Play instance set on the server.
123
+ * @returns A promise resolving to the active Play.
71
124
  */
72
125
  getCurrentPlay(): Promise<Play>;
73
126
  /**
74
- * This function enables the difficulty cursor, to change the difficulty live when playing from Koppeli'App
75
- * @param callback : callback to call when difficulty changes
127
+ * Enables a live difficulty cursor, allowing difficulty changes during gameplay.
128
+ * @param callback Function to execute when the difficulty changes.
76
129
  */
77
130
  enableDifficultyCursor(callback: (difficulty: number) => void): Promise<void>;
78
131
  /**
79
- * @param id
80
- * @param onGrowChange
132
+ * Registers a new growable element on the network and listens for state changes.
133
+ * @param id The unique identifier for the growable element.
134
+ * @param onGrowChange Callback triggered when the 'grown' state of the element changes.
81
135
  */
82
136
  registerNewGrowableElement(id: string, onGrowChange: (grown: boolean) => void): Promise<void>;
83
137
  /**
84
- * @param id
85
- * @param grown
138
+ * Updates the 'grown' state of a registered growable element across the network.
139
+ * @param id The unique identifier of the element.
140
+ * @param grown True if the element is in a grown state, false otherwise.
86
141
  */
87
142
  updateGrowableElement(id: string, grown: boolean): Promise<void>;
88
143
  /**
89
- * Add a new resizable text element and define the callback that will be executed when receive and a resizable
90
- * text update notification form koppelia application
144
+ * Registers a new resizable text element with a default font size (Monitor only).
145
+ * @param id The unique identifier for the text element.
146
+ * @param defaultSize The default font size.
91
147
  */
92
148
  registerNewResizableText(id: string, defaultSize: number): Promise<void>;
149
+ /**
150
+ * Subscribes to size change notifications for a specific resizable text element.
151
+ * @param id The unique identifier of the text element.
152
+ * @param onTextResized Callback executed with the new font size.
153
+ * @returns The unique subscription ID used for unsubscribing.
154
+ */
93
155
  onResizableTextChanged(id: string, onTextResized: (newSize: number) => void): string;
156
+ /**
157
+ * Unsubscribes a previously registered resizable text listener.
158
+ * @param callbackId The subscription ID returned by onResizableTextChanged.
159
+ */
94
160
  unsubResizableText(callbackId: string): void;
161
+ /**
162
+ * Retrieves the list of all registered resizable text elements from the master.
163
+ * @returns A promise resolving to an array of resizable element data objects.
164
+ */
95
165
  getResizableTexts(): Promise<{
96
166
  [key: string]: any;
97
167
  }[]>;
168
+ /**
169
+ * Writes configuration data to the master peer, optionally binding it to the current play.
170
+ * @param config_id The unique identifier for this configuration data.
171
+ * @param config_value The dictionary containing the configuration payload.
172
+ * @param current_play Whether to bind this configuration to the active play session (default: true).
173
+ */
98
174
  writeGameConfig(config_id: string, config_value: {
99
175
  [key: string]: any;
100
176
  }, current_play?: boolean): Promise<void>;
177
+ /**
178
+ * Retrieves persistent configuration data from the master peer.
179
+ * @param config_id The unique identifier of the configuration data to fetch.
180
+ * @param current_play True to fetch data bound specifically to the active play, false otherwise.
181
+ * @returns A promise resolving to the configuration dictionary.
182
+ */
101
183
  getGameConfig(config_id: string, current_play: boolean): Promise<{
102
184
  [key: string]: any;
103
185
  }>;
104
186
  /**
105
- * @param sentence
187
+ * Broadcasts a Text-to-Speech synthesis request to the Maestro peer.
188
+ * @param sentence The text string to be spoken out loud.
106
189
  */
107
190
  say(sentence: string): void;
108
191
  /**
109
- * Set a new or edit a game option
110
- * @param name Name fo the option
111
- * @param value Value of the option
192
+ * Assigns a basic value to a registered game option via the option manager.
193
+ * @param name The unique name of the option.
194
+ * @param value The value to set.
112
195
  */
113
196
  setOption(name: string, value: any): void;
197
+ /**
198
+ * Creates or updates an interactive slider option.
199
+ * @param name The unique identifier for the slider.
200
+ * @param label The display label for the UI.
201
+ * @param value The current numeric value.
202
+ * @param min The minimum allowed value.
203
+ * @param max The maximum allowed value.
204
+ * @param step The increment step size.
205
+ */
114
206
  createSliderOtption(name: string, label: string, value: number, min: number, max: number, step: number): void;
207
+ /**
208
+ * Creates or updates an interactive toggle switch option.
209
+ * @param name The unique identifier for the switch.
210
+ * @param label The display label for the UI.
211
+ * @param value The current boolean state.
212
+ */
115
213
  createSwitchOption(name: string, label: string, value: boolean): void;
214
+ /**
215
+ * Creates or updates a multiple-choice selection option.
216
+ * @param name The unique identifier for the option.
217
+ * @param label The display label for the UI.
218
+ * @param value The currently selected choice.
219
+ * @param choices An array of available string choices.
220
+ */
116
221
  createChoicesOption(name: string, label: string, value: string, choices: string[]): void;
222
+ /**
223
+ * Registers a callback listener to trigger whenever a specific option's value changes.
224
+ * @param name The name of the option to observe.
225
+ * @param callback The function to execute on change.
226
+ */
117
227
  onOptionChanged(name: string, callback: OptionChangedCallback): void;
228
+ /**
229
+ * Executes a broadcasted network request to trigger a registered custom callback.
230
+ * @param callbackName The unique registered name of the custom callback.
231
+ * @param args Dictionary of arguments to pass to the callback.
232
+ */
118
233
  run(callbackName: string, args: {
119
234
  [key: string]: any;
120
235
  }): void;
236
+ /**
237
+ * Registers a local function to listen for network execution of a custom callback.
238
+ * @param callbackName The identifier for this callback.
239
+ * @param callback The local function to execute.
240
+ */
121
241
  on(callbackName: string, callback: (args: {
122
242
  [key: string]: any;
123
243
  }) => void): void;
244
+ /**
245
+ * Unregisters a locally listening custom callback.
246
+ * @param callbackName The identifier of the callback to remove.
247
+ */
124
248
  unsub(callbackName: string): void;
125
249
  }