Skip to content

Commit

Permalink
Handle game chats and write them into the log
Browse files Browse the repository at this point in the history
Players sometimes say things in the game chat, which I would be
interested in at least becoming aware of.
  • Loading branch information
windo committed May 15, 2024
1 parent 9478fd1 commit 8d098d1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class Game extends EventEmitter<Events> {
state: GoEngineConfig;
opponent_evenodd: null | number;
greeted: boolean;
startup_timestamp: number;
bot?: Bot;
using_opening_bot: boolean = false;
ending_bot?: Bot;
Expand Down Expand Up @@ -56,6 +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();
this.state = null;
this.opponent_evenodd = null;
this.greeted = false;
Expand Down Expand Up @@ -226,6 +228,7 @@ export class Game extends EventEmitter<Events> {
// Try to connect again, to get the server to send the gamedata over.
socket.send("game/connect", {
game_id: game_id,
chat: true,
});
return;
}
Expand Down Expand Up @@ -341,8 +344,23 @@ export class Game extends EventEmitter<Events> {
socket.off(`game/${game_id}/move`, on_move);
});

socket.send("chat/join", {
channel: `game-${game_id}`,
});
const on_chat = (d) => {
// If the bot is down for a while, we will miss those chats, rather
// than to reiterate all the chats on all the ongoing games.
handleChatLine(game_id, d.line, this.startup_timestamp);
};
socket.on(`game/${game_id}/chat`, on_chat);
this.on("disconnecting", () => {
socket.off(`game/${game_id}/chat`, on_chat);
});

trace.log("connecting");
socket.send("game/connect", {
game_id: game_id,
chat: true,
});

/*
Expand Down Expand Up @@ -827,6 +845,20 @@ export class Game extends EventEmitter<Events> {
}
}

export function handleChatLine(game_id: string, line: any, cutoff_timestamp: number) {
if (typeof line.body !== "string") {
return;
}
if (line.username === config.username) {
return;
}
if (line.date < cutoff_timestamp) {
return;
}

trace.log(`[game ${game_id}] Game chat from ${line.username}: ${line.body}`);
}

function num2char(num: number): string {
if (num === -1) {
return ".";
Expand Down
45 changes: 44 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { config, config_event_emitter, TimeControlRanges } from "./config";
import { socket } from "./socket";
import { trace } from "./trace";
import { post, api1 } from "./util";
import { Game } from "./Game";
import { Game, handleChatLine } from "./Game";
import { bot_pools } from "./pools";
import { JGOFTimeControl } from "goban/src/JGOF";
import { Speed } from "./types";
Expand Down Expand Up @@ -65,6 +65,7 @@ interface RejectionDetails {
class Main {
notification_connect_interval: ReturnType<typeof setInterval>;
connected_games: { [game_id: string]: Game };
connected_finished_games: { [game_id: string]: boolean };

//games_by_player: { [player_id: string]: Game[] };
connected: boolean;
Expand All @@ -77,6 +78,7 @@ class Main {

constructor() {
this.connected_games = {};
this.connected_finished_games = {};
//this.games_by_player = {}; // Keep track of connected games per player
this.connected = false;

Expand Down Expand Up @@ -329,6 +331,47 @@ class Main {
}
break;

case "lateChatReceivedInGame":
{
this.deleteNotification(notification);
const game_id = notification.game_id;
if (game_id in this.connected_finished_games) {
break;
}

trace.debug(`Connecting to ${game_id} to receive late chats`);
socket.send("chat/join", {
channel: `game-${game_id}`,
});
const on_chat = (chat) => {
handleChatLine(game_id, chat.line, notification.timestamp - 1);
};
socket.on(`game/${game_id}/chat`, on_chat);

// Connecting to a game from outside Game deserves a little
// bit of care, but I think it should be OK, because
// lateChatReceivedInGame implies the game is over, so we
// should not be getting in the way of anything here.
//
// We could connect to the game as we usually do, but this
// would confuse the logic there that expects to handle an
// unfinished game.
this.connected_finished_games[game_id] = true;
socket.send("game/connect", {
game_id: game_id,
chat: true,
});
setTimeout(() => {
trace.debug(`Disconnecting from ${game_id} (chats)`);
delete this.connected_finished_games[game_id];
socket.send("game/disconnect", {
game_id: game_id,
});
socket.off(`game/${game_id}/chat`, on_chat);
}, 5000);
}
break;

default:
{
if (!(notification.type in ignorable_notifications)) {
Expand Down

0 comments on commit 8d098d1

Please sign in to comment.