Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 0 additions & 115 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,20 @@ export class P2PHost {
const isAuthenticated: boolean = this.authenticateClient(client);
// If the client failed to authenticate, don’t register it.
if (!isAuthenticated) return;
const { playerID, credentials } = client.metadata;
const { playerID, credentials, playerName, playerData } = client.metadata;
this.clients.set(client, client);

const { metadata } = this.db.fetch(this.matchID);

// Update the match data with extra server properties
if (playerID) {
const parsedID = Number.parseInt(playerID);
if (metadata.players[parsedID]) {
metadata.players[parsedID].name = playerName;
metadata.players[parsedID].data = playerData;
}
}

this.master.onConnectionChange(this.matchID, playerID, credentials, true);
}

Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type PeerError = Error & {
};

interface P2POpts {
playerName?: string;
playerData?: any;
isHost?: boolean;
peerOptions?: PeerJSOption;
onError?: (error: PeerError) => void;
Expand Down Expand Up @@ -82,6 +84,8 @@ class P2PTransport extends Transport {
private emit?: (data: ClientAction) => void;
private retryHandler: BackoffScheduler;
private privateKey?: string;
private playerName?: string;
private playerData?: any;

constructor({
isHost,
Expand All @@ -97,6 +101,8 @@ class P2PTransport extends Transport {
this.game = opts.game;
this.retryHandler = new BackoffScheduler();
this.setCredentials(opts.credentials);
this.playerName = opts.playerName;
this.playerData = opts.playerData;
}

/** Synthesized peer ID for looking up this match’s host. */
Expand Down Expand Up @@ -127,6 +133,8 @@ class P2PTransport extends Transport {
return {
playerID: this.playerID,
credentials: this.credentials,
playerName: this.playerName,
playerData: this.playerData,
message:
this.playerID && this.privateKey
? signMessage(this.playerID, this.privateKey)
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface Client {
playerID: PlayerID | null;
credentials: string | undefined;
message?: string;
playerName?: string;
playerData?: any;
};
}

Expand Down