@momo2555/koppeliajs 0.0.142 → 0.0.143
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/dist/scripts/koppelia.d.ts +14 -5
- package/dist/scripts/koppelia.js +43 -10
- package/dist/scripts/option.d.ts +40 -0
- package/dist/scripts/option.js +95 -0
- package/package.json +1 -1
|
@@ -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
|
*/
|
|
@@ -84,8 +84,17 @@ export declare class Koppelia {
|
|
|
84
84
|
*/
|
|
85
85
|
registerNewResizableText(id: string, defaultSize: number, onTextResized: (newSize: number) => void): Promise<void>;
|
|
86
86
|
/**
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
*/
|
|
87
|
+
* @param sentence
|
|
88
|
+
*/
|
|
90
89
|
say(sentence: string): void;
|
|
90
|
+
/**
|
|
91
|
+
* Set a new or edit a game option
|
|
92
|
+
* @param name Name fo the option
|
|
93
|
+
* @param value Value of the option
|
|
94
|
+
*/
|
|
95
|
+
setOption(name: string, value: any): void;
|
|
96
|
+
createSliderOtption(name: string, label: string, value: number, min: number, max: number, step: number): void;
|
|
97
|
+
createSwitchOption(name: string, label: string, value: boolean): void;
|
|
98
|
+
createChoicesOption(name: string, label: string, value: string, choices: string[]): void;
|
|
99
|
+
onOptionChanged(name: string, callback: OptionChangedCallback): void;
|
|
91
100
|
}
|
package/dist/scripts/koppelia.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
}
|
|
@@ -266,9 +270,8 @@ export class Koppelia {
|
|
|
266
270
|
});
|
|
267
271
|
}
|
|
268
272
|
/**
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
*/
|
|
273
|
+
* @param sentence
|
|
274
|
+
*/
|
|
272
275
|
say(sentence) {
|
|
273
276
|
// create the message to request the devices
|
|
274
277
|
let sayRequest = new Message();
|
|
@@ -278,4 +281,34 @@ export class Koppelia {
|
|
|
278
281
|
// send the message to the console
|
|
279
282
|
this._console.sendMessage(sayRequest);
|
|
280
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* Set a new or edit a game option
|
|
286
|
+
* @param name Name fo the option
|
|
287
|
+
* @param value Value of the option
|
|
288
|
+
*/
|
|
289
|
+
setOption(name, value) {
|
|
290
|
+
this._option.setOption(name, value, null, {});
|
|
291
|
+
}
|
|
292
|
+
createSliderOtption(name, label, value, min, max, step) {
|
|
293
|
+
this._option.setOption(name, value, "slider", {
|
|
294
|
+
"min": min,
|
|
295
|
+
"max": max,
|
|
296
|
+
"step": step,
|
|
297
|
+
"label": label,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
createSwitchOption(name, label, value) {
|
|
301
|
+
this._option.setOption(name, value, "switch", {
|
|
302
|
+
"label": label,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
createChoicesOption(name, label, value, choices) {
|
|
306
|
+
this._option.setOption(name, value, "choices", {
|
|
307
|
+
"choices": choices,
|
|
308
|
+
"label": label
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
onOptionChanged(name, callback) {
|
|
312
|
+
this._option.onOptionChanged(name, callback);
|
|
313
|
+
}
|
|
281
314
|
}
|
|
@@ -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
|
+
}
|