@momo2555/koppeliajs 0.0.142 → 0.0.144

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.
@@ -3,11 +3,13 @@ import { type AnyState } from "./state.js";
3
3
  import { Device } from "./device.js";
4
4
  import { Play } from "./play.js";
5
5
  import { Resident } from "./resident.js";
6
+ import { type OptionChangedCallback } from "./option.js";
6
7
  export declare class Koppelia {
7
8
  private _console;
8
9
  private _state;
9
10
  private _stage;
10
11
  private static _instance;
12
+ private _option;
11
13
  constructor();
12
14
  static get instance(): Koppelia;
13
15
  get state(): Writable<AnyState>;
@@ -67,13 +69,11 @@ export declare class Koppelia {
67
69
  */
68
70
  enableDifficultyCursor(callback: (difficulty: number) => void): Promise<void>;
69
71
  /**
70
- *
71
72
  * @param id
72
73
  * @param onGrowChange
73
74
  */
74
75
  registerNewGrowableElement(id: string, onGrowChange: (grown: boolean) => void): Promise<void>;
75
76
  /**
76
- *
77
77
  * @param id
78
78
  * @param grown
79
79
  */
@@ -83,9 +83,24 @@ export declare class Koppelia {
83
83
  * text update notification form koppelia application
84
84
  */
85
85
  registerNewResizableText(id: string, defaultSize: number, onTextResized: (newSize: number) => void): Promise<void>;
86
+ writeGameConfig(config_id: string, config_value: {
87
+ [key: string]: any;
88
+ }, current_play?: boolean): Promise<void>;
89
+ getGameConfig(config_id: string, current_play: boolean): Promise<{
90
+ [key: string]: any;
91
+ }>;
86
92
  /**
87
- *
88
- * @param sentence
89
- */
93
+ * @param sentence
94
+ */
90
95
  say(sentence: string): void;
96
+ /**
97
+ * Set a new or edit a game option
98
+ * @param name Name fo the option
99
+ * @param value Value of the option
100
+ */
101
+ setOption(name: string, value: any): void;
102
+ createSliderOtption(name: string, label: string, value: number, min: number, max: number, step: number): void;
103
+ createSwitchOption(name: string, label: string, value: boolean): void;
104
+ createChoicesOption(name: string, label: string, value: string, choices: string[]): void;
105
+ onOptionChanged(name: string, callback: OptionChangedCallback): void;
91
106
  }
@@ -6,13 +6,15 @@ import { Message, PeerType } from "./message.js";
6
6
  import { Stage } from "./stage.js";
7
7
  import { Device } from "./device.js";
8
8
  import { Play } from "./play.js";
9
- import { PUBLIC_GAME_ID } from '$env/static/public';
9
+ import { PUBLIC_GAME_ID } from "$env/static/public";
10
10
  import { Resident } from "./resident.js";
11
+ import { Option } from "./option.js";
11
12
  export class Koppelia {
12
13
  _console;
13
14
  _state;
14
15
  _stage;
15
16
  static _instance;
17
+ _option;
16
18
  constructor() {
17
19
  this._console = new Console();
18
20
  this._console.onReady(() => {
@@ -25,13 +27,15 @@ export class Koppelia {
25
27
  console.log("identify monitor");
26
28
  this._console.identify(PeerType.MONITOR);
27
29
  }
28
- else
30
+ else {
29
31
  console.log("Cannot identifiy type ", type);
32
+ }
30
33
  });
31
34
  this._state = new State(this._console, {
32
- hey: "coucou"
35
+ hey: "coucou",
33
36
  });
34
37
  this._stage = new Stage(this._console);
38
+ this._option = new Option(this._console);
35
39
  }
36
40
  static get instance() {
37
41
  if (!Koppelia._instance) {
@@ -153,7 +157,8 @@ export class Koppelia {
153
157
  getResidentsRequest.setRequest("getResidentsList");
154
158
  getResidentsRequest.setDestination(PeerType.MASTER, "");
155
159
  this._console.sendMessage(getResidentsRequest, (response) => {
156
- let ResidentRawList = response.getParam("residents", {});
160
+ let ResidentRawList = response
161
+ .getParam("residents", {});
157
162
  let residents = [];
158
163
  for (let residentId in ResidentRawList) {
159
164
  let resident = new Resident();
@@ -188,7 +193,6 @@ export class Koppelia {
188
193
  async enableDifficultyCursor(callback) {
189
194
  }
190
195
  /**
191
- *
192
196
  * @param id
193
197
  * @param onGrowChange
194
198
  */
@@ -218,7 +222,6 @@ export class Koppelia {
218
222
  });
219
223
  }
220
224
  /**
221
- *
222
225
  * @param id
223
226
  * @param grown
224
227
  */
@@ -256,8 +259,9 @@ export class Koppelia {
256
259
  if (req == "resizableTextNotification") {
257
260
  if (params.id !== undefined && params.id == id) {
258
261
  let fontSize = defaultSize;
259
- if (params.fontSize != undefined)
262
+ if (params.fontSize != undefined) {
260
263
  fontSize = params.fontSize;
264
+ }
261
265
  onTextResized(fontSize);
262
266
  }
263
267
  }
@@ -265,10 +269,31 @@ export class Koppelia {
265
269
  resolve();
266
270
  });
267
271
  }
272
+ async writeGameConfig(config_id, config_value, current_play = true) {
273
+ let setGameConfigRequest = new Message();
274
+ setGameConfigRequest.setRequest("setGameData");
275
+ setGameConfigRequest.addParam("playId", current_play ? "current" : null);
276
+ setGameConfigRequest.addParam("content", config_value);
277
+ setGameConfigRequest.addParam("dataId", config_id);
278
+ setGameConfigRequest.setDestination(PeerType.MASTER, "");
279
+ this._console.sendMessage(setGameConfigRequest);
280
+ }
281
+ async getGameConfig(config_id, current_play) {
282
+ return new Promise((resolve, reject) => {
283
+ let getGameConfigRequest = new Message();
284
+ getGameConfigRequest.setRequest("getGameData");
285
+ getGameConfigRequest.addParam("playId", current_play ? "current" : null);
286
+ getGameConfigRequest.addParam("dataId", config_id);
287
+ getGameConfigRequest.setDestination(PeerType.MASTER, "");
288
+ this._console.sendMessage(getGameConfigRequest, (response) => {
289
+ let gameConfigContent = response.getParam("content", {});
290
+ resolve(gameConfigContent);
291
+ });
292
+ });
293
+ }
268
294
  /**
269
- *
270
- * @param sentence
271
- */
295
+ * @param sentence
296
+ */
272
297
  say(sentence) {
273
298
  // create the message to request the devices
274
299
  let sayRequest = new Message();
@@ -278,4 +303,34 @@ export class Koppelia {
278
303
  // send the message to the console
279
304
  this._console.sendMessage(sayRequest);
280
305
  }
306
+ /**
307
+ * Set a new or edit a game option
308
+ * @param name Name fo the option
309
+ * @param value Value of the option
310
+ */
311
+ setOption(name, value) {
312
+ this._option.setOption(name, value, null, {});
313
+ }
314
+ createSliderOtption(name, label, value, min, max, step) {
315
+ this._option.setOption(name, value, "slider", {
316
+ "min": min,
317
+ "max": max,
318
+ "step": step,
319
+ "label": label,
320
+ });
321
+ }
322
+ createSwitchOption(name, label, value) {
323
+ this._option.setOption(name, value, "switch", {
324
+ "label": label,
325
+ });
326
+ }
327
+ createChoicesOption(name, label, value, choices) {
328
+ this._option.setOption(name, value, "choices", {
329
+ "choices": choices,
330
+ "label": label,
331
+ });
332
+ }
333
+ onOptionChanged(name, callback) {
334
+ this._option.onOptionChanged(name, callback);
335
+ }
281
336
  }
@@ -0,0 +1,40 @@
1
+ import { Console } from "./console.js";
2
+ export type Options = {
3
+ [key: string]: any;
4
+ };
5
+ export type OptionChangedCallback = (value: any) => void;
6
+ export declare class Option {
7
+ private _options;
8
+ private _console;
9
+ private _callbacks;
10
+ constructor(console: Console);
11
+ get options(): Options;
12
+ /**
13
+ * Update the game options from the server
14
+ */
15
+ updateFromServer(): Promise<void>;
16
+ /**
17
+ * Set a new or edit a game option
18
+ * @param name Name fo the option
19
+ * @param value Value of the option
20
+ */
21
+ setOption(name: string, value: any, type?: string | null, config?: {
22
+ [key: string]: any;
23
+ }): void;
24
+ /**
25
+ * Set a callback when an option has changed
26
+ * @param name
27
+ * @param callback
28
+ */
29
+ onOptionChanged(name: string, callback: OptionChangedCallback): void;
30
+ /**
31
+ * Init all events
32
+ * @param from
33
+ * @param any
34
+ */
35
+ private _initEvents;
36
+ /**
37
+ * callback when a new option is received
38
+ */
39
+ private _onReceiveState;
40
+ }
@@ -0,0 +1,95 @@
1
+ import { Console } from "./console.js";
2
+ import { Message, PeerType } from "./message.js";
3
+ import { get, writable } from "svelte/store";
4
+ export class Option {
5
+ _options;
6
+ _console;
7
+ _callbacks;
8
+ constructor(console) {
9
+ this._options = {};
10
+ this._console = console;
11
+ this._callbacks = {};
12
+ this._initEvents();
13
+ }
14
+ get options() {
15
+ return this._options;
16
+ }
17
+ /**
18
+ * Update the game options from the server
19
+ */
20
+ async updateFromServer() {
21
+ return new Promise((resolve, reject) => {
22
+ let req = new Message();
23
+ req.setRequest("getGameOptions");
24
+ this._console.sendMessage(req, (response) => {
25
+ let options = response.getParam("gameOptions", {});
26
+ for (const optionName in options) {
27
+ this._onReceiveState(response.header.from, optionName, options[optionName]);
28
+ }
29
+ resolve();
30
+ });
31
+ });
32
+ }
33
+ /**
34
+ * Set a new or edit a game option
35
+ * @param name Name fo the option
36
+ * @param value Value of the option
37
+ */
38
+ setOption(name, value, type = null, config = {}) {
39
+ let setOptionRequest = new Message();
40
+ setOptionRequest.setRequest("setGameOption");
41
+ setOptionRequest.setDestination(PeerType.MASTER, "");
42
+ setOptionRequest.addParam("name", name);
43
+ setOptionRequest.addParam("value", value);
44
+ setOptionRequest.addParam("type", type);
45
+ setOptionRequest.addParam("config", config);
46
+ this._console.sendMessage(setOptionRequest);
47
+ // this._options[name] = value;
48
+ }
49
+ /**
50
+ * Set a callback when an option has changed
51
+ * @param name
52
+ * @param callback
53
+ */
54
+ onOptionChanged(name, callback) {
55
+ this._callbacks[name] = callback;
56
+ }
57
+ /**
58
+ * Init all events
59
+ * @param from
60
+ * @param any
61
+ */
62
+ _initEvents() {
63
+ // Get the state when the console is ready
64
+ this._console.onReady(() => {
65
+ this.updateFromServer();
66
+ });
67
+ // update the state when receive a change from console
68
+ this._console.onRequest((request, params, from) => {
69
+ if (request == "gameOptionNotification") {
70
+ let value = {};
71
+ let name = "";
72
+ if (Object.hasOwn(params, "value")) {
73
+ value = params["value"];
74
+ }
75
+ if (Object.hasOwn(params, "name")) {
76
+ name = params["name"];
77
+ }
78
+ this._onReceiveState(from, name, value, true);
79
+ }
80
+ });
81
+ }
82
+ /**
83
+ * callback when a new option is received
84
+ */
85
+ _onReceiveState(from, receivedOption, valueOption, runCallbacks = false) {
86
+ this._options[receivedOption] = valueOption["value"];
87
+ if (runCallbacks) {
88
+ for (const callKey in this._callbacks) {
89
+ if (callKey == receivedOption) {
90
+ this._callbacks[callKey](valueOption);
91
+ }
92
+ }
93
+ }
94
+ }
95
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo2555/koppeliajs",
3
- "version": "0.0.142",
3
+ "version": "0.0.144",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run package",