From 34ab5d4a10ac2dc5766aabfb0f191c4c7828c101 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Wed, 4 Sep 2019 15:34:53 +0200 Subject: [PATCH] Handle players joining --- src/network/client.ts | 3 ++- src/network/server.ts | 52 ++++++++++++++++++++++++++++++++++++++----- src/network/types.ts | 22 +++++++++++++++++- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/network/client.ts b/src/network/client.ts index 243a3bb..1b98418 100644 --- a/src/network/client.ts +++ b/src/network/client.ts @@ -1,9 +1,10 @@ import NetworkPeer from "./peer"; import { DataConnection } from "peerjs"; +import { PeerMetadata } from "./types"; export default class PeerClient extends NetworkPeer { private connection: DataConnection; - public constructor(peerid: string, metadata: Object) { + public constructor(peerid: string, metadata: PeerMetadata) { super(); this.connection = this.peer.connect(peerid, { label: "server", diff --git a/src/network/server.ts b/src/network/server.ts index 0bb1886..5a2d707 100644 --- a/src/network/server.ts +++ b/src/network/server.ts @@ -5,11 +5,15 @@ import { PasswordRequest, Room, ErrorMessage, - PasswordResponse + PasswordResponse, + PeerMetadata, + JoinMessage, + RoomInfoMessage } from "./types"; export default class PeerServer extends NetworkPeer { private room: Room; + public constructor(roomInfo: RoomInfo) { super(); this.room = { @@ -18,13 +22,17 @@ export default class PeerServer extends NetworkPeer { }; 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); + console.info( + "%s (%s) connected!", + (conn.metadata as PeerMetadata).name, + conn.label + ); // Check if room is full if (this.playerCount >= this.room.info.max_players) { - //TODO Reject this.send(conn, { kind: "error", error: "room is full" }); conn.close(); return; @@ -33,9 +41,10 @@ export default class PeerServer extends NetworkPeer { if (this.room.info.password != "") { this.send(conn, { kind: "password-req" }); conn.on("data", this.checkPasswordResponse.bind(this, conn)); - } else { - //TODO Add player + return; } + + this.addPlayer(conn); } private checkPasswordResponse(conn: DataConnection, data: any) { @@ -48,7 +57,7 @@ export default class PeerServer extends NetworkPeer { }); return; } - //TODO Add player + this.addPlayer(conn); } catch (e) { this.send(conn, { kind: "error", @@ -57,7 +66,38 @@ export default class PeerServer extends NetworkPeer { } } + private addPlayer(conn: DataConnection) { + const playerName = conn.metadata.name; + this.room.players[playerName] = { + name: conn.metadata.name, + conn: conn + }; + + // Send the player info about the room + this.send(conn, { + kind: "room-info", + room: { + ...this.room.info, + password: "" + }, + players: Object.keys(this.room.players) + }); + + // Notify other players + this.broadcast({ + kind: "player-joined", + name: conn.metadata.name + }); + } + private get playerCount(): number { return Object.keys(this.room.players).length; } + + private broadcast(message: T) { + for (const playerName in this.room.players) { + const player = this.room.players[playerName]; + this.send(player.conn, message); + } + } } diff --git a/src/network/types.ts b/src/network/types.ts index 414656d..f055f87 100644 --- a/src/network/types.ts +++ b/src/network/types.ts @@ -5,6 +5,10 @@ export interface NetworkPlayer { conn: DataConnection; } +export interface PeerMetadata { + name: string; +} + export interface Room { info: RoomInfo; players: Record; @@ -46,7 +50,12 @@ type DraftInfo = { // Message schemas -export type NetworkMessage = PasswordRequest | PasswordResponse | ErrorMessage; +export type NetworkMessage = + | PasswordRequest + | PasswordResponse + | ErrorMessage + | RoomInfoMessage + | JoinMessage; export interface PasswordRequest { kind: "password-req"; @@ -57,6 +66,17 @@ export interface PasswordResponse { password: string; } +export interface RoomInfoMessage { + kind: "room-info"; + room: RoomInfo; + players: string[]; +} + +export interface JoinMessage { + kind: "player-joined"; + name: string; +} + export interface ErrorMessage { kind: "error"; error: string;