attempt to filter out tracks
This commit is contained in:
parent
c14b7a7308
commit
22111b2b30
2 changed files with 20 additions and 6 deletions
|
|
@ -8,6 +8,7 @@ import type { Question } from "../party-types";
|
|||
import {
|
||||
buildQuestionWindow,
|
||||
getQuestionRange,
|
||||
isUsableText,
|
||||
type PartyAnalytics,
|
||||
type PartyQuestionMember,
|
||||
resolveQuestionSong,
|
||||
|
|
@ -29,7 +30,7 @@ async function getAlbumReleaseYear({
|
|||
db,
|
||||
analytics,
|
||||
index,
|
||||
}: BuildNumericQuestionInput): Promise<NumericQuestion> {
|
||||
}: BuildNumericQuestionInput): Promise<NumericQuestion | null> {
|
||||
const trackName = analytics?.storyClusters?.[0]?.tracks?.[0]?.name;
|
||||
const track = trackName
|
||||
? await db.query.track.findFirst({
|
||||
|
|
@ -40,10 +41,13 @@ async function getAlbumReleaseYear({
|
|||
const song = await resolveQuestionSong(db, analytics, {
|
||||
trackName: track?.name ?? trackName ?? undefined,
|
||||
});
|
||||
const subject = [track?.album?.name, track?.name].find((value) =>
|
||||
isUsableText(value),
|
||||
);
|
||||
if (!subject) return null;
|
||||
const correct =
|
||||
track?.album?.release_date?.getFullYear() ??
|
||||
new Date().getFullYear() - 1 - index;
|
||||
const subject = track?.album?.name ?? track?.name ?? "unknown album";
|
||||
return {
|
||||
type: "numeric",
|
||||
text: `What's the release year of ${subject}?`,
|
||||
|
|
@ -127,7 +131,8 @@ export async function buildNumericQuestion(
|
|||
): Promise<Question | null> {
|
||||
const questions: NumericQuestion[] = [];
|
||||
|
||||
questions.push(await getAlbumReleaseYear(input));
|
||||
const albumYearQ = await getAlbumReleaseYear(input);
|
||||
if (albumYearQ) questions.push(albumYearQ);
|
||||
|
||||
const topTrackQ = await countTopTrackListeners(input);
|
||||
if (topTrackQ) questions.push(topTrackQ);
|
||||
|
|
|
|||
|
|
@ -113,7 +113,9 @@ export function getTopClusterArtists(analytics: PartyAnalytics): string[] {
|
|||
export function getTopClusterTracks(
|
||||
analytics: PartyAnalytics,
|
||||
): AnalyticsTrack[] {
|
||||
return analytics?.storyClusters?.[0]?.tracks ?? [];
|
||||
return (analytics?.storyClusters?.[0]?.tracks ?? []).filter((track) =>
|
||||
isUsableText(track.name),
|
||||
);
|
||||
}
|
||||
|
||||
export function pickRelevantTrack(
|
||||
|
|
@ -164,7 +166,8 @@ export async function resolveQuestionSong(
|
|||
where: { name: trackHint.name },
|
||||
with: { album: true, artists: true },
|
||||
});
|
||||
if (tracks.length === 0) return null;
|
||||
const usableTracks = tracks.filter((track) => isUsableText(track.name));
|
||||
if (usableTracks.length === 0) return null;
|
||||
|
||||
const scoreTrack = (track: (typeof tracks)[number]) => {
|
||||
const artistNames = track.artists?.map((artist) => artist.name) ?? [];
|
||||
|
|
@ -176,7 +179,7 @@ export async function resolveQuestionSong(
|
|||
return (matchesAlbum ? 2 : 0) + (matchesArtist ? 2 : 0);
|
||||
};
|
||||
|
||||
const chosen = tracks
|
||||
const chosen = usableTracks
|
||||
.slice()
|
||||
.sort((a, b) => scoreTrack(b) - scoreTrack(a))[0];
|
||||
if (!chosen) return null;
|
||||
|
|
@ -184,6 +187,12 @@ export async function resolveQuestionSong(
|
|||
return song;
|
||||
}
|
||||
|
||||
export function isUsableText(
|
||||
value: string | null | undefined,
|
||||
): value is string {
|
||||
return typeof value === "string" && value.trim().length > 0;
|
||||
}
|
||||
|
||||
export function getTopTrackListener(
|
||||
track: { memberScores?: { userId: string; score: number }[] },
|
||||
members: PartyQuestionMember[],
|
||||
|
|
|
|||
Loading…
Reference in a new issue