@heyvr/sdk-arena
Version:
The SDK for heyVR's arena system.
133 lines (132 loc) • 6.02 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 { Lobby } from "./Lobby";
import { LobbyP2P } from "./LobbyP2P";
import * as Colyseus from "colyseus.js";
export class Arena {
constructor(game, token, debug) {
this.colyseus = null;
if (Arena.booted) {
return;
}
if (!game) {
throw new Error('Please provide a valid game slug while instantiating the heyVR Arena.');
}
this.game = 'function' === typeof game ? game : () => game;
this.token = 'function' === typeof token ? token : () => token;
Arena.debugMode = debug ? ('function' === typeof debug ? debug : () => debug) : () => false;
Arena.booted = true;
}
connection() {
var _a;
return (_a = this.colyseus) !== null && _a !== void 0 ? _a : new Colyseus.Client({ secure: true, hostname: `arena.${Arena.tld()}`, port: 443 });
}
createLobby(options) {
return __awaiter(this, void 0, void 0, function* () {
if (!options.room) {
throw new Error('Please specify a room name to list the lobbies.');
}
options.name = options.name ? String(options.name) : `Lobby for ${this.game()}`;
options.team = options.team ? String(options.team) : 'default';
options.password = options.password ? String(options.password) : '';
return yield Lobby.create(this.connection(), Object.assign(Object.assign({}, options), { token: this.token(), game: this.game() }));
});
}
duel(room) {
return __awaiter(this, void 0, void 0, function* () {
const join = (id) => __awaiter(this, void 0, void 0, function* () {
const lobby = yield this.joinLobby({
room,
lobby: id,
password: '',
team: 'default'
});
const p2p = new LobbyP2P(lobby);
p2p.getID().then((id) => __awaiter(this, void 0, void 0, function* () { return lobby.data.sendToAll({ key: 'con_req', value: id, guest: lobby.session.username }); }));
return p2p;
});
const host = () => __awaiter(this, void 0, void 0, function* () {
const lobby = yield this.createLobby({
room,
p2p: true,
password: '',
team: 'default',
name: `Duel: ${room}`
});
return new LobbyP2P(lobby);
});
try {
yield this.getLobbyP2P();
}
catch (e) {
const lobbies = yield this.connection().getAvailableRooms(`${this.game()}_${room}`);
const available = lobbies.filter(l => l.metadata.p2p && 2 === l.maxClients && 1 === l.metadata.players.length).shift();
return available ? join(available.roomId) : host();
}
throw new Error("Can't start the matchmaking, you're already in a match.");
});
}
getLobby() {
return new Promise(resolve => resolve(Lobby.getInstance()));
}
getLobbyP2P() {
return new Promise(resolve => resolve(LobbyP2P.getInstance()));
}
joinLobby(options) {
return __awaiter(this, void 0, void 0, function* () {
if (!options.room) {
throw new Error('Please specify a room name to join.');
}
if (!options.lobby) {
throw new Error('Please specify a lobby ID to join.');
}
options.team = options.team ? String(options.team) : 'default';
options.password = options.password ? String(options.password) : '';
return yield Lobby.join(this.connection(), Object.assign(Object.assign({}, options), { token: this.token(), game: this.game() }));
});
}
lobbies(roomId) {
return __awaiter(this, void 0, void 0, function* () {
if (!roomId) {
throw new Error('Please specify a room name to list the lobbies.');
}
const rooms = yield this.connection().getAvailableRooms(`${this.game()}_${roomId}`);
return rooms.filter(r => !r.metadata.p2p).map(room => ({
id: room.roomId,
name: room.metadata.name,
players: room.metadata.players,
private: room.metadata.private,
maxPlayers: room.maxClients
}));
});
}
static log(message, level = 'log') {
Arena.debugMode() && console[level](`heyVR Arena: ${message}`);
}
p2p(id) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.getLobbyP2P();
}
catch (e) {
const p2p = new LobbyP2P();
yield p2p.getID();
id && 'string' === typeof id && p2p.connect(id.toUpperCase());
return p2p;
}
throw new Error("You're already connected to a remote peer. Please disconnect first.");
});
}
static tld() {
var _a;
return (_a = ['heyvr.test', 'heyvr.de', 'heyvr.dev', 'heyvr.io'].find(h => window.location.hostname.endsWith(h))) !== null && _a !== void 0 ? _a : 'heyvr.io';
}
}
Arena.booted = false;