Skip to content

Commit

Permalink
Merge 106b630 into 0e0c3a2
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilNarayana committed May 11, 2021
2 parents 0e0c3a2 + 106b630 commit 39c4c76
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions src/console/dolphinConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ export class DolphinConnection extends EventEmitter implements Connection {

const dataString = data.toString("ascii");
const message = JSON.parse(dataString);
const { dolphin_closed } = message;
if (dolphin_closed) {
// We got a disconnection request
this.disconnect();
return;
}
this.emit(ConnectionEvent.MESSAGE, message);
switch (message.type) {
case DolphinMessageType.CONNECT_REPLY:
Expand All @@ -114,26 +120,29 @@ export class DolphinConnection extends EventEmitter implements Connection {
this.version = message.version;
this.emit(ConnectionEvent.HANDSHAKE, this.getDetails());
break;
case DolphinMessageType.GAME_EVENT:
const { payload, cursor } = message;
case DolphinMessageType.GAME_EVENT: {
const { payload } = message;
//TODO: remove after game start and end messages have been in stable Ishii for a bit
if (!payload) {
// We got a disconnection request
this.disconnect();
return;
}

if (this.gameCursor !== cursor) {
const err = new Error(
`Unexpected game data cursor. Expected: ${this.gameCursor} but got: ${cursor}. Payload: ${dataString}`,
);
console.error(err);
this.emit(ConnectionEvent.ERROR, err);
}
this._updateCursor(message, dataString);

const gameData = Buffer.from(payload, "base64");
this.gameCursor = message.next_cursor;
this._handleReplayData(gameData);
break;
}
case DolphinMessageType.START_GAME: {
this._updateCursor(message, dataString);
break;
}
case DolphinMessageType.END_GAME: {
this._updateCursor(message, dataString);
break;
}
}
});

Expand Down Expand Up @@ -163,4 +172,18 @@ export class DolphinConnection extends EventEmitter implements Connection {
this.emit(ConnectionEvent.STATUS_CHANGE, this.connectionStatus);
}
}

private _updateCursor(message: { cursor: number; next_cursor: number }, dataString: string): void {
const { cursor, next_cursor } = message;

if (this.gameCursor !== cursor) {
const err = new Error(
`Unexpected game data cursor. Expected: ${this.gameCursor} but got: ${cursor}. Payload: ${dataString}`,
);
console.error(err);
this.emit(ConnectionEvent.ERROR, err);
}

this.gameCursor = next_cursor;
}
}

0 comments on commit 39c4c76

Please sign in to comment.