Skip to content

Commit

Permalink
public chat message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
norkator committed Jan 10, 2025
1 parent 32730a6 commit aa8c357
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/games/gameHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
getUserTables
} from '../database/queries';
import {HoldemBot} from './holdem/holdemBot';
import {getPublicChatMessages, handlePublicChatMessage} from '../publicChat';

let playerIdIncrement = 0;
const players = new Map<WebSocket, Player>();
Expand Down Expand Up @@ -341,6 +342,9 @@ class GameHandler implements GameHandlerInterface {
} else if (table && table instanceof FiveCardDrawTable) {
logger.info(`Player ${player.playerId} send chat message ${chatMsg} into table ${table.tableName}`);
table.handleChatMessage(player.playerId, chatMsg)
} else if (tableId === -1) {
handlePublicChatMessage(players, player, chatMsg);
logger.info(`Player ${player.playerId} send public chat message ${chatMsg}`);
}
}
break;
Expand All @@ -354,6 +358,8 @@ class GameHandler implements GameHandlerInterface {
table.getChatMessages(player.playerId);
} else if (table && table instanceof FiveCardDrawTable) {
table.getChatMessages(player.playerId);
} else if (tableId === -1) {
getPublicChatMessages(player);
}
}
break;
Expand Down
39 changes: 39 additions & 0 deletions src/publicChat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {ChatMessage} from './types';
import {ClientResponse} from './interfaces';
import {SocketState} from './enums';
import {Player} from './player';
import WebSocket from 'ws';

const chatMessages: ChatMessage[] = [];
const chatMaxSize: number = 50;

export function getPublicChatMessages(player: Player): void {
if (player && player.socket) {
const response: ClientResponse = {
key: 'getChatMessages', data: {
messages: [...chatMessages],
}
};
if (player.socket.readyState === SocketState.OPEN) {
player.socket.send(JSON.stringify(response));
}
}
}

export function handlePublicChatMessage(
players: Map<WebSocket, Player>, player: Player, message: string
): void {
if (player) {
if (chatMessages.length >= chatMaxSize) {
chatMessages.shift();
}
const newMessage: ChatMessage = {playerName: player.playerName, message};
chatMessages.push(newMessage);
const response: ClientResponse = {key: 'chatMessage', data: {chatMessage: newMessage}};
players.forEach((p: Player) => {
if (p.selectedTableId === -1 && p.socket && p.socket.readyState === SocketState.OPEN) {
p.socket.send(JSON.stringify(response));
}
});
}
}

0 comments on commit aa8c357

Please sign in to comment.