@heyvr/sdk-arena
Version:
The SDK for heyVR's arena system.
252 lines (251 loc) • 9.79 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { BaseLobby } from "./BaseLobby";
export class Lobby extends BaseLobby {
constructor(colyseus) {
super();
if (Lobby.instance) {
throw new Error('Can not have more than one lobby instance.');
}
Lobby.instance = this;
this.colyseus = colyseus;
}
get actions() {
return {
listPlayers: () => {
return new Promise((resolve) => {
this.on('command.listPlayers.result', result => resolve(result));
this.getRoom().send('command.listPlayers');
});
},
abandon: () => {
this.getRoom().leave().then(() => this.destroy());
},
disconnect: () => {
this.getRoom().leave();
},
reconnect: () => {
return this.colyseus.reconnect(this.getRoom().reconnectionToken)
.then(room => Lobby.room = room)
.then(() => this);
},
destroy: () => {
this.getRoom().send('command.destroy');
this.destroy();
}
};
}
get commands() {
return {
kickPlayer: (username) => {
return new Promise((resolve, reject) => {
this.on('command.kick.result', (result) => result.success ? resolve(username) : reject(result));
this.getRoom().send('command.kick', username);
});
},
banPlayer: (username) => {
return new Promise((resolve, reject) => {
this.on('command.ban.result', (result) => result.success ? resolve(username) : reject(result));
this.getRoom().send('command.ban', username);
});
},
unbanPlayer: (username) => {
return new Promise((resolve, reject) => {
this.on('command.unban.result', (result) => result.success ? resolve(username) : reject(result));
this.getRoom().send('command.unban', username);
});
},
};
}
get communications() {
return {
allChat: (message) => {
return new Promise((resolve, reject) => {
this.on('communication.allChat.result', result => result.success ? resolve(result.payload) : reject(result));
this.getRoom().send('communication.allChat', { message: message });
});
},
teamChat: (message, team) => {
return new Promise((resolve, reject) => {
this.on('communication.teamChat.result', result => result.success ? resolve(result.payload) : reject(result));
this.getRoom().send('communication.teamChat', { message: message, team: team ? String(team) : '' });
});
},
whisper: (message, username) => {
return new Promise((resolve, reject) => {
this.on('communication.whisper.result', result => result.success ? resolve(result.payload) : reject(result));
this.getRoom().send('communication.whisper', {
message: message,
username: username,
whisper: true
});
});
},
mute: (username) => {
return new Promise((resolve, reject) => {
this.on('communication.mute.result', result => result.success ? resolve(result.payload) : reject(result));
this.getRoom().send('communication.mute', username);
});
},
unmute: (username) => {
return new Promise((resolve, reject) => {
this.on('communication.unmute.result', result => result.success ? resolve(result.payload) : reject(result));
this.getRoom().send('communication.unmute', username);
});
},
call: () => {
throw new Error('Voice call is not implemented yet.');
},
hangUp: () => {
throw new Error('Voice call is not implemented yet.');
},
onMessage: (callback) => {
this.getRoom().onMessage('communication.message', callback);
}
};
}
get data() {
return {
sendToAll: (data) => {
data && this.getRoom().send('rawDataAll', { payload: data });
},
sendToTeam: (data, team) => {
data && this.getRoom().send('rawDataTeam', { payload: data, team: team ? String(team) : '' });
},
onReceiveAll: (callback) => {
this.getRoom().onMessage('rawDataAll', callback);
},
onReceiveTeam: (callback) => {
this.getRoom().onMessage('rawDataTeam', callback);
},
};
}
get events() {
return {
onDisconnect: (callback) => {
this.getRoom().onLeave(code => {
callback(code);
this.destroy();
});
},
onError: (callback) => {
this.getRoom().onError(callback);
},
onMessage: (callback) => {
this.on('system.message', callback);
},
onPlayerSync: (callback) => {
this.on('system.players.sync', callback);
}
};
}
get session() {
var _a;
return {
userId: this.getRoom().sessionId,
lobbyId: this.getRoom().id,
username: (_a = this.username) !== null && _a !== void 0 ? _a : 'Unknown'
};
}
get state() {
return {
get current() {
throw new Error('Server state management has not been implemented yet.');
},
onChange: (callback) => {
this.getRoom().onStateChange(callback);
}
};
}
static create(colyseus, config) {
return __awaiter(this, void 0, void 0, function* () {
if (Lobby.instance) {
throw new Error("You're already connected to a lobby. Please abandon the last lobby before connecting.");
}
const lobby = new Lobby(colyseus);
try {
Lobby.room = yield colyseus.create(`${config.game}_${config.room}`, {
accessToken: config.token,
password: config.password,
team: config.team,
name: config.name,
p2p: config.p2p
});
yield lobby.registerListeners();
return lobby;
}
catch (e) {
lobby.destroy();
throw e;
}
});
}
destroy() {
Lobby.room = Lobby.instance = undefined;
}
static getInstance() {
if (!Lobby.instance) {
throw new Error("You're not connected to any lobbies at the moment.");
}
return Lobby.instance;
}
getRoom() {
if (!Lobby.room) {
throw new Error('Can not access the lobby before initialization.');
}
return Lobby.room;
}
static join(colyseus, config) {
return __awaiter(this, void 0, void 0, function* () {
if (Lobby.instance) {
throw new Error("You're already connected to a lobby. Please abandon the last lobby before connecting.");
}
const lobby = new Lobby(colyseus);
try {
Lobby.room = yield colyseus.joinById(config.lobby, {
accessToken: config.token,
password: config.password,
team: config.team
});
yield lobby.registerListeners();
return lobby;
}
catch (e) {
lobby.destroy();
throw e;
}
});
}
registerListeners() {
return __awaiter(this, void 0, void 0, function* () {
Lobby.events.forEach(name => this.getRoom().onMessage(name, message => this.dispatch(name, message)));
return new Promise(resolve => {
this.on('command.username.result', u => (this.username = u) && resolve());
this.getRoom().send('command.username');
});
});
}
}
Lobby.events = [
'command.kick.result',
'command.ban.result',
'command.unban.result',
'command.listPlayers.result',
'command.username.result',
'communication.mute.result',
'communication.unmute.result',
'communication.allChat.result',
'communication.teamChat.result',
'communication.whisper.result',
'system.message',
'system.players.sync',
];
Lobby.instance = undefined;
Lobby.room = undefined;