mirror of
https://github.com/Pandipipas/scoreko-dev.git
synced 2026-06-06 03:32:06 +00:00
feat: implement start.gg OAuth integration and services
- Added start.gg OAuth server and session management in startgg.ts - Implemented functions to fetch recent tournaments and tournament players from start.gg - Created utility functions for string and country code handling - Introduced Challonge OAuth server and services for tournament data fetching - Refactored shared types and utility functions for better organization - Updated scoreboard graphics to use new country resolution utilities - Removed legacy startgg.ts file to streamline codebase
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import { nodecg } from '../util/nodecg.js';
|
||||
import { getStringProp } from '../../shared/utils/string.js';
|
||||
import { startggOAuthServer, getOAuthMode } from '../oauth/startgg.js';
|
||||
import { fetchRecentTournaments, fetchTournamentPlayers } from '../services/startgg.js';
|
||||
import type { OAuthConfig } from '../util/oauth-server.js';
|
||||
|
||||
const sendAck = (ack: unknown, error: string | null, response?: unknown) => {
|
||||
if (typeof ack === 'function') ack(error, response);
|
||||
};
|
||||
|
||||
nodecg.listenFor('startgg:createOAuthSession', async (_payload: unknown, ack) => {
|
||||
const mode = getOAuthMode();
|
||||
let serverConfig: OAuthConfig;
|
||||
|
||||
if (mode.type === 'dev') {
|
||||
serverConfig = {
|
||||
clientId: mode.clientId,
|
||||
callbackPort: mode.callbackPort,
|
||||
};
|
||||
} else {
|
||||
try {
|
||||
const res = await fetch(`${mode.proxyBaseUrl}/oauth/startgg/client-id`);
|
||||
if (!res.ok) throw new Error(`Proxy responded with ${res.status}`);
|
||||
const data = await res.json() as { clientId?: string };
|
||||
const clientId = String(data.clientId ?? '').trim();
|
||||
if (!clientId) throw new Error('Proxy did not return a clientId');
|
||||
serverConfig = { clientId, callbackPort: mode.callbackPort };
|
||||
} catch (err) {
|
||||
sendAck(
|
||||
ack,
|
||||
err instanceof Error ? err.message : 'Could not fetch OAuth config from proxy',
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await startggOAuthServer.ensureServer(serverConfig);
|
||||
} catch (err) {
|
||||
sendAck(ack, err instanceof Error ? err.message : 'Could not start the OAuth callback server');
|
||||
return;
|
||||
}
|
||||
|
||||
sendAck(ack, null, startggOAuthServer.createSession(serverConfig));
|
||||
});
|
||||
|
||||
nodecg.listenFor('startgg:getOAuthSessionStatus', (payload: unknown, ack) => {
|
||||
const sessionId = getStringProp(payload, 'sessionId');
|
||||
if (!sessionId) {
|
||||
sendAck(ack, 'Missing OAuth session id');
|
||||
return;
|
||||
}
|
||||
|
||||
const status = startggOAuthServer.getSessionStatus(sessionId);
|
||||
if (!status) {
|
||||
sendAck(ack, 'OAuth session not found');
|
||||
return;
|
||||
}
|
||||
|
||||
sendAck(ack, null, status);
|
||||
});
|
||||
|
||||
nodecg.listenFor('startgg:fetchRecentTournaments', async (payload: unknown, ack) => {
|
||||
const token = getStringProp(payload, 'token');
|
||||
if (!token) {
|
||||
sendAck(ack, 'Missing start.gg API token');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const tournaments = await fetchRecentTournaments(token);
|
||||
sendAck(ack, null, tournaments);
|
||||
} catch (error) {
|
||||
sendAck(ack, error instanceof Error ? error.message : 'Unknown error while loading tournaments');
|
||||
}
|
||||
});
|
||||
|
||||
nodecg.listenFor('startgg:fetchTournamentPlayers', async (payload: unknown, ack) => {
|
||||
const token = getStringProp(payload, 'token');
|
||||
const slug = getStringProp(payload, 'slug');
|
||||
|
||||
if (!token) { sendAck(ack, 'Missing start.gg API token'); return; }
|
||||
if (!slug) { sendAck(ack, 'Missing tournament slug'); return; }
|
||||
|
||||
try {
|
||||
const players = await fetchTournamentPlayers(slug, token);
|
||||
sendAck(ack, null, players);
|
||||
} catch (error) {
|
||||
sendAck(ack, error instanceof Error ? error.message : 'Unknown error while importing players');
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user