implement on_user_connection_change
This commit is contained in:
parent
5e242ca9ba
commit
228fd8cef7
@ -8,3 +8,4 @@ export const channel_on_game_crashed = "on_game_crashed"
|
||||
export const channel_on_game_user_leave = "on_game_user_leave"
|
||||
export const channel_ping = "ping"
|
||||
export const channel_pong = "pong"
|
||||
export const channel_on_user_connection_change = "channel_on_user_connection_change"
|
||||
@ -9,11 +9,13 @@ import {
|
||||
channel_on_game_crashed,
|
||||
channel_on_game_start,
|
||||
channel_on_game_update,
|
||||
channel_on_game_user_leave
|
||||
channel_on_game_user_leave,
|
||||
channel_on_user_connection_change
|
||||
} from "../consts/channel_names";
|
||||
import { GameMove } from "../models/GameMove";
|
||||
import { GameCrashManager } from "./GameCrashManager";
|
||||
import { MatchMaker } from "../matchmaker/MatchMaker";
|
||||
import { UserConnectionInfo } from "../models/UserConnectionInfo";
|
||||
|
||||
export class GameManager {
|
||||
gameStore: GameStore;
|
||||
@ -30,6 +32,7 @@ export class GameManager {
|
||||
private initialize() {
|
||||
this.listenOnPlayersPaired();
|
||||
this.listenRtmtMessages();
|
||||
this.listenUserConnectionChange();
|
||||
}
|
||||
|
||||
private listenRtmtMessages() {
|
||||
@ -108,6 +111,16 @@ export class GameManager {
|
||||
public startGame(game: MancalaGame) {
|
||||
this.rtmt.sendMessage(game.player1Id, channel_on_game_start, game);
|
||||
this.rtmt.sendMessage(game.player2Id, channel_on_game_start, game);
|
||||
this.sendUserConnectionInfo(game, game.player1Id);
|
||||
this.sendUserConnectionInfo(game, game.player2Id);
|
||||
}
|
||||
|
||||
public sendUserConnectionInfo(game: MancalaGame, playerId: string, isOnline?: boolean) {
|
||||
//todo: reimplement when watch game feature added
|
||||
const _isOnline = isOnline === undefined ? this.rtmt.isClientOnline(playerId) : isOnline;
|
||||
const otherUser = game.player1Id === playerId ? game.player2Id : game.player1Id;
|
||||
const userConnectionInfo: UserConnectionInfo = { userId: playerId, isOnline: _isOnline };
|
||||
this.rtmt.sendMessage(otherUser, channel_on_user_connection_change, userConnectionInfo);
|
||||
}
|
||||
|
||||
public deleteGame(game: MancalaGame) {
|
||||
@ -116,4 +129,17 @@ export class GameManager {
|
||||
this.gameStore.remove(game.player2Id);
|
||||
}
|
||||
}
|
||||
|
||||
private listenUserConnectionChange() {
|
||||
this.rtmt.listenOnClientConnectionChange((clientId: string, isOnline: boolean) => {
|
||||
const game = this.gameStore.get(clientId);
|
||||
if (game) {
|
||||
this.sendUserConnectionInfo(game, clientId, isOnline);
|
||||
if (isOnline) {
|
||||
const otherUser = game.player1Id === clientId ? game.player2Id : game.player1Id;
|
||||
this.sendUserConnectionInfo(game, otherUser);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user