Skip to content

Commit

Permalink
Show car classes and license levels, Scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinsina committed Dec 13, 2023
1 parent 5f2b0be commit b74e60c
Showing 1 changed file with 90 additions and 30 deletions.
120 changes: 90 additions & 30 deletions src/pages/user/[id]/scheduling/index.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
import { connectToDatabase } from "$lib/mongodb";
import DefaultLayout from "$lib/layouts/default.astro";
// @todo Properly assert types in this file
export async function getStaticPaths() {
const dbConnection = await connectToDatabase();
const db = dbConnection.db;
Expand Down Expand Up @@ -66,35 +67,64 @@ const trackPackages = await userCollection
const trackIds = trackPackages
.map((trackPackage) => trackPackage.content_ids)
.flat();
const carsCollection = db.collection("cars");
const cars = await carsCollection.find().toArray();
const carsMap = new Map();
cars.forEach((car) => {
const { car_id } = car;
carsMap.set(car_id, car);
});
const carClassCollection = db.collection("carclasses");
const carClassIdsForCarIds = await carClassCollection.distinct("car_class_id", {
"cars_in_class.car_id": { $in: carIds },
});
const carClasses = await carClassCollection.find().toArray();
const carClassesMap = new Map();
carClasses.forEach((carClass) => {
const { car_class_id, cars_in_class } = carClass;
const carsInClass = cars_in_class.map((carInClass) =>
carsMap.get(carInClass.car_id)
);
carClassesMap.set(car_class_id, { ...carClass, cars_in_class: carsInClass });
});
const seasonsCollection = db.collection("seasons");
const seasons = await seasonsCollection
.find({}, { })
.toArray();
const seasons = await seasonsCollection.find({}, {}).toArray();
const carClassIdSet = new Set(carClassIdsForCarIds);
const trackIdSet = new Set(trackIds);
const licenseGroupToNameMap: Record<string, string> = {
"1": "Rookie",
"2": "Class D",
"3": "Class C",
"4": "Class B",
"5": "Class A",
};
const results = seasons
.map((season) => {
const { _id, schedules, season_name, car_class_ids } = season;
const tracks: Array<Record<string,unknown>> = schedules.map((schedule: any, index: number) => ({
week: index + 1,
...schedule.track,
}));
const { schedules, season_name, car_class_ids } = season;
const carClassData = car_class_ids.map((car_class_id: number) =>
carClassesMap.get(car_class_id)
);
const tracks: Array<Record<string, unknown>> = schedules.map(
(schedule: any, index: number) => ({
week: index + 1,
...schedule.track,
})
);
const ownedTrackWeeks: Array<number> = tracks
.filter((track: Record<string,unknown>) => trackIdSet.has(track.track_id))
.map((track: Record<string,unknown>) => Number(track.week));
.filter((track: Record<string, unknown>) =>
trackIdSet.has(track.track_id)
)
.map((track: Record<string, unknown>) => Number(track.week));
const trackPercentageOwned = Math.round(
(ownedTrackWeeks.length / tracks.length) * 100
);
const ownedCarClasses = car_class_ids.filter((carClassId: number) =>
carClassIdSet.has(carClassId)
);
return {
"Season ID": _id,
"Season Name": String(season_name),
"License Level": licenseGroupToNameMap[String(season.license_group)],
carClassData,
tracks,
ownedTrackWeeks,
"Own Car": ownedCarClasses.length ? "Yes" : "No",
Expand All @@ -105,7 +135,11 @@ const results = seasons
(a, b) =>
b["Overall Track Percentage Owned"] - a["Overall Track Percentage Owned"]
);
const keysToOmitInTable = new Set(["tracks", "ownedTrackWeeks"]);
const keysToOmitInTable = new Set([
"carClassData",
"tracks",
"ownedTrackWeeks",
]);
---

<DefaultLayout>
Expand All @@ -117,28 +151,54 @@ const keysToOmitInTable = new Set(["tracks", "ownedTrackWeeks"]);
}
{
results.map((result) => (
<div class="col-span-4 grid grid-cols-4" x-data="dropdown" x-on:click="toggle">
<div
class="col-span-4 grid grid-cols-4"
x-data="dropdown"
x-on:click="toggle"
>
{Object.keys(result)
.filter((key) => !keysToOmitInTable.has(key))
.map((key: string) => (
<div class="border-2 col-span-1">{result[key]}</div>
))}
<div class="col-span-4 grid grid-cols-4" x-cloak x-show="opened" x-collapse>
{result.tracks.map((track) =>
result.ownedTrackWeeks.includes(Number(track.week)) ? (
<div class="col-span-1 border-x-2" />
<div class="bg-green-950 col-span-2 border-x-2">
{track.week}. {track.track_name} {track.config_name}
<div
class="col-span-4 grid grid-cols-4"
x-cloak
x-show="opened"
x-collapse
>
<div class="col-span-2 border-x-2">
{result.carClassData.map((carClass) => (
<div>
{carClass.cars_in_class.map((car) =>
carIds.includes(car.car_id) ? (
<div class="bg-green-950 grid grid-cols-2">
<div class="col-span-1">Car Class: {carClass.name}</div>
<div class="col-span-1">{car.car_name}</div>
</div>
) : (
<div class="bg-red-950 grid grid-cols-2">
<div class="col-span-1">Car Class: {carClass.name}</div>
<div class="col-span-1">{car.car_name}</div>
</div>
)
)}
</div>
<div class="col-span-1 border-x-2" />
) : (
<div class="col-span-1 border-x-2" />
<div class="bg-red-950 col-span-2 border-x-2">
{track.week}. {track.track_name} {track.config_name}
</div>
<div class="col-span-1 border-x-2" />
)
)}
))}
</div>
<div class="col-span-2 border-x-2">
{result.tracks.map((track) =>
result.ownedTrackWeeks.includes(Number(track.week)) ? (
<div class="bg-green-950">
{track.week}. {track.track_name} {track.config_name}
</div>
) : (
<div class="bg-red-950">
{track.week}. {track.track_name} {track.config_name}
</div>
)
)}
</div>
</div>
</div>
))
Expand All @@ -151,8 +211,8 @@ const keysToOmitInTable = new Set(["tracks", "ownedTrackWeeks"]);
Alpine.data("dropdown", () => ({
opened: false,
toggle() {
this.opened = !this.opened
}
this.opened = !this.opened;
},
}));
Alpine.start();
</script>
Expand Down

0 comments on commit b74e60c

Please sign in to comment.