@momo2555/koppeliajs 0.0.122 → 0.0.124

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.
@@ -10,7 +10,7 @@
10
10
  let koppelia = Koppelia.instance;
11
11
 
12
12
  koppelia.onReady(async () => {
13
- await koppelia.registerNewResizableText(id, fontSize, (newFontSize: number) => {
13
+ await koppelia.registerNewResizableText(id, defaultFontSize, (newFontSize: number) => {
14
14
  fontSize = newFontSize;
15
15
  });
16
16
  });
@@ -9,6 +9,7 @@ export type DeviceDataCallback = (device: string, from_addr: string, event: stri
9
9
  export type AnyRequestCallback = (request: string, params: {
10
10
  [key: string]: any;
11
11
  }, from: string, address: string) => void;
12
+ export type MediaResponseData = string | Blob | ArrayBuffer | any;
12
13
  /**
13
14
  * This class define the koppelia console, send and receive requests
14
15
  */
@@ -23,6 +24,7 @@ export declare class Console {
23
24
  private _anyRequestHandlers;
24
25
  private _ready;
25
26
  private _onReadyCallback;
27
+ private _mediaApiUrl;
26
28
  constructor();
27
29
  /**
28
30
  * Sdnd a message to the console, set header.to to dispatch the message
@@ -64,6 +66,8 @@ export declare class Console {
64
66
  * Get if the connection with the console is ready
65
67
  */
66
68
  get ready(): boolean;
69
+ getMediaUrl(path: string): string;
70
+ getMedia(path: string): Promise<MediaResponseData>;
67
71
  private _initEvents;
68
72
  private _processReceivedData;
69
73
  private _execDeviceDataHandlers;
@@ -4,6 +4,7 @@ import { page } from '$app/stores';
4
4
  import { get } from 'svelte/store';
5
5
  import { routeType } from '../stores/routeStore.js';
6
6
  const PORT = 2225;
7
+ const API_PORT = 8000;
7
8
  /**
8
9
  * This class define the koppelia console, send and receive requests
9
10
  */
@@ -18,6 +19,7 @@ export class Console {
18
19
  _anyRequestHandlers;
19
20
  _ready = false;
20
21
  _onReadyCallback;
22
+ _mediaApiUrl;
21
23
  constructor() {
22
24
  this.consoleHostname = "";
23
25
  if (!import.meta.env.SSR) {
@@ -25,6 +27,7 @@ export class Console {
25
27
  }
26
28
  let consoleUrl = "ws://" + this.consoleHostname + ":" + PORT;
27
29
  this.consoleSocket = new KoppeliaWebsocket(consoleUrl);
30
+ this._mediaApiUrl = "http://" + this.consoleHostname + ":" + API_PORT;
28
31
  this._ready = false;
29
32
  this._changeStateHandlers = [];
30
33
  this._changeStageHandlers = [];
@@ -107,6 +110,30 @@ export class Console {
107
110
  get ready() {
108
111
  return this._ready;
109
112
  }
113
+ getMediaUrl(path) {
114
+ if (!path.startsWith("/"))
115
+ path = "/" + path;
116
+ return this._mediaApiUrl + path;
117
+ }
118
+ async getMedia(path) {
119
+ const response = await fetch(this.getMediaUrl(path));
120
+ if (!response.ok) {
121
+ throw new Error(`HTTP error! status: ${response.status}`);
122
+ }
123
+ const contentType = response.headers.get("Content-Type") || "";
124
+ if (contentType.includes("application/json")) {
125
+ return await response.json(); // Object
126
+ }
127
+ else if (contentType.startsWith("text/")) {
128
+ return await response.text(); // String
129
+ }
130
+ else if (contentType.startsWith("image/") || contentType.includes("application/octet-stream")) {
131
+ return await response.blob(); // Blob (images, downloads, etc.)
132
+ }
133
+ else {
134
+ return await response.arrayBuffer(); // Generic binary
135
+ }
136
+ }
110
137
  _initEvents() {
111
138
  this.consoleSocket.onOpen(() => {
112
139
  this._ready = true;
@@ -74,7 +74,8 @@ export declare class Koppelia {
74
74
  */
75
75
  updateGrowableElement(id: string, grown: boolean): Promise<void>;
76
76
  /**
77
- *
77
+ * Add a new resizable text element and define the callback that will be executed when receive and a resizable
78
+ * text update notification form koppelia application
78
79
  */
79
80
  registerNewResizableText(id: string, defaultSize: number, onTextResized: (newSize: number) => void): Promise<void>;
80
81
  }
@@ -212,7 +212,8 @@ export class Koppelia {
212
212
  });
213
213
  }
214
214
  /**
215
- *
215
+ * Add a new resizable text element and define the callback that will be executed when receive and a resizable
216
+ * text update notification form koppelia application
216
217
  */
217
218
  async registerNewResizableText(id, defaultSize, onTextResized) {
218
219
  return new Promise((resolve, reject) => {
@@ -1,4 +1,5 @@
1
1
  import { Console } from "./console.js";
2
+ import type { MediaResponseData } from "./console.js";
2
3
  export declare class Play {
3
4
  private _playCreationDate;
4
5
  private _playCreatorId;
@@ -10,29 +11,21 @@ export declare class Play {
10
11
  private _playName;
11
12
  private _playId;
12
13
  private _playData;
13
- private _base64Medias;
14
14
  private _console;
15
15
  private _refreshed;
16
16
  constructor(console: Console, playId: string, playRawObj: {
17
17
  [key: string]: any;
18
18
  });
19
- refresh(): Promise<void>;
20
- /**
21
- * Get the list of all media files of the play
22
- */
23
- get mediasList(): string[];
24
- /**
25
- * Get the media file by using its name. This function returns a base64 file
26
- * @param mediaName
27
- * @returns
28
- */
29
- getMedia(mediaName: string): string | undefined;
19
+ getPlayMediaPath(mediaName: string): string;
20
+ getMediaLink(mediaName: string): string;
21
+ getMediaContent(mediaName: string): Promise<MediaResponseData>;
22
+ getMediaData(): Promise<{
23
+ [key: string]: any;
24
+ }>;
30
25
  /**
31
26
  * Get the data of the play
32
27
  */
33
- get data(): {
34
- [key: string]: any;
35
- };
28
+ get data(): string;
36
29
  /**
37
30
  * Get the id of the play
38
31
  */
@@ -10,72 +10,35 @@ export class Play {
10
10
  _playMedias = [];
11
11
  _playName = "";
12
12
  _playId = "";
13
- _playData = {};
14
- _base64Medias = {};
13
+ _playData = "";
15
14
  _console;
16
15
  _refreshed = true;
17
16
  constructor(console, playId, playRawObj) {
18
17
  this._console = console;
19
18
  this._playId = playId;
20
- if (playRawObj._play_data_json !== undefined) {
21
- this._playData = playRawObj._play_data_json;
22
- this._refreshed = true;
23
- }
24
- ;
25
- if (playRawObj._play_medias_raw !== undefined) {
26
- this._base64Medias = playRawObj._play_medias_raw;
27
- }
28
19
  if (playRawObj.playName !== undefined) {
29
20
  this._playName = playRawObj.playName;
30
21
  }
31
- if (playRawObj.playMedias !== undefined) {
32
- this._playMedias = playRawObj.playMedias;
22
+ if (playRawObj.playGameId !== undefined) {
23
+ this._playGameId = playRawObj.playGameId;
33
24
  }
34
- if (playRawObj._play_image_raw !== undefined) {
35
- this._playImagebase64 = playRawObj._play_image_raw;
25
+ if (playRawObj.playData !== undefined) {
26
+ this._playData = playRawObj.playData;
36
27
  }
37
28
  }
38
- async refresh() {
39
- return new Promise((resolve, reject) => {
40
- let getPlaysRequest = new Message();
41
- getPlaysRequest.setRequest("getPlayRaw");
42
- getPlaysRequest.addParam("playId", this.id);
43
- getPlaysRequest.setDestination(PeerType.MASTER, "");
44
- this._console.sendMessage(getPlaysRequest, (response) => {
45
- let playRawObj = response.getParam("play", {});
46
- if (playRawObj._play_data_json !== undefined) {
47
- this._playData = playRawObj._play_data_json;
48
- this._refreshed = true;
49
- }
50
- ;
51
- if (playRawObj._play_medias_raw !== undefined) {
52
- this._base64Medias = playRawObj._play_medias_raw;
53
- }
54
- if (playRawObj._play_image_raw !== undefined) {
55
- this._playImagebase64 = playRawObj._play_image_raw;
56
- }
57
- resolve();
58
- });
59
- });
29
+ getPlayMediaPath(mediaName) {
30
+ return `game/${this._playGameId}/play/${this._playId}/${mediaName}`;
60
31
  }
61
- /**
62
- * Get the list of all media files of the play
63
- */
64
- get mediasList() {
65
- return Object.keys(this._base64Medias);
32
+ getMediaLink(mediaName) {
33
+ let mediaPath = this.getPlayMediaPath(mediaName);
34
+ return this._console.getMediaUrl(mediaPath);
66
35
  }
67
- /**
68
- * Get the media file by using its name. This function returns a base64 file
69
- * @param mediaName
70
- * @returns
71
- */
72
- getMedia(mediaName) {
73
- if (this._base64Medias[mediaName] !== undefined) {
74
- return this._base64Medias[mediaName];
75
- }
76
- else {
77
- return undefined;
78
- }
36
+ async getMediaContent(mediaName) {
37
+ let mediaPath = this.getPlayMediaPath(mediaName);
38
+ return await this._console.getMedia(mediaPath);
39
+ }
40
+ async getMediaData() {
41
+ return await this.getMediaContent(this._playData);
79
42
  }
80
43
  /**
81
44
  * Get the data of the play
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo2555/koppeliajs",
3
- "version": "0.0.122",
3
+ "version": "0.0.124",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run package",