- Spectators are removed from games when they disconnect; the lookup iterated an iterator with for...in and never found anything - Finished and left games are removed from memory, not only from the player mappings - The connection info is sent once when a game starts
54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
import { AuthUser } from "../../src/auth";
|
|
import { OnClientConnectionChange, OnMessage, RTMT } from "../../src/rtmt/rtmt";
|
|
|
|
export type SentMessage = { clientID: string, channel: string, message: any };
|
|
|
|
// In-memory RTMT: tests connect users, send messages as them and inspect what the server sent back.
|
|
export class FakeRtmt implements RTMT {
|
|
readonly sent: SentMessage[] = [];
|
|
private readonly listeners = new Map<string, OnMessage>();
|
|
private readonly online = new Map<string, AuthUser>();
|
|
private onConnectionChange?: OnClientConnectionChange;
|
|
|
|
connect(user: AuthUser) {
|
|
this.online.set(user.id, user);
|
|
this.onConnectionChange?.(user.id, true);
|
|
}
|
|
|
|
disconnect(userId: string) {
|
|
this.online.delete(userId);
|
|
this.onConnectionChange?.(userId, false);
|
|
}
|
|
|
|
receive(userId: string, channel: string, message: Object = {}) {
|
|
const listener = this.listeners.get(channel);
|
|
if (!listener) throw new Error(`No listener for ${channel}`);
|
|
listener(userId, message);
|
|
}
|
|
|
|
messagesTo(userId: string, channel: string): any[] {
|
|
return this.sent.filter((m) => m.clientID === userId && m.channel === channel).map((m) => m.message);
|
|
}
|
|
|
|
sendMessage(clientID: string, channel: string, message: Object) {
|
|
// Copy, like serialising over the wire would.
|
|
this.sent.push({ clientID, channel, message: JSON.parse(JSON.stringify(message)) });
|
|
}
|
|
|
|
listenMessage(channel: string, callback: OnMessage) {
|
|
this.listeners.set(channel, callback);
|
|
}
|
|
|
|
isClientOnline(clientID: string): boolean {
|
|
return this.online.has(clientID);
|
|
}
|
|
|
|
getUser(clientID: string): AuthUser | undefined {
|
|
return this.online.get(clientID);
|
|
}
|
|
|
|
listenOnClientConnectionChange(onUserConnectionChange: OnClientConnectionChange): void {
|
|
this.onConnectionChange = onUserConnectionChange;
|
|
}
|
|
}
|