From e0fd248466fa3bd88834658c6e89fe7017f3b65a Mon Sep 17 00:00:00 2001 From: jhalitaksoy Date: Tue, 29 Jun 2021 03:25:15 +0300 Subject: [PATCH] added matchmaker --- src/matcmaker.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/matcmaker.ts 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