current user stones are allways at the botom

This commit is contained in:
djmil 2023-11-17 21:50:04 +01:00
parent 9b0a3753aa
commit dbb70cabba
8 changed files with 23 additions and 27 deletions

View File

@ -1,5 +1,6 @@
import { usePolling, doPushing } from '../hook/api'; import { usePolling, doPushing } from '../hook/api';
import { gamesInitialState } from '../reducer/games'; import { gamesInitialState } from '../reducer/games';
import { Color } from '../components/Checkers';
export default function useGamesApi(gamesReducer, config) { export default function useGamesApi(gamesReducer, config) {
const [games, dispatchGames] = gamesReducer; const [games, dispatchGames] = gamesReducer;
@ -20,8 +21,8 @@ export default function useGamesApi(gamesReducer, config) {
return { return {
pollGamesList: usePollingGamesList, pollGamesList: usePollingGamesList,
pushNewGame: ({ opponentName, opponentColor, board, message }) => ifNot(games.isPushingNewGame) && pushNewGame: ({ opponentName, myColor, board, message }) => ifNot(games.isPushingNewGame) &&
doPushing('/api/gameproposal', 'POST', { opponentName, opponentColor, board, message }, { doPushing('/api/gameproposal', 'POST', { opponentName, opponentColor: Color.opposite(myColor), board, message }, {
onPushing: (isPushingNewGame) => dispatchGames({ type: 'next', isPushingNewGame }), onPushing: (isPushingNewGame) => dispatchGames({ type: 'next', isPushingNewGame }),
onSuccess: (game) => dispatchGames({ type: 'next', gamesList: [game, ...games.gamesList], newGame: gamesInitialState.newGame }) onSuccess: (game) => dispatchGames({ type: 'next', gamesList: [game, ...games.gamesList], newGame: gamesInitialState.newGame })
}), }),

View File

@ -10,6 +10,10 @@
align-items: center; align-items: center;
} }
.Board.flip {
flex-direction: column-reverse;
}
.Tile { .Tile {
border: 1px solid #e4e4e4; border: 1px solid #e4e4e4;
float: left; float: left;

View File

@ -83,7 +83,7 @@ export function Board({ game, onClick }) {
); );
} }
return <div className='Board'> return <div className={'Board' + (game?.myColor === Color.black ? ' flip' : '')} >
<div className='row'> <div className='row'>
<BlackTile /> <WhiteTile id={1} /> <BlackTile /> <WhiteTile id={1} />
<BlackTile /> <WhiteTile id={2} /> <BlackTile /> <WhiteTile id={2} />

View File

@ -59,7 +59,7 @@ function ViewProvider({ dispatchGames, players }) {
<Routes> <Routes>
<Route path='new' element={ <Route path='new' element={
<NewGame setOpponent={(opponentName, opponentColor) => dispatchGames({ type: 'nextNewGame', opponentName, opponentColor })} <NewGame setPlayers={(opponentName, myColor) => dispatchGames({ type: 'nextNewGame', opponentName, myColor })}
players={players} players={players}
/> />
} /> } />

View File

@ -8,17 +8,17 @@ import Wobler from '../../components/Wobler';
export function Create({ onClick }) { export function Create({ onClick }) {
const games = useContext(GamesContext); const games = useContext(GamesContext);
const hasOpponent = games.newGame.opponentName && games.newGame.opponentColor; const hasPlayers = games.newGame.opponentName && games.newGame.myColor;
const validateNewGame = () => { const validateNewGame = () => {
if (!hasOpponent) if (!hasPlayers)
return alert("You have to select an opponent"); return alert("You have to select an opponent");
onClick(games.newGame); onClick(games.newGame);
} }
return ( return (
<button className={'Create' + (hasOpponent ? ' ready' : '')} <button className={'Create' + (hasPlayers ? ' ready' : '')}
onClick={validateNewGame} onClick={validateNewGame}
> >
<Wobler text="Create" dance={games.isPushingNewGame} /> <Wobler text="Create" dance={games.isPushingNewGame} />

View File

@ -32,7 +32,7 @@ export default function GameBoard({ username, onNewGameBoardStone, onActiveGameM
const [game, onClick] = (() => { const [game, onClick] = (() => {
if (matchPath('/games/new', pathname)) if (matchPath('/games/new', pathname))
return [gameFrom(games.newGame), onClick_NewGame]; return [games.newGame, onClick_NewGame];
if (matchPath('/games/proposal', pathname)) if (matchPath('/games/proposal', pathname))
return [games.findGame({ uuid: games.proposal.selectedUUID }), null]; return [games.findGame({ uuid: games.proposal.selectedUUID }), null];
@ -41,7 +41,7 @@ export default function GameBoard({ username, onNewGameBoardStone, onActiveGameM
else if (matchPath('/games/archive', pathname)) else if (matchPath('/games/archive', pathname))
return [games.findGame({ uuid: games.archive.selectedUUID }), null]; return [games.findGame({ uuid: games.archive.selectedUUID }), null];
return [{}, null]; // defaults return [{}, null];
})(); // <<-- Execute })(); // <<-- Execute
const opponentColor = Color.opposite(game?.myColor); const opponentColor = Color.opposite(game?.myColor);
@ -56,13 +56,4 @@ export default function GameBoard({ username, onNewGameBoardStone, onActiveGameM
<Player color={game?.myColor || Color.white} name={myName} /> <Player color={game?.myColor || Color.white} name={myName} />
</div> </div>
) )
}
function gameFrom(newGame) {
return {
myColor: Color.opposite(newGame.opponentColor),
opponentName: newGame.opponentName,
opponentColor: newGame.opponentColor,
board: newGame.board,
};
} }

View File

@ -5,16 +5,16 @@ import { GamesContext } from '../../../context/games';
import DropdownList from '../../../components/DropdownList'; import DropdownList from '../../../components/DropdownList';
import { Color, WhiteStone, BlackStone } from '../../../components/Checkers'; import { Color, WhiteStone, BlackStone } from '../../../components/Checkers';
export default function NewGame({ players, setOpponent }) { export default function NewGame({ players, setPlayers }) {
const games = useContext(GamesContext); const games = useContext(GamesContext);
const [whitePlayer, blackPlayer] = (() => { const [whitePlayer, blackPlayer] = (() => {
if (games.newGame.opponentColor === Color.white) if (games.newGame.myColor === Color.white)
return [games.newGame.opponentName, players.currentUser];
if (games.newGame.opponentColor === Color.black)
return [players.currentUser, games.newGame.opponentName]; return [players.currentUser, games.newGame.opponentName];
if (games.newGame.myColor === Color.black)
return [games.newGame.opponentName, players.currentUser];
return ['', '']; return ['', ''];
})(); // <<-- Execute! })(); // <<-- Execute!
@ -37,11 +37,11 @@ export default function NewGame({ players, setOpponent }) {
/* /*
* The Component * The Component
*/ */
const onSelect = (name, color) => { const onSelect = (name, myColor) => {
if (players.isCurrentUser(name)) if (players.isCurrentUser(name))
setOpponent(games.newGame.opponentName, Color.opposite(color)); setPlayers(games.newGame.opponentName, myColor);
else else
setOpponent(name, color); setPlayers(name, Color.opposite(myColor));
} }
return ( return (

View File

@ -7,7 +7,7 @@ export const gamesInitialState = {
newGame: { newGame: {
opponentName: '', opponentName: '',
opponentColor: '', myColor: '',
board: defaultBoard, board: defaultBoard,
message: '', message: '',
}, },