Appearance
RpgWorld
Finding players can be interesting to carry out commands on a group of players. The other interest is to use the RpgWord module in an ExpressJS or other server.
Example with ExpressJS (in src/server/rpg.ts). We recover all the players of the game
ts
import http from 'http'
import express from 'express'
import { Server } from 'socket.io'
import { RpgWorld } from '@rpgjs/server'
const app = express()
const server = http.createServer(app)
app.get('/players', (req, res) => {
const players = RpgWorld.getPlayers()
res.json(players)
})
// ....Example in an event
ts
import { RpgEvent, EventData, RpgPlayer, RpgWorld } from '@rpgjs/server'
@EventData({
name: 'EV-1'
})
export class CharaEvent extends RpgEvent {
onAction(player: RpgPlayer) {
const map = player.getCurrentMap()
const players = RpgWorld.getPlayersOfMap(map.id)
players.forEach(player => player.hp -= 100)
}
}API
Summary
Subscribe to the world
- Property:
RpgWorld.changes - Type:
Observable - Optional:
false - Usage:
Listen to the changes on all the rooms
ts
import { RpgWorld } from '@rpgjs/server'
import { map } from 'rxjs/operators' // install rxjs
RpgWorld.changes
.pipe(
map(rooms => rooms['mymap'])
)
.subscribe((room) => {
const users: any = Object.values(room.users)
console.log(users)
})Get Player
- Method:
RpgWorld.getPlayer(player) - Arguments:
- {
RpgPlayer | string}player. identifier (Optional:false)
- {
- Return:
RpgPlayer - Usage:
Retrieve a player according to his ID
ts
import { RpgWorld } from '@rpgjs/server'
const player = RpgWorld.getPlayer(player) // player is RpgPlayer (player.id) or string (id)Get all Players
- Method:
RpgWorld.getPlayers() - Return:
Array< RpgPlayer> - Usage:
Recover all the players of the game
ts
import { RpgWorld } from '@rpgjs/server'
const players = RpgWorld.getPlayers()Get all objects of map
- Method:
RpgWorld.getObjectsOfMap(map,playerId?) - Arguments:
- {
string}map. Map Name (Optional:false) - {
RpgPlayer | string}playerId. player identifier (Optional:false)
- {
- Return:
Array< RpgPlayer> - Usage:
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
ts
import { RpgWorld } from '@rpgjs/server'
const objects = RpgWorld.getObjectsOfMap('mapname')
console.log(objects)Also retrieve events in Scenario mode:
ts
import { RpgWorld } from '@rpgjs/server'
const objects = RpgWorld.getObjectsOfMap('mapname', 'playerid')Get all shapes of map
- Method:
RpgWorld.getShapesOfMap(map) - Arguments:
- {
string}map. Map Name (Optional:false)
- {
- Return:
Array< RpgShape> - Usage:
Find all the shapes of the map
ts
import { RpgWorld } from '@rpgjs/server'
const shapes = RpgWorld.getShapesOfMap('mapname')
console.log(shapes)Get all Players a map
- Method:
RpgWorld.getPlayersOfMap(map) - Arguments:
- {
string}map. Map Name (Optional:false)
- {
- Return:
Array< RpgPlayer> - Usage:
Recover all the players of a map
ts
import { RpgWorld } from '@rpgjs/server'
const players = RpgWorld.getPlayersOfMap('mapname')