check waiting player is online
This commit is contained in:
parent
f248d8333a
commit
6fb4631d8a
@ -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();
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user