diff --git a/src/matchmaker/MatchMaker.ts b/src/matchmaker/MatchMaker.ts new file mode 100644 index 0000000..bdc84db --- /dev/null +++ b/src/matchmaker/MatchMaker.ts @@ -0,0 +1,6 @@ +export type OnPlayersPaired = (player1Id: string, player2Id: string)=> void; + +export interface MatchMaker { + join(playerId : string): void; + listenOnPlayersPaired(onPlayersPaired: OnPlayersPaired ) : void; +} \ No newline at end of file diff --git a/src/matchmaker/MatchMakerImpl.ts b/src/matchmaker/MatchMakerImpl.ts new file mode 100644 index 0000000..335cffc --- /dev/null +++ b/src/matchmaker/MatchMakerImpl.ts @@ -0,0 +1,26 @@ +import { MatchMaker, OnPlayersPaired } from "./MatchMaker"; + +export class MatchMakerImpl implements MatchMaker { + private waitingUserKey : string | undefined + + private onPlayersPaired: OnPlayersPaired | undefined = undefined; + + private fireOnPlayerPaired(player1Id: string, player2Id: string) : void { + this.onPlayersPaired?.(player1Id, player2Id) + } + + public join(playerId : string): void { + if(this.waitingUserKey === playerId) return; + if(this.waitingUserKey){ + const user1 = this.waitingUserKey as string + this.waitingUserKey = undefined + this.fireOnPlayerPaired(user1, playerId) + }else{ + this.waitingUserKey = playerId + } + } + + public listenOnPlayersPaired(onPlayersPaired: OnPlayersPaired ) : void { + this.onPlayersPaired = onPlayersPaired; + } +} \ No newline at end of file diff --git a/src/matcmaker.ts b/src/matcmaker.ts deleted file mode 100644 index 0ac5d19..0000000 --- a/src/matcmaker.ts +++ /dev/null @@ -1,22 +0,0 @@ - -// todo : use queue -export class MatchMaker { - private waitingUserKey : string | undefined - - public onPlayersPaired!: (userKey1: string, userKey2: string) => void - - public find(userKey : string) : void{ - if(this.waitingUserKey === userKey) return; - if(this.waitingUserKey){ - const user1 = this.waitingUserKey as string - this.waitingUserKey = undefined - this.fireOnPlayerPaired(user1, userKey) - }else{ - this.waitingUserKey = userKey - } - } - - private fireOnPlayerPaired(userKey1 : string, userKey2 : string) : void { - this.onPlayersPaired!!(userKey1, userKey2) - } -} \ No newline at end of file