From 9164023115359c36a254ba64ece17fc277eba7bb Mon Sep 17 00:00:00 2001 From: "Anton@habdevs" Date: Sun, 3 Sep 2023 04:28:14 +0500 Subject: [PATCH] feat: added newPlayerResult Context to page game-result #190 --- components/GameResult/GameResult.tsx | 204 ++++++++++++++------------- components/Players/Players.tsx | 77 +++++----- contexts/GameStateContext.tsx | 156 +++----------------- pages/game-result/index.tsx | 21 ++- pages/games/[gameType].tsx | 13 +- yarn.lock | 6 +- 6 files changed, 184 insertions(+), 293 deletions(-) diff --git a/components/GameResult/GameResult.tsx b/components/GameResult/GameResult.tsx index eacaa86..6e7ea27 100644 --- a/components/GameResult/GameResult.tsx +++ b/components/GameResult/GameResult.tsx @@ -1,109 +1,113 @@ -'use client'; -import classNames from 'classnames'; -import styles from './GameResult.module.scss'; -import imgWin from 'public/images/win.png'; -import playerImg from 'public/logo/account-avatar.png'; -import playerImg2 from 'public/logo/account-avatar2.png'; -import loseImg from 'public/images/lose.svg'; -import drawImg from 'public/images/draw.svg'; -import team from 'data/team.json'; -import {IGameResultProps} from './GameResultProps'; -import {BlockPayedGame, CustomButton, PurpleIcon, WhiteIcon} from 'components/shared'; -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 {FinishedGameState} from 'gameApi'; -import {IGameStateContext, useGameStateContext} from "../../contexts/GameStateContext"; +'use client' +import classNames from 'classnames' +import styles from './GameResult.module.scss' +import imgWin from 'public/images/win.png' +import loseImg from 'public/images/lose.svg' +import drawImg from 'public/images/draw.svg' +import team from 'data/team.json' + +import { BlockPayedGame, CustomButton } from 'components/shared' +import { TeamMemberBasic } from 'components/shared/ui/TeamMemberBasic' +import { useGameStateContext } from '../../contexts/GameStateContext' import { Player } from '../Player' -import { Players } from '../Players' +import { PlayerI } from '../../types/game' export const GameResult = () => { - const {finishResult, setFinishResult} = useGameStateContext(); - - - console.log('Данные из finishResult: ', finishResult); + const { + finishResult, setFinishResult, playerResult, setPlayerResult, + } = useGameStateContext() + + console.log('Данные из finishResult: ', finishResult) console.log('Данные из setFinishResult: ', setFinishResult) let winner, isDraw, cheatWin = false if (finishResult !== null) { - winner = finishResult.winner; - isDraw = finishResult.isDraw; - cheatWin = finishResult.cheatWin; + winner = finishResult.winner + isDraw = finishResult.isDraw + cheatWin = finishResult.cheatWin } - console.log('Данные из контекста в GameResult: ', finishResult); - console.log('Данные из winner: ', winner) - // } else return
finishResult NULL
- - - // const player1 = { - // playerName: '0xh20...7260', - // playerImg: playerImg, - // showWinText: setFinishResult?.winner === true, - // gameType: 'tic-tac-toe', - // icon: gameType === 'tic-tac-toe' ? : , - // }; - // const player2 = { - // playerName: '0xh07...6035', - // playerImg: playerImg2, - // showWinText: setFinishResult?.winner === false, - // gameType: 'tic-tac-toe', - // icon: gameType === 'tic-tac-toe' ? : , - // }; - - return ( -
-

- {winner === true && Winner!} - {!winner === true && Better Luck Next Time} - {isDraw === true && Better Luck Next Time} -

- {winner && Win} - {winner === false && ( -

- You lose lose -

- )} - {isDraw === true && ( -

- Draw draw -

- )} -
- {/**/} - {/**/} - {/**/} -
- -

Read more about our technology

-
- - -
-

- The dream team for your future -
games is here! -

-
-
- {team && team.map((teamMember) => )} -
-
-
- - + + console.log('Данные из finishResult в GameResult: ', finishResult) + console.log('Данные из winner: ', winner) + // if (playerResult !== null) { + // const players = playerResult + // } + + const players = playerResult! as unknown as PlayerI[] + const player1 = players[0] + const player2 = players[1] + // const player1 = playerResult?.players ? players : [0] + // const player2 = playerResult?.players ? players : [1] + console.log('Данные из playerResult в GameResult', playerResult) + + return (
+

