mirror of
https://github.com/Pandipipas/scoreko-dev.git
synced 2026-06-06 03:32:06 +00:00
3a6289a2ea
- 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
92 lines
3.0 KiB
TypeScript
92 lines
3.0 KiB
TypeScript
import { nodecg } from '../util/nodecg.js';
|
|
import { getStringProp, normalizeTournamentSlug } from '../../shared/utils/string.js';
|
|
import { challongeOAuthServer, getOAuthMode } from '../oauth/challonge.js';
|
|
import { fetchRecentTournaments, fetchTournamentPlayers } from '../services/challonge.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('challonge: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/challonge/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 challongeOAuthServer.ensureServer(serverConfig);
|
|
} catch (err) {
|
|
sendAck(ack, err instanceof Error ? err.message : 'Could not start the OAuth callback server');
|
|
return;
|
|
}
|
|
|
|
sendAck(ack, null, challongeOAuthServer.createSession(serverConfig));
|
|
});
|
|
|
|
nodecg.listenFor('challonge:getOAuthSessionStatus', (payload: unknown, ack) => {
|
|
const sessionId = getStringProp(payload, 'sessionId');
|
|
if (!sessionId) {
|
|
sendAck(ack, 'Missing OAuth session id');
|
|
return;
|
|
}
|
|
|
|
const status = challongeOAuthServer.getSessionStatus(sessionId);
|
|
if (!status) {
|
|
sendAck(ack, 'OAuth session not found');
|
|
return;
|
|
}
|
|
|
|
sendAck(ack, null, status);
|
|
});
|
|
|
|
nodecg.listenFor('challonge:fetchRecentTournaments', async (payload: unknown, ack) => {
|
|
const token = getStringProp(payload, 'token');
|
|
if (!token) {
|
|
sendAck(ack, 'Missing Challonge 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('challonge:fetchTournamentPlayers', async (payload: unknown, ack) => {
|
|
const token = getStringProp(payload, 'token');
|
|
const slug = normalizeTournamentSlug(getStringProp(payload, 'slug'));
|
|
|
|
if (!token) { sendAck(ack, 'Missing Challonge 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');
|
|
}
|
|
});
|