Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/Goban/OGSConnectivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
deepEqual,
deepClone,
encodeMove,
GobanSocket,
IGobanSocket,
GobanSocketEvents,
ConditionalMoveTree,
GobanEngine,
Expand Down Expand Up @@ -67,7 +67,7 @@ interface JGOFPlayerClockWithTimedOut extends JGOFPlayerClock {
*/
export abstract class OGSConnectivity extends GobanInteractive {
public sent_timed_out_message: boolean = false;
protected socket!: GobanSocket;
protected socket!: IGobanSocket;
protected socket_event_bindings: Array<[keyof GobanSocketEvents, () => void]> = [];
protected connectToReviewSent?: boolean;

Expand Down Expand Up @@ -134,7 +134,7 @@ export abstract class OGSConnectivity extends GobanInteractive {
return 0;
}

protected connect(server_socket: GobanSocket): void {
protected connect(server_socket: IGobanSocket): void {
const socket = (this.socket = server_socket);

this.disconnectedFromGame = false;
Expand Down
4 changes: 2 additions & 2 deletions src/GobanBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
} from "./engine/formats/JGOF";
import { AdHocPackedMove } from "./engine/formats/AdHocFormat";
import { MessageID } from "./engine/messages";
import type { GobanSocket } from "./engine/GobanSocket";
import type { IGobanSocket } from "./engine/GobanSocket";
import type { ServerToClient, GameChatLine } from "./engine/protocol";
import { EventEmitter } from "eventemitter3";
import { setGobanCallbacks } from "./Goban/callbacks";
Expand Down Expand Up @@ -127,7 +127,7 @@ export interface GobanConfig extends GobanEngineConfig, PuzzleConfig {

// deprecated
username?: string;
server_socket?: GobanSocket;
server_socket?: IGobanSocket;
connect_to_chat?: number | boolean;
}

Expand Down
33 changes: 32 additions & 1 deletion src/engine/GobanSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interface ErrorResponse {
message: string;
}

interface GobanSocketOptions {
export interface GobanSocketOptions {
/** Don't automatically send pings */
dont_ping?: boolean;

Expand All @@ -67,6 +67,37 @@ const DEFAULT_PING_INTERVAL = 10000;
export type DataArgument<Entry> = Entry extends (...args: infer A) => void ? A[0] : never;
export type ProtocolResponseType<Entry> = Entry extends (...args: any[]) => infer R ? R : never;

/**
* Interface describing the public API of a GobanSocket (or compatible proxy).
* Both GobanSocket and GobanSocketProxy satisfy this interface.
* Extends EventEmitter<GobanSocketEvents> so on/off/emit types match exactly.
*/
export interface IGobanSocket<
SendProtocol extends ClientToServerBase = ClientToServer,
RecvProtocol = ServerToClient,
> extends EventEmitter<GobanSocketEvents> {
readonly url: string;
clock_drift: number;
latency: number;
options: GobanSocketOptions;
readonly connected: boolean;

send<Command extends keyof SendProtocol>(
command: Command,
data: DataArgument<SendProtocol[Command]>,
cb?: (data: ProtocolResponseType<SendProtocol[Command]>, error?: any) => void,
): void;

sendPromise<Command extends keyof SendProtocol>(
command: Command,
data: DataArgument<SendProtocol[Command]>,
): Promise<ProtocolResponseType<SendProtocol[Command]>>;

authenticate(authentication: DataArgument<SendProtocol["authenticate"]>): void;
disconnect(): void;
ping(): void;
}

/**
* This is a simple wrapper around the WebSocket API that provides a
* simple interface to connect to the Online-Go.com servers. It provides:
Expand Down
6 changes: 3 additions & 3 deletions src/engine/ai/AIReviewData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { EventEmitter } from "eventemitter3";
import { GobanSocket, GobanSocketEvents } from "../GobanSocket";
import { IGobanSocket, GobanSocketEvents } from "../GobanSocket";
import { JGOFAIReview, JGOFAIReviewMove } from "../formats/JGOF";
import * as protocol from "../protocol";
import { MoveTree } from "../MoveTree";
Expand Down Expand Up @@ -52,14 +52,14 @@ export interface AIReviewDataEvents {
* Takes care of socket communication and updating AI review data as it streams in.
* */
export class AIReviewData extends EventEmitter<AIReviewDataEvents> implements JGOFAIReview {
public readonly socket: GobanSocket<protocol.ClientToAIServer, protocol.AIServerToClient>;
public readonly socket: IGobanSocket<protocol.ClientToAIServer, protocol.AIServerToClient>;
public readonly uuid: string;
private ai_review: JGOFAIReview;
public readonly move_tree: MoveTree;
private analysis_requests_made: { [id: string]: boolean } = {};

constructor(
socket: GobanSocket<protocol.ClientToAIServer, protocol.AIServerToClient>,
socket: IGobanSocket<protocol.ClientToAIServer, protocol.AIServerToClient>,
move_tree: MoveTree,
ai_review: JGOFAIReview,
game_id: number | string,
Expand Down
8 changes: 5 additions & 3 deletions test/unit_tests/AIReviewData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@
(global as any).CLIENT = true;

import WS from "jest-websocket-mock";
import { GobanSocket } from "../../src/engine/GobanSocket";
import { GobanSocket, IGobanSocket } from "../../src/engine/GobanSocket";
import { AIReviewData } from "../../src/engine/ai/AIReviewData";
import type { ClientToAIServer } from "../../src/engine/protocol/ClientToAIServer";
import type { AIServerToClient } from "../../src/engine/protocol/AIServerToClient";
import { JGOFAIReview } from "../../src/engine/formats/JGOF";
import { GobanEngine } from "../../src/engine/GobanEngine";

describe("AIReviewData", () => {
let socket_server: WS;
let mock_socket: GobanSocket<any, any>;
let mock_socket: IGobanSocket<ClientToAIServer, AIServerToClient>;
const port = 48890;

beforeEach(async () => {
socket_server = new WS(`ws://localhost:${port}`, { jsonProtocol: true });
mock_socket = new GobanSocket(`ws://localhost:${port}`, {
mock_socket = new GobanSocket<ClientToAIServer, AIServerToClient>(`ws://localhost:${port}`, {
dont_ping: true,
quiet: true,
});
Expand Down
Loading