Skip to content

Commit

Permalink
feat: change gamestatecontext #190
Browse files Browse the repository at this point in the history
  • Loading branch information
Храмыцких Антон committed Aug 23, 2023
1 parent 73c154e commit cd16363
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
14 changes: 8 additions & 6 deletions components/GameField/GameField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<GameFieldPropsI> = ({
children,
rivalPlayerAddress,
Expand Down Expand Up @@ -122,6 +122,8 @@ export const GameField: React.FC<GameFieldPropsI> = ({

useEffect(() => {
if (!!finishGameCheckResult) {
console.log('use effect', finishGameCheckResult)

setShowShade(true);
setIsWaiting(false);
setIsConnecting(false);
Expand Down Expand Up @@ -212,10 +214,10 @@ export const GameField: React.FC<GameFieldPropsI> = ({
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 (
Expand Down
15 changes: 10 additions & 5 deletions components/GameResult/GameResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions contexts/GameStateContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export const GameStateContextDefault: IGameStateContext = {
}

export const GameStateContext = createContext<IGameStateContext>(GameStateContextDefault);
export const useGameStateContext = () => useContext(GameStateContext);

export const GameStateContextProvider: React.FC<{ children: React.ReactNode }> = ({children}) => {
const [finishResult, setFinishResult] = useState<IGameStateContext["finishResult"] | null>(null);

const contextValue = useMemo(() => ({ finishResult, setFinishResult }), [finishResult, setFinishResult]);

return <GameStateContext.Provider value={contextValue}>{children}</GameStateContext.Provider>;
}

// export const useGameStateContext = () => useContext(GameStateContext);

// export interface IGameStateContext {
// finishGameCheckResult: null | { winner: boolean; isDraw: boolean; cheatWin: boolean };
Expand Down
6 changes: 3 additions & 3 deletions pages/game-result/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className={styles.wrapper}>
Expand Down
5 changes: 1 addition & 4 deletions pages/games/[gameType].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,7 @@ const Game: NextPage<IGamePageProps> = ({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});
Expand Down

0 comments on commit cd16363

Please sign in to comment.