Start adding shim for local player instance on servers

This commit is contained in:
Hamcha 2019-09-04 16:17:21 +02:00
parent 34ab5d4a10
commit c164a70fc1
Signed by: hamcha
GPG key ID: 44AD3571EB09A39E
2 changed files with 16 additions and 3 deletions

View file

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

View file

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