CalendarEventResults seed

This commit is contained in:
Kalle 2022-08-07 16:07:43 +03:00
parent 8fb4f515ad
commit 28fd38f49d
4 changed files with 50 additions and 3 deletions

View File

@ -222,7 +222,7 @@ const findWinnersByEventIdStm = sql.prepare(`
order by "placement" asc
`);
export function findWinnersByEventId(eventId: CalendarEvent["id"]) {
export function findResultsByEventId(eventId: CalendarEvent["id"]) {
const rows = findWinnersByEventIdStm.all({ eventId }) as Array<{
id: CalendarEventResultTeam["id"];
teamName: CalendarEventResultTeam["name"];

View File

@ -35,6 +35,7 @@ const basicSeeds = [
patrons,
calendarEvents,
calendarEventBadges,
calendarEventResults,
];
export function seed() {
@ -320,7 +321,7 @@ function userIdsInRandomOrder() {
return sql
.prepare(`select "id" from "User" order by random()`)
.all()
.map((u) => u.id);
.map((u) => u.id) as number[];
}
function calendarEvents() {
@ -439,3 +440,48 @@ function calendarEventBadges() {
}
}
}
function calendarEventResults() {
let userIds = userIdsInRandomOrder();
const eventIdsOfPast = new Set<number>(
sql
.prepare(
`select "CalendarEvent"."id"
from "CalendarEvent"
join "CalendarEventDate" on "CalendarEventDate"."eventId" = "CalendarEvent"."id"
where "CalendarEventDate"."startTime" < $startTime`
)
.all({ startTime: dateToDatabaseTimestamp(new Date()) })
.map((r) => r.id)
);
for (const eventId of eventIdsOfPast) {
if (Math.random() < 0.3) continue;
db.calendarEvents.upsertReportedScores({
eventId,
participantCount: faker.datatype.number({ min: 10, max: 250 }),
results: new Array(faker.helpers.arrayElement([1, 1, 2, 3, 3, 3, 8, 8]))
.fill(null)
// eslint-disable-next-line no-loop-func
.map((_, i) => ({
placement: i + 1,
teamName: capitalize(faker.word.noun()),
players: new Array(
faker.helpers.arrayElement([1, 2, 3, 4, 4, 4, 4, 4, 5, 6])
)
.fill(null)
.map(() => {
const withStringName = Math.random() < 0.2;
return {
name: withStringName ? faker.name.firstName() : null,
userId: withStringName ? null : userIds.pop()!,
};
}),
})),
});
userIds = userIdsInRandomOrder();
}
}

View File

@ -63,6 +63,7 @@ export const loader = async ({ params, request }: LoaderArgs) => {
event,
badgePrizes: db.calendarEvents.findBadgesById(parsedParams.id),
title: makeTitle([event.name, t("pages.calendar")]),
results: db.calendarEvents.findResultsByEventId(parsedParams.id),
});
};

View File

@ -138,7 +138,7 @@ export const loader = async ({ request, params }: LoaderArgs) => {
return json({
name: event.name,
participantCount: event.participantCount,
winners: db.calendarEvents.findWinnersByEventId(parsedParams.id),
winners: db.calendarEvents.findResultsByEventId(parsedParams.id),
});
};