@rpgjs/server 5.0.0-alpha.6 → 5.0.0-alpha.8
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/RpgServer.d.ts +224 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/RpgServer.ts +232 -1
- package/src/core/context.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rpgjs/server",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.8",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"publishConfig": {
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"description": "",
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@rpgjs/common": "5.0.0-alpha.
|
|
14
|
+
"@rpgjs/common": "5.0.0-alpha.8",
|
|
15
15
|
"@rpgjs/database": "^4.3.0",
|
|
16
16
|
"@signe/di": "^2.3.2",
|
|
17
17
|
"@signe/reactive": "^2.3.2",
|
package/src/RpgServer.ts
CHANGED
|
@@ -227,20 +227,251 @@ export interface RpgPlayerHooks {
|
|
|
227
227
|
canChangeMap?: (player: RpgPlayer, nextMap: RpgClassMap<RpgMap>) => boolean | Promise<boolean>
|
|
228
228
|
}
|
|
229
229
|
|
|
230
|
+
/**
|
|
231
|
+
* Event hooks interface for handling various event lifecycle methods
|
|
232
|
+
*
|
|
233
|
+
* @interface RpgEventHooks
|
|
234
|
+
* @since 4.0.0
|
|
235
|
+
*/
|
|
230
236
|
export interface RpgEventHooks {
|
|
237
|
+
/**
|
|
238
|
+
* Called as soon as the event is created on the map
|
|
239
|
+
*
|
|
240
|
+
* @param {RpgEvent} event - The event instance being initialized
|
|
241
|
+
* @returns {any}
|
|
242
|
+
* @memberof RpgEventHooks
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* const eventHooks: RpgEventHooks = {
|
|
246
|
+
* onInit(event) {
|
|
247
|
+
* console.log(`Event ${event.name} initialized`)
|
|
248
|
+
* event.graphic('default-sprite')
|
|
249
|
+
* }
|
|
250
|
+
* }
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
231
253
|
onInit?: (event: RpgEvent) => any,
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Called when the event collides with a player and the player presses the action key
|
|
257
|
+
*
|
|
258
|
+
* @param {RpgEvent} event - The event being interacted with
|
|
259
|
+
* @param {RpgPlayer} player - The player performing the action
|
|
260
|
+
* @returns {any}
|
|
261
|
+
* @memberof RpgEventHooks
|
|
262
|
+
* @example
|
|
263
|
+
* ```ts
|
|
264
|
+
* const eventHooks: RpgEventHooks = {
|
|
265
|
+
* onAction(event, player) {
|
|
266
|
+
* player.showText('You activated the chest!')
|
|
267
|
+
* player.addItem('POTION', 1)
|
|
268
|
+
* }
|
|
269
|
+
* }
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
232
272
|
onAction?: (event: RpgEvent, player: RpgPlayer) => any
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Called before an event object is created and added to the map
|
|
276
|
+
* Allows modification of event properties before instantiation
|
|
277
|
+
*
|
|
278
|
+
* @param {any} object - The event object data before creation
|
|
279
|
+
* @param {RpgMap} map - The map where the event will be created
|
|
280
|
+
* @returns {any}
|
|
281
|
+
* @memberof RpgEventHooks
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* const eventHooks: RpgEventHooks = {
|
|
285
|
+
* onBeforeCreated(object, map) {
|
|
286
|
+
* // Modify event properties based on map conditions
|
|
287
|
+
* if (map.id === 'dungeon') {
|
|
288
|
+
* object.graphic = 'monster-sprite'
|
|
289
|
+
* }
|
|
290
|
+
* }
|
|
291
|
+
* }
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
233
294
|
onBeforeCreated?: (object: any, map: RpgMap) => any
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Called when a player or another event enters a shape attached to this event
|
|
298
|
+
*
|
|
299
|
+
* @param {RpgEvent} event - The event with the attached shape
|
|
300
|
+
* @param {RpgPlayer} player - The player entering the shape
|
|
301
|
+
* @param {RpgShape} shape - The shape being entered
|
|
302
|
+
* @returns {any}
|
|
303
|
+
* @since 4.1.0
|
|
304
|
+
* @memberof RpgEventHooks
|
|
305
|
+
* @example
|
|
306
|
+
* ```ts
|
|
307
|
+
* const eventHooks: RpgEventHooks = {
|
|
308
|
+
* onDetectInShape(event, player, shape) {
|
|
309
|
+
* console.log(`Player ${player.name} entered detection zone`)
|
|
310
|
+
* player.showText('You are being watched...')
|
|
311
|
+
* }
|
|
312
|
+
* }
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
234
315
|
onDetectInShape?: (event: RpgEvent, player: RpgPlayer, shape: RpgShape) => any
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Called when a player or another event leaves a shape attached to this event
|
|
319
|
+
*
|
|
320
|
+
* @param {RpgEvent} event - The event with the attached shape
|
|
321
|
+
* @param {RpgPlayer} player - The player leaving the shape
|
|
322
|
+
* @param {RpgShape} shape - The shape being left
|
|
323
|
+
* @returns {any}
|
|
324
|
+
* @since 4.1.0
|
|
325
|
+
* @memberof RpgEventHooks
|
|
326
|
+
* @example
|
|
327
|
+
* ```ts
|
|
328
|
+
* const eventHooks: RpgEventHooks = {
|
|
329
|
+
* onDetectOutShape(event, player, shape) {
|
|
330
|
+
* console.log(`Player ${player.name} left detection zone`)
|
|
331
|
+
* player.showText('You escaped the watch...')
|
|
332
|
+
* }
|
|
333
|
+
* }
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
235
336
|
onDetectOutShape?: (event: RpgEvent, player: RpgPlayer, shape: RpgShape) => any
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Called when the event enters a shape on the map
|
|
340
|
+
*
|
|
341
|
+
* @param {RpgEvent} event - The event entering the shape
|
|
342
|
+
* @param {RpgShape} shape - The shape being entered
|
|
343
|
+
* @returns {any}
|
|
344
|
+
* @memberof RpgEventHooks
|
|
345
|
+
* @example
|
|
346
|
+
* ```ts
|
|
347
|
+
* const eventHooks: RpgEventHooks = {
|
|
348
|
+
* onInShape(event, shape) {
|
|
349
|
+
* console.log(`Event entered shape: ${shape.id}`)
|
|
350
|
+
* event.speed = 1 // Slow down in this area
|
|
351
|
+
* }
|
|
352
|
+
* }
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
236
355
|
onInShape?: (event: RpgEvent, shape: RpgShape) => any
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Called when the event leaves a shape on the map
|
|
359
|
+
*
|
|
360
|
+
* @param {RpgEvent} event - The event leaving the shape
|
|
361
|
+
* @param {RpgShape} shape - The shape being left
|
|
362
|
+
* @returns {any}
|
|
363
|
+
* @memberof RpgEventHooks
|
|
364
|
+
* @example
|
|
365
|
+
* ```ts
|
|
366
|
+
* const eventHooks: RpgEventHooks = {
|
|
367
|
+
* onOutShape(event, shape) {
|
|
368
|
+
* console.log(`Event left shape: ${shape.id}`)
|
|
369
|
+
* event.speed = 3 // Resume normal speed
|
|
370
|
+
* }
|
|
371
|
+
* }
|
|
372
|
+
* ```
|
|
373
|
+
*/
|
|
237
374
|
onOutShape?: (event: RpgEvent, shape: RpgShape) => any
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Called when the event collides with a player (without requiring action key press)
|
|
378
|
+
*
|
|
379
|
+
* @param {RpgEvent} event - The event touching the player
|
|
380
|
+
* @param {RpgPlayer} player - The player being touched
|
|
381
|
+
* @returns {any}
|
|
382
|
+
* @memberof RpgEventHooks
|
|
383
|
+
* @example
|
|
384
|
+
* ```ts
|
|
385
|
+
* const eventHooks: RpgEventHooks = {
|
|
386
|
+
* onPlayerTouch(event, player) {
|
|
387
|
+
* player.hp -= 10 // Damage on touch
|
|
388
|
+
* player.showText('Ouch! You touched a spike!')
|
|
389
|
+
* }
|
|
390
|
+
* }
|
|
391
|
+
* ```
|
|
392
|
+
*/
|
|
238
393
|
onPlayerTouch?: (event: RpgEvent, player: RpgPlayer) => any
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Called whenever any event on the map (including itself) is executed or changes state
|
|
397
|
+
* Useful for creating reactive events that respond to map state changes
|
|
398
|
+
*
|
|
399
|
+
* @param {RpgEvent} event - The event listening for changes
|
|
400
|
+
* @param {RpgPlayer} player - The player involved in the change
|
|
401
|
+
* @returns {any}
|
|
402
|
+
* @memberof RpgEventHooks
|
|
403
|
+
* @example
|
|
404
|
+
* ```ts
|
|
405
|
+
* const eventHooks: RpgEventHooks = {
|
|
406
|
+
* onChanges(event, player) {
|
|
407
|
+
* // Change chest graphic based on game state
|
|
408
|
+
* if (player.getVariable('BATTLE_END')) {
|
|
409
|
+
* event.graphic('chest-open')
|
|
410
|
+
* } else {
|
|
411
|
+
* event.graphic('chest-close')
|
|
412
|
+
* }
|
|
413
|
+
* }
|
|
414
|
+
* }
|
|
415
|
+
* ```
|
|
416
|
+
*/
|
|
239
417
|
onChanges?: (event: RpgEvent, player: RpgPlayer) => any
|
|
240
418
|
}
|
|
241
419
|
|
|
420
|
+
/**
|
|
421
|
+
* Map hooks interface for handling map lifecycle events
|
|
422
|
+
*
|
|
423
|
+
* @interface RpgMapHooks
|
|
424
|
+
* @since 4.0.0
|
|
425
|
+
*/
|
|
242
426
|
export interface RpgMapHooks {
|
|
243
|
-
|
|
427
|
+
/**
|
|
428
|
+
* Called before a map is updated with new data
|
|
429
|
+
* Allows modification of map data before the update is applied
|
|
430
|
+
*
|
|
431
|
+
* The `mapData` parameter contains the loaded map data (retrieved from request body)
|
|
432
|
+
* You can modify the map before the update is processed
|
|
433
|
+
*
|
|
434
|
+
* @template T - Type of the incoming map data
|
|
435
|
+
* @template U - Type of the map instance (defaults to RpgMap)
|
|
436
|
+
* @param {T} mapData - The map data loaded from external source (e.g., request body)
|
|
437
|
+
* @param {U} map - The current map instance being updated
|
|
438
|
+
* @returns {U | Promise<U>} The modified map instance or a promise resolving to it
|
|
439
|
+
* @memberof RpgMapHooks
|
|
440
|
+
* @example
|
|
441
|
+
* ```ts
|
|
442
|
+
* const mapHooks: RpgMapHooks = {
|
|
443
|
+
* onBeforeUpdate(mapData, map) {
|
|
444
|
+
* // Modify map properties based on incoming data
|
|
445
|
+
* if (mapData.weather === 'rain') {
|
|
446
|
+
* map.setWeatherEffect('rain')
|
|
447
|
+
* }
|
|
448
|
+
*
|
|
449
|
+
* // Add custom properties from external data
|
|
450
|
+
* map.customProperty = mapData.customValue
|
|
451
|
+
*
|
|
452
|
+
* return map
|
|
453
|
+
* }
|
|
454
|
+
* }
|
|
455
|
+
* ```
|
|
456
|
+
*
|
|
457
|
+
* @example
|
|
458
|
+
* ```ts
|
|
459
|
+
* // Async example with database operations
|
|
460
|
+
* const mapHooks: RpgMapHooks = {
|
|
461
|
+
* async onBeforeUpdate(mapData, map) {
|
|
462
|
+
* // Load additional data from database
|
|
463
|
+
* const additionalData = await database.getMapExtras(map.id)
|
|
464
|
+
*
|
|
465
|
+
* // Apply modifications
|
|
466
|
+
* map.events = [...map.events, ...additionalData.events]
|
|
467
|
+
* map.npcs = additionalData.npcs
|
|
468
|
+
*
|
|
469
|
+
* return map
|
|
470
|
+
* }
|
|
471
|
+
* }
|
|
472
|
+
* ```
|
|
473
|
+
*/
|
|
474
|
+
onBeforeUpdate<T, U = RpgMap>(mapData: T, map: U): U | Promise<U>
|
|
244
475
|
}
|
|
245
476
|
|
|
246
477
|
export interface RpgServer {
|
package/src/core/context.ts
CHANGED