From cbc283a93e7377938a215b651aa9de8b8e4893ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sindre=20B=C3=B8yum?= Date: Sat, 3 Dec 2022 14:33:21 +0100 Subject: [PATCH] feat: total stats --- Firebase/functions/src/api/winMatch.ts | 17 ++++++++++------- Firebase/functions/src/firebase/player.ts | 2 ++ .../functions/src/helpers/player.helpers.ts | 1 + Firebase/functions/src/types/Player.ts | 1 + 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Firebase/functions/src/api/winMatch.ts b/Firebase/functions/src/api/winMatch.ts index 0611e02..7709199 100644 --- a/Firebase/functions/src/api/winMatch.ts +++ b/Firebase/functions/src/api/winMatch.ts @@ -47,7 +47,7 @@ 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 ?? [])])]; @@ -55,7 +55,10 @@ export const winMatch = functions.https.onRequest( 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); @@ -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), @@ -191,7 +194,7 @@ export const winMatch = functions.https.onRequest( }); } - console.log({ match }); + functions.logger.info({ match }, { structuredData: true }); response.send(match); }, diff --git a/Firebase/functions/src/firebase/player.ts b/Firebase/functions/src/firebase/player.ts index dff32bf..434dd9a 100644 --- a/Firebase/functions/src/firebase/player.ts +++ b/Firebase/functions/src/firebase/player.ts @@ -20,6 +20,7 @@ const playerConverter: admin.firestore.FirestoreDataConverter = { 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, }; @@ -76,6 +77,7 @@ export const updatePlayer = async (player: Player): Promise => { 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, diff --git a/Firebase/functions/src/helpers/player.helpers.ts b/Firebase/functions/src/helpers/player.helpers.ts index df26d8f..bb32f35 100644 --- a/Firebase/functions/src/helpers/player.helpers.ts +++ b/Firebase/functions/src/helpers/player.helpers.ts @@ -33,6 +33,7 @@ export const getEmptyPlayer = (): Omit => { getEmptyStats(Sport.TableTennis), getEmptyStats(Sport.Pool), ], + totalStats: [], winStreak: 0, lastActive: null, teamId: null, diff --git a/Firebase/functions/src/types/Player.ts b/Firebase/functions/src/types/Player.ts index a34e60f..91d212c 100644 --- a/Firebase/functions/src/types/Player.ts +++ b/Firebase/functions/src/types/Player.ts @@ -13,6 +13,7 @@ export type Player = { /** @deprecated Use stats instead */ poolStats?: Stats; stats: Array; + totalStats: Array; // TODO: Make optional /** @deprecated Use teamId instead */ team: Team; teamId?: string | null;