Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
feat: total stats
Browse files Browse the repository at this point in the history
  • Loading branch information
boyum authored and Øyvind Hauge committed Oct 5, 2023
1 parent 9cd4e63 commit cbc283a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
17 changes: 10 additions & 7 deletions Firebase/functions/src/api/winMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ export const winMatch = functions.https.onRequest(
sport,
}: WinMatchBody = request.body;

console.log({ body: request.body });
functions.logger.info({ body: request.body }, { structuredData: true });

const winnerIds = [...new Set([winnerId ?? wIds![0], ...(wIds ?? [])])];
const loserIds = [...new Set([loserId ?? lIds![0], ...(lIds ?? [])])];

const winners = await Promise.all(winnerIds.map(getPlayer));
const losers = await Promise.all(loserIds.map(getPlayer));

console.log({ winnerId, loserId: loserId, sport, winners, losers });
functions.logger.info(
{ winnerId, loserId: loserId, sport, winners, losers },
{ structuredData: true },
);

const someWinnersCouldNotBeFound = !winners.every(isDefined);
const someLosersCouldNotBeFound = !losers.every(isDefined);
Expand Down Expand Up @@ -121,15 +124,15 @@ export const winMatch = functions.https.onRequest(
loserStats.reduce((sum, { score }) => (sum += score ?? initialScore), 0) /
losers.length;

// const oldWinnerScore = winnerStats.score ?? initialScore;
// const oldLoserScore = loserStats.score ?? initialScore;

const { playerRating: newWinnerScore, opponentRating: newLoserScore } =
EloRating.calculate(averageWinnerScore, averageLoserScore);

const delta = newWinnerScore - averageWinnerScore;

console.log({ newWinnerScore, newLoserScore });
functions.logger.info(
{ newWinnerScore, newLoserScore },
{ structuredData: true },
);

const now = new firebase.firestore.Timestamp(
Math.floor(Date.now() / 1000),
Expand Down Expand Up @@ -191,7 +194,7 @@ export const winMatch = functions.https.onRequest(
});
}

console.log({ match });
functions.logger.info({ match }, { structuredData: true });

response.send(match);
},
Expand Down
2 changes: 2 additions & 0 deletions Firebase/functions/src/firebase/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const playerConverter: admin.firestore.FirestoreDataConverter<Player> = {
team: snapshot.get("team"),
teamId: snapshot.get("teamId") ?? snapshot.get("team").id,
stats: snapshot.get("stats") ?? [snapshot.get("foosballStats")],
totalStats: snapshot.get("totalStats") ?? [],
lastActive: snapshot.get("lastActive"),
winStreak: snapshot.get("winStreak") ?? 0,
};
Expand Down Expand Up @@ -76,6 +77,7 @@ export const updatePlayer = async (player: Player): Promise<void> => {
player.tableTennisStats ?? getEmptyStats(Sport.TableTennis),
player.poolStats ?? getEmptyStats(Sport.Pool),
],
totalStats: player.totalStats ?? [],
team: player.team,
teamId: player.teamId ?? player.team.id ?? undefined,
lastActive: player.lastActive,
Expand Down
1 change: 1 addition & 0 deletions Firebase/functions/src/helpers/player.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const getEmptyPlayer = (): Omit<Player, "userId"> => {
getEmptyStats(Sport.TableTennis),
getEmptyStats(Sport.Pool),
],
totalStats: [],
winStreak: 0,
lastActive: null,
teamId: null,
Expand Down
1 change: 1 addition & 0 deletions Firebase/functions/src/types/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type Player = {
/** @deprecated Use stats instead */
poolStats?: Stats;
stats: Array<Stats>;
totalStats: Array<Stats>; // TODO: Make optional
/** @deprecated Use teamId instead */
team: Team;
teamId?: string | null;
Expand Down

0 comments on commit cbc283a

Please sign in to comment.