From 7d4aaf10dfd58bfe2caa72d4ceed4f3b513e82b4 Mon Sep 17 00:00:00 2001 From: djmil Date: Sun, 29 Oct 2023 19:04:23 +0100 Subject: [PATCH] front: action: CreateGameProposal: use Definitions --- webapp/package.json | 4 +- .../src/components/Game/GameAction/Create.jsx | 71 +++++++++++++++---- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/webapp/package.json b/webapp/package.json index 3385172..2cb2646 100644 --- a/webapp/package.json +++ b/webapp/package.json @@ -33,8 +33,8 @@ "not op_mini all" ], "development": [ - "last 1 chrome version", - "last 1 firefox version" + "last 1 firefox version", + "last 1 chrome version" ] } } diff --git a/webapp/src/components/Game/GameAction/Create.jsx b/webapp/src/components/Game/GameAction/Create.jsx index c52e8f9..d32bf4a 100644 --- a/webapp/src/components/Game/GameAction/Create.jsx +++ b/webapp/src/components/Game/GameAction/Create.jsx @@ -1,36 +1,77 @@ import React from 'react'; import { AppContext } from '../../../context/app' import { AppData } from '../../../context/data' +import { WHITE, BLACK } from '../Stone' export default function Create() { - const [ctx] = React.useContext(AppContext) - const [data] = React.useContext(AppData) - - const hasPlayers = ctx.newGame.whitePlayer !== ctx.newGame.blackPlayer - && ctx.newGame.blackPlayer !== '' - && ctx.newGame.whitePlayer !== '' - - const hasCurrentUser = data.isCurrentUser(ctx.newGame.blackPlayer) - || data.isCurrentUser(ctx.newGame.whitePlayer) - - const isEnabled = hasPlayers && hasCurrentUser + const ctx = Definitions() const onClick = () => { - if (!hasPlayers) + if (!ctx.hasPlayers) return alert("Choose both black and white players"); - if (!hasCurrentUser) + if (!ctx.hasCurrentUser) return alert("You must be one of the players"); - console.log("TODO: send GameCreateRequest to the server!") + const request = ctx.get_GameProposalRequest() + console.log("TODO: send GameCreateRequest to the server!", request) } return ( ) } + +function Definitions() { + const [ctx] = React.useContext(AppContext) + const [data] = React.useContext(AppData) + + const isCurrentUser = data.isCurrentUser + const whitePlayerName = ctx.newGame.whitePlayer + const blackPlayerName = ctx.newGame.blackPlayer + + const hasPlayers = whitePlayerName !== blackPlayerName + && whitePlayerName !== '' + && blackPlayerName !== '' + + const hasCurrentUser = isCurrentUser(whitePlayerName) || isCurrentUser(blackPlayerName) + + const isEnabled = hasPlayers && hasCurrentUser + + const get_GameProposalRequest = () => { + const [opponentName, opponentColor] = get_Opponent(isCurrentUser, whitePlayerName, blackPlayerName) + + return { + opponentName, + opponentColor, + board: null, + message: "let's play a game" + } + } + + return { + hasPlayers, + hasCurrentUser, + isEnabled, + + get_GameProposalRequest, + } +} + +function get_Opponent(isCurrentUser, whitePlayerName, blackPlayerName) { + if (isCurrentUser(whitePlayerName)) { + return [blackPlayerName, BLACK()] + } + + if (isCurrentUser(blackPlayerName)) { + return [whitePlayerName, WHITE()] + } + + return ['', ''] +} + \ No newline at end of file