@rpgjs/server 4.3.0 → 5.0.0-alpha.1
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/{lib → dist}/Gui/DialogGui.d.ts +1 -2
- package/{lib → dist}/Gui/Gui.d.ts +1 -2
- package/dist/Player/BattleManager.d.ts +22 -0
- package/dist/Player/ClassManager.d.ts +18 -0
- package/dist/Player/Event.d.ts +0 -0
- package/dist/Player/ItemManager.d.ts +17 -0
- package/dist/Player/MoveManager.d.ts +177 -0
- package/dist/Player/ParameterManager.d.ts +42 -0
- package/dist/Player/Player.d.ts +73 -0
- package/dist/Player/SkillManager.d.ts +23 -0
- package/dist/Player/StateManager.d.ts +39 -0
- package/{lib → dist}/RpgServer.d.ts +32 -15
- package/dist/RpgServerEngine.d.ts +5 -0
- package/dist/core/context.d.ts +2 -0
- package/dist/core/inject.d.ts +5 -0
- package/dist/core/setup.d.ts +6 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +29673 -0
- package/dist/index.js.map +1 -0
- package/dist/module.d.ts +2 -0
- package/dist/rooms/lobby.d.ts +6 -0
- package/dist/rooms/map.d.ts +110 -0
- package/package.json +15 -37
- package/src/Gui/DialogGui.ts +3 -4
- package/src/Gui/Gui.ts +4 -6
- package/src/Player/BattleManager.ts +108 -99
- package/src/Player/ClassManager.ts +47 -46
- package/src/Player/ComponentManager.ts +26 -535
- package/src/Player/EffectManager.ts +44 -59
- package/src/Player/ElementManager.ts +70 -72
- package/src/Player/Event.ts +0 -0
- package/src/Player/GoldManager.ts +24 -6
- package/src/Player/GuiManager.ts +106 -97
- package/src/Player/ItemFixture.ts +22 -17
- package/src/Player/ItemManager.ts +234 -225
- package/src/Player/MoveManager.ts +1047 -457
- package/src/Player/ParameterManager.ts +59 -37
- package/src/Player/Player.ts +272 -1058
- package/src/Player/SkillManager.ts +146 -137
- package/src/Player/StateManager.ts +124 -98
- package/src/Player/VariableManager.ts +23 -3
- package/src/RpgServer.ts +36 -12
- package/src/RpgServerEngine.ts +7 -0
- package/src/core/context.ts +3 -0
- package/src/core/inject.ts +17 -0
- package/src/core/setup.ts +20 -0
- package/src/index.ts +9 -31
- package/src/module.ts +32 -0
- package/src/presets/index.ts +1 -3
- package/src/rooms/lobby.ts +24 -0
- package/src/rooms/map.ts +311 -0
- package/tsconfig.json +26 -23
- package/vite.config.ts +21 -0
- package/CHANGELOG.md +0 -174
- package/LICENSE +0 -19
- package/browser/manifest.json +0 -7
- package/browser/rpg.server.js +0 -22379
- package/browser/rpg.server.umd.cjs +0 -22410
- package/lib/Game/EventManager.d.ts +0 -54
- package/lib/Game/EventManager.js +0 -107
- package/lib/Game/EventManager.js.map +0 -1
- package/lib/Game/Map.d.ts +0 -191
- package/lib/Game/Map.js +0 -419
- package/lib/Game/Map.js.map +0 -1
- package/lib/Game/WorldMaps.d.ts +0 -17
- package/lib/Game/WorldMaps.js +0 -28
- package/lib/Game/WorldMaps.js.map +0 -1
- package/lib/Gui/DialogGui.js +0 -57
- package/lib/Gui/DialogGui.js.map +0 -1
- package/lib/Gui/Gui.js +0 -37
- package/lib/Gui/Gui.js.map +0 -1
- package/lib/Gui/MenuGui.js +0 -23
- package/lib/Gui/MenuGui.js.map +0 -1
- package/lib/Gui/NotificationGui.js +0 -8
- package/lib/Gui/NotificationGui.js.map +0 -1
- package/lib/Gui/ShopGui.js +0 -40
- package/lib/Gui/ShopGui.js.map +0 -1
- package/lib/Gui/index.js +0 -7
- package/lib/Gui/index.js.map +0 -1
- package/lib/Interfaces/Gui.d.ts +0 -4
- package/lib/Interfaces/Gui.js +0 -2
- package/lib/Interfaces/Gui.js.map +0 -1
- package/lib/Interfaces/StateStore.d.ts +0 -5
- package/lib/Interfaces/StateStore.js +0 -2
- package/lib/Interfaces/StateStore.js.map +0 -1
- package/lib/MatchMaker.d.ts +0 -24
- package/lib/MatchMaker.js +0 -42
- package/lib/MatchMaker.js.map +0 -1
- package/lib/Monitor/index.d.ts +0 -17
- package/lib/Monitor/index.js +0 -72
- package/lib/Monitor/index.js.map +0 -1
- package/lib/Player/BattleManager.d.ts +0 -36
- package/lib/Player/BattleManager.js +0 -101
- package/lib/Player/BattleManager.js.map +0 -1
- package/lib/Player/ClassManager.d.ts +0 -47
- package/lib/Player/ClassManager.js +0 -65
- package/lib/Player/ClassManager.js.map +0 -1
- package/lib/Player/ComponentManager.d.ts +0 -397
- package/lib/Player/ComponentManager.js +0 -505
- package/lib/Player/ComponentManager.js.map +0 -1
- package/lib/Player/EffectManager.d.ts +0 -46
- package/lib/Player/EffectManager.js +0 -75
- package/lib/Player/EffectManager.js.map +0 -1
- package/lib/Player/ElementManager.d.ts +0 -108
- package/lib/Player/ElementManager.js +0 -120
- package/lib/Player/ElementManager.js.map +0 -1
- package/lib/Player/GoldManager.d.ts +0 -17
- package/lib/Player/GoldManager.js +0 -27
- package/lib/Player/GoldManager.js.map +0 -1
- package/lib/Player/GuiManager.d.ts +0 -247
- package/lib/Player/GuiManager.js +0 -291
- package/lib/Player/GuiManager.js.map +0 -1
- package/lib/Player/ItemFixture.d.ts +0 -7
- package/lib/Player/ItemFixture.js +0 -19
- package/lib/Player/ItemFixture.js.map +0 -1
- package/lib/Player/ItemManager.d.ts +0 -331
- package/lib/Player/ItemManager.js +0 -475
- package/lib/Player/ItemManager.js.map +0 -1
- package/lib/Player/MoveManager.d.ts +0 -333
- package/lib/Player/MoveManager.js +0 -450
- package/lib/Player/MoveManager.js.map +0 -1
- package/lib/Player/ParameterManager.d.ts +0 -316
- package/lib/Player/ParameterManager.js +0 -408
- package/lib/Player/ParameterManager.js.map +0 -1
- package/lib/Player/Player.d.ts +0 -828
- package/lib/Player/Player.js +0 -927
- package/lib/Player/Player.js.map +0 -1
- package/lib/Player/SkillManager.d.ts +0 -163
- package/lib/Player/SkillManager.js +0 -223
- package/lib/Player/SkillManager.js.map +0 -1
- package/lib/Player/StateManager.d.ts +0 -185
- package/lib/Player/StateManager.js +0 -228
- package/lib/Player/StateManager.js.map +0 -1
- package/lib/Player/VariableManager.d.ts +0 -46
- package/lib/Player/VariableManager.js +0 -52
- package/lib/Player/VariableManager.js.map +0 -1
- package/lib/Query.d.ts +0 -127
- package/lib/Query.js +0 -159
- package/lib/Query.js.map +0 -1
- package/lib/RpgServer.js +0 -2
- package/lib/RpgServer.js.map +0 -1
- package/lib/Scenes/Map.d.ts +0 -136
- package/lib/Scenes/Map.js +0 -273
- package/lib/Scenes/Map.js.map +0 -1
- package/lib/decorators/event.d.ts +0 -43
- package/lib/decorators/event.js +0 -13
- package/lib/decorators/event.js.map +0 -1
- package/lib/decorators/map.d.ts +0 -178
- package/lib/decorators/map.js +0 -43
- package/lib/decorators/map.js.map +0 -1
- package/lib/entry-point.d.ts +0 -34
- package/lib/entry-point.js +0 -70
- package/lib/entry-point.js.map +0 -1
- package/lib/express/api.d.ts +0 -3
- package/lib/express/api.js +0 -105
- package/lib/express/api.js.map +0 -1
- package/lib/express/errors/NotAuthorized.d.ts +0 -4
- package/lib/express/errors/NotAuthorized.js +0 -7
- package/lib/express/errors/NotAuthorized.js.map +0 -1
- package/lib/express/errors/NotFound.d.ts +0 -4
- package/lib/express/errors/NotFound.js +0 -7
- package/lib/express/errors/NotFound.js.map +0 -1
- package/lib/express/server.d.ts +0 -18
- package/lib/express/server.js +0 -70
- package/lib/express/server.js.map +0 -1
- package/lib/index.d.ts +0 -20
- package/lib/index.js +0 -19
- package/lib/index.js.map +0 -1
- package/lib/inject.d.ts +0 -22
- package/lib/inject.js +0 -29
- package/lib/inject.js.map +0 -1
- package/lib/logs/index.js +0 -6
- package/lib/logs/index.js.map +0 -1
- package/lib/logs/item.js +0 -34
- package/lib/logs/item.js.map +0 -1
- package/lib/logs/log.js +0 -7
- package/lib/logs/log.js.map +0 -1
- package/lib/logs/skill.js +0 -19
- package/lib/logs/skill.js.map +0 -1
- package/lib/logs/state.js +0 -13
- package/lib/logs/state.js.map +0 -1
- package/lib/models/Item.d.ts +0 -10
- package/lib/models/Item.js +0 -2
- package/lib/models/Item.js.map +0 -1
- package/lib/presets/index.js +0 -65
- package/lib/presets/index.js.map +0 -1
- package/lib/server.d.ts +0 -137
- package/lib/server.js +0 -443
- package/lib/server.js.map +0 -1
- package/rpg.toml +0 -14
- package/src/Game/EventManager.ts +0 -125
- package/src/Game/Map.ts +0 -500
- package/src/Game/WorldMaps.ts +0 -45
- package/src/Interfaces/Gui.ts +0 -4
- package/src/Interfaces/StateStore.ts +0 -5
- package/src/MatchMaker.ts +0 -65
- package/src/Monitor/index.ts +0 -78
- package/src/Query.ts +0 -172
- package/src/Scenes/Map.ts +0 -310
- package/src/decorators/event.ts +0 -58
- package/src/decorators/map.ts +0 -226
- package/src/entry-point.ts +0 -111
- package/src/express/api.ts +0 -118
- package/src/express/errors/NotAuthorized.ts +0 -6
- package/src/express/errors/NotFound.ts +0 -6
- package/src/express/server.ts +0 -94
- package/src/inject.ts +0 -33
- package/src/models/Item.ts +0 -11
- package/src/server.ts +0 -459
- /package/{lib → dist}/Gui/MenuGui.d.ts +0 -0
- /package/{lib → dist}/Gui/NotificationGui.d.ts +0 -0
- /package/{lib → dist}/Gui/ShopGui.d.ts +0 -0
- /package/{lib → dist}/Gui/index.d.ts +0 -0
- /package/{lib → dist}/logs/index.d.ts +0 -0
- /package/{lib → dist}/logs/item.d.ts +0 -0
- /package/{lib → dist}/logs/log.d.ts +0 -0
- /package/{lib → dist}/logs/skill.d.ts +0 -0
- /package/{lib → dist}/logs/state.d.ts +0 -0
- /package/{lib → dist}/presets/index.d.ts +0 -0
package/src/MatchMaker.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import axios from 'axios'
|
|
3
|
-
import { Utils } from '@rpgjs/common'
|
|
4
|
-
import { RpgPlayer } from './Player/Player'
|
|
5
|
-
import { inject } from './inject'
|
|
6
|
-
import { RpgServerEngine } from './server'
|
|
7
|
-
|
|
8
|
-
interface MatchMakerPayload {
|
|
9
|
-
playerId: string
|
|
10
|
-
mapName: string
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface MatchMakerResponse {
|
|
14
|
-
url: string
|
|
15
|
-
port: number
|
|
16
|
-
serverId: string
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface MatchMakerOption {
|
|
20
|
-
endpoint?: string
|
|
21
|
-
headers?: any
|
|
22
|
-
callback?: (payload: MatchMakerPayload) => MatchMakerResponse
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export class RpgMatchMaker {
|
|
26
|
-
private endpoint?: string
|
|
27
|
-
private headers?: any
|
|
28
|
-
private callback?: (payload: MatchMakerPayload) => MatchMakerResponse
|
|
29
|
-
|
|
30
|
-
constructor(private options: MatchMakerOption) {
|
|
31
|
-
this.endpoint = options.endpoint
|
|
32
|
-
this.headers = options.headers
|
|
33
|
-
this.callback = options.callback
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async getServer(player: RpgPlayer): Promise<MatchMakerResponse | null> {
|
|
37
|
-
const currentServerId = inject(RpgServerEngine).serverId
|
|
38
|
-
const payload: MatchMakerPayload = {
|
|
39
|
-
playerId: player.id,
|
|
40
|
-
mapName: player.map
|
|
41
|
-
}
|
|
42
|
-
let res = {} as MatchMakerResponse
|
|
43
|
-
if (this.callback) {
|
|
44
|
-
res = this.callback(payload)
|
|
45
|
-
if (Utils.isPromise(res)) {
|
|
46
|
-
res = await res
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
if (this.endpoint) {
|
|
50
|
-
try {
|
|
51
|
-
res = await axios.post<MatchMakerResponse>(this.endpoint, payload, {
|
|
52
|
-
headers: this.headers
|
|
53
|
-
}).then(res => res.data)
|
|
54
|
-
}
|
|
55
|
-
catch (err) {
|
|
56
|
-
console.log('There is a problem with the MatchMaker webservice.')
|
|
57
|
-
throw err
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (currentServerId == res.serverId) {
|
|
61
|
-
return null
|
|
62
|
-
}
|
|
63
|
-
return res
|
|
64
|
-
}
|
|
65
|
-
}
|
package/src/Monitor/index.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from '@rpgjs/common'
|
|
2
|
-
import os from 'os'
|
|
3
|
-
|
|
4
|
-
class Monitor extends EventEmitter {
|
|
5
|
-
private monitors: Map<string, any> = new Map()
|
|
6
|
-
private lastTime: any
|
|
7
|
-
loopMs
|
|
8
|
-
totalConnected: number = 0
|
|
9
|
-
|
|
10
|
-
get status() {
|
|
11
|
-
return {
|
|
12
|
-
loop: this.loopMs,
|
|
13
|
-
totalConnected: this.totalConnected
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
getStatusOf(id) {
|
|
18
|
-
return this.monitors.get(id)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
update(server) {
|
|
22
|
-
const { scheduler, options } = server
|
|
23
|
-
if (this.lastTime) {
|
|
24
|
-
const hrtime = process.hrtime(this.lastTime)
|
|
25
|
-
this.loopMs = hrtime[1] / 1e6
|
|
26
|
-
}
|
|
27
|
-
const period = scheduler.options.period
|
|
28
|
-
if (this.loopMs > period + 20) {
|
|
29
|
-
const { stepRate } = options
|
|
30
|
-
console.warn('%s - Warning Low FPS. %s players connected. Game Loop: %s FPS', new Date(), this.totalConnected, Math.round(stepRate / (this.loopMs / period)))
|
|
31
|
-
}
|
|
32
|
-
this.lastTime = process.hrtime()
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
addMonitor(socket) {
|
|
36
|
-
// TODO
|
|
37
|
-
/*
|
|
38
|
-
let bytesIn = 0
|
|
39
|
-
let bytesOut = 0
|
|
40
|
-
const update = () => {
|
|
41
|
-
this.monitors.set(id, {
|
|
42
|
-
bytesIn: bytesIn.toFixed(2),
|
|
43
|
-
bytesOut: bytesOut.toFixed(2)
|
|
44
|
-
})
|
|
45
|
-
}
|
|
46
|
-
const id = socket.id
|
|
47
|
-
socket.use((packet, next) => {
|
|
48
|
-
const size = ('42'+JSON.stringify(packet)).length
|
|
49
|
-
bytesIn += size
|
|
50
|
-
update()
|
|
51
|
-
this.emit('update', this.monitors.get(id))
|
|
52
|
-
next()
|
|
53
|
-
})
|
|
54
|
-
socket._emit = socket.emit.bind(socket)
|
|
55
|
-
socket.emit = (name, data) => {
|
|
56
|
-
let size = 0
|
|
57
|
-
let obj = data
|
|
58
|
-
if (name == 'worldUpdate') {
|
|
59
|
-
obj = Object.assign({}, data)
|
|
60
|
-
size += Buffer.byteLength(obj.dataBuffer)
|
|
61
|
-
obj.dataBuffer = {_placeholder: true, num: 0}
|
|
62
|
-
}
|
|
63
|
-
size += `451-["${name},${JSON.stringify(obj)}"]`.length
|
|
64
|
-
bytesOut += size
|
|
65
|
-
update()
|
|
66
|
-
return socket._emit(name, data)
|
|
67
|
-
}
|
|
68
|
-
*/
|
|
69
|
-
this.totalConnected++
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
removeMonitor(id) {
|
|
73
|
-
this.monitors.delete(id)
|
|
74
|
-
this.totalConnected--
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export default new Monitor()
|
package/src/Query.ts
DELETED
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import { World } from 'simple-room'
|
|
2
|
-
import { RpgShape, Utils } from '@rpgjs/common'
|
|
3
|
-
import { RpgPlayer } from './Player/Player'
|
|
4
|
-
import { Observable } from 'rxjs'
|
|
5
|
-
|
|
6
|
-
const { isString } = Utils
|
|
7
|
-
|
|
8
|
-
class QueryClass {
|
|
9
|
-
/**
|
|
10
|
-
* Listen to the changes on all the rooms
|
|
11
|
-
*
|
|
12
|
-
* ```ts
|
|
13
|
-
* import { RpgWorld } from '@rpgjs/server'
|
|
14
|
-
* import { map } from 'rxjs/operators' // install rxjs
|
|
15
|
-
*
|
|
16
|
-
* RpgWorld.changes
|
|
17
|
-
* .pipe(
|
|
18
|
-
* map(rooms => rooms['mymap'])
|
|
19
|
-
* )
|
|
20
|
-
* .subscribe((room) => {
|
|
21
|
-
* const users: any = Object.values(room.users)
|
|
22
|
-
* console.log(users)
|
|
23
|
-
* })
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* @title Subscribe to the world
|
|
27
|
-
* @prop {Observable} RpgWorld.changes
|
|
28
|
-
* @memberof RpgWorld
|
|
29
|
-
* */
|
|
30
|
-
get changes(): Observable<any> {
|
|
31
|
-
return World.changes.asObservable() as any
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Retrieve a player according to his ID
|
|
36
|
-
*
|
|
37
|
-
* ```ts
|
|
38
|
-
* import { RpgWorld } from '@rpgjs/server'
|
|
39
|
-
*
|
|
40
|
-
* const player = RpgWorld.getPlayer(player) // player is RpgPlayer (player.id) or string (id)
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* @title Get Player
|
|
44
|
-
* @method RpgWorld.getPlayer(player)
|
|
45
|
-
* @param {RpgPlayer | string} player identifier
|
|
46
|
-
* @returns {RpgPlayer}
|
|
47
|
-
* @memberof RpgWorld
|
|
48
|
-
*/
|
|
49
|
-
getPlayer(player: RpgPlayer | string): RpgPlayer {
|
|
50
|
-
const id: any = isString(player) ? player : '' + (player as RpgPlayer).id
|
|
51
|
-
const _player: any = World.getUser(id)
|
|
52
|
-
return _player as RpgPlayer
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Recover all the players of the game
|
|
57
|
-
*
|
|
58
|
-
* ```ts
|
|
59
|
-
* import { RpgWorld } from '@rpgjs/server'
|
|
60
|
-
*
|
|
61
|
-
* const players = RpgWorld.getPlayers()
|
|
62
|
-
* ```
|
|
63
|
-
*
|
|
64
|
-
* @title Get all Players
|
|
65
|
-
* @method RpgWorld.getPlayers()
|
|
66
|
-
* @returns {Array<RpgPlayer>}
|
|
67
|
-
* @memberof RpgWorld
|
|
68
|
-
*/
|
|
69
|
-
getPlayers(): RpgPlayer[] {
|
|
70
|
-
const users: any = World.getUsers()
|
|
71
|
-
const array = Object.values(users) as RpgPlayer[]
|
|
72
|
-
return array.map((user: RpgPlayer) => this.getPlayer(user))
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Recover all map objects: players and events. If you specify the `player` parameter, it also retrieves the events in scenario mode of the player in question
|
|
77
|
-
*
|
|
78
|
-
* ```ts
|
|
79
|
-
* import { RpgWorld } from '@rpgjs/server'
|
|
80
|
-
*
|
|
81
|
-
* const objects = RpgWorld.getObjectsOfMap('mapname')
|
|
82
|
-
* console.log(objects)
|
|
83
|
-
* ```
|
|
84
|
-
*
|
|
85
|
-
* Also retrieve events in Scenario mode:
|
|
86
|
-
*
|
|
87
|
-
* ```ts
|
|
88
|
-
* import { RpgWorld } from '@rpgjs/server'
|
|
89
|
-
*
|
|
90
|
-
* const objects = RpgWorld.getObjectsOfMap('mapname', 'playerid')
|
|
91
|
-
* ```
|
|
92
|
-
*
|
|
93
|
-
* @title Get all objects of map
|
|
94
|
-
* @method RpgWorld.getObjectsOfMap(map,playerId?)
|
|
95
|
-
* @param {string} map Map Name
|
|
96
|
-
* @param {RpgPlayer | string} playerId player identifier
|
|
97
|
-
* @returns {Array<RpgPlayer>}
|
|
98
|
-
* @memberof RpgWorld
|
|
99
|
-
*/
|
|
100
|
-
getObjectsOfMap(map: string, playerId?: RpgPlayer | string): RpgPlayer[] {
|
|
101
|
-
return Object.values(this._getObjectsOfMap(map, playerId)) as RpgPlayer[]
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
_getObjectsOfMap(map: string, playerId?: RpgPlayer | string): { [id: string]: RpgPlayer } {
|
|
105
|
-
const room: any = World.getRoom(map)
|
|
106
|
-
let player: any = null
|
|
107
|
-
if (playerId) {
|
|
108
|
-
player = this.getPlayer(playerId)
|
|
109
|
-
}
|
|
110
|
-
return {
|
|
111
|
-
...room.users,
|
|
112
|
-
...room.events,
|
|
113
|
-
...(player ? player.events : {})
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Find all the shapes of the map
|
|
119
|
-
*
|
|
120
|
-
* ```ts
|
|
121
|
-
* import { RpgWorld } from '@rpgjs/server'
|
|
122
|
-
*
|
|
123
|
-
* const shapes = RpgWorld.getShapesOfMap('mapname')
|
|
124
|
-
* console.log(shapes)
|
|
125
|
-
* ```
|
|
126
|
-
*
|
|
127
|
-
* @title Get all shapes of map
|
|
128
|
-
* @method RpgWorld.getShapesOfMap(map)
|
|
129
|
-
* @param {string} map Map Name
|
|
130
|
-
* @returns {Array<RpgShape>}
|
|
131
|
-
* @memberof RpgWorld
|
|
132
|
-
*/
|
|
133
|
-
getShapesOfMap(map: string): RpgShape[] {
|
|
134
|
-
return Object.values(this._getShapesOfMap(map))
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
_getShapesOfMap(map: string): { [id: string]: RpgShape } {
|
|
138
|
-
const room: any = World.getRoom(map)
|
|
139
|
-
return room.shapes
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// TODO
|
|
143
|
-
getRooms<T>(): Map<string, T> {
|
|
144
|
-
return World.getRooms() as Map<string, T>
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
getRoom<T>(name: string): T {
|
|
148
|
-
return World.getRoom(name) as T
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Recover all the players of a map
|
|
153
|
-
*
|
|
154
|
-
* ```ts
|
|
155
|
-
* import { RpgWorld } from '@rpgjs/server'
|
|
156
|
-
*
|
|
157
|
-
* const players = RpgWorld.getPlayersOfMap('mapname')
|
|
158
|
-
* ```
|
|
159
|
-
*
|
|
160
|
-
* @title Get all Players a map
|
|
161
|
-
* @method RpgWorld.getPlayersOfMap(map)
|
|
162
|
-
* @param {string} map Map Name
|
|
163
|
-
* @returns {Array<RpgPlayer>}
|
|
164
|
-
* @memberof RpgWorld
|
|
165
|
-
*/
|
|
166
|
-
getPlayersOfMap(map: string): RpgPlayer[] {
|
|
167
|
-
const room: any = World.getRoom(map)
|
|
168
|
-
return Object.values(room.users) as RpgPlayer[]
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export const Query = new QueryClass()
|
package/src/Scenes/Map.ts
DELETED
|
@@ -1,310 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import { HookServer, RpgCommonMap, RpgPlugin, Utils } from '@rpgjs/common'
|
|
3
|
-
import { World } from 'simple-room'
|
|
4
|
-
import { isTiledFormat, TiledMap } from '@rpgjs/tiled'
|
|
5
|
-
import { MapOptions, MapData } from '../decorators/map'
|
|
6
|
-
import { RpgMap } from '../Game/Map'
|
|
7
|
-
import { RpgWorldMaps, WorldMap } from '../Game/WorldMaps'
|
|
8
|
-
import { RpgEvent, RpgPlayer } from '../Player/Player'
|
|
9
|
-
import { RpgServerEngine } from '../server'
|
|
10
|
-
import { inject } from '../inject'
|
|
11
|
-
|
|
12
|
-
export interface RpgClassMap<T> {
|
|
13
|
-
id?: string
|
|
14
|
-
file?: string
|
|
15
|
-
new(server: any): T,
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
type SceneMapObject = {
|
|
19
|
-
maps: any[],
|
|
20
|
-
worldMaps: WorldMap[]
|
|
21
|
-
events: RpgEvent[]
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export class SceneMap {
|
|
25
|
-
static readonly id: string = 'map'
|
|
26
|
-
|
|
27
|
-
private maps: any[] = []
|
|
28
|
-
private mapsById: {
|
|
29
|
-
[mapId: string]: RpgClassMap<RpgMap>
|
|
30
|
-
} = {}
|
|
31
|
-
private worldMaps: Map<string, RpgWorldMaps> = new Map()
|
|
32
|
-
private server: RpgServerEngine = inject(RpgServerEngine)
|
|
33
|
-
|
|
34
|
-
constructor(sceneMapObject: SceneMapObject) {
|
|
35
|
-
const { maps, worldMaps, events } = sceneMapObject
|
|
36
|
-
this.maps = maps
|
|
37
|
-
this.mapsById = {}
|
|
38
|
-
RpgCommonMap.buffer.clear()
|
|
39
|
-
if (this.maps) {
|
|
40
|
-
for (let map of this.maps) {
|
|
41
|
-
this.createDynamicMap(map)
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
if (worldMaps) {
|
|
45
|
-
for (let worldMap of worldMaps) {
|
|
46
|
-
this.createDynamicWorldMaps(worldMap)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Returns an array of RpgClassMap objects that represent maps with static properties.
|
|
53
|
-
*
|
|
54
|
-
* @title Get maps
|
|
55
|
-
* @method sceneMap.getMaps()
|
|
56
|
-
* @returns {RpgClassMap<RpgMap>[]} Array of RpgClassMap objects.
|
|
57
|
-
* @since 4.0.0
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* const maps = scene.getMaps();
|
|
61
|
-
* console.log(maps);
|
|
62
|
-
* // Output: [
|
|
63
|
-
* // { file: 'maps/level1.tmx', id: 'level1', type: 'map' },
|
|
64
|
-
* // { file: 'maps/level2.tmx', id: 'level1', type: 'map' },
|
|
65
|
-
* // { file: 'maps/level3.tmx', id: 'level1', type: 'map' }
|
|
66
|
-
* // ]
|
|
67
|
-
* ```
|
|
68
|
-
* @memberof SceneMap
|
|
69
|
-
*/
|
|
70
|
-
getMaps(): RpgClassMap<RpgMap>[] {
|
|
71
|
-
return Object.values(this.mapsById)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
getMapBydId(id: string): RpgClassMap<RpgMap> | null {
|
|
75
|
-
let mapClass = this.mapsById[id]
|
|
76
|
-
if (!mapClass) {
|
|
77
|
-
return null
|
|
78
|
-
}
|
|
79
|
-
if (!Utils.isClass(mapClass)) mapClass = Utils.createConstructor<RpgClassMap<RpgMap>>(mapClass)
|
|
80
|
-
return mapClass
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async loadMap(id: string): Promise<RpgMap | undefined> {
|
|
84
|
-
const mapClass = this.getMapBydId(id)
|
|
85
|
-
|
|
86
|
-
if (!mapClass) {
|
|
87
|
-
console.log(`Map ${id} not exists`)
|
|
88
|
-
return
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
let mapInstance
|
|
92
|
-
|
|
93
|
-
if (mapClass['buffer'].has(id)) {
|
|
94
|
-
return mapClass['buffer'].get(id)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (RpgCommonMap.buffer.has(id)) {
|
|
98
|
-
mapInstance = RpgCommonMap.buffer.get(id)
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
const room = new mapClass(this.server)
|
|
102
|
-
room.$schema.users = [
|
|
103
|
-
{
|
|
104
|
-
...RpgPlayer.schemas,
|
|
105
|
-
...this.server['playerProps']
|
|
106
|
-
}
|
|
107
|
-
]
|
|
108
|
-
mapInstance = World.addRoom(id, room)
|
|
109
|
-
await mapInstance.load()
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return mapInstance
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Loads the content of a `.world` file from Tiled Map Editor into the map scene
|
|
117
|
-
*
|
|
118
|
-
* > Note, that if the map already exists (i.e. you have already defined an RpgMap), the world will retrieve the already existing map. Otherwise it will create a new map
|
|
119
|
-
*
|
|
120
|
-
* @title Create worlds dynamically
|
|
121
|
-
* @method sceneMap.createDynamicWorldMaps(world)
|
|
122
|
-
* @param {object} world
|
|
123
|
-
* object is
|
|
124
|
-
* ```ts
|
|
125
|
-
* {
|
|
126
|
-
* id?: string
|
|
127
|
-
* maps: {
|
|
128
|
-
* id?: string
|
|
129
|
-
* properties?: object
|
|
130
|
-
* fileName: string;
|
|
131
|
-
height: number;
|
|
132
|
-
width: number;
|
|
133
|
-
x: number;
|
|
134
|
-
y: number;
|
|
135
|
-
* }[],
|
|
136
|
-
onlyShowAdjacentMaps: boolean, // only for Tiled Map Editor
|
|
137
|
-
type: 'world' // only for Tiled Map Editor
|
|
138
|
-
* }
|
|
139
|
-
* ```
|
|
140
|
-
*
|
|
141
|
-
* @since 3.0.0-beta.8
|
|
142
|
-
* @memberof SceneMap
|
|
143
|
-
*/
|
|
144
|
-
createDynamicWorldMaps(world: WorldMap): RpgWorldMaps {
|
|
145
|
-
world.id = world.id || Utils.generateUID()
|
|
146
|
-
const worldMap = new RpgWorldMaps(world.id).load(world, this)
|
|
147
|
-
this.worldMaps.set(world.id, worldMap)
|
|
148
|
-
return worldMap
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Recover a world
|
|
153
|
-
*
|
|
154
|
-
* @title Recover a world
|
|
155
|
-
* @method sceneMap.getWorldMaps(id)
|
|
156
|
-
* @param {string} id world id
|
|
157
|
-
* @return { RpgWorldMaps | undefined }
|
|
158
|
-
* @since 3.0.0-beta.8
|
|
159
|
-
* @memberof SceneMap
|
|
160
|
-
*/
|
|
161
|
-
getWorldMaps(id: string): RpgWorldMaps | undefined {
|
|
162
|
-
return this.worldMaps.get(id)
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Delete a world
|
|
167
|
-
*
|
|
168
|
-
* @title Delete a world
|
|
169
|
-
* @method sceneMap.deleteWorldMaps(id)
|
|
170
|
-
* @param {string} id world id
|
|
171
|
-
* @since 3.0.0-beta.8
|
|
172
|
-
* @memberof SceneMap
|
|
173
|
-
*/
|
|
174
|
-
deleteWorldMaps(id: string): void {
|
|
175
|
-
this.worldMaps.delete(id)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Create a dynamic map
|
|
180
|
-
*
|
|
181
|
-
* Since version 3.0.0-beta.8, you can just pass the path to the file. The identifier will then be the name of the file
|
|
182
|
-
*
|
|
183
|
-
* @method sceneMap.createDynamicMap(mapData)
|
|
184
|
-
* @title Create a dynamic map
|
|
185
|
-
* @param {object | RpgMap | string} mapData The same property as [@MapData decorator](https://docs.rpgjs.dev/classes/map.html#mapdata-decorator)
|
|
186
|
-
* @returns {RpgMap}
|
|
187
|
-
* @since 3.0.0-beta.4
|
|
188
|
-
* @memberof SceneMap
|
|
189
|
-
* @example
|
|
190
|
-
* ```ts
|
|
191
|
-
* sceneMap.createDynamicMap({
|
|
192
|
-
* id: 'myid',
|
|
193
|
-
* file: require('./tmx/mymap.tmx')
|
|
194
|
-
* })
|
|
195
|
-
* ```
|
|
196
|
-
*
|
|
197
|
-
* And later, on the player:
|
|
198
|
-
*
|
|
199
|
-
* ```ts
|
|
200
|
-
* player.changeMap('myid')
|
|
201
|
-
* ```
|
|
202
|
-
*
|
|
203
|
-
* ---
|
|
204
|
-
*
|
|
205
|
-
* since beta.8
|
|
206
|
-
*
|
|
207
|
-
* ```ts
|
|
208
|
-
* sceneMap.createDynamicMap(require('./tmx/mymap.tmx')) // id is "mymap"
|
|
209
|
-
* ```
|
|
210
|
-
*/
|
|
211
|
-
createDynamicMap(mapData: MapOptions | string | RpgClassMap<RpgMap> | TiledMap): RpgClassMap<RpgMap> | never {
|
|
212
|
-
if (Utils.isString(mapData)) {
|
|
213
|
-
const id = Utils.extractId(mapData as string)
|
|
214
|
-
if (!id) {
|
|
215
|
-
throw new Error('Unable to extract the file identifier. Check that the file has only the following characters: [a-zA-Z0-9-_$!]+')
|
|
216
|
-
}
|
|
217
|
-
mapData = {
|
|
218
|
-
id: id[1],
|
|
219
|
-
file: mapData
|
|
220
|
-
} as MapOptions
|
|
221
|
-
}
|
|
222
|
-
if (isTiledFormat(mapData)) {
|
|
223
|
-
const tiledData = (mapData as TiledMap)
|
|
224
|
-
mapData = {
|
|
225
|
-
file: { ...tiledData }
|
|
226
|
-
} as MapOptions
|
|
227
|
-
}
|
|
228
|
-
if (!(mapData as MapOptions).id) (mapData as MapOptions).id = Utils.generateUID()
|
|
229
|
-
if (!Utils.isClass(mapData)) {
|
|
230
|
-
@MapData(mapData as MapOptions)
|
|
231
|
-
class DynamicMap extends RpgMap { }
|
|
232
|
-
mapData = DynamicMap
|
|
233
|
-
}
|
|
234
|
-
const map: RpgClassMap<RpgMap> = mapData as any
|
|
235
|
-
this.mapsById[map.id as string] = map
|
|
236
|
-
return map
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
async changeMap(
|
|
240
|
-
mapId: string,
|
|
241
|
-
player: RpgPlayer,
|
|
242
|
-
positions?: { x: number, y: number, z?: number } | string
|
|
243
|
-
): Promise<RpgMap | null | boolean> {
|
|
244
|
-
|
|
245
|
-
const boolArray: boolean[] = await RpgPlugin.emit(HookServer.PlayerCanChangeMap, [player, this.getMapBydId(mapId)], true)
|
|
246
|
-
|
|
247
|
-
if (boolArray.some(el => el === false)) {
|
|
248
|
-
return null
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// if just teleport, not change map
|
|
252
|
-
if (player.map === mapId) {
|
|
253
|
-
await player.teleport(positions || 'start')
|
|
254
|
-
return null
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
player.emit('preLoadScene', {
|
|
258
|
-
id: mapId
|
|
259
|
-
})
|
|
260
|
-
|
|
261
|
-
player.prevMap = player.map
|
|
262
|
-
|
|
263
|
-
if (player.prevMap) {
|
|
264
|
-
await player.execMethod('onLeaveMap', <any>[player.getCurrentMap()])
|
|
265
|
-
World.leaveRoom(player.prevMap, player.id)
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
player.map = mapId
|
|
269
|
-
player.events = {}
|
|
270
|
-
player.tmpPositions = positions as any
|
|
271
|
-
|
|
272
|
-
const scalabilityArray: boolean[] = await RpgPlugin.emit(HookServer.ScalabilityChangeServer, player)
|
|
273
|
-
|
|
274
|
-
if (scalabilityArray.some(el => el === true)) {
|
|
275
|
-
return true
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
player.tmpPositions = null
|
|
279
|
-
|
|
280
|
-
const mapInstance = await this.loadMap(mapId)
|
|
281
|
-
|
|
282
|
-
if (!mapInstance) return null
|
|
283
|
-
|
|
284
|
-
if (!player.height) player.height = mapInstance.tileHeight
|
|
285
|
-
if (!player.width) player.width = mapInstance.tileWidth
|
|
286
|
-
if (!player.hitbox.h) player.hitbox.h = mapInstance.tileHeight
|
|
287
|
-
if (!player.hitbox.w) player.hitbox.w = mapInstance.tileWidth
|
|
288
|
-
|
|
289
|
-
player.emitSceneMap()
|
|
290
|
-
|
|
291
|
-
// if room is removed after load map (for unit tests)
|
|
292
|
-
if (!World.getRoom(mapId)) {
|
|
293
|
-
return null
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
player.teleport(positions || 'start')
|
|
297
|
-
|
|
298
|
-
World.joinRoom(mapId, player.id)
|
|
299
|
-
|
|
300
|
-
player = World.getUser(player.id) as RpgPlayer
|
|
301
|
-
|
|
302
|
-
if (player) {
|
|
303
|
-
mapInstance.loadCommonEvents(this.server.inputOptions.events, player)
|
|
304
|
-
player.createDynamicEvent(<any>mapInstance._events, false)
|
|
305
|
-
await player.execMethod('onJoinMap', <any>[mapInstance])
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
return mapInstance
|
|
309
|
-
}
|
|
310
|
-
}
|
package/src/decorators/event.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { EventMode } from "../Game/EventManager"
|
|
2
|
-
|
|
3
|
-
export interface EventOptions {
|
|
4
|
-
/**
|
|
5
|
-
* The mode of the event, shared evening or scenario
|
|
6
|
-
*
|
|
7
|
-
* The scenario mode allows you to have events specific to the player. Thus, the graphics, the positions of the event will be different for each player. Beware of performance! The event is duplicated by each player.
|
|
8
|
-
*
|
|
9
|
-
* `shared` mode by default
|
|
10
|
-
*
|
|
11
|
-
* ```ts
|
|
12
|
-
* import { RpgEvent, EventData, RpgPlayer, EventMode } from '@rpgjs/server'
|
|
13
|
-
* @EventData({
|
|
14
|
-
* name: 'EV-1',
|
|
15
|
-
* mode: EventMode.Scenario // or EventMode.Shared
|
|
16
|
-
* })
|
|
17
|
-
* export class CharaEvent extends RpgEvent { }
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* @prop {string} [mode] Either "shared" or "scenario".
|
|
21
|
-
* @memberof EventData
|
|
22
|
-
* */
|
|
23
|
-
mode?: EventMode,
|
|
24
|
-
|
|
25
|
-
width?: number,
|
|
26
|
-
height?: number,
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* The hitbox of the event. By default, this is the size of the tile of the map
|
|
30
|
-
*
|
|
31
|
-
* @prop { { width: number, height: number }} [hitbox]
|
|
32
|
-
* @memberof EventData
|
|
33
|
-
* */
|
|
34
|
-
hitbox?: {
|
|
35
|
-
width?: number,
|
|
36
|
-
height?: number
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Name of the event. This is its identifier. it allows you to retrieve an event and place it on the map
|
|
41
|
-
*
|
|
42
|
-
* @prop {string} name
|
|
43
|
-
* @memberof EventData
|
|
44
|
-
* */
|
|
45
|
-
name: string
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function EventData(options: EventOptions) {
|
|
49
|
-
return (target) => {
|
|
50
|
-
target.mode = options.mode || EventMode.Shared
|
|
51
|
-
target.width = options.width
|
|
52
|
-
target.height = options.height
|
|
53
|
-
target.hitbox = options.hitbox
|
|
54
|
-
target._name = options.name
|
|
55
|
-
target.prototype._name = options.name
|
|
56
|
-
target.prototype.mode = target.mode
|
|
57
|
-
}
|
|
58
|
-
}
|