Merge pull request #9 from jhalitaksoy/fix/match-maker
check waiting player is online
This commit is contained in:
commit
6ab4bc202f
@ -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();
|
||||||
|
|||||||
@ -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
|
public join(newcomerPlayerId: string): void {
|
||||||
this.fireOnPlayerPaired(user1, playerId)
|
if (this.waitingPlayerId === newcomerPlayerId) return;
|
||||||
}else{
|
if (this.waitingPlayerId && this.rtmt.isClientOnline(this.waitingPlayerId)) {
|
||||||
this.waitingUserKey = playerId
|
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;
|
this.onPlayersPaired = onPlayersPaired;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user