Skip to content

Commit

Permalink
Merge pull request #34 from Shinsina/jankity-jank-corrections
Browse files Browse the repository at this point in the history
Head to Head Matchup results and correct bug preventing multiple classes per season being viewable
  • Loading branch information
Shinsina authored Feb 28, 2024
2 parents b7f11f0 + f9d4f9e commit b39bdcb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 32 deletions.
26 changes: 21 additions & 5 deletions src/lib/layouts/standings.astro
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,23 @@ const finalData: Array<Record<string, any>> = standingsResults.map(
};
}
);
const generateLinkForKey = (key: string, resultAtKey: unknown) => {
const generateLinkForKey = ({
key,
resultAtKey,
carClassId,
}: {
key: string;
resultAtKey: unknown;
carClassId: string;
}) => {
if (key === "season_id") {
return `/Stat-N-Track/user/${id}/season/${resultAtKey}`;
return `/Stat-N-Track/user/${id}/season/${resultAtKey}/car-class/${carClassId}`;
} else if (key === "year") {
return `/Stat-N-Track/user/${id}/standings/by-year/${resultAtKey}`;
} else if (key === "car_class_id") {
return `/Stat-N-Track/user/${id}/standings/by-car-class/${resultAtKey}`;
} else if (key === "Season Summary") {
return `/Stat-N-Track/user/${id}/season/${resultAtKey}/season-summary`;
return `/Stat-N-Track/user/${id}/season/${resultAtKey}/car-class/${carClassId}/season-summary`;
}
return null;
};
Expand Down Expand Up @@ -97,11 +105,19 @@ const generateLinkForKey = (key: string, resultAtKey: unknown) => {
id={`${key}~${rowNumber}~${result[key]}`}
class="border-2"
>
{generateLinkForKey(key, result[key]) ? (
{generateLinkForKey({
key,
resultAtKey: result[key],
carClassId: result.car_class_id,
}) ? (
<a
class="underline"
rel="prefetch"
href={generateLinkForKey(key, result[key])}
href={generateLinkForKey({
key,
resultAtKey: result[key],
carClassId: result.car_class_id,
})}
>
{result[key]}
</a>
Expand Down
58 changes: 44 additions & 14 deletions src/pages/shared-subsessions.astro
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
---
import { connectToDatabase } from "$lib/mongodb";
import type { Subsession } from "$lib/types";
import type { Session, SessionResult, Subsession } from "$lib/types";
import DefaultLayout from "$lib/layouts/default.astro";
import RaceResults from "$lib/components/session-results/race.astro";
import userIds from "$lib/utils/user-ids";
import parseLapTime from "$lib/utils/parse-lap-time";
import fieldIdToLabelMap from "$lib/utils/field-id-to-label-map";
import keysToDisplay from "$lib/utils/race-session-keys-to-display";
// @todo Fix all this nonsense you jankily slapped together at 2AM on a Saturday
const dbConnection = await connectToDatabase();
const { db } = dbConnection;
const collection = db.collection<Subsession>("subsessions");
const $and = userIds.map((userId) => ({
"session_results.2.results": { $elemMatch: { cust_id: userId } },
}));
const subsessions = await collection.find({ $and }).sort({ _id: -1 }).toArray();
const results = subsessions.map((subsession: Subsession) => {
const headToHeadWins: Record<string,Array<number>> = {};
const results: Array<{
subsessionInfo: Record<string, unknown>;
session: Session;
}> = subsessions.map((subsession: Subsession) => {
const {
subsession_id,
track,
Expand All @@ -32,31 +35,38 @@ const results = subsessions.map((subsession: Subsession) => {
} = subsession;
// Current Users in race session
const results = subsession.session_results[2].results
.filter((v: any) => userIds.includes(v.cust_id))
.map((w) => {
// @todo figure out how to fix the typing here
// @ts-ignore
.filter((v: SessionResult) => userIds.includes(v.cust_id))
.map((w: SessionResult) => {
return Object.keys(w).reduce((object, key) => {
if (keysToDisplay.has(key)) {
// @ts-ignore
object[key] = w[key];
}
return object;
}, {});
// @ts-ignore
}, {} as SessionResult);
})
.sort((x, y) => {
// @ts-ignore
.sort((x: SessionResult, y: SessionResult) => {
if (x.finish_position > y.finish_position) {
return 1;
// @ts-ignore
} else if (y.finish_position > x.finish_position) {
return -1;
}
return 0;
});
const [winner] = results;
const { display_name } = winner;
if(headToHeadWins[display_name]) {
headToHeadWins[display_name].push(subsession_id)
} else {
headToHeadWins[display_name] = [subsession_id];
}
const { group_name: License } = allowed_licenses.slice(0, 2).pop() || {};
const session = {
simsession_number: 0,
simsession_type: 0,
simsession_type_name: "",
simsession_subtype: 0,
simsession_name: "",
results,
};
return {
Expand All @@ -79,7 +89,6 @@ const results = subsessions.map((subsession: Subsession) => {
session,
};
});
// @todo Don't cheat the types here
export const handleKey = ({
key,
subsessionInfo,
Expand All @@ -98,14 +107,35 @@ export const handleKey = ({
}
return String(subsessionInfo[key]);
};
const correctlyKeyedH2HWins = Object.keys((headToHeadWins)).reduce((object, key) => {
const wins = headToHeadWins[key].length;
object[key] = { key, wins };
return object;
}, {} as Record<string,{ key: string, wins: number }>);
const h2hOutput = Object.keys(correctlyKeyedH2HWins).map((key) => correctlyKeyedH2HWins[key]).sort((a, b) => {
if (a.wins > b.wins) {
return -1;
} else if (b.wins > a.wins) {
return 1;
}
return 0;
});
---

<DefaultLayout>
<div class="text-center lg:text-2xl md:text-md sm:text-sm py-4">
<div class="font-bold">Head to Head Wins</div>
{h2hOutput.map((value) => (
<div>{value.key}: {value.wins}</div>
))}
</div>
<div
id="top"
x-data={JSON.stringify(
results.reduce((object, result) => {
//@ts-expect-error
object.previouslyToggled = "";
//@ts-expect-error
object[`toggle${result.subsessionInfo.subsession_id}`] = false;
return object;
}, {})
Expand All @@ -117,7 +147,7 @@ export const handleKey = ({
{
results.map((result) => (
<div
id={result.subsessionInfo.subsession_id}
id={String(result.subsessionInfo.subsession_id)}
x-cloak
x-show={`toggle${result.subsessionInfo.subsession_id}`}
x-transition:enter.duration.500ms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ export async function getStaticPaths() {
params: {
id: Number(season.season_driver_data.cust_id),
seasonId: Number(season.season_id),
carClassId: Number(season.car_class_id),
},
}));
}
const { id, seasonId } = Astro.params;
const { id, seasonId, carClassId } = Astro.params;
const dbConnection = await connectToDatabase();
const db = dbConnection.db;
const collection = db.collection<Subsession>("subsessions");
const subsessions: Array<Subsession> = await collection
.find({
season_id: Number(seasonId),
"session_results.2.results": { $elemMatch: { cust_id: Number(id) } },
"session_results.2.results": {
$elemMatch: { cust_id: Number(id), car_class_id: Number(carClassId) },
},
})
.sort({ _id: 1 })
.toArray();
Expand Down Expand Up @@ -160,7 +163,7 @@ const { keysArray, handledResults } = handleResults({ keysToDisplay, results });
</div>
<script>
import Alpine from "alpinejs";
import Table from "../../../../../lib/components/table/index.ts";
import Table from "$lib/components/table/index.ts";
Alpine.data("table", Table);
Alpine.start();
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
Track,
} from "$lib/types";
import userIds from "$lib/utils/user-ids";
// @todo Properly assert types in this file
export async function getStaticPaths() {
const dbConnection = await connectToDatabase();
const db = dbConnection.db;
Expand All @@ -24,24 +23,26 @@ export async function getStaticPaths() {
params: {
id: Number(season.season_driver_data.cust_id),
seasonId: Number(season.season_id),
carClassId: Number(season.car_class_id),
},
}));
}
const { id, seasonId } = Astro.params;
const { id, seasonId, carClassId } = Astro.params;
const dbConnection = await connectToDatabase();
const db = dbConnection.db;
const subsessionCollection = db.collection<Subsession>("subsessions");
const subsessions: Array<Subsession> = await subsessionCollection
.find({
season_id: Number(seasonId),
"session_results.2.results": { $elemMatch: { cust_id: Number(id) } },
"session_results.2.results": {
$elemMatch: { cust_id: Number(id), car_class_id: Number(carClassId) },
},
})
.sort({ _id: 1 })
.toArray();
const standingsCollection = db.collection("standings");
const season = await standingsCollection.findOne({
"season_driver_data.cust_id": Number(id),
season_id: String(seasonId),
_id: `${seasonId}_${carClassId}_${id}`,
});
const pastSeasonsCollection = db.collection("pastseasons");
const pastSeasonInformation = await pastSeasonsCollection.findOne({
Expand Down Expand Up @@ -85,8 +86,10 @@ trackScheduleMap.forEach((value) => {
resultsPerWeek.push(bestUserResult);
}
} else if (userResults.length === 0) {
const bestUserResult = { subsession: { track: value }, userResult: {} };
// @ts-expect-error
const bestUserResult = {
subsession: { track: value } as Subsession,
userResult: {} as SessionResult,
};
resultsPerWeek.push(bestUserResult);
} else {
const [bestUserResult] = userResults;
Expand Down Expand Up @@ -123,11 +126,10 @@ const results = resultsPerWeek.map((result) => {
car_name,
aggregate_champ_points,
} = userResult;
// @ts-expect-error
const { group_name: License } =
allowed_licenses && allowed_licenses.length
? allowed_licenses.slice(0, 2).pop()
: {};
? (allowed_licenses.slice(0, 2).pop() as License)
: ({} as License);
const sof = {
"Strength Of Field": subsession.race_summary
? subsession.race_summary.field_strength
Expand Down

0 comments on commit b39bdcb

Please sign in to comment.