2024-04-27 18:45:18 +02:00
|
|
|
import {type Handler} from "@netlify/functions";
|
|
|
|
import {api} from "./shared/api.js";
|
2025-02-18 02:14:01 +01:00
|
|
|
import {type SpeedruncomInfo} from "#Infos/Speedrun/Speedruncom.js";
|
2023-05-08 02:31:43 +02:00
|
|
|
|
2025-02-17 13:26:10 +01:00
|
|
|
interface Runs {
|
|
|
|
data: {
|
|
|
|
place: number;
|
|
|
|
run: {
|
|
|
|
weblink: string;
|
|
|
|
game: string;
|
|
|
|
level?: string;
|
|
|
|
category?: string;
|
|
|
|
videos: {
|
|
|
|
links: {
|
|
|
|
uri: string
|
|
|
|
}[]
|
|
|
|
}
|
|
|
|
date: string;
|
|
|
|
times: {
|
|
|
|
primary_t: number
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}[]
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Game {
|
|
|
|
data: {
|
|
|
|
names: {
|
|
|
|
international: string;
|
|
|
|
};
|
|
|
|
assets: {
|
|
|
|
"cover-tiny": {
|
|
|
|
uri: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Level {
|
|
|
|
data: {
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-05 21:01:24 +01:00
|
|
|
const handler: Handler = async () => {
|
|
|
|
// using the API's embedding would be stupid here, as that'd create lag due to irrelevant runs
|
2025-02-17 13:26:10 +01:00
|
|
|
const speedruncom = await api<Runs>("https://www.speedrun.com/api/v1/users/j03v45mj/personal-bests");
|
|
|
|
const data = speedruncom.data.at(0);
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
return {
|
|
|
|
statusCode: 404,
|
|
|
|
};
|
|
|
|
}
|
2023-05-08 02:31:43 +02:00
|
|
|
|
2024-04-27 21:22:48 +02:00
|
|
|
const detailsToRequest = [new Promise((resolve) => {
|
2025-02-17 13:26:10 +01:00
|
|
|
resolve(api<Game>(`https://www.speedrun.com/api/v1/games/${data.run.game}`));
|
2024-04-27 18:45:18 +02:00
|
|
|
})];
|
2023-05-08 02:31:43 +02:00
|
|
|
|
2025-02-17 13:26:10 +01:00
|
|
|
if (data.run.level) {
|
2024-04-27 21:22:48 +02:00
|
|
|
detailsToRequest.push(new Promise((resolve) => {
|
2025-02-17 13:26:10 +01:00
|
|
|
resolve(api<Level>(`https://www.speedrun.com/api/v1/levels/${data.run.level}`));
|
2024-04-27 18:45:18 +02:00
|
|
|
}));
|
2023-05-08 02:31:43 +02:00
|
|
|
}
|
|
|
|
|
2025-02-17 13:26:10 +01:00
|
|
|
if (data.run.category) {
|
2024-04-27 21:22:48 +02:00
|
|
|
detailsToRequest.push(new Promise((resolve) => {
|
2025-02-17 13:26:10 +01:00
|
|
|
resolve(api<Level>(`https://www.speedrun.com/api/v1/categories/${data.run.category}`));
|
2024-04-27 18:45:18 +02:00
|
|
|
}));
|
2023-05-08 02:31:43 +02:00
|
|
|
}
|
|
|
|
|
2025-02-17 13:26:10 +01:00
|
|
|
const requests = await Promise.all(detailsToRequest);
|
|
|
|
const game = requests[0] as Game;
|
|
|
|
const details = requests.slice(1) as Level[];
|
2023-05-08 02:31:43 +02:00
|
|
|
|
2023-11-05 21:01:24 +01:00
|
|
|
const run: SpeedruncomInfo = {
|
2025-02-17 13:26:10 +01:00
|
|
|
place: data.place,
|
|
|
|
link: data.run.weblink,
|
|
|
|
date: data.run.date,
|
|
|
|
thumbnail: game.data.assets["cover-tiny"].uri,
|
|
|
|
game: game.data.names.international,
|
|
|
|
details: details.map((d) => d.data.name),
|
|
|
|
time: sec2time(data.run.times.primary_t),
|
|
|
|
video: data.run.videos.links.at(0)?.uri,
|
2024-04-27 18:45:18 +02:00
|
|
|
};
|
2023-05-08 02:31:43 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
statusCode: 200,
|
2024-04-27 18:45:18 +02:00
|
|
|
body: JSON.stringify(run),
|
|
|
|
};
|
|
|
|
};
|
2023-05-08 02:31:43 +02:00
|
|
|
|
2025-02-17 13:26:10 +01:00
|
|
|
// https://gist.github.com/vankasteelj/74ab7793133f4b257ea3
|
|
|
|
function sec2time(timeInSeconds: number) {
|
|
|
|
const pad = (num: number, size: number) => ("000" + num).slice(size * -1);
|
|
|
|
const time = Number(parseFloat(timeInSeconds.toString()).toFixed(3));
|
|
|
|
const hours = Math.floor(time / 60 / 60);
|
|
|
|
const minutes = Math.floor(time / 60) % 60;
|
|
|
|
const seconds = Math.floor(time - minutes * 60);
|
|
|
|
const milliseconds = Number(time.toString().slice(-3));
|
|
|
|
return pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "." + pad(milliseconds, 3);
|
|
|
|
};
|
|
|
|
|
2024-04-27 18:45:18 +02:00
|
|
|
export {handler};
|