@momo2555/koppeliajs 0.0.162 → 0.0.163

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.
@@ -12,6 +12,11 @@ import { Option } from "./option.js";
12
12
  import { logger, setDebugMode } from "./logger.js";
13
13
  import { CustomCallbacks } from "./customCallback.js";
14
14
  import { Song } from "./song.js";
15
+ /**
16
+ * The main Koppelia framework entry point.
17
+ * Implements a Singleton pattern to provide global access to the console, state, stages,
18
+ * devices, and game synchronization features across the Svelte application.
19
+ */
15
20
  export class Koppelia {
16
21
  _console;
17
22
  _state;
@@ -40,78 +45,105 @@ export class Koppelia {
40
45
  this._stage = new Stage(this._console);
41
46
  this._option = new Option(this._console);
42
47
  }
48
+ /**
49
+ * Retrieves the singleton instance of the Koppelia class.
50
+ * Instantiates it if it does not yet exist.
51
+ */
43
52
  static get instance() {
44
53
  if (!Koppelia._instance) {
45
54
  Koppelia._instance = new Koppelia();
46
55
  }
47
56
  return Koppelia._instance;
48
57
  }
58
+ /**
59
+ * Retrieves the global Svelte writable store representing the synchronized game state.
60
+ */
49
61
  get state() {
50
62
  return this._state.state;
51
63
  }
64
+ /**
65
+ * Merges a partial update into the current global state and broadcasts the change.
66
+ * @param stateUpdate A dictionary containing the keys/values to update.
67
+ */
52
68
  updateState(stateUpdate) {
53
69
  this._state.updateState(stateUpdate);
54
70
  }
71
+ /**
72
+ * Completely overwrites the global state with a new state object.
73
+ * @param newState The new state object to apply.
74
+ */
55
75
  setState(newState) {
56
76
  this._state.setState(newState);
57
77
  }
78
+ /**
79
+ * Checks if the underlying WebSocket console connection is fully established.
80
+ */
58
81
  get ready() {
59
82
  return this._console.ready;
60
83
  }
84
+ /**
85
+ * Enables or disables debug mode for extended console logging.
86
+ * @param enable True to enable debug logs, false to disable.
87
+ */
61
88
  setDebugMode(enable) {
62
89
  setDebugMode(enable);
63
90
  }
64
91
  /**
65
- * Add a callback that will be called when the connection to the console is entirely ready
66
- *
67
- * @param callback
92
+ * Registers a callback to execute when the console connection is fully ready.
93
+ * @param callback The function to execute.
68
94
  */
69
95
  onReady(callback) {
70
96
  this._console.onReady(callback);
71
97
  }
72
98
  /**
73
- * Init the default state of the game and the list of all stages
74
- *
75
- * @param defaultState Default state that be initialized
76
- * @param stages List of all stages
99
+ * Initializes the default state and the routing stages of the game.
100
+ * Specifically executes for "monitor" peers to ensure the primary game view sets the rules.
101
+ * @param defaultState The initial state structure.
102
+ * @param stages An array of valid stage names for application routing.
77
103
  */
78
104
  init(defaultState, stages) {
79
105
  this._console.onReady(() => {
80
106
  let type = get(routeType);
81
107
  if (type == "monitor") {
82
- this._state.setState(defaultState, true); // set the state
83
- this._stage.initStages(stages); // init the list of stages
108
+ this._state.setState(defaultState, true);
109
+ this._stage.initStages(stages);
84
110
  }
85
111
  });
86
112
  }
87
113
  /**
88
- * Go to a stage, the stage must be in the stage list
89
- * Before changing the staging all callbacks will be destroyed
90
- * If the stage list is empty the console will return an error
91
- * @param stageName
114
+ * Requests a transition to a specific stage (view) across the network.
115
+ * Note: All active console event listeners will be destroyed before transition.
116
+ * @param stageName The target stage to navigate to.
92
117
  */
93
118
  goto(stageName) {
94
119
  this._stage.goto(stageName);
95
120
  }
121
+ /**
122
+ * Normalizes a media URL to ensure cross-client compatibility.
123
+ * @param mediaUrl The raw media URL.
124
+ * @returns The corrected URL.
125
+ */
96
126
  fixMediaUrl(mediaUrl) {
97
127
  return this._console.fixMediaUrl(mediaUrl);
98
128
  }
129
+ /**
130
+ * Constructs the full URL for a given relative media path.
131
+ * @param path The relative media path.
132
+ * @returns The full URL string.
133
+ */
99
134
  getMediaLink(path) {
100
135
  return this._console.getMediaUrl(path);
101
136
  }
102
137
  /**
103
- * Get the list of devices in a callback
104
- * @param callback
138
+ * Asynchronously fetches the list of available connected devices from the master peer.
139
+ * @returns A promise resolving to an array of instantiated Device objects.
105
140
  */
106
141
  async getDevices() {
107
142
  return new Promise((resolve, reject) => {
108
- // create the message to request the devices
109
143
  let getDevicesRequest = new Message();
110
144
  getDevicesRequest.setRequest("getDevices");
111
145
  getDevicesRequest.setDestination(PeerType.MASTER, "");
112
- // send the message to the console
113
146
  this._console.sendMessage(getDevicesRequest, (response) => {
114
- // convert the response to al list of device objects
115
147
  let devices_raw = response.getParam("devices", []);
116
148
  let devices = [];
117
149
  for (let device_raw of devices_raw) {
@@ -124,19 +156,19 @@ export class Koppelia {
124
156
  });
125
157
  }
126
158
  /**
127
- * Get the game ID of the current game
128
- * @returns the game id as a string
159
+ * Retrieves the unique identifier of the currently loaded game.
160
+ * @returns The game ID string provided by public environment variables.
129
161
  */
130
162
  getGameId() {
131
163
  return PUBLIC_GAME_ID;
132
164
  }
133
165
  /**
134
- * Get the list of plays, the plays returned by this function doesn't include the raw
135
- * You have to call a function in from the play object to download all the raw files
136
- * @param count limit of plays to get
137
- * @param index index from which to start fetching the plays
138
- * @param orderBy order by date or name
139
- * @returns the List of plays an array of objects of type Play,
166
+ * Asynchronously fetches a paginated list of Play sessions.
167
+ * Note: This only fetches metadata. Specific Play content must be downloaded via Play methods.
168
+ * @param count Maximum number of plays to retrieve (default: 10).
169
+ * @param index The starting offset index (default: 0).
170
+ * @param orderBy Sorting criteria, e.g., "date" or "name" (default: "date").
171
+ * @returns A promise resolving to an array of Play instances.
140
172
  */
141
173
  async getPlays(count = 10, index = 0, orderBy = "date") {
142
174
  return new Promise((resolve, reject) => {
@@ -157,6 +189,10 @@ export class Koppelia {
157
189
  });
158
190
  });
159
191
  }
192
+ /**
193
+ * Asynchronously fetches the list of registered residents/players.
194
+ * @returns A promise resolving to an array of Resident instances.
195
+ */
160
196
  async getResidents() {
161
197
  return new Promise((resolve, reject) => {
162
198
  let getResidentsRequest = new Message();
@@ -175,6 +211,11 @@ export class Koppelia {
175
211
  });
176
212
  });
177
213
  }
214
+ /**
215
+ * Asynchronously fetches a specific song by its unique ID.
216
+ * @param songId The ID of the song to retrieve.
217
+ * @returns A promise resolving to the corresponding Song instance.
218
+ */
178
219
  async getSongById(songId) {
179
220
  return new Promise((resolve, reject) => {
180
221
  let getSongRequest = new Message();
@@ -190,6 +231,10 @@ export class Koppelia {
190
231
  });
191
232
  });
192
233
  }
234
+ /**
235
+ * Asynchronously fetches a dictionary of all songs associated with the currently active play.
236
+ * @returns A promise resolving to a map of song IDs to Song instances.
237
+ */
193
238
  getCurrentPlaySongs() {
194
239
  return new Promise((resolve, reject) => {
195
240
  let getSongRequest = new Message();
@@ -209,8 +254,8 @@ export class Koppelia {
209
254
  });
210
255
  }
211
256
  /**
212
- * Get the current play that has been set
213
- * @returns the current play
257
+ * Asynchronously retrieves the currently active Play instance set on the server.
258
+ * @returns A promise resolving to the active Play.
214
259
  */
215
260
  async getCurrentPlay() {
216
261
  return new Promise((resolve, reject) => {
@@ -226,24 +271,23 @@ export class Koppelia {
226
271
  });
227
272
  }
228
273
  /**
229
- * This function enables the difficulty cursor, to change the difficulty live when playing from Koppeli'App
230
- * @param callback : callback to call when difficulty changes
274
+ * Enables a live difficulty cursor, allowing difficulty changes during gameplay.
275
+ * @param callback Function to execute when the difficulty changes.
231
276
  */
232
277
  async enableDifficultyCursor(callback) {
233
278
  }
234
279
  /**
235
- * @param id
236
- * @param onGrowChange
280
+ * Registers a new growable element on the network and listens for state changes.
281
+ * @param id The unique identifier for the growable element.
282
+ * @param onGrowChange Callback triggered when the 'grown' state of the element changes.
237
283
  */
238
284
  async registerNewGrowableElement(id, onGrowChange) {
239
285
  return new Promise((resolve, reject) => {
240
- // create the message to request the devices
241
286
  if (get(routeType) == "controller") {
242
287
  let addGrowableElRequest = new Message();
243
288
  addGrowableElRequest.setRequest("addGrowableElement");
244
289
  addGrowableElRequest.addParam("id", id);
245
290
  addGrowableElRequest.setDestination(PeerType.MASTER, "");
246
- // send the message to the console (only the controller sends the )
247
291
  this._console.sendMessage(addGrowableElRequest, (response) => {
248
292
  });
249
293
  }
@@ -261,26 +305,26 @@ export class Koppelia {
261
305
  });
262
306
  }
263
307
  /**
264
- * @param id
265
- * @param grown
308
+ * Updates the 'grown' state of a registered growable element across the network.
309
+ * @param id The unique identifier of the element.
310
+ * @param grown True if the element is in a grown state, false otherwise.
266
311
  */
267
312
  async updateGrowableElement(id, grown) {
268
313
  return new Promise((resolve, reject) => {
269
- // create the message to request the devices
270
314
  let addGrowableElRequest = new Message();
271
315
  addGrowableElRequest.setRequest("updateGrowableElement");
272
316
  addGrowableElRequest.addParam("id", id);
273
317
  addGrowableElRequest.addParam("grown", grown);
274
318
  addGrowableElRequest.setDestination(PeerType.MASTER, "");
275
- // send the message to the console
276
319
  this._console.sendMessage(addGrowableElRequest, (response) => {
277
320
  resolve();
278
321
  });
279
322
  });
280
323
  }
281
324
  /**
282
- * Add a new resizable text element and define the callback that will be executed when receive and a resizable
283
- * text update notification form koppelia application
325
+ * Registers a new resizable text element with a default font size (Monitor only).
326
+ * @param id The unique identifier for the text element.
327
+ * @param defaultSize The default font size.
284
328
  */
285
329
  async registerNewResizableText(id, defaultSize) {
286
330
  return new Promise((resolve, reject) => {
@@ -290,13 +334,18 @@ export class Koppelia {
290
334
  addGrowableElRequest.addParam("id", id);
291
335
  addGrowableElRequest.addParam("defaultSize", defaultSize);
292
336
  addGrowableElRequest.setDestination(PeerType.MASTER, "");
293
- // send the message to the console (only the controller sends the )
294
337
  this._console.sendMessage(addGrowableElRequest, (response) => {
295
338
  });
296
339
  }
297
340
  resolve();
298
341
  });
299
342
  }
343
+ /**
344
+ * Subscribes to size change notifications for a specific resizable text element.
345
+ * @param id The unique identifier of the text element.
346
+ * @param onTextResized Callback executed with the new font size.
347
+ * @returns The unique subscription ID used for unsubscribing.
348
+ */
300
349
  onResizableTextChanged(id, onTextResized) {
301
350
  return this._console.onRequest((req, params) => {
302
351
  if (req == "resizableTextNotification") {
@@ -308,21 +357,34 @@ export class Koppelia {
308
357
  }
309
358
  });
310
359
  }
360
+ /**
361
+ * Unsubscribes a previously registered resizable text listener.
362
+ * @param callbackId The subscription ID returned by onResizableTextChanged.
363
+ */
311
364
  unsubResizableText(callbackId) {
312
365
  this._console.unsubscribeCallback(callbackId);
313
366
  }
367
+ /**
368
+ * Retrieves the list of all registered resizable text elements from the master.
369
+ * @returns A promise resolving to an array of resizable element data objects.
370
+ */
314
371
  async getResizableTexts() {
315
372
  return new Promise((resolve, reject) => {
316
373
  let addGrowableElRequest = new Message();
317
374
  addGrowableElRequest.setRequest("getResizableTexts");
318
375
  addGrowableElRequest.setDestination(PeerType.MASTER, "");
319
- // send the message to the console (only the controller sends the )
320
376
  this._console.sendMessage(addGrowableElRequest, (response) => {
321
377
  let resizables = response.getParam("resizableTexts", []);
322
378
  resolve(resizables);
323
379
  });
324
380
  });
325
381
  }
382
+ /**
383
+ * Writes configuration data to the master peer, optionally binding it to the current play.
384
+ * @param config_id The unique identifier for this configuration data.
385
+ * @param config_value The dictionary containing the configuration payload.
386
+ * @param current_play Whether to bind this configuration to the active play session (default: true).
387
+ */
326
388
  async writeGameConfig(config_id, config_value, current_play = true) {
327
389
  let setGameConfigRequest = new Message();
328
390
  setGameConfigRequest.setRequest("setGameData");
@@ -332,6 +394,12 @@ export class Koppelia {
332
394
  setGameConfigRequest.setDestination(PeerType.MASTER, "");
333
395
  this._console.sendMessage(setGameConfigRequest);
334
396
  }
397
+ /**
398
+ * Retrieves persistent configuration data from the master peer.
399
+ * @param config_id The unique identifier of the configuration data to fetch.
400
+ * @param current_play True to fetch data bound specifically to the active play, false otherwise.
401
+ * @returns A promise resolving to the configuration dictionary.
402
+ */
335
403
  async getGameConfig(config_id, current_play) {
336
404
  return new Promise((resolve, reject) => {
337
405
  let getGameConfigRequest = new Message();
@@ -346,25 +414,33 @@ export class Koppelia {
346
414
  });
347
415
  }
348
416
  /**
349
- * @param sentence
417
+ * Broadcasts a Text-to-Speech synthesis request to the Maestro peer.
418
+ * @param sentence The text string to be spoken out loud.
350
419
  */
351
420
  say(sentence) {
352
- // create the message to request the devices
353
421
  let sayRequest = new Message();
354
422
  sayRequest.setRequest("sayRequest");
355
423
  sayRequest.addParam("sentence", sentence);
356
424
  sayRequest.setDestination(PeerType.MAESTRO, "");
357
- // send the message to the console
358
425
  this._console.sendMessage(sayRequest);
359
426
  }
360
427
  /**
361
- * Set a new or edit a game option
362
- * @param name Name fo the option
363
- * @param value Value of the option
428
+ * Assigns a basic value to a registered game option via the option manager.
429
+ * @param name The unique name of the option.
430
+ * @param value The value to set.
364
431
  */
365
432
  setOption(name, value) {
366
433
  this._option.setOption(name, value, null, {});
367
434
  }
435
+ /**
436
+ * Creates or updates an interactive slider option.
437
+ * @param name The unique identifier for the slider.
438
+ * @param label The display label for the UI.
439
+ * @param value The current numeric value.
440
+ * @param min The minimum allowed value.
441
+ * @param max The maximum allowed value.
442
+ * @param step The increment step size.
443
+ */
368
444
  createSliderOtption(name, label, value, min, max, step) {
369
445
  this._option.setOption(name, value, "slider", {
370
446
  "min": min,
@@ -373,26 +449,58 @@ export class Koppelia {
373
449
  "label": label,
374
450
  });
375
451
  }
452
+ /**
453
+ * Creates or updates an interactive toggle switch option.
454
+ * @param name The unique identifier for the switch.
455
+ * @param label The display label for the UI.
456
+ * @param value The current boolean state.
457
+ */
376
458
  createSwitchOption(name, label, value) {
377
459
  this._option.setOption(name, value, "switch", {
378
460
  "label": label,
379
461
  });
380
462
  }
463
+ /**
464
+ * Creates or updates a multiple-choice selection option.
465
+ * @param name The unique identifier for the option.
466
+ * @param label The display label for the UI.
467
+ * @param value The currently selected choice.
468
+ * @param choices An array of available string choices.
469
+ */
381
470
  createChoicesOption(name, label, value, choices) {
382
471
  this._option.setOption(name, value, "choices", {
383
472
  "choices": choices,
384
473
  "label": label,
385
474
  });
386
475
  }
476
+ /**
477
+ * Registers a callback listener to trigger whenever a specific option's value changes.
478
+ * @param name The name of the option to observe.
479
+ * @param callback The function to execute on change.
480
+ */
387
481
  onOptionChanged(name, callback) {
388
482
  this._option.onOptionChanged(name, callback);
389
483
  }
484
+ /**
485
+ * Executes a broadcasted network request to trigger a registered custom callback.
486
+ * @param callbackName The unique registered name of the custom callback.
487
+ * @param args Dictionary of arguments to pass to the callback.
488
+ */
390
489
  run(callbackName, args) {
391
490
  this._callbacks.runCustomCallback(callbackName, args);
392
491
  }
492
+ /**
493
+ * Registers a local function to listen for network execution of a custom callback.
494
+ * @param callbackName The identifier for this callback.
495
+ * @param callback The local function to execute.
496
+ */
393
497
  on(callbackName, callback) {
394
498
  this._callbacks.registerCustomCallback(callbackName, callback);
395
499
  }
500
+ /**
501
+ * Unregisters a locally listening custom callback.
502
+ * @param callbackName The identifier of the callback to remove.
503
+ */
396
504
  unsub(callbackName) {
397
505
  this._callbacks.unregisterCustomCallback(callbackName);
398
506
  }
@@ -12,62 +12,67 @@ export declare class KoppeliaWebsocket {
12
12
  websocketUrl: string;
13
13
  openCallback?: () => void;
14
14
  /**
15
- * Constructor of the Koppelia websocket class
16
- * @param websocketUrl
17
- * @param timeout
15
+ * Initializes the Koppelia WebSocket client.
16
+ * @param websocketUrl The URL of the WebSocket server to connect to.
17
+ * @param timeout The maximum time in milliseconds to wait for a response before timing out a request. Defaults to 20000ms.
18
18
  */
19
19
  constructor(websocketUrl: string, timeout?: number);
20
20
  /**
21
- * Send a new request: Add a request Id to the request and add it to the ongoing requests
22
- * Add an interval to timeout when there is no response after a certain amount of time
23
- * @param data
24
- * @param callback
21
+ * Sends a new message request through the WebSocket.
22
+ * Automatically generates a request ID, registers the request as ongoing, and sets a timeout.
23
+ * @param data The message payload to send.
24
+ * @param callback Optional callback to be executed when a response to this specific request is received.
25
25
  */
26
26
  send(data: Message, callback?: Callback): void;
27
27
  /**
28
- * Add a receive callback
29
- * @param callback
28
+ * Registers a global callback to be triggered whenever a message is received.
29
+ * @param callback The function to execute upon receiving a message.
30
30
  */
31
31
  onReceive(callback: Callback): void;
32
32
  /**
33
- * Define a callback for the websocket open event
34
- * @param callback
33
+ * Registers a callback to be triggered when the WebSocket connection successfully opens.
34
+ * @param callback The function to execute upon connection.
35
35
  */
36
36
  onOpen(callback: () => void): void;
37
+ /**
38
+ * Establishes the WebSocket connection.
39
+ * @param websockerUrl The URL of the WebSocket server.
40
+ */
37
41
  private _connectWebsocket;
38
42
  /**
39
- * Handle a websocket response
40
- * - Execute a callback of a reception if a request was send before (by checking ongoing request)
41
- * - Execute a callbacj for a genral reception response
42
- * @param data
43
- * @returns
43
+ * Handles incoming WebSocket responses.
44
+ * Checks if the message corresponds to an ongoing request. If so, triggers its specific callback.
45
+ * Otherwise, broadcasts the message to all global receive callbacks.
46
+ * @param data The raw data payload received from the WebSocket.
44
47
  */
45
48
  private _handleWebsocketResponse;
46
49
  /**
47
- * Init all websocker events
50
+ * Initializes all WebSocket event listeners (open, message, close).
51
+ * Handles automatic reconnection on connection close or error.
48
52
  */
49
53
  private _setupEvents;
50
54
  /**
51
- * Timeout the request if there is no response
52
- * To timout the response, the function simply delete the id from on going request list
53
- * @param requestId Id of the request
55
+ * Retrieves the index of an ongoing request in the internal list.
56
+ * @param requestId The unique identifier of the request.
57
+ * @returns The array index of the request, or -1 if not found.
54
58
  */
55
59
  private _getRequestIndex;
56
60
  /**
57
- * Delete the request from the list of onGoing Requests
58
- * @param requestId
61
+ * Removes a request from the ongoing requests list.
62
+ * Used both when a request is successfully resolved or when it times out.
63
+ * @param requestId The unique identifier of the request to remove.
59
64
  */
60
65
  private _deleteRequest;
61
66
  /**
62
- * get the Ongoing the request and get theh callback
63
- * @param requestId
64
- * @returns
67
+ * Retrieves an ongoing request object by its ID.
68
+ * @param requestId The unique identifier of the request.
69
+ * @returns The ongoing request object, or undefined if not found.
65
70
  */
66
71
  private _getOnGoingRequest;
67
72
  /**
68
- * Add a new request to ongoing requests
69
- * @param requestId
70
- * @param callback
73
+ * Registers a new request in the ongoing requests list.
74
+ * @param requestId The unique identifier generated for the request.
75
+ * @param callback Optional callback to trigger when the response arrives.
71
76
  */
72
77
  private _addNewRequest;
73
78
  }