173 lines
4.8 KiB
TypeScript
173 lines
4.8 KiB
TypeScript
import { and, eq, inArray } from "drizzle-orm";
|
|
import type { db } from "../db";
|
|
import {
|
|
topArtist as topArtistTable,
|
|
topTrack as topTrackTable,
|
|
} from "../db/schema";
|
|
import type { Question, QuizRound } from "../party-types";
|
|
import {
|
|
buildQuestionWindow,
|
|
getReleaseYearRange,
|
|
isUsableText,
|
|
type PartyAnalytics,
|
|
type PartyQuestionMember,
|
|
pickQuestionCandidate,
|
|
type QuestionCandidate,
|
|
resolveQuestionSong,
|
|
} from "./question-utils";
|
|
|
|
type NumericQuestion = Omit<
|
|
Extract<Question, { type: "numeric" }>,
|
|
"startTimestamp" | "endTimestamp"
|
|
>;
|
|
|
|
type BuildNumericQuestionInput = {
|
|
db: typeof db;
|
|
analytics: PartyAnalytics;
|
|
index: number;
|
|
members: PartyQuestionMember[];
|
|
history: QuizRound[];
|
|
};
|
|
|
|
async function getAlbumReleaseYear({
|
|
db,
|
|
analytics,
|
|
}: BuildNumericQuestionInput): Promise<NumericQuestion | null> {
|
|
const trackName = analytics?.storyClusters?.[0]?.tracks?.[0]?.name;
|
|
const track = trackName
|
|
? await db.query.track.findFirst({
|
|
where: { name: trackName },
|
|
with: { album: true },
|
|
})
|
|
: null;
|
|
const subject = [track?.album?.name, track?.name].find((value) =>
|
|
isUsableText(value),
|
|
);
|
|
if (!subject) return null;
|
|
if (!track?.album?.release_date) return null;
|
|
const song = await resolveQuestionSong(db, analytics, {
|
|
trackName: track?.name ?? trackName ?? undefined,
|
|
});
|
|
const correct = track.album.release_date.getFullYear();
|
|
return {
|
|
type: "numeric",
|
|
text: `What's the release year of ${subject}?`,
|
|
correct,
|
|
range: getReleaseYearRange(correct),
|
|
points: 10,
|
|
song: song ?? undefined,
|
|
questionKey: `numeric:album-year:${subject}`,
|
|
subjectKey: `album:${subject}`,
|
|
};
|
|
}
|
|
|
|
async function countTopTrackListeners({
|
|
db,
|
|
analytics,
|
|
members,
|
|
}: BuildNumericQuestionInput): Promise<NumericQuestion | null> {
|
|
const trackName = analytics?.storyClusters?.[0]?.tracks?.[0]?.name;
|
|
if (!trackName || members.length === 0) return null;
|
|
const dbTrack = await db.query.track.findFirst({
|
|
where: { name: trackName },
|
|
});
|
|
if (!dbTrack) return null;
|
|
const song = await resolveQuestionSong(db, analytics, { trackName });
|
|
const memberIds = members.map((m) => m.userId);
|
|
const entries = await db
|
|
.select({ userId: topTrackTable.userId })
|
|
.from(topTrackTable)
|
|
.where(
|
|
and(
|
|
eq(topTrackTable.trackId, dbTrack.id),
|
|
inArray(topTrackTable.userId, memberIds),
|
|
),
|
|
);
|
|
const correct = new Set(entries.map((e) => e.userId)).size;
|
|
if (correct <= 0) return null;
|
|
return {
|
|
type: "numeric",
|
|
text: `For how many players in the party is "${trackName}" a top track?`,
|
|
correct,
|
|
range: { min: 0, max: members.length },
|
|
points: 10,
|
|
song: song ?? undefined,
|
|
questionKey: `numeric:top-track-count:${trackName}`,
|
|
subjectKey: `track:${trackName}`,
|
|
};
|
|
}
|
|
|
|
async function countFavouriteArtistListeners({
|
|
db,
|
|
analytics,
|
|
members,
|
|
}: BuildNumericQuestionInput): Promise<NumericQuestion | null> {
|
|
const artistName = analytics?.storyClusters?.[0]?.artists?.[0]?.name;
|
|
if (!artistName || members.length === 0) return null;
|
|
const dbArtist = await db.query.artist.findFirst({
|
|
where: { name: artistName },
|
|
});
|
|
if (!dbArtist) return null;
|
|
const song = await resolveQuestionSong(db, analytics, {
|
|
artistNames: [artistName],
|
|
});
|
|
const memberIds = members.map((m) => m.userId);
|
|
const entries = await db
|
|
.select({ userId: topArtistTable.userId })
|
|
.from(topArtistTable)
|
|
.where(
|
|
and(
|
|
eq(topArtistTable.artistId, dbArtist.id),
|
|
inArray(topArtistTable.userId, memberIds),
|
|
),
|
|
);
|
|
const correct = new Set(entries.map((e) => e.userId)).size;
|
|
if (correct <= 0) return null;
|
|
return {
|
|
type: "numeric",
|
|
text: `How many players in the party have "${artistName}" as a favourite artist?`,
|
|
correct,
|
|
range: { min: 0, max: members.length },
|
|
points: 10,
|
|
song: song ?? undefined,
|
|
questionKey: `numeric:artist-count:${artistName}`,
|
|
subjectKey: `artist:${artistName}`,
|
|
};
|
|
}
|
|
|
|
export async function buildNumericQuestion(
|
|
input: BuildNumericQuestionInput,
|
|
): Promise<Question | null> {
|
|
const questions: Array<QuestionCandidate<NumericQuestion>> = [];
|
|
|
|
const albumYearQ = await getAlbumReleaseYear(input);
|
|
if (albumYearQ) {
|
|
questions.push({
|
|
key: albumYearQ.questionKey ?? `numeric:album-year:${albumYearQ.text}`,
|
|
subjectKey: albumYearQ.subjectKey,
|
|
question: albumYearQ,
|
|
});
|
|
}
|
|
|
|
const topTrackQ = await countTopTrackListeners(input);
|
|
if (topTrackQ) {
|
|
questions.push({
|
|
key: topTrackQ.questionKey ?? `numeric:top-track-count:${topTrackQ.text}`,
|
|
subjectKey: topTrackQ.subjectKey,
|
|
question: topTrackQ,
|
|
});
|
|
}
|
|
|
|
const artistQ = await countFavouriteArtistListeners(input);
|
|
if (artistQ) {
|
|
questions.push({
|
|
key: artistQ.questionKey ?? `numeric:artist-count:${artistQ.text}`,
|
|
subjectKey: artistQ.subjectKey,
|
|
question: artistQ,
|
|
});
|
|
}
|
|
|
|
const question = pickQuestionCandidate(questions, input.history, input.index);
|
|
if (!question) return null;
|
|
return buildQuestionWindow(question);
|
|
}
|