Skip to content

Commit

Permalink
feat: Move keyOverlay to their own socket (only v2)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyperdark committed Feb 19, 2024
1 parent ca9c50b commit 1b6430b
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 30 deletions.
10 changes: 5 additions & 5 deletions packages/common/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const config = {
debugLogging: (process.env.DEBUG_LOG || '') === 'true',
calculatePP: (process.env.CALCULATE_PP || '') === 'true',
enableKeyOverlay: (process.env.ENABLE_KEY_OVERLAY || '') === 'true',
wsSendInterval: Number(process.env.WS_SEND_INTERVAL || '500'),
// wsSendInterval: Number(process.env.WS_SEND_INTERVAL || '500'),
pollRate: Number(process.env.POLL_RATE || '500'),
keyOverlayPollRate: Number(process.env.KEYOVERLAY_POLL_RATE || '100'),
serverIP: process.env.SERVER_IP || '127.0.0.1',
Expand All @@ -79,10 +79,10 @@ export const updateConfig = () => {
fs.appendFileSync(configPath, '\nENABLE_KEY_OVERLAY=true', 'utf8');
}

if (!process.env.WS_SEND_INTERVAL) {
newOptions += 'WS_SEND_INTERVAL, ';
fs.appendFileSync(configPath, '\nWS_SEND_INTERVAL=150', 'utf8');
}
// if (!process.env.WS_SEND_INTERVAL) {
// newOptions += 'WS_SEND_INTERVAL, ';
// fs.appendFileSync(configPath, '\nWS_SEND_INTERVAL=150', 'utf8');
// }

if (!process.env.POLL_RATE) {
newOptions += 'POLL_RATE, ';
Expand Down
39 changes: 37 additions & 2 deletions packages/server/socket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const WebSocketV1 = (instancesManager: any) => {
}

ws.send(JSON.stringify(osuInstances[0].getState(instancesManager)));
await sleep(config.wsSendInterval);
await sleep(config.pollRate);
}
});

Expand Down Expand Up @@ -64,7 +64,42 @@ export const WebSocketV2 = (instancesManager: any) => {
ws.send(
JSON.stringify(osuInstances[0].getStateV2(instancesManager))
);
await sleep(config.wsSendInterval);
await sleep(config.pollRate);
}
});

return wss;
};

