Showing
2 changed files
with
42 additions
and
0 deletions
| ... | @@ -16,4 +16,6 @@ export const MessageType = { | ... | @@ -16,4 +16,6 @@ export const MessageType = { |
| 16 | ROOM_LEAVE: "leaveRoom", | 16 | ROOM_LEAVE: "leaveRoom", |
| 17 | ROOM_USER_UPDATE: "updateRoomUser", | 17 | ROOM_USER_UPDATE: "updateRoomUser", |
| 18 | ROOM_CHAT: "chat", | 18 | ROOM_CHAT: "chat", |
| 19 | + ROOM_READY: "ready", | ||
| 20 | + ROOM_START: "startGame" | ||
| 19 | } as const | 21 | } as const |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
web/src/components/room/Ready.tsx
0 → 100644
| 1 | +import React, { useCallback, useContext } from 'react'; | ||
| 2 | +import SocketContext from '../../contexts/SocketContext'; | ||
| 3 | +import { MessageType, RawMessage } from '../common/types'; | ||
| 4 | + | ||
| 5 | +interface ReadyProps { | ||
| 6 | + isReady: boolean; | ||
| 7 | + isAdmin: boolean; | ||
| 8 | + isAllReady: boolean; | ||
| 9 | +} | ||
| 10 | + | ||
| 11 | +export const Ready: React.FC<ReadyProps> = ({ isReady, isAdmin, isAllReady }) => { | ||
| 12 | + const socket = useContext(SocketContext); | ||
| 13 | + const handleReady = useCallback(() => { | ||
| 14 | + if (isAdmin && isAllReady) { | ||
| 15 | + const rawMessage: RawMessage = { | ||
| 16 | + type: MessageType.ROOM_READY, | ||
| 17 | + message: {} | ||
| 18 | + } | ||
| 19 | + socket.emit('msg', rawMessage, () => {}); | ||
| 20 | + } else { | ||
| 21 | + const rawMessage: RawMessage = { | ||
| 22 | + type: MessageType.ROOM_READY, | ||
| 23 | + message: { ready: !isReady } | ||
| 24 | + } | ||
| 25 | + socket.emit('msg', rawMessage, () => {}); | ||
| 26 | + } | ||
| 27 | + }, []); | ||
| 28 | + | ||
| 29 | + return ( | ||
| 30 | + <button className={`${isAdmin ? isAllReady ? 'bg-green-500' : 'bg-gray-400' | ||
| 31 | + : isReady ? 'bg-green-600' | ||
| 32 | + : 'bg-green-500 active:bg-green-600'} | ||
| 33 | + text-white font-bold uppercase text-sm | ||
| 34 | + px-5 py-2 ml-3 rounded shadow | ||
| 35 | + outline-none focus:outline-none hover:shadow-md | ||
| 36 | + ease-linear transition-all duration-100`} | ||
| 37 | + type="button" | ||
| 38 | + onClick={() => {}}>{isAdmin ? 'Start' : 'Ready'}</button> | ||
| 39 | + ); | ||
| 40 | +} |
-
Please register or login to post a comment