diff --git a/src/matcmaker.ts b/src/matcmaker.ts new file mode 100644 index 0000000..e118794 --- /dev/null +++ b/src/matcmaker.ts @@ -0,0 +1,21 @@ + +// 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){ + 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