35 lines
924 B
TypeScript
35 lines
924 B
TypeScript
import NetworkPeer from "./peer";
|
|
import { DataConnection } from "peerjs";
|
|
import { RoomInfo, PasswordRequest, Room } from "./types";
|
|
|
|
export default class PeerServer extends NetworkPeer {
|
|
private room: Room;
|
|
public constructor(roomInfo: RoomInfo) {
|
|
super();
|
|
this.room = {
|
|
info: roomInfo,
|
|
players: {}
|
|
};
|
|
this.peer.on("connection", this._connection);
|
|
}
|
|
private async _connection(conn: DataConnection) {
|
|
// Check if this connection should be allowed
|
|
console.info("%s (%s) connected!", conn.metadata.name, conn.label);
|
|
|
|
// Check if room is full
|
|
if (this.playerCount >= this.room.info.max_players) {
|
|
//TODO Reject
|
|
}
|
|
|
|
if (this.room.info.password != "") {
|
|
this.send<PasswordRequest>(conn, { kind: "password-req" });
|
|
} else {
|
|
//TODO Add player
|
|
}
|
|
}
|
|
|
|
private get playerCount(): number {
|
|
return Object.keys(this.room.players).length;
|
|
}
|
|
}
|