1- import React , { useEffect , useState } from " react" ;
2- import { useRecoilValue } from " recoil" ;
3- import { gameState } from " ../../recoil/atoms/gameState" ;
4- import { Line } from " react-chartjs-2" ;
1+ import React , { useEffect , useState } from ' react' ;
2+ import { useRecoilValue } from ' recoil' ;
3+ import { gameState } from ' ../../recoil/atoms/gameState' ;
4+ import { Line } from ' react-chartjs-2' ;
55import {
66 Chart as ChartJS ,
77 CategoryScale ,
@@ -11,8 +11,8 @@ import {
1111 Title ,
1212 Tooltip ,
1313 Legend ,
14- } from " chart.js" ;
15- import { RoomDataProps } from " ../../types/RoomData.type.ts" ;
14+ } from ' chart.js' ;
15+ import { RoomDataProps } from ' ../../types/RoomData.type.ts' ;
1616
1717// Chart.js 모듈 등록
1818ChartJS . register ( CategoryScale , LinearScale , PointElement , LineElement , Title , Tooltip , Legend ) ;
@@ -23,33 +23,36 @@ const TeamChart: React.FC = () => {
2323 const [ teamScores , setTeamScores ] = useState < { [ key : string ] : number [ ] } > ( { } ) ; // 팀 점수 저장
2424
2525 useEffect ( ( ) => {
26- if ( game ?. currentTime !== undefined && game ?. teams ) {
26+ if ( game ?. currentTime !== undefined && game ?. currentTime !== 0 && game ?. teams ) {
2727 // X축 레이블에 현재 시간을 추가
2828 setTimeLabels ( ( prev ) => [ ...prev , `${ game . currentTime } s` ] ) ;
2929
30- // 게임 타입별로 다르게 처리
30+ // 게임 타입별로 점수 계산 및 업데이트
3131 if ( game ?. gameType === 'FREE_FOR_ALL' ) {
32- const updatedScores = { ...teamScores } ;
33- // team.users에서 각 사용자의 클릭 수 합산
34- game . teams [ 0 ] . users . forEach ( ( user ) => {
35- if ( ! updatedScores [ user . nickname ] ) {
36- updatedScores [ user . nickname ] = [ ] ;
37- }
38- updatedScores [ user . nickname ] . push ( user . clickCount ) ;
32+ setTeamScores ( ( prevScores ) => {
33+ const updatedScores = { ...prevScores } ;
34+ game . teams [ 0 ] . users . forEach ( ( user ) => {
35+ if ( ! updatedScores [ user . nickname ] ) {
36+ updatedScores [ user . nickname ] = [ ] ; // 새로운 유저 초기화
37+ }
38+ // 이전 값 유지하며 새로운 값 추가
39+ updatedScores [ user . nickname ] = [ ...updatedScores [ user . nickname ] , user . clickCount ] ;
40+ } ) ;
41+ return updatedScores ;
3942 } ) ;
40- setTeamScores ( updatedScores ) ;
41- } else {
42- // 팀별 클릭 수 합산
43- const updatedScores = { ...teamScores } ;
44- game . teams . forEach ( ( team ) => {
45- const totalClicks = team . teamScore
46- if ( ! updatedScores [ team . teamName ] ) {
47- updatedScores [ team . teamName ] = [ ] ;
48- }
49- updatedScores [ team . teamName ] . push ( totalClicks ) ;
43+ } else {
44+ setTeamScores ( ( prevScores ) => {
45+ const updatedScores = { ...prevScores } ;
46+ game . teams . forEach ( ( team ) => {
47+ const totalClicks = team . teamScore ;
48+ if ( ! updatedScores [ team . teamName ] ) {
49+ updatedScores [ team . teamName ] = [ ] ; // 새로운 팀 초기화
50+ }
51+ // 이전 값 유지하며 새로운 값 추가
52+ updatedScores [ team . teamName ] = [ ...updatedScores [ team . teamName ] , totalClicks ] ;
53+ } ) ;
54+ return updatedScores ;
5055 } ) ;
51-
52- setTeamScores ( updatedScores ) ;
5356 }
5457 }
5558 } , [ game ?. currentTime ] ) ; // currentTime
@@ -72,25 +75,25 @@ const TeamChart: React.FC = () => {
7275 maintainAspectRatio : false ,
7376 plugins : {
7477 legend : {
75- position : " top" as const ,
78+ position : ' top' as const ,
7679 } ,
7780 title : {
7881 display : true ,
79- text : " Click Score Chart" ,
82+ text : ' Click Score Chart' ,
8083 } ,
8184 } ,
8285 scales : {
8386 x : {
8487 title : {
8588 display : true ,
86- text : " Time (s)" ,
89+ text : ' Time (s)' ,
8790 } ,
8891 } ,
8992 y : {
9093 beginAtZero : true ,
9194 title : {
9295 display : true ,
93- text : " Team Click Score" ,
96+ text : ' Team Click Score' ,
9497 } ,
9598 } ,
9699 } ,
0 commit comments