Skip to content

Commit

Permalink
app routes room $roomName summary: add batting average
Browse files Browse the repository at this point in the history
  • Loading branch information
cmnord committed Aug 19, 2024
1 parent 5aa56b7 commit 250132a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
32 changes: 32 additions & 0 deletions app/routes/room.$roomName_.summary/coryat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
import { State } from "~/engine";

/** getBattingAverage returns the number of correct responses and number of
* possible correct responses for a player. */
export function getBattingAverage(userId: string, state: State) {
const player = state.players.get(userId);
if (!player) {
return [0, 0];
}

let numResponses = 0;
let numCorrectResponses = 0;

for (const roundIsAnswered of state.isAnswered) {
for (const rowIsAnswered of roundIsAnswered) {
for (const { isAnswered, answeredBy } of rowIsAnswered) {
if (!isAnswered) {
continue;
}
const correct = answeredBy.get(player.userId);
if (correct === undefined) {
continue;
}
numResponses += 1;
if (correct) {
numCorrectResponses += 1;
}
}
}
}

return [numCorrectResponses, numResponses];
}

/** getCoryat returns the player's score without any wagerable clues. If correct,
* add the "natural" value of a clue in this row back in.
*/
Expand Down
15 changes: 14 additions & 1 deletion app/routes/room.$roomName_.summary/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { BASE_URL, formatDollars } from "~/utils";

import { getSolve, markSolved } from "~/models/solves.server";
import ScoreChart from "./chart";
import { getCoryat } from "./coryat";
import { getBattingAverage, getCoryat } from "./coryat";
import GameSummary from "./summary";

export const meta: MetaFunction<typeof loader> = ({ data }) => {
Expand Down Expand Up @@ -65,9 +65,13 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
getCoryat(player.userId, state),
);
const combinedCoryat = coryats.reduce((acc, coryat) => acc + coryat, 0);
const battingAverages = sortedPlayers.map((player) =>
getBattingAverage(player.userId, state),
);

return json({
sortedPlayers,
battingAverages,
coryats,
combinedCoryat,
game,
Expand Down Expand Up @@ -119,6 +123,15 @@ export default function PlayGame() {
</span>
))}
</div>
<h3 className="text-lg">Batting averages</h3>
<div className="flex flex-col gap-2 sm:grid sm:grid-cols-3">
{data.sortedPlayers.map((p, i) => (
<span>
{p.name}: {data.battingAverages[i][0]} /{" "}
{data.battingAverages[i][1]}
</span>
))}
</div>
<ScoreChart
game={data.game}
players={data.sortedPlayers}
Expand Down

0 comments on commit 250132a

Please sign in to comment.