Basic networking #9

Merged
hamcha merged 17 commits from feature/basic-networking into master 2019-09-06 12:36:11 +00:00
2 changed files with 36 additions and 2 deletions
Showing only changes of commit 2ed732fe7b - Show all commits

View file

@ -1,6 +1,12 @@
import NetworkPeer from "./peer";
import { DataConnection } from "peerjs";
import { RoomInfo, PasswordRequest, Room } from "./types";
import {
RoomInfo,
PasswordRequest,
Room,
ErrorMessage,
PasswordResponse
} from "./types";
export default class PeerServer extends NetworkPeer {
private room: Room;
@ -19,15 +25,38 @@ export default class PeerServer extends NetworkPeer {
// Check if room is full
if (this.playerCount >= this.room.info.max_players) {
//TODO Reject
this.send<ErrorMessage>(conn, { kind: "error", error: "room is full" });
conn.close();
return;
}
if (this.room.info.password != "") {
this.send<PasswordRequest>(conn, { kind: "password-req" });
conn.on("data", this.checkPasswordResponse.bind(this, conn));
} else {
//TODO Add player
}
}
private checkPasswordResponse(conn: DataConnection, data: any) {
try {
let resp = data as PasswordResponse;
if (resp.password != this.room.info.password) {
this.send<ErrorMessage>(conn, {
kind: "error",
error: "invalid password"
});
return;
}
//TODO Add player
} catch (e) {
this.send<ErrorMessage>(conn, {
kind: "error",
error: "not a password"
});
}
}
private get playerCount(): number {
return Object.keys(this.room.players).length;
}

View file

@ -46,7 +46,7 @@ type DraftInfo = {
// Message schemas
export type NetworkMessage = PasswordRequest | PasswordResponse;
export type NetworkMessage = PasswordRequest | PasswordResponse | ErrorMessage;
export interface PasswordRequest {
kind: "password-req";
@ -56,3 +56,8 @@ export interface PasswordResponse {
kind: "password-resp";
password: string;
}
export interface ErrorMessage {
kind: "error";
error: string;
}