From 6fb4631d8aab2a0b25c50fdd10919db7539ed395 Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Sat, 16 Jul 2022 22:00:23 +0300 Subject: [PATCH] check waiting player is online --- src/index.ts | 2 +- src/matchmaker/MatchMakerImpl.ts | 38 ++++++++++++++++++++------------ src/rtmt/rtmt_websocket.ts | 6 ++--- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/index.ts b/src/index.ts index f5b97d8..eb86d54 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ import { WebServer } from "./server/WebServer"; const rtmt = new RTMTWS(); const gameStore = new GameStoreImpl(); -const matchMaker = new MatchMakerImpl(); +const matchMaker = new MatchMakerImpl({ rtmt }); const gameManager = new GameManager({ rtmt, gameStore, matchMaker }) const expressApp = new ExpressApp(); diff --git a/src/matchmaker/MatchMakerImpl.ts b/src/matchmaker/MatchMakerImpl.ts index 335cffc..a9f1ca2 100644 --- a/src/matchmaker/MatchMakerImpl.ts +++ b/src/matchmaker/MatchMakerImpl.ts @@ -1,26 +1,36 @@ +import { RTMT } from "../rtmt/rtmt"; import { MatchMaker, OnPlayersPaired } from "./MatchMaker"; export class MatchMakerImpl implements MatchMaker { - private waitingUserKey : string | undefined + private waitingPlayerId?: string; - private onPlayersPaired: OnPlayersPaired | undefined = undefined; + private onPlayersPaired?: OnPlayersPaired; - private fireOnPlayerPaired(player1Id: string, player2Id: string) : void { + private rtmt: RTMT; + + constructor(params: { rtmt: RTMT }) { + this.rtmt = params.rtmt; + } + + 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 - } + private setWaitingPlayerId(playerKey?: string) { + this.waitingPlayerId = playerKey; } - public listenOnPlayersPaired(onPlayersPaired: OnPlayersPaired ) : void { + public join(newcomerPlayerId: string): void { + if (this.waitingPlayerId === newcomerPlayerId) return; + if (this.waitingPlayerId && this.rtmt.isClientOnline(this.waitingPlayerId)) { + this.fireOnPlayerPaired(this.waitingPlayerId, newcomerPlayerId) + this.setWaitingPlayerId(undefined); + } else { + this.waitingPlayerId = newcomerPlayerId + } + } + + public listenOnPlayersPaired(onPlayersPaired: OnPlayersPaired): void { this.onPlayersPaired = onPlayersPaired; - } + } } \ No newline at end of file diff --git a/src/rtmt/rtmt_websocket.ts b/src/rtmt/rtmt_websocket.ts index bb84d7f..37d0fe0 100644 --- a/src/rtmt/rtmt_websocket.ts +++ b/src/rtmt/rtmt_websocket.ts @@ -5,7 +5,7 @@ import WebSocket from "ws" import * as http from 'http'; import { channel_ping, channel_pong } from "../consts/channel_names"; -const PING_INTERVAL = 1000; +const PING_INTERVAL = 3000; export class RTMTWS implements RTMT { @@ -128,8 +128,6 @@ export class RTMTWS implements RTMT { } isClientOnline(clientID: string): boolean { - const ws = this.clients.has(clientID); - //@ts-ignore - return ws && ws.isAlive; + return this.clients.has(clientID); } } \ No newline at end of file