Merge pull request #9 from jhalitaksoy/fix/match-maker

check waiting player is online
This commit is contained in:
Halit Aksoy 2022-07-16 22:27:17 +03:00 committed by GitHub
commit 6ab4bc202f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 gameStore = new GameStoreImpl();
const matchMaker = new MatchMakerImpl();
const matchMaker = new MatchMakerImpl({ rtmt });
const gameManager = new GameManager({ rtmt, gameStore, matchMaker })
const expressApp = new ExpressApp();

View File

@ -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 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 {
public listenOnPlayersPaired(onPlayersPaired: OnPlayersPaired): void {
this.onPlayersPaired = onPlayersPaired;
}
}

View File

@ -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);
}
}