Skip to content

Commit

Permalink
Indicate messages as low priority and skip if 0 viewers
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonnold committed Jul 24, 2023
1 parent 9911a53 commit c16f194
Showing 1 changed file with 62 additions and 40 deletions.
102 changes: 62 additions & 40 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type UpdateResult = [EmitType, boolean, ...Array<any>];
type ConfigItem = {
fn: (tokens: CommandTokens) => Promise<UpdateResult> | UpdateResult;
split: boolean;
lowPrio: boolean;
};

type CommandConfig = {
Expand All @@ -63,25 +64,25 @@ class Handler {
this._game = this._broadcast.game;

this._commandConfig = {
[Command.FEN]: { fn: this.onFen.bind(this), split: true },
[Command.WPLAYER]: { fn: this.onPlayer.bind(this), split: false },
[Command.BPLAYER]: { fn: this.onPlayer.bind(this), split: false },
[Command.WPV]: { fn: this.onPV.bind(this), split: true },
[Command.BPV]: { fn: this.onPV.bind(this), split: true },
[Command.WTIME]: { fn: this.onTime.bind(this), split: true },
[Command.BTIME]: { fn: this.onTime.bind(this), split: true },
[Command.WMOVE]: { fn: this.onMove.bind(this), split: true },
[Command.BMOVE]: { fn: this.onMove.bind(this), split: true },
[Command.SITE]: { fn: this.onSite.bind(this), split: false },
[Command.CTRESET]: { fn: this.onCTReset.bind(this), split: false },
[Command.CT]: { fn: this.onCT.bind(this), split: false },
[Command.PONG]: { fn: () => [EmitType.UPDATE, false], split: false },
[Command.ADDUSER]: { fn: this.onAddUser.bind(this), split: false },
[Command.DELUSER]: { fn: this.onDelUser.bind(this), split: false },
[Command.CHAT]: { fn: this.onChat.bind(this), split: false },
[Command.MENU]: { fn: this.onMenu.bind(this), split: true },
[Command.RESULT]: { fn: this.onResult.bind(this), split: false },
[Command.FMR]: { fn: this.onFmr.bind(this), split: false },
[Command.FEN]: { fn: this.onFen.bind(this), split: true, lowPrio: false },
[Command.WPLAYER]: { fn: this.onPlayer.bind(this), split: false, lowPrio: false },
[Command.BPLAYER]: { fn: this.onPlayer.bind(this), split: false, lowPrio: false },
[Command.WPV]: { fn: this.onPV.bind(this), split: true, lowPrio: true },
[Command.BPV]: { fn: this.onPV.bind(this), split: true, lowPrio: true },
[Command.WTIME]: { fn: this.onTime.bind(this), split: true, lowPrio: true },
[Command.BTIME]: { fn: this.onTime.bind(this), split: true, lowPrio: true },
[Command.WMOVE]: { fn: this.onMove.bind(this), split: true, lowPrio: false },
[Command.BMOVE]: { fn: this.onMove.bind(this), split: true, lowPrio: false },
[Command.SITE]: { fn: this.onSite.bind(this), split: false, lowPrio: false },
[Command.CTRESET]: { fn: this.onCTReset.bind(this), split: false, lowPrio: false },
[Command.CT]: { fn: this.onCT.bind(this), split: false, lowPrio: false },
[Command.PONG]: { fn: () => [EmitType.UPDATE, false], split: false, lowPrio: false },
[Command.ADDUSER]: { fn: this.onAddUser.bind(this), split: false, lowPrio: false },
[Command.DELUSER]: { fn: this.onDelUser.bind(this), split: false, lowPrio: false },
[Command.CHAT]: { fn: this.onChat.bind(this), split: false, lowPrio: false },
[Command.MENU]: { fn: this.onMenu.bind(this), split: true, lowPrio: false },
[Command.RESULT]: { fn: this.onResult.bind(this), split: false, lowPrio: false },
[Command.FMR]: { fn: this.onFmr.bind(this), split: false, lowPrio: false },
};
}

Expand Down Expand Up @@ -149,10 +150,11 @@ class Handler {
this._game[color].nodes = parseInt(rest[3]);
this._game[color].usedTime = parseInt(rest[2]) * 10;

const pv = rest.slice(4);

const pvPlayout = new Chess();
pvPlayout.loadPgn(this._game.instance.pgn());

const pv = rest.slice(4);
const parsed = new Array<string>();
const pvAlg = new Array<string>();

Expand Down Expand Up @@ -359,26 +361,46 @@ class Handler {
let updateEmit: SerializedBroadcast | null = null;
const chatEmit: string[] = [];

for (const msg of messages) {
const [cmd, rest] = splitOnCommand(msg);

const commandConfig = this._commandConfig[cmd] as ConfigItem | undefined;

if (!commandConfig) logger.warn(`Unable to process ${cmd}!`, { port: this._broadcast.port });
else {
const [emit, updated, ...updateData] = await commandConfig.fn(
commandConfig.split ? [cmd, ...rest.trim().split(/\s+/)] : [cmd, rest],
);

if (updated) {
switch (emit) {
case EmitType.UPDATE:
updateEmit = this._broadcast.toJSON();
break;
case EmitType.CHAT:
chatEmit.push(updateData[0]);
break;
}
const processLowPrio = this._broadcast.browserCount > 0;
if (!processLowPrio)
logger.info('No one viewing broadcast, skipping processing of low-priority messages.', {
port: this._broadcast.port,
});

const highPrioMessages: [Command, string][] = [];
const lowPriorityMessages = new Map<Command, string>();

messages
.map((msg) => splitOnCommand(msg))
.forEach(([cmd, rest]) => {
const config = this._commandConfig[cmd] as ConfigItem | undefined;
if (!config) {
logger.warn(`Unable to process ${cmd}!`, { port: this._broadcast.port });
return;
}

if (config.lowPrio) {
lowPriorityMessages.set(cmd, rest);
} else {
highPrioMessages.push([cmd, rest]);
}
});

for (const [cmd, rest] of [...highPrioMessages, ...(processLowPrio ? lowPriorityMessages : [])]) {
const commandConfig = this._commandConfig[cmd];

const [emit, updated, ...updateData] = await commandConfig.fn(
commandConfig.split ? [cmd, ...rest.trim().split(/\s+/)] : [cmd, rest],
);

if (updated) {
switch (emit) {
case EmitType.UPDATE:
updateEmit = this._broadcast.toJSON();
break;
case EmitType.CHAT:
chatEmit.push(updateData[0]);
break;
}
}
}
Expand Down

0 comments on commit c16f194

Please sign in to comment.