export const WebSocketKeys = (instancesManager: any) => {
const wss = new WebSocket.Server({ noServer: true });
wss.on('connection', async (ws) => {
wLogger.debug('>>> websocketV2: CONNECTED');
let isSocketConnected = true;

ws.on('close', function (reasonCode, description) {
isSocketConnected = false;
wLogger.debug('>>> websocketV2: CLOSED');
});

ws.on('error', function (reasonCode, description) {
isSocketConnected = false;
wLogger.debug(
`>>> websocketV2: error: ${reasonCode} [${description}]`
);
});

while (isSocketConnected) {
const osuInstances: any = Object.values(
instancesManager.osuInstances || {}
);
if (osuInstances.length < 1) {
await sleep(500);
continue;
}

ws.send(JSON.stringify(osuInstances[0].getKeyOverlay()));
await sleep(config.keyOverlayPollRate);
}
});

Expand Down
10 changes: 9 additions & 1 deletion packages/tosu/src/api/router/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,25 @@ import { readSongsFolder } from '../handlers/songs';

export const ApiV2 = ({
app,
webSocket
webSocket,
keysWebsocket
}: {
app: HttpServer;
webSocket: WebSocket.Server;
keysWebsocket: WebSocket.Server;
}) => {
app.server.on('upgrade', function (request, socket, head) {
if (request.url == '/websocket/v2') {
webSocket.handleUpgrade(request, socket, head, function (ws) {
webSocket.emit('connection', ws, request);
});
}

if (request.url == '/websocket/v2/keys') {
keysWebsocket.handleUpgrade(request, socket, head, function (ws) {
keysWebsocket.emit('connection', ws, request);
});
}
});

// todo
Expand Down
1 change: 0 additions & 1 deletion packages/tosu/src/api/types/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export interface ApiV2Answer {
beatmap: Beatmap;
player: Player;
leaderboard: Leaderboard[];
keyOverlay: KeyOverlay;
performance: Performance;
resultsScreen: ResultsScreen;
folders: Folders;
Expand Down
42 changes: 24 additions & 18 deletions packages/tosu/src/api/utils/buildResultV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getOsuModsString } from '@/utils/osuMods';
import {
ApiV2Answer,
BeatmapStatuses,
KeyOverlay,
Leaderboard,
Modes,
Tourney,
Expand Down Expand Up @@ -318,24 +319,6 @@ export const buildResult = (
convertMemoryPlayerToResult(slot, Modes[gamePlayData.Mode])
)
: [],
keyOverlay: {
k1: {
isPressed: gamePlayData.KeyOverlay.K1Pressed,
count: gamePlayData.KeyOverlay.K1Count
},
k2: {
isPressed: gamePlayData.KeyOverlay.K2Pressed,
count: gamePlayData.KeyOverlay.K2Count
},
m1: {
isPressed: gamePlayData.KeyOverlay.M1Pressed,
count: gamePlayData.KeyOverlay.M1Count
},
m2: {
isPressed: gamePlayData.KeyOverlay.M2Pressed,
count: gamePlayData.KeyOverlay.M2Count
}
},
performance: {
accuracy: beatmapPpData.ppAcc,
graph: beatmapPpData.strainsAll
Expand Down Expand Up @@ -412,6 +395,29 @@ export const buildResult = (
};
};

export const buildKeyOverlay = (service: DataRepo): KeyOverlay => {
const { gamePlayData } = service.getServices(['gamePlayData']);

return {
k1: {
isPressed: gamePlayData.KeyOverlay.K1Pressed,
count: gamePlayData.KeyOverlay.K1Count
},
k2: {
isPressed: gamePlayData.KeyOverlay.K2Pressed,
count: gamePlayData.KeyOverlay.K2Count
},
m1: {
isPressed: gamePlayData.KeyOverlay.M1Pressed,
count: gamePlayData.KeyOverlay.M1Count
},
m2: {
isPressed: gamePlayData.KeyOverlay.M2Pressed,
count: gamePlayData.KeyOverlay.M2Count
}
};
};

const buildTourneyData = (
instancesManager: InstanceManager
): Tourney | undefined => {
Expand Down
9 changes: 7 additions & 2 deletions packages/tosu/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
updateConfig,
wLogger
} from '@tosu/common';
import { HttpServer, WebSocketV1, WebSocketV2 } from '@tosu/server';
import { HttpServer, WebSocketV1, WebSocketV2, WebSocketKeys } from '@tosu/server';
import { autoUpdater } from '@tosu/updater';

import { httpMiddleware } from './api/middleware';
Expand Down Expand Up @@ -33,11 +33,16 @@ import { InstanceManager } from './objects/instanceManager/instanceManager';
const httpServer = new HttpServer();
const legacyWebSocket = WebSocketV1(instancesManager);
const webSocketV2 = WebSocketV2(instancesManager);
const keysWebsocket = WebSocketKeys(instancesManager);

httpMiddleware({ app: httpServer, instanceManager: instancesManager });
baseApi(httpServer);
legacyApi({ app: httpServer, webSocket: legacyWebSocket });
ApiV2({ app: httpServer, webSocket: webSocketV2 });
ApiV2({
app: httpServer,
webSocket: webSocketV2,
keysWebsocket: keysWebsocket
});

httpServer.listen(config.serverPort, config.serverIP);
})();
9 changes: 8 additions & 1 deletion packages/tosu/src/objects/instanceManager/osuInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import path from 'path';
import { Process } from 'tsprocess/dist/process';

import { buildResult } from '@/api/utils/buildResult';
import { buildResult as buildResultV2 } from '@/api/utils/buildResultV2';
import {
buildKeyOverlay,
buildResult as buildResultV2
} from '@/api/utils/buildResultV2';
import { AllTimesData } from '@/entities/AllTimesData';
import { BassDensityData } from '@/entities/BassDensityData';
import { BeatmapPPData } from '@/entities/BeatmapPpData';
Expand Down Expand Up @@ -399,4 +402,8 @@ export class OsuInstance {
getStateV2(instancesManager: InstanceManager) {
return buildResultV2(this.entities, instancesManager);
}

getKeyOverlay() {
return buildKeyOverlay(this.entities);
}
}

0 comments on commit 1b6430b

Please sign in to comment.