Toggle navigation
Toggle navigation
This project
Loading...
Sign in
강동현
/
nodejs-game
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Overnap
2021-06-10 03:57:30 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
26951faa38ffa640f8b646235c625ece11dd5161
26951faa
1 parent
7ece7184
GameBoard 컴포넌트 추가 및 관련 컴포넌트 수정 작업
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
111 additions
and
17 deletions
web/src/components/room/Canvas.tsx
web/src/components/room/GameBoard.tsx
web/src/pages/Room.tsx
web/src/components/room/Canvas.tsx
View file @
26951fa
...
...
@@ -3,7 +3,11 @@ import { Vector } from './types';
// 참고 : https://basketdeveloper.tistory.com/79
export const Canvas: React.FC = () => {
interface CanvasProps {
isDrawer: boolean;
}
export const Canvas: React.FC<CanvasProps> = ({ isDrawer }) => {
const canvasRef = useRef<HTMLCanvasElement>(null);
const [mousePosition, setMousePosition] = useState<Vector>({ x:0, y:0 });
...
...
@@ -77,7 +81,7 @@ export const Canvas: React.FC = () => {
}, []);
useEffect(() => {
if (canvasRef.current) {
if (canvasRef.current
&& isDrawer
) {
const canvas: HTMLCanvasElement = canvasRef.current;
canvas.addEventListener('mousedown', startPaint);
...
...
@@ -91,8 +95,10 @@ export const Canvas: React.FC = () => {
canvas.removeEventListener('mouseup', exitPaint);
canvas.removeEventListener('mouseleave', exitPaint);
};
} else {
// 받아서 그리기
}
}, [startPaint, paint, exitPaint]);
}, [
isDrawer,
startPaint, paint, exitPaint]);
return (
<div className='mx-3 px-2 py-1 rounded shadow'>
...
...
web/src/components/room/GameBoard.tsx
0 → 100644
View file @
26951fa
import React, { useCallback, useContext, useEffect, useState } from 'react';
import { useLocation } from 'react-router';
import SocketContext from '../../contexts/SocketContext';
import { MessageType, RawMessage } from '../common/types';
import { Canvas } from './Canvas';
import { Role, RoundData } from './types';
import { Word } from './Word';
interface GameBoardLocation {
state: { username: string }
}
interface GameBoardProps {
isInGame: boolean
}
export const GameBoard: React.FC<GameBoardProps> = ({ isInGame }) => {
const socket = useContext(SocketContext);
const location: GameBoardLocation = useLocation();
const [ isDrawer, setIsDrawer ] = useState(false);
const [ words, setWords ] = useState<string[]>([]);
const [ wordChosen, setWordChosen ] = useState('');
const handleWordSet = useCallback((rawMessage: RawMessage) => {
if (rawMessage.type === MessageType.GAME_WORDSET) {
console.log('단어 도착');
const { words } = rawMessage.message as { words: string[] };
setWords(words);
}
}, []);
const handleStart = useCallback((rawMessage: RawMessage) => {
if (rawMessage.type === MessageType.GAME_START) {
setWords([]);
const data = rawMessage.message as RoundData;
console.log('테스트 location ', location.state.username);
console.log('테스트 rolse ', data.roles);
const index = data.roles.findIndex(x => x.username === location.state.username);
setIsDrawer(data.roles[index].role === 'drawer');
setWordChosen('');
}
}, []);
const handleGetWordLength = useCallback((rawMessage: RawMessage) => {
if (rawMessage.type === MessageType.GAME_WORD) {
if (wordChosen !== '') {
const { length } = rawMessage.message as { length: number };
setWordChosen('_'.repeat(length));
}
}
}, [wordChosen]);
useEffect(() => {
socket.on('msg', handleStart);
socket.on('msg', handleGetWordLength);
socket.on('msg', handleWordSet);
return () => {
socket.off('msg', handleStart);
socket.off('msg', handleGetWordLength);
socket.off('msg', handleWordSet);
}
}, []);
return (
<div className={`w-auto ${isInGame ? '' : 'hidden'}`}>
<div className='w-full flex flex-col justify-center items-center'>
{words.map((word, i) => (<Word key={word} index={i} word={word} setWordChosen={setWordChosen} setWords={setWords} />))}
</div>
<Canvas isDrawer={isDrawer && wordChosen !== ''}/>
</div>
);
}
\ No newline at end of file
web/src/pages/Room.tsx
View file @
26951fa
...
...
@@ -4,6 +4,7 @@ import { Main } from '../components/common/Main';
import { MessageResponse, MessageType, RawMessage } from '../components/common/types';
import { Canvas } from '../components/room/Canvas';
import { Chat } from '../components/room/Chat';
import { GameBoard } from '../components/room/GameBoard';
import { Ready } from '../components/room/Ready';
import { RoomInfo } from '../components/room/RoomInfo';
import { RoomData, UpdateRoomUser } from '../components/room/types';
...
...
@@ -29,6 +30,14 @@ export const Room: React.FC = () => {
});
const [ isInGame, setIsInGame ] = useState(false);
const handleInGame = useCallback((rawMessage: RawMessage) => {
if (rawMessage.type === MessageType.GAME_START) {
setIsInGame(true);
} else if (rawMessage.type === MessageType.GAME_FINISH_GAME) {
setIsInGame(false);
}
}, []);
const handleUpdateRoomUser = useCallback((rawMessage: RawMessage) => {
if (rawMessage.type == MessageType.ROOM_USER_UPDATE) {
const data = rawMessage.message as UpdateRoomUser;
...
...
@@ -81,8 +90,11 @@ export const Room: React.FC = () => {
}
setRoomData(location.state.roomData);
socket.on('msg', handleInGame);
return () => {
socket.off('msg', handleInGame);
const rawMessage: RawMessage = {
type: MessageType.ROOM_LEAVE,
message: ''
...
...
@@ -94,20 +106,21 @@ export const Room: React.FC = () => {
return (
<Main>
<RoomInfo roomData={roomData}/>
{
isInGame ? (
<div className='w-full flex'>
<Canvas />
<Chat w='w-4/12' h='h-80' />
</div>
) : (
<div className='w-full flex flex-col justify-center items-center'>
<UserInfo users={roomData.users}/>
<Ready users={roomData.users} />
<Chat w='w-7/12' h='h-96' />
</div>
)
}
<div className='w-full flex'>
{/* 게임보드를 계속 살려둬서 리스너를 항상 열어놓도록 하자 */}
<GameBoard isInGame={isInGame} />
{
isInGame ? (
<Chat w='w-4/12' h='h-80' />
) : (
<div className='w-full flex flex-col justify-center items-center'>
<UserInfo users={roomData.users}/>
<Ready users={roomData.users} />
<Chat w='w-7/12' h='h-96' />
</div>
)
}
</div>
</Main>
);
}
\ No newline at end of file
...
...
Please
register
or
login
to post a comment