diff --git a/components/GameField/GameField.tsx b/components/GameField/GameField.tsx index 200ead1..36f0f76 100644 --- a/components/GameField/GameField.tsx +++ b/components/GameField/GameField.tsx @@ -5,11 +5,11 @@ import { badgesQuery } from 'queries'; import styles from './GameField.module.scss'; import { Button } from 'components/shared'; import { useAccount } from 'wagmi'; -import { useEffect, useState } from 'react'; +import {useContext, useEffect, useState} from 'react'; import Link from 'next/link'; import cn from 'classnames'; import { FinishedGameState } from 'gameApi'; -import {IGameStateContext, useGameStateContext} from "../../contexts/GameStateContext"; +import {GameStateContext, IGameStateContext, useGameStateContext} from "../../contexts/GameStateContext"; export const GameField: React.FC = ({ children, rivalPlayerAddress, @@ -122,6 +122,8 @@ export const GameField: React.FC = ({ useEffect(() => { if (!!finishGameCheckResult) { + console.log('use effect', finishGameCheckResult) + setShowShade(true); setIsWaiting(false); setIsConnecting(false); @@ -212,10 +214,10 @@ export const GameField: React.FC = ({ isDraw: finishGameCheckResult.isDraw, cheatWin: finishGameCheckResult.cheatWin }; - const gameStateContext: IGameStateContext = useGameStateContext(); - gameStateContext.setFinishResult(result); - console.log('Сomponent GAMEFIELD обновление gameStateContext', gameStateContext) - console.log('Обновление контекста в GameField:', result); + const { setFinishResult } = useContext(GameStateContext); + setFinishResult(result); + console.log('Обновление gameStateContext в GameField', GameStateContext) + console.log('Обновление result в GameField:', result); } return ( diff --git a/components/GameResult/GameResult.tsx b/components/GameResult/GameResult.tsx index 4190e82..54a0368 100644 --- a/components/GameResult/GameResult.tsx +++ b/components/GameResult/GameResult.tsx @@ -12,15 +12,20 @@ import { BlockPayedGame, CustomButton, PurpleIcon, WhiteIcon } from 'components/ import { ScoreCard } from 'components/GameResult/ScoreCard/index'; import { TeamMemberBasic } from 'components/shared/ui/TeamMemberBasic'; import { OIcon, XIcon } from 'components/shared/ui/XOIcons'; -import { useEffect, useState } from 'react'; +import {useContext, useEffect, useState} from 'react'; import { FinishedGameState } from 'gameApi'; -import {IGameStateContext, useGameStateContext} from "../../contexts/GameStateContext"; +import {GameStateContext, IGameStateContext,} from "../../contexts/GameStateContext"; export const GameResult= () => { - const gameStateContext = useGameStateContext(); - console.log('Контекст в компоненте GameResult:', gameStateContext); + const [countContextFinishResult, setCountContextFinishResult] = useState(0); + const { finishResult } = useContext(GameStateContext); - const { finishResult } = gameStateContext; + useEffect(() => { + setCountContextFinishResult(prevCount => prevCount + 1); + }, [finishResult]); + + console.log('Контекст в компоненте GameResult:', GameStateContext); console.log('Данные из контекста в GameResult:', finishResult); + console.log('Счетчик обновлений данных:', countContextFinishResult); // const player1 = { // playerName: '0xh20...7260', diff --git a/contexts/GameStateContext.tsx b/contexts/GameStateContext.tsx index a60fd8c..17becad 100644 --- a/contexts/GameStateContext.tsx +++ b/contexts/GameStateContext.tsx @@ -13,7 +13,7 @@ export const GameStateContextDefault: IGameStateContext = { } export const GameStateContext = createContext(GameStateContextDefault); -export const useGameStateContext = () => useContext(GameStateContext); + export const GameStateContextProvider: React.FC<{ children: React.ReactNode }> = ({children}) => { const [finishResult, setFinishResult] = useState(null); @@ -21,7 +21,7 @@ export const GameStateContextProvider: React.FC<{ children: React.ReactNode }> = return {children}; } - +// export const useGameStateContext = () => useContext(GameStateContext); // export interface IGameStateContext { // finishGameCheckResult: null | { winner: boolean; isDraw: boolean; cheatWin: boolean }; diff --git a/pages/game-result/index.tsx b/pages/game-result/index.tsx index f404b42..a07d7a6 100644 --- a/pages/game-result/index.tsx +++ b/pages/game-result/index.tsx @@ -5,9 +5,9 @@ import {FinishedGameState} from "../../gameApi"; import {GameStateContextProvider, useGameStateContext} from "../../contexts/GameStateContext"; const GameResultPage = () => { - const gameStateContext = useGameStateContext(); - const { finishResult } = gameStateContext; - console.log('Данные из контекста в GameResultPage:', finishResult); + // const gameStateContext = useGameStateContext(); + // const { finishResult } = gameStateContext; + // console.log('Данные из контекста в GameResultPage:', finishResult); return (
diff --git a/pages/games/[gameType].tsx b/pages/games/[gameType].tsx index bc1c65e..603b655 100644 --- a/pages/games/[gameType].tsx +++ b/pages/games/[gameType].tsx @@ -433,10 +433,7 @@ const Game: NextPage = ({gameType, version}) => { if (winnerId !== null) { setFinishGameCheckResult({winner: playerIngameId === winnerId, isDraw: false, cheatWin: isValid}); setNextGameState(nextGameState); - // TODO: check context @habdevs #190 - // const gameStateContext = useGameStateContext(); - // gameStateContext.setFinishResult({winner: playerIngameId === winnerId, isDraw: false, cheatWin: isValid}); - // console.log('PAGE GameType ', gameStateContext.finishResult) + // TODO: check for context overwriting? @habdevs #190 } else if (gameType === 'tic-tac-toe' && signedMove.gameMove.nonce === 8) { setFinishGameCheckResult({winner: false, isDraw: true, cheatWin: isValid});