mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-01-14 05:57:09 -08:00
Webatrice websocket refactor (#4435)
* add unit tests for websocket events * add unit tests for KeepAliveService, clean up keepAlive termination flow * put keepAlive command in protobuf service and expose thru webClient * secure wss * rename files tsx to ts * add localhost support for ws/wss connection Co-authored-by: Jeremy Letto <jeremy.letto@datasite.com>
This commit is contained in:
46
webclient/src/websocket/utils/NormalizeService.ts
Normal file
46
webclient/src/websocket/utils/NormalizeService.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Game, GametypeMap, Log, LogGroups, Message, Room } from 'types';
|
||||
|
||||
export default class NormalizeService {
|
||||
// Flatten room gameTypes into map object
|
||||
static normalizeRoomInfo(roomInfo: Room): void {
|
||||
roomInfo.gametypeMap = {};
|
||||
|
||||
const { gametypeList, gametypeMap, gameList } = roomInfo;
|
||||
|
||||
gametypeList.reduce((map, type) => {
|
||||
map[type.gameTypeId] = type.description;
|
||||
return map;
|
||||
}, gametypeMap);
|
||||
|
||||
gameList.forEach((game) => NormalizeService.normalizeGameObject(game, gametypeMap));
|
||||
}
|
||||
|
||||
// Flatten gameTypes[] into gameType field
|
||||
// Default sortable values ("" || 0 || -1)
|
||||
static normalizeGameObject(game: Game, gametypeMap: GametypeMap): void {
|
||||
const { gameTypes, description } = game;
|
||||
const hasType = gameTypes && gameTypes.length;
|
||||
game.gameType = hasType ? gametypeMap[gameTypes[0]] : "";
|
||||
|
||||
game.description = description || "";
|
||||
}
|
||||
|
||||
// Flatten logs[] into object mapped by targetType (room, game, chat)
|
||||
static normalizeLogs(logs: Log[]): LogGroups {
|
||||
return logs.reduce((obj, log) => {
|
||||
const { targetType } = log;
|
||||
obj[targetType] = obj[targetType] || [];
|
||||
obj[targetType].push(log);
|
||||
return obj;
|
||||
}, {} as LogGroups);
|
||||
}
|
||||
|
||||
// messages sent by current user dont have their username prepended
|
||||
static normalizeUserMessage(message: Message): void {
|
||||
const { name } = message;
|
||||
|
||||
if (name) {
|
||||
message.message = `${name}: ${message.message}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user