Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FTC Support #50

Open
wants to merge 2 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions electron/helpers/ffmpegCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export interface SplitBlock {
export interface SplitFixedDetails {
id: string;
inputFile: string;
outputFile: string;
outputDirectory: string;
outputFileName: string;
blocks: SplitBlock[];
}

Expand Down Expand Up @@ -158,16 +159,20 @@ export async function splitFixedLength(
)
);

await concatVideoFiles(videoParts, details.outputFile, (progress) => {
const msSinceLastUpdate = Date.now() - lastProgressSent;
if (msSinceLastUpdate < progressReportRateMs) return;
lastProgressSent = Date.now();

event.sender.send('split-progress', {
id: details.id,
percent: 100,
});
});
await concatVideoFiles(
videoParts,
path.join(details.outputDirectory, details.outputFileName),
(progress) => {
const msSinceLastUpdate = Date.now() - lastProgressSent;
if (msSinceLastUpdate < progressReportRateMs) return;
lastProgressSent = Date.now();

event.sender.send('split-progress', {
id: details.id,
percent: 100,
});
}
);

event.sender.send('split-end', { matchKey: details.id });
}
15 changes: 12 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"fluent-ffmpeg": "^2.1.2",
"luxon": "^3.4.3",
"react-player": "^2.13.0",
"react-viewport-list": "^7.1.2",
"tailwind-merge": "^2.0.0",
"tailwindcss": "^3.3.5",
"uuid": "^9.0.1",
Expand Down
81 changes: 81 additions & 0 deletions src/api/FTCTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
export interface EventList {
events: Event[];
eventCount: number;
}

export interface Event {
eventId: string;
code: string;
divisionCode: null | string;
name: string;
remote: boolean;
hybrid: boolean;
fieldCount: number;
published: boolean;
type: string;
typeName: string;
regionCode: string;
leagueCode: null | string;
districtCode: string;
venue: string;
address: string;
city: string;
stateprov: string;
country: string;
website: null | string;
liveStreamUrl: null | string;
timezone: string;
dateStart: string;
dateEnd: string;
}

export interface HybridSchedule {
schedule: FTCMatch[];
}

export interface FTCMatch {
description: string;
tournamentLevel: TournamentLevel;
series: number;
matchNumber: number;
startTime: string;
actualStartTime: string;
postResultTime: string;
scoreRedFinal: number;
scoreRedFoul: number;
scoreRedAuto: number;
scoreBlueFinal: number;
scoreBlueFoul: number;
scoreBlueAuto: number;
scoreBlueDriveControlled: number;
scoreBlueEndgame: number;
redWins: boolean;
blueWins: boolean;
teams: Team[];
}

export interface Team {
teamNumber: number;
displayTeamNumber: string;
station: Station;
surrogate: boolean;
noShow: boolean;
dq: boolean;
onField: boolean;
teamName: string;
}

export enum Station {
Blue1 = 'Blue1',
Blue2 = 'Blue2',
Blue3 = 'Blue3',
Red1 = 'Red1',
Red2 = 'Red2',
Red3 = 'Red3',
}

export enum TournamentLevel {
Qualification = 'QUALIFICATION',
Semifinal = 'SEMIFINAL',
Final = 'FINAL',
}
File renamed without changes.
47 changes: 47 additions & 0 deletions src/api/ftcApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { EventList, HybridSchedule } from './FTCTypes';

const ftcRoot = 'https://ftc-api.firstinspires.org/v2.0';
function authHeader() {
const ftcUser = localStorage.getItem('ftcApiUsername')
? JSON.parse(localStorage.getItem('ftcApiUsername')!)
: '';
const ftcPass = localStorage.getItem('ftcApiPassword')
? JSON.parse(localStorage.getItem('ftcApiPassword')!)
: '';

return `Basic ${btoa(`${ftcUser}:${ftcPass}`)}`;
}

export async function getMatches(year: number, eventKey?: string) {
if (!eventKey) {
return Promise.reject('No event key provided');
}
const reqQual = await fetch(
`${ftcRoot}/${year}/schedule/${eventKey}/qual/hybrid`,
{
headers: { Authorization: authHeader() },
}
);
const resQual = (await reqQual.json()) as HybridSchedule;
const reqPlayoff = await fetch(
`${ftcRoot}/${year}/schedule/${eventKey}/playoff/hybrid`,
{
headers: { Authorization: authHeader() },
}
);
const resPlayoff = (await reqPlayoff.json()) as HybridSchedule;
return [...resQual.schedule, ...resPlayoff.schedule];
}

export async function getEvents(year: number) {
const res = await fetch(`${ftcRoot}/${encodeURIComponent(year)}/events`, {
headers: {
Authorization: authHeader(),
},
});
if (res.status === 401) {
throw new Error('Invalid FTC credentials');
}
const events = (await res.json()) as EventList;
return events.events.sort((a, b) => a.dateStart.localeCompare(b.dateEnd));
}
4 changes: 4 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './TBATypes';
export * from './tbaApi';
export * from './useApiEvents';
export * from './useApiMatches';
File renamed without changes.
48 changes: 48 additions & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export type Program = 'frc' | 'ftc';

export type ApiEvent = FrcApiEvent | FtcApiEvent;

interface FrcApiEvent {
program: 'frc';
key: string;
name: string;
startDate: string;
endDate: string;
venue: string;
city: string;
stateProv: string;
country: string;
}

interface FtcApiEvent {
program: 'ftc';
key: { year: number; code: string };
name: string;
startDate: string;
endDate: string;
venue: string;
city: string;
stateProv: string;
country: string;
}

export interface ApiMatch {
id: string;
name: string;
series?: number;
number: number;
scheduledTime?: Date;
startTime?: Date;
resultsTime?: Date;
level: MatchLevel;
redAlliance: string[];
blueAlliance: string[];
}

export enum MatchLevel {
Qualification,
EighthFinal,
QuarterFinal,
SemiFinal,
Final,
}
42 changes: 42 additions & 0 deletions src/api/useApiEvents.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { UseQueryResult, useQuery } from '@tanstack/react-query';
import { getEvents as getFtcEvents } from './ftcApi';
import { getEvents as getTbaEvents } from './tbaApi';
import { ApiEvent, Program } from './types';

export function useApiEvents(program: Program, year: number): UseQueryResult<ApiEvent[], Error> {
const events = useQuery({
queryKey: ['events', program, year],
retry: false,
queryFn: async () => {
if (program == 'frc') {
const events = await getTbaEvents(year);
return events.map((event) => ({
program: 'frc',
key: event.key,
name: event.name,
startDate: event.start_date,
endDate: event.end_date,
venue: event.location_name,
city: event.city,
stateProv: event.state_prov,
country: event.country,
} as ApiEvent))
} else {
const events = await getFtcEvents(year);
return events.map((event) => ({
program: 'ftc',
key: { year: year, code: event.code },
name: event.name,
startDate: event.dateStart.split('T')[0],
endDate: event.dateEnd.split('T')[0],
venue: event.venue,
city: event.city,
stateProv: event.stateprov,
country: event.country,
} as ApiEvent))
}
}
});

return events;
}
Loading