Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions apps/frontend/src/components/ShareGame.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useState } from "react"
import { Button } from "./Button";

export const ShareGame = ({className,gameId}:{className?:string,gameId:string}) => {

const url = window.origin+"/game/"+gameId;
const [copied,setCopied] = useState(false);

const handleCopy = ()=>{
window.navigator.clipboard.writeText(url);
setCopied((p)=>true);
}

return (
<div className={`flex flex-col items-center gap-y-4 ${className}`}>

<h3 className="scroll-m-20 text-4xl font-semibold tracking-tight text-green-500">
Play with Friends
</h3>

<div className="flex items-center gap-x-2">

<LinkSvg/>

<div onClick={handleCopy} className="text-blue-500 cursor-pointer">
{url}
</div>
</div>

<Button onClick={handleCopy}>
{copied ? `Copied to Clipboard !!` : `Copy the link`}
</Button>
</div>
)
}

const LinkSvg = () => {
return (
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" className="lucide lucide-link text-white">
<path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/>
</svg>
)
}
16 changes: 16 additions & 0 deletions apps/frontend/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,19 @@
@apply bg-background text-foreground;
}
}

.chess-board {
background-color: #302e2b;
}

@layer utilities {
/* Hide scrollbar for Chrome, Safari and Opera */
.no-scrollbar::-webkit-scrollbar {
display: none;
}
/* Hide scrollbar for IE, Edge and Firefox */
.no-scrollbar {
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
}
}
12 changes: 9 additions & 3 deletions apps/frontend/src/screens/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { useRecoilValue, useSetRecoilState } from 'recoil';
import { movesAtom, userSelectedMoveIndexAtom } from '@repo/store/chessBoard';
import GameEndModal from '@/components/GameEndModal';
import { Waitopponent } from '@/components/ui/waitopponent';
import { ShareGame } from '../components/ShareGame';

const moveAudio = new Audio(MoveSound);

Expand All @@ -67,7 +68,7 @@ export const Game = () => {
>(null);
const [player1TimeConsumed, setPlayer1TimeConsumed] = useState(0);
const [player2TimeConsumed, setPlayer2TimeConsumed] = useState(0);

const [gameID,setGameID] = useState("");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this?

doesnt const { gameId } = useParams(); work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No ser, that won't work. When the first player initially joins he will be at the route /game/random. After receiving the game_added message we will actually get the game ID, using which we create the link ..

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, i see the login is different, would be better if we push the user who is wainting to the actual url with the ID: /game/id...

for now mergin this.

const setMoves = useSetRecoilState(movesAtom);
const userSelectedMoveIndex = useRecoilValue(userSelectedMoveIndexAtom);
const userSelectedMoveIndexRef = useRef(userSelectedMoveIndex);
Expand All @@ -91,6 +92,7 @@ export const Game = () => {
switch (message.type) {
case GAME_ADDED:
setAdded(true);
setGameID((p)=>message.gameId);
break;
case INIT_GAME:
setBoard(chess.board());
Expand Down Expand Up @@ -305,11 +307,15 @@ export const Game = () => {
</div>
</div>
</div>
<div className="rounded-md pt-2 bg-bgAuxiliary3 flex-1 overflow-auto h-[95vh]">
<div className="rounded-md pt-2 bg-bgAuxiliary3 flex-1 overflow-auto h-[95vh] overflow-y-scroll no-scrollbar">
{!started && (
<div className="pt-8 flex justify-center w-full">
{added ? (
<div className="text-white"><Waitopponent/></div>
<div className='flex flex-col items-center space-y-4 justify-center'>
<div className="text-white"><Waitopponent/></div>
<ShareGame gameId={gameID}/>
</div>

) : (
gameId === 'random' && (
<Button
Expand Down
9 changes: 9 additions & 0 deletions apps/ws/src/GameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class GameManager {
game.gameId,
JSON.stringify({
type: GAME_ADDED,
gameId:game.gameId,
}),
);
}
Expand Down Expand Up @@ -118,6 +119,14 @@ export class GameManager {
},
});

// There is a game created but no second player available

if (availableGame && !availableGame.player2UserId) {
socketManager.addUser(user, availableGame.gameId);
await availableGame.updateSecondPlayer(user.userId);
return;
}

if (!gameFromDb) {
user.socket.send(
JSON.stringify({
Expand Down