Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chat: correctly handle player and non-player chats #427

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Game extends EventEmitter<Events> {
state: GobanEngineConfig;
opponent_evenodd: null | number;
greeted: boolean;
startup_timestamp: number;
player_chat_cutoff: number;
bot?: Bot;
using_opening_bot: boolean = false;
ending_bot?: Bot;
Expand Down Expand Up @@ -57,7 +57,7 @@ export class Game extends EventEmitter<Events> {
this.verbose = trace.debug.bind(null, `[game ${game_id}]`);
this.warn = trace.warn.bind(null, `[game ${game_id}]`);
this.error = trace.error.bind(null, `[game ${game_id}]`);
this.startup_timestamp = Date.now() / 1000;
this.player_chat_cutoff = Date.now() / 1000;
this.state = null;
this.opponent_evenodd = null;
this.greeted = false;
Expand Down Expand Up @@ -349,10 +349,28 @@ export class Game extends EventEmitter<Events> {
channel: `game-${game_id}`,
});
const on_chat = (d) => {
// Since there is no explicit tracking of which chats are
// "read", we assume anything from before we connected to the
// game has already been dealt with.
handleChatLine(game_id, d.line, this.startup_timestamp);
// Player chats are potentially sent multiple times:
//
// 1. Immediately when posted
// 2. Resent any time we reconnect to the game
// 3. Resent when the game ends
//
// Chats from non-players are sent just once:
//
// 1. Immediately, when posted after the game
// 2. At the end of the game for any chats that were sent
// during the game
let chat_cutoff = 0;
if (
d.line.player_id === this.state.players.black.id ||
d.line.player_id === this.state.players.white.id
) {
chat_cutoff = this.player_chat_cutoff;
if (d.line.date > this.player_chat_cutoff) {
this.player_chat_cutoff = d.line.date;
}
}
handleChatLine(game_id, d.line, chat_cutoff);
};
socket.on(`game/${game_id}/chat`, on_chat);
this.on("disconnecting", () => {
Expand Down Expand Up @@ -864,14 +882,16 @@ export class Game extends EventEmitter<Events> {
}

export function handleChatLine(game_id: string, line: any, cutoff_timestamp: number) {
// Variations, etc.
if (typeof line.body !== "string") {
return;
}
// Greetings/etc from the bot itself.
if (line.username === config.username) {
return;
}
// Both are UNIX epoch times.
if (line.date < cutoff_timestamp) {
if (line.date <= cutoff_timestamp) {
return;
}

Expand Down