@dcl-regenesislabs/sdk 0.0.0-stage → 7.0.0-42722791062.commit-bc8d909
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/LICENSE +201 -0
- package/README.md +119 -2
- package/composite-provider.d.ts +2 -0
- package/composite-provider.js +67 -0
- package/ecs.d.ts +16 -0
- package/ecs.js +17 -0
- package/ethereum-provider/index.d.ts +24 -0
- package/ethereum-provider/index.js +27 -0
- package/ethereum-provider/text-encoder.d.ts +1 -0
- package/ethereum-provider/text-encoder.js +5 -0
- package/explorer-ui/index.d.ts +118 -0
- package/explorer-ui/index.js +235 -0
- package/index.d.ts +1 -0
- package/index.js +45 -0
- package/internal/Observable.d.ts +192 -0
- package/internal/Observable.js +324 -0
- package/internal/language-events.d.ts +1 -0
- package/internal/language-events.js +8 -0
- package/internal/provider.d.ts +22 -0
- package/internal/provider.js +29 -0
- package/internal/transports/logger.d.ts +2 -0
- package/internal/transports/logger.js +38 -0
- package/internal/transports/rendererTransport.d.ts +7 -0
- package/internal/transports/rendererTransport.js +40 -0
- package/internal/utf8.d.ts +15 -0
- package/internal/utf8.js +172 -0
- package/math.d.ts +16 -0
- package/math.js +17 -0
- package/message-bus.d.ts +14 -0
- package/message-bus.js +49 -0
- package/network/binary-message-bus.d.ts +18 -0
- package/network/binary-message-bus.js +68 -0
- package/network/entities.d.ts +11 -0
- package/network/entities.js +121 -0
- package/network/filter.d.ts +2 -0
- package/network/filter.js +43 -0
- package/network/index.d.ts +2 -0
- package/network/index.js +8 -0
- package/network/message-bus-sync.d.ts +18 -0
- package/network/message-bus-sync.js +174 -0
- package/network/state.d.ts +7 -0
- package/network/state.js +84 -0
- package/network/utils.d.ts +4 -0
- package/network/utils.js +16 -0
- package/observables.d.ts +179 -0
- package/observables.js +259 -0
- package/package.json +35 -3
- package/platform/index.d.ts +21 -0
- package/platform/index.js +70 -0
- package/players/index.d.ts +34 -0
- package/players/index.js +81 -0
- package/react-ecs.d.ts +21 -0
- package/react-ecs.js +27 -0
- package/src/composite-provider.ts +67 -0
- package/src/ecs.ts +17 -0
- package/src/ethereum-provider/index.ts +30 -0
- package/src/ethereum-provider/text-encoder.ts +4 -0
- package/src/explorer-ui/index.ts +398 -0
- package/src/index.ts +49 -0
- package/src/internal/Observable.ts +394 -0
- package/src/internal/language-events.ts +8 -0
- package/src/internal/provider.ts +51 -0
- package/src/internal/transports/logger.ts +45 -0
- package/src/internal/transports/rendererTransport.ts +48 -0
- package/src/internal/utf8.ts +167 -0
- package/src/math.ts +17 -0
- package/src/message-bus.ts +56 -0
- package/src/network/README.md +122 -0
- package/src/network/binary-message-bus.ts +74 -0
- package/src/network/entities.ts +149 -0
- package/src/network/filter.ts +63 -0
- package/src/network/index.ts +10 -0
- package/src/network/message-bus-sync.ts +208 -0
- package/src/network/state.ts +136 -0
- package/src/network/utils.ts +22 -0
- package/src/observables.ts +386 -0
- package/src/platform/index.ts +82 -0
- package/src/players/index.ts +110 -0
- package/src/react-ecs.ts +29 -0
- package/src/testing/assert.ts +134 -0
- package/src/testing/index.ts +42 -0
- package/src/testing/runtime.ts +223 -0
- package/src/testing/types.ts +21 -0
- package/src/text-codec.ts +70 -0
- package/testing/assert.d.ts +11 -0
- package/testing/assert.js +126 -0
- package/testing/index.d.ts +7 -0
- package/testing/index.js +36 -0
- package/testing/runtime.d.ts +8 -0
- package/testing/runtime.js +197 -0
- package/testing/types.d.ts +18 -0
- package/testing/types.js +2 -0
- package/text-codec.d.ts +28 -0
- package/text-codec.js +65 -0
- package/tsconfig.json +12 -0
- package/types/tsconfig.ecs7.json +31 -0
- package/types/tsconfig.ecs7.strict.json +4 -0
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { Observable } from './internal/Observable'
|
|
2
|
+
import {
|
|
3
|
+
AvatarBase,
|
|
4
|
+
AvatarEmoteCommand,
|
|
5
|
+
AvatarEquippedData,
|
|
6
|
+
Entity,
|
|
7
|
+
PlayerIdentityData,
|
|
8
|
+
PointerEventsResult,
|
|
9
|
+
RealmInfo,
|
|
10
|
+
Vector3Type,
|
|
11
|
+
engine
|
|
12
|
+
} from '@dcl/ecs'
|
|
13
|
+
import { ManyEntityAction, SendBatchResponse, subscribe } from '~system/EngineApi'
|
|
14
|
+
import players from './players'
|
|
15
|
+
import { localeChangedEvents } from './internal/language-events'
|
|
16
|
+
|
|
17
|
+
/// --- EVENTS ---
|
|
18
|
+
|
|
19
|
+
/** @public */
|
|
20
|
+
export type IEventNames = keyof IEvents
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @public
|
|
24
|
+
* Note: Don't use `on` prefix for IEvents to avoid redundancy with `event.on("onEventName")` syntax.
|
|
25
|
+
*/
|
|
26
|
+
export interface IEvents {
|
|
27
|
+
playerExpression: {
|
|
28
|
+
expressionId: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* This event gets triggered when the user enters the scene
|
|
33
|
+
*/
|
|
34
|
+
onEnterScene: {
|
|
35
|
+
userId: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* This event gets triggered when the user leaves the scene
|
|
40
|
+
*/
|
|
41
|
+
onLeaveScene: {
|
|
42
|
+
userId: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* This event gets triggered after receiving a comms message.
|
|
47
|
+
*/
|
|
48
|
+
comms: {
|
|
49
|
+
sender: string
|
|
50
|
+
message: string
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Triggered when the player switches the client language. `locale` is a BCP-47 tag */
|
|
54
|
+
localeChanged: {
|
|
55
|
+
locale: string
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* This is triggered once the scene should start.
|
|
60
|
+
*/
|
|
61
|
+
sceneStart: unknown
|
|
62
|
+
|
|
63
|
+
/** This is triggered at least for each videoStatus change */
|
|
64
|
+
videoEvent: {
|
|
65
|
+
componentId: string
|
|
66
|
+
videoClipId: string
|
|
67
|
+
/** Status, can be NONE = 0, ERROR = 1, LOADING = 2, READY = 3, PLAYING = 4,BUFFERING = 5 */
|
|
68
|
+
videoStatus: number
|
|
69
|
+
/** Current offset position in seconds */
|
|
70
|
+
currentOffset: number
|
|
71
|
+
/** Video length in seconds. Can be -1 */
|
|
72
|
+
totalVideoLength: number
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** This is trigger everytime a profile is changed */
|
|
76
|
+
profileChanged: {
|
|
77
|
+
ethAddress: string
|
|
78
|
+
version: number
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Triggered when peer's avatar is connected and visible */
|
|
82
|
+
playerConnected: {
|
|
83
|
+
userId: string
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Triggered when peer disconnect and/or it avatar is set invisible by comms */
|
|
87
|
+
playerDisconnected: {
|
|
88
|
+
userId: string
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Triggered when current realm or island changes */
|
|
92
|
+
onRealmChanged: {
|
|
93
|
+
domain: string
|
|
94
|
+
room: string
|
|
95
|
+
serverName: string
|
|
96
|
+
displayName: string
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Triggered when other player's avatar is clicked */
|
|
100
|
+
playerClicked: {
|
|
101
|
+
userId: string
|
|
102
|
+
ray: {
|
|
103
|
+
origin: Vector3Type
|
|
104
|
+
direction: Vector3Type
|
|
105
|
+
distance: number
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @internal
|
|
112
|
+
* This function generates a callback that is passed to the Observable
|
|
113
|
+
* constructor to subscribe to the events of the DecentralandInterface
|
|
114
|
+
*/
|
|
115
|
+
function createSubscriber(eventName: keyof IEvents) {
|
|
116
|
+
return () => {
|
|
117
|
+
if (eventName === 'comms') {
|
|
118
|
+
subscribe({ eventId: eventName }).catch(console.error)
|
|
119
|
+
} else {
|
|
120
|
+
SDK7ComponentsObservable?.subscribe(eventName)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* These events are triggered after your character enters the scene.
|
|
127
|
+
* @public
|
|
128
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
129
|
+
*/
|
|
130
|
+
export const onEnterSceneObservable = new Observable<IEvents['onEnterScene']>(createSubscriber('onEnterScene'))
|
|
131
|
+
/** @public
|
|
132
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
133
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed. Use onEnterSceneObservable instead. */
|
|
134
|
+
export const onEnterScene = onEnterSceneObservable
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* These events are triggered after your character leaves the scene.
|
|
138
|
+
* @public
|
|
139
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
140
|
+
*/
|
|
141
|
+
export const onLeaveSceneObservable = new Observable<IEvents['onLeaveScene']>(createSubscriber('onLeaveScene'))
|
|
142
|
+
|
|
143
|
+
/** @public
|
|
144
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
145
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed. Use onLeaveSceneObservable instead. */
|
|
146
|
+
export const onLeaveScene = onLeaveSceneObservable
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* This event is triggered after all the resources of the scene were loaded (models, textures, etc...)
|
|
150
|
+
* @public
|
|
151
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
152
|
+
*/
|
|
153
|
+
export const onSceneReadyObservable = new Observable<IEvents['sceneStart']>(createSubscriber('sceneStart'))
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @public
|
|
157
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
158
|
+
*/
|
|
159
|
+
export const onPlayerExpressionObservable = new Observable<IEvents['playerExpression']>(
|
|
160
|
+
createSubscriber('playerExpression')
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @public
|
|
165
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
166
|
+
*/
|
|
167
|
+
export const onVideoEvent = new Observable<IEvents['videoEvent']>(createSubscriber('videoEvent'))
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @public
|
|
171
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
172
|
+
*/
|
|
173
|
+
export const onProfileChanged = new Observable<IEvents['profileChanged']>(createSubscriber('profileChanged'))
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @public
|
|
177
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
178
|
+
*/
|
|
179
|
+
export const onPlayerConnectedObservable = new Observable<IEvents['playerConnected']>(
|
|
180
|
+
createSubscriber('playerConnected')
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* @public
|
|
185
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
186
|
+
*/
|
|
187
|
+
export const onPlayerDisconnectedObservable = new Observable<IEvents['playerDisconnected']>(
|
|
188
|
+
createSubscriber('playerDisconnected')
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* @public
|
|
193
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
194
|
+
*/
|
|
195
|
+
export const onRealmChangedObservable = new Observable<IEvents['onRealmChanged']>(createSubscriber('onRealmChanged'))
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @public
|
|
199
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
200
|
+
*/
|
|
201
|
+
export const onPlayerClickedObservable = new Observable<IEvents['playerClicked']>(createSubscriber('playerClicked'))
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* @internal
|
|
205
|
+
* @deprecated This function is an inheritance of ECS6, it's here temporary for the feature parity, please read the news and docs to know how handle when it's removed.
|
|
206
|
+
*/
|
|
207
|
+
export const onCommsMessage = new Observable<IEvents['comms']>(createSubscriber('comms'))
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* @internal
|
|
211
|
+
* @deprecated this is an OLD API.
|
|
212
|
+
* This function uses the SDK6 sendBatch to poll events from the renderer
|
|
213
|
+
*/
|
|
214
|
+
export async function pollEvents(sendBatch: (body: ManyEntityAction) => Promise<SendBatchResponse>) {
|
|
215
|
+
const { events } = await sendBatch({ actions: [] })
|
|
216
|
+
for (const e of events) {
|
|
217
|
+
if (e.generic) {
|
|
218
|
+
const data = JSON.parse(e.generic.eventData)
|
|
219
|
+
switch (e.generic.eventId) {
|
|
220
|
+
case 'comms': {
|
|
221
|
+
onCommsMessage.notifyObservers(data as IEvents['comms'])
|
|
222
|
+
break
|
|
223
|
+
}
|
|
224
|
+
case 'localeChanged': {
|
|
225
|
+
localeChangedEvents.notifyObservers(data as IEvents['localeChanged'])
|
|
226
|
+
break
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const SDK7ComponentsObservable = processObservables()
|
|
234
|
+
function processObservables() {
|
|
235
|
+
const subscriptions = new Set<keyof IEvents>()
|
|
236
|
+
|
|
237
|
+
function subscribe(eventName: keyof IEvents) {
|
|
238
|
+
if (subscriptions.has(eventName)) return
|
|
239
|
+
switch (eventName) {
|
|
240
|
+
case 'playerClicked': {
|
|
241
|
+
subscribePlayerClick()
|
|
242
|
+
break
|
|
243
|
+
}
|
|
244
|
+
case 'onEnterScene':
|
|
245
|
+
case 'playerConnected': {
|
|
246
|
+
subscribeEnterScene()
|
|
247
|
+
break
|
|
248
|
+
}
|
|
249
|
+
case 'onLeaveScene':
|
|
250
|
+
case 'playerDisconnected': {
|
|
251
|
+
subscribeLeaveScene()
|
|
252
|
+
break
|
|
253
|
+
}
|
|
254
|
+
case 'onRealmChanged': {
|
|
255
|
+
subscribeRealmChange()
|
|
256
|
+
break
|
|
257
|
+
}
|
|
258
|
+
case 'playerExpression': {
|
|
259
|
+
subscribePlayerExpression()
|
|
260
|
+
break
|
|
261
|
+
}
|
|
262
|
+
case 'profileChanged': {
|
|
263
|
+
subscribeProfileChange()
|
|
264
|
+
break
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
subscriptions.add(eventName)
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* PLAYER ENTER/CONNECTED observable
|
|
271
|
+
*/
|
|
272
|
+
function subscribeEnterScene() {
|
|
273
|
+
players.onEnterScene((player) => {
|
|
274
|
+
if (subscriptions.has('onEnterScene')) {
|
|
275
|
+
onEnterSceneObservable.notifyObservers({ userId: player.userId })
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (subscriptions.has('playerConnected')) {
|
|
279
|
+
onPlayerConnectedObservable.notifyObservers({ userId: player.userId })
|
|
280
|
+
}
|
|
281
|
+
})
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* PLAYER LEAVE/DISCONNECTED observable
|
|
285
|
+
*/
|
|
286
|
+
function subscribeLeaveScene() {
|
|
287
|
+
players.onLeaveScene((userId) => {
|
|
288
|
+
if (subscriptions.has('onLeaveScene')) {
|
|
289
|
+
onLeaveSceneObservable.notifyObservers({ userId })
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (subscriptions.has('playerDisconnected')) {
|
|
293
|
+
onPlayerDisconnectedObservable.notifyObservers({ userId })
|
|
294
|
+
}
|
|
295
|
+
})
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* REALM CHANGE observable
|
|
299
|
+
*/
|
|
300
|
+
function subscribeRealmChange() {
|
|
301
|
+
RealmInfo.onChange(engine.RootEntity, (value) => {
|
|
302
|
+
if (value) {
|
|
303
|
+
onRealmChangedObservable.notifyObservers({
|
|
304
|
+
domain: value.baseUrl,
|
|
305
|
+
displayName: value.realmName,
|
|
306
|
+
room: value.room ?? '',
|
|
307
|
+
serverName: value.realmName
|
|
308
|
+
})
|
|
309
|
+
}
|
|
310
|
+
})
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* PLAYER/AVATAR CLICKED observable
|
|
314
|
+
*/
|
|
315
|
+
function subscribePlayerClick() {
|
|
316
|
+
const playerEntities = new Set<Entity>()
|
|
317
|
+
engine.addSystem(() => {
|
|
318
|
+
for (const [entity] of engine.getEntitiesWith(PlayerIdentityData)) {
|
|
319
|
+
if (playerEntities.has(entity)) continue
|
|
320
|
+
playerEntities.add(entity)
|
|
321
|
+
|
|
322
|
+
PointerEventsResult.onChange(entity, (data) => {
|
|
323
|
+
if (data?.hit) {
|
|
324
|
+
onPlayerClickedObservable.notifyObservers({
|
|
325
|
+
userId: PlayerIdentityData.getOrNull(entity)?.address ?? '',
|
|
326
|
+
ray: {
|
|
327
|
+
direction: data.hit.direction!,
|
|
328
|
+
distance: data.hit.length,
|
|
329
|
+
origin: data.hit.globalOrigin!
|
|
330
|
+
}
|
|
331
|
+
})
|
|
332
|
+
}
|
|
333
|
+
})
|
|
334
|
+
}
|
|
335
|
+
})
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Player expression observable
|
|
340
|
+
*/
|
|
341
|
+
function subscribePlayerExpression() {
|
|
342
|
+
AvatarEmoteCommand.onChange(engine.PlayerEntity, (value) => {
|
|
343
|
+
onPlayerExpressionObservable.notifyObservers({ expressionId: value?.emoteUrn ?? '' })
|
|
344
|
+
})
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* PROFILE CHANGE observable
|
|
349
|
+
*/
|
|
350
|
+
function subscribeProfileChange() {
|
|
351
|
+
let lastAvatarBaseProfile: string | undefined
|
|
352
|
+
AvatarBase.onChange(engine.PlayerEntity, (value) => {
|
|
353
|
+
if (!profileAddress) return
|
|
354
|
+
// talking flips on every speaking start/stop and is not profile data
|
|
355
|
+
const avatarBaseProfile = JSON.stringify({ ...value, talking: undefined })
|
|
356
|
+
if (avatarBaseProfile === lastAvatarBaseProfile) return
|
|
357
|
+
lastAvatarBaseProfile = avatarBaseProfile
|
|
358
|
+
onProfileChanged.notifyObservers({ ethAddress: profileAddress, version: 0 })
|
|
359
|
+
})
|
|
360
|
+
|
|
361
|
+
AvatarEquippedData.onChange(engine.PlayerEntity, () => {
|
|
362
|
+
if (!profileAddress) return
|
|
363
|
+
onProfileChanged.notifyObservers({ ethAddress: profileAddress, version: 0 })
|
|
364
|
+
})
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Flag to call once the scene is initalized.
|
|
368
|
+
let sceneReady = false
|
|
369
|
+
let profileAddress: string | undefined
|
|
370
|
+
|
|
371
|
+
function observableSystem() {
|
|
372
|
+
if (sceneReady && profileAddress) {
|
|
373
|
+
return engine.removeSystem(observableSystem)
|
|
374
|
+
}
|
|
375
|
+
if (!sceneReady) {
|
|
376
|
+
sceneReady = true
|
|
377
|
+
onSceneReadyObservable.notifyObservers({})
|
|
378
|
+
}
|
|
379
|
+
if (profileAddress) return
|
|
380
|
+
profileAddress = PlayerIdentityData.getOrNull(engine.PlayerEntity)?.address
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
engine.addSystem(observableSystem)
|
|
384
|
+
|
|
385
|
+
return { subscribe }
|
|
386
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { getExplorerInformation } from '~system/Runtime'
|
|
2
|
+
import { subscribe } from '~system/EngineApi'
|
|
3
|
+
import { Observable } from '../internal/Observable'
|
|
4
|
+
import { localeChangedEvents } from '../internal/language-events'
|
|
5
|
+
|
|
6
|
+
export type Platform = 'mobile' | 'desktop' | 'web'
|
|
7
|
+
|
|
8
|
+
const VALID_PLATFORMS: Set<string> = new Set<string>(['mobile', 'desktop', 'web'])
|
|
9
|
+
|
|
10
|
+
const DEFAULT_LANGUAGE = 'en'
|
|
11
|
+
const BCP47_TAG = /^[a-zA-Z]{2,3}(-[a-zA-Z0-9]{2,8})*$/
|
|
12
|
+
|
|
13
|
+
let platform: Platform | null = null
|
|
14
|
+
let language: string = DEFAULT_LANGUAGE
|
|
15
|
+
let languageSetByEvent = false
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Triggered when the player switches the client language mid-session.
|
|
19
|
+
* Older clients never emit it.
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export const onPlayerLanguageChanged = new Observable<{ language: string }>()
|
|
23
|
+
|
|
24
|
+
function normalizeLocale(locale: string | undefined): string {
|
|
25
|
+
const tag = (locale ?? '').trim().replace(/_/g, '-')
|
|
26
|
+
return BCP47_TAG.test(tag) ? tag : DEFAULT_LANGUAGE
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function setLanguage(next: string) {
|
|
30
|
+
if (next === language) return
|
|
31
|
+
language = next
|
|
32
|
+
onPlayerLanguageChanged.notifyObservers({ language })
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
localeChangedEvents.add((event) => {
|
|
36
|
+
languageSetByEvent = true
|
|
37
|
+
setLanguage(normalizeLocale(event.locale))
|
|
38
|
+
})
|
|
39
|
+
subscribe({ eventId: 'localeChanged' }).catch(console.error)
|
|
40
|
+
|
|
41
|
+
void getExplorerInformation({})
|
|
42
|
+
.then((response) => {
|
|
43
|
+
// a mid-session change that arrived first is more recent than this response
|
|
44
|
+
if (!languageSetByEvent) {
|
|
45
|
+
setLanguage(normalizeLocale(response.configurations?.['locale']))
|
|
46
|
+
}
|
|
47
|
+
const normalized = response.platform.toLowerCase()
|
|
48
|
+
if (VALID_PLATFORMS.has(normalized)) {
|
|
49
|
+
platform = normalized as Platform
|
|
50
|
+
} else {
|
|
51
|
+
console.error(`Unknown platform value: "${response.platform}"`)
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
.catch((error) => {
|
|
55
|
+
console.error('Failed to get explorer information:', error)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
export function getPlatform(): Platform | null {
|
|
59
|
+
return platform
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function isMobile(): boolean {
|
|
63
|
+
return getPlatform() === 'mobile'
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function isDesktop(): boolean {
|
|
67
|
+
return getPlatform() === 'desktop'
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function isWeb(): boolean {
|
|
71
|
+
return getPlatform() === 'web'
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Player's client language as a BCP-47 tag (e.g. `'es'`, `'pt-BR'`).
|
|
76
|
+
* Returns `'en'` until the explorer responds, and on clients that don't report a locale.
|
|
77
|
+
* Use `onPlayerLanguageChanged` to react to mid-session changes.
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
export function getPlayerLanguage(): string {
|
|
81
|
+
return language
|
|
82
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Entity, IEngine, TransformType, engine } from '@dcl/ecs'
|
|
2
|
+
import {
|
|
3
|
+
PlayerIdentityData as definePlayerIdenityData,
|
|
4
|
+
AvatarBase as defineAvatarBase,
|
|
5
|
+
AvatarEquippedData as defineAvatarEquippedData,
|
|
6
|
+
PBAvatarBase,
|
|
7
|
+
PBAvatarEquippedData,
|
|
8
|
+
Transform as defineTransform
|
|
9
|
+
} from '@dcl/ecs/dist/components'
|
|
10
|
+
|
|
11
|
+
type GetPlayerDataReq = {
|
|
12
|
+
userId: string
|
|
13
|
+
}
|
|
14
|
+
type GetPlayerDataRes = {
|
|
15
|
+
entity: Entity
|
|
16
|
+
name: string
|
|
17
|
+
isGuest: boolean
|
|
18
|
+
userId: string
|
|
19
|
+
avatar?: PBAvatarBase
|
|
20
|
+
wearables: PBAvatarEquippedData['wearableUrns']
|
|
21
|
+
emotes: PBAvatarEquippedData['emoteUrns']
|
|
22
|
+
position: TransformType['position'] | undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function definePlayerHelper(engine: IEngine) {
|
|
26
|
+
const Transform = defineTransform(engine)
|
|
27
|
+
const PlayerIdentityData = definePlayerIdenityData(engine)
|
|
28
|
+
const AvatarEquippedData = defineAvatarEquippedData(engine)
|
|
29
|
+
const AvatarBase = defineAvatarBase(engine)
|
|
30
|
+
const playerEntities = new Map<Entity, string>()
|
|
31
|
+
|
|
32
|
+
const onEnterSceneCb: ((player: GetPlayerDataRes) => void)[] = []
|
|
33
|
+
const onLeaveSceneCb: ((userId: string) => void)[] = []
|
|
34
|
+
|
|
35
|
+
engine.addSystem(() => {
|
|
36
|
+
const players = Array.from(engine.getEntitiesWith(PlayerIdentityData, AvatarBase))
|
|
37
|
+
if (players.length === playerEntities.size) return
|
|
38
|
+
|
|
39
|
+
for (const [entity, identity] of players) {
|
|
40
|
+
if (!playerEntities.has(entity)) {
|
|
41
|
+
playerEntities.set(entity, identity.address)
|
|
42
|
+
|
|
43
|
+
// Call onEnter callback
|
|
44
|
+
if (onEnterSceneCb.length) {
|
|
45
|
+
onEnterSceneCb.forEach((cb) => cb(getPlayer({ userId: identity.address })!))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Check for changes/remove callbacks
|
|
49
|
+
AvatarBase.onChange(entity, (value) => {
|
|
50
|
+
if (!value && playerEntities.get(entity)) {
|
|
51
|
+
onLeaveSceneCb.forEach((cb) => cb(playerEntities.get(entity)!))
|
|
52
|
+
playerEntities.delete(entity)
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
onEnterScene(cb: (player: GetPlayerDataRes) => void) {
|
|
61
|
+
onEnterSceneCb.push(cb)
|
|
62
|
+
},
|
|
63
|
+
onLeaveScene(cb: (userId: string) => void) {
|
|
64
|
+
onLeaveSceneCb.push(cb)
|
|
65
|
+
},
|
|
66
|
+
/**
|
|
67
|
+
* Returns the info of the player if it's in the scene.
|
|
68
|
+
*/
|
|
69
|
+
getPlayer(user?: GetPlayerDataReq): GetPlayerDataRes | null {
|
|
70
|
+
function getEntity() {
|
|
71
|
+
if (!user?.userId) return engine.PlayerEntity
|
|
72
|
+
for (const [entity, data] of engine.getEntitiesWith(PlayerIdentityData)) {
|
|
73
|
+
if (data.address === user.userId) {
|
|
74
|
+
return entity
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return undefined
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const userEntity = getEntity()
|
|
81
|
+
if (!userEntity) return null
|
|
82
|
+
|
|
83
|
+
const playerData = PlayerIdentityData.getOrNull(userEntity)
|
|
84
|
+
const avatarData = AvatarBase.getOrNull(userEntity)
|
|
85
|
+
const wearablesData = AvatarEquippedData.getOrNull(userEntity)
|
|
86
|
+
|
|
87
|
+
if (!playerData && !avatarData && !wearablesData) return null
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
entity: userEntity,
|
|
91
|
+
name: avatarData?.name ?? '',
|
|
92
|
+
isGuest: !!playerData?.isGuest,
|
|
93
|
+
userId: playerData?.address ?? '',
|
|
94
|
+
avatar: avatarData ?? undefined,
|
|
95
|
+
// Component reads come back deeply readonly, so these are copied rather than
|
|
96
|
+
// aliased: the public Player type promises plain mutable arrays, and handing
|
|
97
|
+
// out the component's own frozen array would throw on any caller that writes.
|
|
98
|
+
wearables: [...(wearablesData?.wearableUrns ?? [])],
|
|
99
|
+
emotes: [...(wearablesData?.emoteUrns ?? [])],
|
|
100
|
+
position: Transform.getOrNull(userEntity)?.position
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const players = definePlayerHelper(engine)
|
|
107
|
+
const { getPlayer, onEnterScene, onLeaveScene } = players
|
|
108
|
+
|
|
109
|
+
export { getPlayer, onEnterScene, onLeaveScene }
|
|
110
|
+
export default players
|
package/src/react-ecs.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The module react-ecs is exposed by the sdk in the /react-ecs path.
|
|
3
|
+
*
|
|
4
|
+
* UI components and semantics for the SDK 7.
|
|
5
|
+
*
|
|
6
|
+
* JSX & Flexbox is used due to its market adoption and availability of implementations and documentation and expertise.
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* import ReactEcs, { Label, ReactEcsRenderer } from '@dcl/sdk/react-ecs'
|
|
10
|
+
* const Ui = () => <Label value="SDK 7" />
|
|
11
|
+
* ReactEcsRenderer.setUiRenderer(ui)
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
*
|
|
15
|
+
* Go to the Function section to see all the UI Components.
|
|
16
|
+
* @module ReactEcs
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import ReactEcs from '@dcl/react-ecs'
|
|
21
|
+
import { setIsMobileProvider } from '@dcl/react-ecs/dist/platform'
|
|
22
|
+
import { isMobile } from './platform'
|
|
23
|
+
|
|
24
|
+
// react-ecs cannot reach ~system/Runtime itself, so the platform check used
|
|
25
|
+
// for virtual screen size defaults is injected here.
|
|
26
|
+
setIsMobileProvider(isMobile)
|
|
27
|
+
|
|
28
|
+
export * from '@dcl/react-ecs'
|
|
29
|
+
export default ReactEcs
|