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 16 additions and 3 deletions
Showing only changes of commit c164a70fc1 - Show all commits

View file

@ -18,7 +18,9 @@ export default class PeerServer extends NetworkPeer {
super();
this.room = {
info: roomInfo,
players: {}
players: {
//TODO Add local player
}
};
this.peer.on("connection", this._connection);
}
@ -69,6 +71,7 @@ export default class PeerServer extends NetworkPeer {
private addPlayer(conn: DataConnection) {
const playerName = conn.metadata.name;
this.room.players[playerName] = {
kind: "remote",
name: conn.metadata.name,
conn: conn
};
@ -97,7 +100,11 @@ export default class PeerServer extends NetworkPeer {
private broadcast<T>(message: T) {
for (const playerName in this.room.players) {
const player = this.room.players[playerName];
this.send<T>(player.conn, message);
if (player.kind == "remote") {
this.send<T>(player.conn, message);
} else {
//TODO Local wrapper
}
}
}
}

View file

@ -1,6 +1,12 @@
import { DataConnection } from "peerjs";
export interface LocalPlayer {
kind: "local";
name: string;
}
export interface NetworkPlayer {
kind: "remote";
name: string;
conn: DataConnection;
}
@ -11,7 +17,7 @@ export interface PeerMetadata {
export interface Room {
info: RoomInfo;
players: Record<string, NetworkPlayer>;
players: Record<string, NetworkPlayer | LocalPlayer>;
}
export interface RoomInfo {