+ {winner === true && Winner!} + {!winner && Better Luck Next Time} + {isDraw === true && Better Luck Next Time} +

+ {winner && Win} + {winner === false && (

+ You lose lose +

)} + {isDraw === true && (

+ Draw draw +

)} +
+ + + {/**/} + {/**/} + {/**/} +
+ +

Read more about our technology

+
+ + +
+

+ The dream team for your future +
games is here! +

+
+
+ {team && team.map((teamMember) => )}
- ); -} \ No newline at end of file +
+ + +
+
) +} + +// const player1 = { +// playerName: '0xh20...7260', +// playerImg: playerImg, +// showWinText: setFinishResult?.winner === true, +// gameType: 'tic-tac-toe', +// icon: gameType === 'tic-tac-toe' ? : , +// }; +// const player2 = { +// playerName: '0xh07...6035', +// playerImg: playerImg2, +// showWinText: setFinishResult?.winner === false, +// gameType: 'tic-tac-toe', +// icon: gameType === 'tic-tac-toe' ? : , +// }; \ No newline at end of file diff --git a/components/Players/Players.tsx b/components/Players/Players.tsx index ed913ee..79550e0 100644 --- a/components/Players/Players.tsx +++ b/components/Players/Players.tsx @@ -1,31 +1,31 @@ -import {useTranslation} from 'react-i18next'; +import { useTranslation } from 'react-i18next' import cn from 'classnames' -import {Button, Player} from 'components'; -import {PlayersPropsI} from './PlayersProps'; -import styles from './Players.module.scss'; -import {useState} from 'react'; +import { Button, Player } from 'components' +import { PlayersPropsI } from './PlayersProps' +import styles from './Players.module.scss' +import React, { useState } from 'react' export const Players: React.FC = ({ - player1, - player2, - isTimeoutAllowed, - initTimeout, - isResolveTimeoutAllowed, - resolveTimeout, - isFinishTimeOutAllowed, - finishTimeout, - isTimeoutRequested, - // connectPlayer, - }) => { - const [legend, setLegend] = useState<'finish' | 'init' | 'resolve' | null>(null); - const {t} = useTranslation(); + player1, + player2, + isTimeoutAllowed, + initTimeout, + isResolveTimeoutAllowed, + resolveTimeout, + isFinishTimeOutAllowed, + finishTimeout, + isTimeoutRequested, // connectPlayer, +}) => { + const [legend, setLegend] = useState<'finish' | 'init' | 'resolve' | null>( + null) + const { t } = useTranslation() const closeLegendHandler: React.MouseEventHandler = () => { - setLegend(null); + setLegend(null) } const showLegendHandler = (type: 'finish' | 'init' | 'resolve'): React.MouseEventHandler => (event) => { - event.stopPropagation(); - setLegend(type); + event.stopPropagation() + setLegend(type) } @@ -34,13 +34,12 @@ export const Players: React.FC = ({ 'init': { disabled: boolean, cb: () => Promise }; 'resolve': { disabled: boolean, cb: () => Promise }; } = { - 'finish': {disabled: !isFinishTimeOutAllowed, cb: finishTimeout}, - 'init': {disabled: !isTimeoutAllowed, cb: initTimeout}, - 'resolve': {disabled: !isResolveTimeoutAllowed, cb: resolveTimeout}, + 'finish': { disabled: !isFinishTimeOutAllowed, cb: finishTimeout }, + 'init': { disabled: !isTimeoutAllowed, cb: initTimeout }, + 'resolve': { disabled: !isResolveTimeoutAllowed, cb: resolveTimeout }, } - return ( -
+ return (
@@ -60,27 +59,33 @@ export const Players: React.FC = ({
}
+ className={cn(styles.popup_button, + !isTimeoutAllowed ? styles.disabled : null)}> {t('players.timeoutButtons.init')} - ? + ?
+ className={cn(styles.popup_button, + !isResolveTimeoutAllowed ? styles.disabled : null)}> {t('players.timeoutButtons.resolve')} - ? + ?
+ className={cn(styles.popup_button, + !isFinishTimeOutAllowed ? styles.disabled : null)}> {t('players.timeoutButtons.finish')} - ? + ?
-
+
{isTimeoutRequested && t('players.timeout')}
-
- ); -}; +
) +} diff --git a/contexts/GameStateContext.tsx b/contexts/GameStateContext.tsx index 217ed53..f005aeb 100644 --- a/contexts/GameStateContext.tsx +++ b/contexts/GameStateContext.tsx @@ -1,24 +1,13 @@ -import React, {createContext, useContext, useMemo, useState} from "react"; +import React, { createContext, useContext, useState } from 'react' +import { PlayerI } from '../types/game' export interface IGameStateContext { finishResult: { winner: boolean, isDraw: boolean, cheatWin: boolean } | null; setFinishResult: (newFinishResult: { - winner: boolean, - isDraw: boolean, - cheatWin: boolean - } | null) => void; - playerResult: { - playerName: string | null, - address: string | null, - avatarUrl: string, - moves?: boolean - } | null; - setPlayerResult: (newPlayerResult: { - playerName: string | null, - address: string | null, - avatarUrl: string, - moves?: boolean + winner: boolean, isDraw: boolean, cheatWin: boolean } | null) => void; + playerResult: { players: PlayerI[] } | null; + setPlayerResult: (newPlayerResult: {players: PlayerI[]}) => void; } export const GameStateContextDefault: IGameStateContext = { @@ -27,133 +16,26 @@ export const GameStateContextDefault: IGameStateContext = { playerResult: null, setPlayerResult: () => {}, } -export const useGameStateContext = () => useContext(GameStateContext); -export const GameStateContext = createContext(GameStateContextDefault); +export const useGameStateContext = () => useContext(GameStateContext) +export const GameStateContext = createContext( + GameStateContextDefault) -export const GameStateContextProvider: React.FC<{ children: React.ReactNode }> = ({children}) => { +export const GameStateContextProvider: React.FC<{ + children: React.ReactNode +}> = ({ children }) => { const [finishResult, setFinishResult] = useState<{ - winner: boolean, - isDraw: boolean, - cheatWin: boolean - } | null>(null) - const [playerResult, setPlayerResult] = useState<{ - playerName: string | null, - address: string | null, - avatarUrl: string, - moves?: boolean + winner: boolean, isDraw: boolean, cheatWin: boolean } | null>(null) - + const [playerResult, setPlayerResult] = useState<{ players: PlayerI[] } | null>(null) + // TODO NEXT STEP habdevs 02/09/23 const contextValue: IGameStateContext = { - finishResult, - setFinishResult, - playerResult, - setPlayerResult, + finishResult, setFinishResult, playerResult, setPlayerResult, } - console.log('CONTEXT setFinishResult',setFinishResult) + console.log('CONTEXT setFinishResult', setFinishResult) console.log('CONTEXT finishResult', finishResult) - console.log('CONTEXT setPlayerResult',setPlayerResult) + console.log('CONTEXT setPlayerResult', setPlayerResult) console.log('CONTEXT PlayerResult', playerResult) - return {children}; + return {children} } - - - - - - - - - - - - - - - -// export interface IGameStateContext { -// finishGameCheckResult: null | { winner: boolean; isDraw: boolean; cheatWin: boolean }; -// finishedGameState: FinishedGameState | null; -// playerIngameId: 0 | 1; -// winner: string | null; -// loser: string | null; -// isDraw: boolean; -// disqualified: string | null; -// resigned: string | null; -// players: PlayerI[]; -// setFinishGameCheckResult: React.Dispatch>; -// setFinishedGameState: React.Dispatch>; -// setGameId: React.Dispatch>; -// setWinner: React.Dispatch>; -// setLoser: React.Dispatch>; -// setIsDraw: React.Dispatch>; -// setDisqualified: React.Dispatch>; -// setResigned: React.Dispatch>; -// setPlayers: React.Dispatch>; -// setPlayerIngameId: React.Dispatch>; -// gameType: string | number; -// } -// -// export const GameStateContextDefault: IGameStateContext = { -// finishGameCheckResult: null, -// finishedGameState: null, -// playerIngameId: 0, -// winner: null, -// loser: null, -// isDraw: false, -// disqualified: null, -// resigned: null, -// players: [], -// setFinishGameCheckResult: () => { -// }, -// setFinishedGameState: () => { -// }, -// setGameId: () => { -// }, -// setWinner: () => { -// }, -// setLoser: () => { -// }, -// setIsDraw: () => { -// }, -// setDisqualified: () => { -// }, -// setResigned: () => { -// }, -// setPlayers: () => { -// }, -// setPlayerIngameId: () => { -// }, -// gameType: 0 -// } - -// export const GameStateContextProvider: React.FC<{ children: React.ReactNode }> = ({children}) => { -// const [finishedGameState, setFinishedGameState] = useState(null); -// const [players, setPlayers] = useState([]); -// const [finishGameCheckResult, setFinishGameCheckResult] = useState(null); -// const [playerIngameId, setPlayerIngameId] = useState<0 | 1>(0); - -// const [gameState, setGameState] = useState({ -// finishGameCheckResult: {winner: true}, -// finishedGameState: null, -// gameId: 0, -// winner: true, -// loser: false, -// isDraw: false, -// disqualified: null, -// resigned: null, -// players: null, -// -// }); -// console.log('GameStateContextProvider - gameState:', gameState); - -// return {children}; -// }; diff --git a/pages/game-result/index.tsx b/pages/game-result/index.tsx index 8d0aef3..3133cc6 100644 --- a/pages/game-result/index.tsx +++ b/pages/game-result/index.tsx @@ -1,17 +1,12 @@ -import {GameResult} from 'components/GameResult'; -import styles from './gameResult.module.scss'; -import {GameStateContextProvider} from "../../contexts/GameStateContext"; +import { GameResult } from 'components/GameResult' +import styles from './gameResult.module.scss' const GameResultPage = () => { - return ( -
-
- {/**/} - - {/**/} -
+ return (
+
+
- ); -}; -export default GameResultPage; +
) +} +export default GameResultPage diff --git a/pages/games/[gameType].tsx b/pages/games/[gameType].tsx index 37202af..91e08ed 100644 --- a/pages/games/[gameType].tsx +++ b/pages/games/[gameType].tsx @@ -1,4 +1,4 @@ -import { createContext, ReactNode, useEffect, useState } from 'react' +import { useEffect, useState } from 'react' import { GetStaticPaths, GetStaticProps, NextPage } from 'next' import router, { useRouter } from 'next/router' @@ -54,7 +54,7 @@ import { PlayerI, TGameType } from 'types/game' import { useTranslation } from 'react-i18next' import Link from 'next/link' import { - GameStateContext, GameStateContextProvider, useGameStateContext, + useGameStateContext, } from '../../contexts/GameStateContext' interface IGamePageProps { @@ -96,7 +96,7 @@ const Game: NextPage = ({ gameType, version }) => { []) const { query } = useRouter() const account = useAccount() - const { finishResult, setFinishResult } = useGameStateContext() + const { setPlayerResult, setFinishResult } = useGameStateContext() const playersTypesMap: { [id in TGameType]: { 0: JSX.Element; 1: JSX.Element @@ -196,6 +196,7 @@ const Game: NextPage = ({ gameType, version }) => { isDraw: finishGameCheckResult?.isDraw || false, cheatWin: finishGameCheckResult?.cheatWin || false, } + console.log('Попытка записи данных в контекст:', newFinishResult) setFinishResult(newFinishResult) // setFinishGameCheckResult(null); @@ -379,7 +380,6 @@ const Game: NextPage = ({ gameType, version }) => { } async function processOneMessage (i: number) { - //TODO console.log(lastMessages) const lastMessage = lastMessages[i] @@ -569,6 +569,11 @@ const Game: NextPage = ({ gameType, version }) => { moves: !finishedGameState && !isInDispute && !isPlayerMoves(gameType, gameState, playerIngameId), }]) + // TODO: check update context @habdevs #190 setPlayerResult + const newPlayerResult = { players } + setPlayerResult(newPlayerResult) + console.log('попытка записи в SETPLAYERRESULT', newPlayerResult) + }, [opponentAddress, gameId, gameType, gameState]) useEffect(() => { diff --git a/yarn.lock b/yarn.lock index 26d9bfe..6815cbb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1826,9 +1826,9 @@ integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA== "@types/node@*", "@types/node@>=13.7.0": - version "20.5.8" - resolved "https://registry.yarnpkg.com/@types/node/-/node-20.5.8.tgz#fb171fd22d37ca6e2ea97fde88e6a13ee14bc327" - integrity sha512-eajsR9aeljqNhK028VG0Wuw+OaY5LLxYmxeoXynIoE6jannr9/Ucd1LL0hSSoafk5LTYG+FfqsyGt81Q6Zkybw== + version "20.5.9" + resolved "https://registry.yarnpkg.com/@types/node/-/node-20.5.9.tgz#a70ec9d8fa0180a314c3ede0e20ea56ff71aed9a" + integrity sha512-PcGNd//40kHAS3sTlzKB9C9XL4K0sTup8nbG5lC14kzEteTNuAFh9u5nA0o5TWnSG2r/JNPRXFVcHJIIeRlmqQ== "@types/node@^12.12.54": version "12.20.55"