check waiting player is online

This commit is contained in:
Halit Aksoy 2022-07-16 22:00:23 +03:00
parent f248d8333a
commit 6fb4631d8a
3 changed files with 27 additions and 19 deletions

View File

@ -8,7 +8,7 @@ import { WebServer } from "./server/WebServer";
const rtmt = new RTMTWS(); const rtmt = new RTMTWS();
const gameStore = new GameStoreImpl(); const gameStore = new GameStoreImpl();
const matchMaker = new MatchMakerImpl(); const matchMaker = new MatchMakerImpl({ rtmt });
const gameManager = new GameManager({ rtmt, gameStore, matchMaker }) const gameManager = new GameManager({ rtmt, gameStore, matchMaker })
const expressApp = new ExpressApp(); const expressApp = new ExpressApp();

View File

@ -1,26 +1,36 @@
import { RTMT } from "../rtmt/rtmt";
import { MatchMaker, OnPlayersPaired } from "./MatchMaker"; import { MatchMaker, OnPlayersPaired } from "./MatchMaker";
export class MatchMakerImpl implements 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) this.onPlayersPaired?.(player1Id, player2Id)
} }
public join(playerId : string): void { private setWaitingPlayerId(playerKey?: string) {
if(this.waitingUserKey === playerId) return; this.waitingPlayerId = playerKey;
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 { 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; this.onPlayersPaired = onPlayersPaired;
} }
} }

View File

@ -5,7 +5,7 @@ import WebSocket from "ws"
import * as http from 'http'; import * as http from 'http';
import { channel_ping, channel_pong } from "../consts/channel_names"; import { channel_ping, channel_pong } from "../consts/channel_names";
const PING_INTERVAL = 1000; const PING_INTERVAL = 3000;
export class RTMTWS implements RTMT { export class RTMTWS implements RTMT {
@ -128,8 +128,6 @@ export class RTMTWS implements RTMT {
} }
isClientOnline(clientID: string): boolean { isClientOnline(clientID: string): boolean {
const ws = this.clients.has(clientID); return this.clients.has(clientID);
//@ts-ignore
return ws && ws.isAlive;
} }
} }