From 8c2835117e7eb75a514224feb5682108e8c6d3de Mon Sep 17 00:00:00 2001 From: Baptiste Studer Date: Wed, 16 Aug 2023 23:33:27 +0200 Subject: [PATCH] refactor(useTeamValues): Extract function to build team values --- .../context/hooks/shared/useTeamValues.ts | 69 ++------------- .../hooks/shared/useTeamValuesForTeam.ts | 63 +------------- .../play/context/hooks/shared/utils.ts | 83 +++++++++++++++++-- 3 files changed, 87 insertions(+), 128 deletions(-) diff --git a/packages/client/src/modules/play/context/hooks/shared/useTeamValues.ts b/packages/client/src/modules/play/context/hooks/shared/useTeamValues.ts index acf9f38a..41da3b6a 100644 --- a/packages/client/src/modules/play/context/hooks/shared/useTeamValues.ts +++ b/packages/client/src/modules/play/context/hooks/shared/useTeamValues.ts @@ -1,7 +1,6 @@ import { useMemo } from "react"; import { TeamValues, usePersonaByUserId, usePlay } from "../../playContext"; -import { mean } from "../../../../../lib/math"; -import { buildStepToData } from "./utils"; +import { buildTeamValues } from "./utils"; export { useTeamValues }; @@ -19,66 +18,12 @@ function useTeamValues(): { const teamValues = useMemo(() => { return teams.map((team) => { - const playersInTeam = players.filter((p) => p.teamId === team.id); - const playerRepresentingTeam = playersInTeam[0] || null; - const personaRepresentingTeam = - personaByUserId[playerRepresentingTeam?.userId || -1] || null; - const currentPersonaRepresentingTeam = - personaRepresentingTeam?.getPersonaAtStep?.(game.step) || null; - - return { - id: team.id, - playerCount: playersInTeam.length, - points: mean( - playersInTeam.map( - ({ userId }) => personaByUserId[userId].currentPersona.points - ) - ), - budget: mean( - playersInTeam.map( - ({ userId }) => personaByUserId[userId].currentPersona.budget - ) - ), - budgetSpent: mean( - playersInTeam - .map(({ userId }) => personaByUserId[userId]) - .map( - (persona) => - persona.getPersonaAtStep(0).budget - - persona.currentPersona.budget - ) - ), - carbonFootprint: mean( - playersInTeam.map( - ({ userId }) => - personaByUserId[userId].currentPersona.carbonFootprint - ) - ), - carbonFootprintReduction: mean( - playersInTeam - .map(({ userId }) => personaByUserId[userId]) - .map( - (persona) => - (1 - - persona.currentPersona.carbonFootprint / - persona.getPersonaAtStep(0).carbonFootprint) * - 100 - ) - ), - stepToConsumption: buildStepToData( - "consumption", - game, - playersInTeam, - personaByUserId - ), - stepToProduction: buildStepToData( - "production", - game, - playersInTeam, - personaByUserId - ), - productionCurrent: currentPersonaRepresentingTeam?.production || [], - }; + return buildTeamValues({ + game, + personaByUserId, + players, + team, + }); }); // TODO: check `personaByUserId` in deps doesn't trigger infinite renders. }, [game, personaByUserId, players, teams]); diff --git a/packages/client/src/modules/play/context/hooks/shared/useTeamValuesForTeam.ts b/packages/client/src/modules/play/context/hooks/shared/useTeamValuesForTeam.ts index a4d6e88f..20d752ff 100644 --- a/packages/client/src/modules/play/context/hooks/shared/useTeamValuesForTeam.ts +++ b/packages/client/src/modules/play/context/hooks/shared/useTeamValuesForTeam.ts @@ -1,7 +1,6 @@ import { useMemo } from "react"; import { TeamValues, usePersonaByUserId, usePlay } from "../../playContext"; -import { mean } from "../../../../../lib/math"; -import { buildStepToData } from "./utils"; +import { buildTeamValues } from "./utils"; export { useTeamValuesForTeam }; @@ -18,65 +17,7 @@ function useTeamValuesForTeam({ teamId }: { teamId?: number }): { const teamValues = useMemo(() => { const team = teams.find((t) => t.id === teamId); - - const playersInTeam = players.filter((p) => p.teamId === team?.id); - const playerRepresentingTeam = playersInTeam[0] || null; - const personaRepresentingTeam = - personaByUserId[playerRepresentingTeam?.userId || -1] || null; - const currentPersonaRepresentingTeam = - personaRepresentingTeam?.getPersonaAtStep?.(game.step) || null; - - return { - id: team?.id || 0, - playerCount: playersInTeam.length, - points: mean( - playersInTeam.map( - ({ userId }) => personaByUserId[userId].currentPersona.points - ) - ), - budget: mean( - playersInTeam.map( - ({ userId }) => personaByUserId[userId].currentPersona.budget - ) - ), - budgetSpent: mean( - playersInTeam - .map(({ userId }) => personaByUserId[userId]) - .map( - (persona) => - persona.getPersonaAtStep(0).budget - persona.currentPersona.budget - ) - ), - carbonFootprint: mean( - playersInTeam.map( - ({ userId }) => personaByUserId[userId].currentPersona.carbonFootprint - ) - ), - carbonFootprintReduction: mean( - playersInTeam - .map(({ userId }) => personaByUserId[userId]) - .map( - (persona) => - (1 - - persona.currentPersona.carbonFootprint / - persona.getPersonaAtStep(0).carbonFootprint) * - 100 - ) - ), - stepToConsumption: buildStepToData( - "consumption", - game, - playersInTeam, - personaByUserId - ), - stepToProduction: buildStepToData( - "production", - game, - playersInTeam, - personaByUserId - ), - productionCurrent: currentPersonaRepresentingTeam?.production || [], - }; + return buildTeamValues({ game, personaByUserId, players, team }); }, [game, personaByUserId, players, teamId, teams]); return { diff --git a/packages/client/src/modules/play/context/hooks/shared/utils.ts b/packages/client/src/modules/play/context/hooks/shared/utils.ts index 5b1512f4..13a6570f 100644 --- a/packages/client/src/modules/play/context/hooks/shared/utils.ts +++ b/packages/client/src/modules/play/context/hooks/shared/utils.ts @@ -1,17 +1,19 @@ import range from "lodash/range"; -import { IGame, Player } from "../../../../../utils/types"; +import { IGame, ITeam, Player } from "../../../../../utils/types"; import { GameStepType, isStepOfType } from "../../../constants"; -import { usePersonaByUserId } from "../../playContext"; +import { TeamValues, usePersonaByUserId } from "../../playContext"; import { mean } from "../../../../../lib/math"; import { sumAllValues } from "../../../../persona"; -export { buildStepToData }; +export { buildStepToData, buildTeamValues }; + +type PersonaByUserId = ReturnType; function buildStepToData( dataType: GameStepType, game: IGame, players: Player[], - personaByUserId: ReturnType + personaByUserId: PersonaByUserId ) { return Object.fromEntries( range(0, game.lastFinishedStep + 1) @@ -27,7 +29,7 @@ function buildStepData( dataType: GameStepType, step: number, players: Player[], - personaByUserId: ReturnType + personaByUserId: PersonaByUserId ) { return mean( players @@ -37,3 +39,74 @@ function buildStepData( ) ); } + +function buildTeamValues({ + game, + personaByUserId, + players, + team, +}: { + game: IGame; + personaByUserId: PersonaByUserId; + players: Player[]; + team?: ITeam; +}): TeamValues { + const playersInTeam = players.filter((p) => p.teamId === team?.id); + const playerRepresentingTeam = playersInTeam[0] || null; + const personaRepresentingTeam = + personaByUserId[playerRepresentingTeam?.userId || -1] || null; + const currentPersonaRepresentingTeam = + personaRepresentingTeam?.getPersonaAtStep?.(game.step) || null; + + return { + id: team?.id || 0, + playerCount: playersInTeam.length, + points: mean( + playersInTeam.map( + ({ userId }) => personaByUserId[userId].currentPersona.points + ) + ), + budget: mean( + playersInTeam.map( + ({ userId }) => personaByUserId[userId].currentPersona.budget + ) + ), + budgetSpent: mean( + playersInTeam + .map(({ userId }) => personaByUserId[userId]) + .map( + (persona) => + persona.getPersonaAtStep(0).budget - persona.currentPersona.budget + ) + ), + carbonFootprint: mean( + playersInTeam.map( + ({ userId }) => personaByUserId[userId].currentPersona.carbonFootprint + ) + ), + carbonFootprintReduction: mean( + playersInTeam + .map(({ userId }) => personaByUserId[userId]) + .map( + (persona) => + (1 - + persona.currentPersona.carbonFootprint / + persona.getPersonaAtStep(0).carbonFootprint) * + 100 + ) + ), + stepToConsumption: buildStepToData( + "consumption", + game, + playersInTeam, + personaByUserId + ), + stepToProduction: buildStepToData( + "production", + game, + playersInTeam, + personaByUserId + ), + productionCurrent: currentPersonaRepresentingTeam?.production || [], + }; +}