Skip to content

Commit 64409cb

Browse files
authored
Merge pull request #183 from JJongBin/develop
에러 수정 및 타입 추가
2 parents 6695f52 + ce92cea commit 64409cb

24 files changed

+206
-78
lines changed

frontend/src/component/Chat/ChatContent.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { chatType } from '../../types/types';
12
import { chat, chatTime, chatUser, info } from './chat.styled';
23

3-
const ChatContent = ({ data }: { data: any }) => {
4+
const ChatContent = ({ data }: { data: chatType }) => {
45
const { type, timestamp, nickname, message } = data;
56

67
return (

frontend/src/component/Chat/ChatMessage.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as style from './chat.styled';
33
import { useRecoilValue } from 'recoil';
44
import { socketState } from '../../store/atom/socket';
55
import ChatContent from './ChatContent';
6+
import { chatType, userType } from '../../types/types';
67

78
const ChatMessage = ({
89
updateChat,
@@ -12,7 +13,7 @@ const ChatMessage = ({
1213
}: {
1314
updateChat: Function;
1415
isExtend: boolean;
15-
chatDatas: any;
16+
chatDatas: chatType[];
1617
setChatDatas: Function;
1718
}) => {
1819
const socket = useRecoilValue(socketState);
@@ -26,11 +27,11 @@ const ChatMessage = ({
2627
}
2728

2829
// 다른 사람의 채팅받기
29-
socket.on('publicChat', (chat: any) => {
30+
socket.on('publicChat', (chat: chatType) => {
3031
updateChat(chat);
3132
});
3233

33-
socket.on('userCreated', (data: any) => {
34+
socket.on('userCreated', (data: userType) => {
3435
const chat = {
3536
type: 'info',
3637
nickname: data.nickname,
@@ -40,7 +41,7 @@ const ChatMessage = ({
4041
updateChat(chat);
4142
});
4243

43-
socket.on('userLeaved', (data: any) => {
44+
socket.on('userLeaved', (data: userType) => {
4445
const chat = {
4546
type: 'info',
4647
nickname: data.nickname,
@@ -66,7 +67,7 @@ const ChatMessage = ({
6667
return (
6768
<div css={style.chatTextWrapper(isExtend)} ref={chatRef}>
6869
<ul css={style.chatText}>
69-
{chatDatas.map((data: any, idx: any) => (
70+
{chatDatas.map((data, idx: number) => (
7071
<ChatContent data={data} key={idx} />
7172
))}
7273
</ul>

frontend/src/component/Chat/index.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import { useState } from 'react';
2+
import { chatType } from '../../types/types';
23
import * as style from './chat.styled';
34
import ChatMessage from './ChatMessage';
45
import Input from './Input';
56
import { calcTimeFromMs } from './util';
67

78
const Chat = () => {
8-
const [isExtend, setIsExtend] = useState(true);
9-
const [chatDatas, setChatDatas] = useState<any[]>([]);
9+
const [isExtend, setIsExtend] = useState(false);
10+
const [chatDatas, setChatDatas] = useState<chatType[]>([]);
1011

1112
// 채팅 업데이트
12-
const updateChat = (chat: any) => {
13+
const updateChat = (chat: chatType) => {
1314
chat.timestamp = calcTimeFromMs(chat.timestamp);
1415

1516
sessionStorage.setItem('chat', JSON.stringify([...chatDatas, chat]));
16-
setChatDatas((chatDatas: any) => [...chatDatas, chat]);
17+
setChatDatas(chatDatas => [...chatDatas, chat]);
1718
};
1819

1920
// 채팅창 크기 변경

frontend/src/component/Chat/util.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export const calcTimeFromMs = (ms: number) => {
1+
export const calcTimeFromMs = (ms: number | string) => {
2+
if (typeof ms === 'string') return '';
3+
24
const date = new Date(ms);
35
const h = date.getHours().toString().padStart(2, '0');
46
const m = date.getMinutes().toString().padStart(2, '0');

frontend/src/component/Game/Phaser/Player/myPlayer.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ export class MyPlayer extends Player {
8585

8686
sortHeldDirection(this, cursors);
8787
if (this.heldDirection.length) {
88-
const move: any = calcMoveToPos(this, this.heldDirection);
88+
const move: { x: number; y: number } | undefined = calcMoveToPos(
89+
this,
90+
this.heldDirection
91+
);
92+
if (!move) return;
93+
8994
this.getBody().setVelocity(move.x * this.speed, move.y * this.speed);
9095

9196
if (move.x !== 0) {

frontend/src/component/Game/Phaser/Player/otherPlayer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { userType } from '../../../../types/types';
12
import { changeDirection, changePosition, changeState } from '../../util';
23
import { Player } from './player';
34

45
export class OtherPlayer extends Player {
5-
constructor(scene: Phaser.Scene, data: any) {
6+
constructor(scene: Phaser.Scene, data: userType) {
67
super(scene, data.x, data.y, data.id, data.characterName, data.nickname);
78
}
89
update(state: string, x: number, y: number, direction: string) {

frontend/src/component/Game/game.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import rollImg from '../../assets/character/roll/roll.png';
1616
import jumpImg from '../../assets/character/jump/jump.png';
1717
import attackImg from '../../assets/character/attack/attack.png';
1818
import attackTool from '../../assets/character/tool/attack.png';
19-
import { stringObjectType } from '../../types/types';
19+
import { gameInitType, stringObjectType, userType } from '../../types/types';
2020

2121
const characterImg: stringObjectType = {
2222
wait: waitImg,
@@ -32,7 +32,7 @@ export default class Game extends Phaser.Scene {
3232
otherPlayer: { [key: string]: OtherPlayer };
3333
socket?: Socket;
3434
autoPlay: boolean;
35-
townLayer: any;
35+
townLayer?: Phaser.Tilemaps.TilemapLayer;
3636

3737
constructor(config: Phaser.Types.Core.GameConfig) {
3838
super(config);
@@ -44,7 +44,7 @@ export default class Game extends Phaser.Scene {
4444
}
4545

4646
init() {
47-
emitter.on('init', (data: any) => {
47+
emitter.on('init', (data: gameInitType) => {
4848
this.socket = data.socket.connect();
4949

5050
this.myPlayer = new MyPlayer(
@@ -63,7 +63,8 @@ export default class Game extends Phaser.Scene {
6363
// collidingTileColor: new Phaser.Display.Color(243, 234, 48, 255),
6464
// });
6565

66-
this.physics.add.collider(this.myPlayer, this.townLayer);
66+
if (this.townLayer)
67+
this.physics.add.collider(this.myPlayer, this.townLayer);
6768

6869
this.socket?.on('connect', () => {
6970
this.socketInit();
@@ -85,6 +86,8 @@ export default class Game extends Phaser.Scene {
8586
if (this.myPlayer) this.myPlayer.isCanMove = !checkInput;
8687
this.input.keyboard.manager.enabled = !checkInput;
8788
};
89+
90+
emitter.emit('game-start');
8891
}
8992

9093
preload() {
@@ -211,10 +214,10 @@ export default class Game extends Phaser.Scene {
211214
socketInit() {
212215
if (!this.socket) return;
213216

214-
this.socket.on('userInitiated', (data: any) => {
217+
this.socket.on('userInitiated', (data: userType[]) => {
215218
if (!Array.isArray(data)) data = [data];
216219

217-
data.forEach((user: any) => {
220+
data.forEach((user: userType) => {
218221
const id = user.id.toString().trim();
219222
if (this.myPlayer?.id === id) return;
220223
if (this.otherPlayer[id]) return;
@@ -223,26 +226,26 @@ export default class Game extends Phaser.Scene {
223226
});
224227
});
225228

226-
this.socket.on('userCreated', (user: any) => {
229+
this.socket.on('userCreated', (user: userType) => {
227230
const id = user.id.toString().trim();
228231
this.otherPlayer[id] = new OtherPlayer(this, user);
229232
});
230233

231-
this.socket.on('move', (data: any) => {
234+
this.socket.on('move', (data: userType) => {
232235
const id = data.id.toString().trim();
233236

234237
if (!this.otherPlayer[id]) return;
235238
const { state, x, y, direction } = data;
236239
this.otherPlayer[id].update(state, x, y, direction);
237240
});
238241

239-
this.socket.on('userLeaved', (data: any) => {
242+
this.socket.on('userLeaved', (data: userType) => {
240243
const id = data.id.toString().trim();
241244
this.otherPlayer[id].delete();
242245
delete this.otherPlayer[id];
243246
});
244247

245-
this.socket.on('userDataChanged', (data: any) => {
248+
this.socket.on('userDataChanged', (data: userType) => {
246249
const { id, nickname, characterName } = data;
247250
this.otherPlayer[id].updateNickname(nickname);
248251
this.otherPlayer[id].updateHair(characterName);

frontend/src/component/Game/util.ts

+32-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { MyPlayer } from './Phaser/Player/myPlayer';
2+
import { OtherPlayer } from './Phaser/Player/otherPlayer';
3+
14
const responsiveness = 5;
25

3-
export const changeState = (player: any) => {
4-
if (!player.character || !player.hair) return;
6+
export const changeState = (player: MyPlayer | OtherPlayer) => {
7+
if (!player.character || !player.hair || !player.dust || !player.tool) return;
58

69
player.character.play(`character-${player.state}`);
710
player.hair.play(`${player.hairName}-${player.state}`);
@@ -22,8 +25,11 @@ export const changeState = (player: any) => {
2225
}
2326
};
2427

25-
export const changeDirection = (player: any, direction: string) => {
26-
if (!player.character || !player.hair) return;
28+
export const changeDirection = (
29+
player: MyPlayer | OtherPlayer,
30+
direction: string
31+
) => {
32+
if (!player.character || !player.hair || !player.dust || !player.tool) return;
2733
if (player.direction === direction) return;
2834

2935
player.character.toggleFlipX();
@@ -34,8 +40,11 @@ export const changeDirection = (player: any, direction: string) => {
3440
player.direction = direction;
3541
};
3642

37-
export const calcMoveToPos = (player: any, dir: string[]) => {
38-
if (!player.character || !player.hair) return;
43+
export const calcMoveToPos = (
44+
player: MyPlayer | OtherPlayer,
45+
dir: string[]
46+
) => {
47+
if (!player.character || !player.hair || !player.dust || !player.tool) return;
3948

4049
const move = { x: 0, y: 0 };
4150
const leftIdx = dir.indexOf('left');
@@ -58,8 +67,19 @@ export const calcMoveToPos = (player: any, dir: string[]) => {
5867
return move;
5968
};
6069

61-
export const changePosition = (player: any, x: number, y: number) => {
62-
if (!player.character || !player.hair) return;
70+
export const changePosition = (
71+
player: MyPlayer | OtherPlayer,
72+
x: number,
73+
y: number
74+
) => {
75+
if (
76+
!player.character ||
77+
!player.hair ||
78+
!player.dust ||
79+
!player.tool ||
80+
!player.nicknameText
81+
)
82+
return;
6383

6484
player.x += x;
6585
player.y += y;
@@ -75,16 +95,16 @@ export const changePosition = (player: any, x: number, y: number) => {
7595
player.dust.setPosition(player.x + editPosNum, player.y + 5);
7696
};
7797

78-
export const sortHeldDirection = (scene: any, cursors: any) => {
98+
export const sortHeldDirection = (player: MyPlayer, cursors: any) => {
7999
const directions = ['left', 'right', 'up', 'down'];
80100

81101
directions.forEach((dir: string) => {
82-
const idx = scene.heldDirection.indexOf(dir);
102+
const idx = player.heldDirection.indexOf(dir);
83103

84104
if (cursors[dir].isDown && idx === -1) {
85-
scene.heldDirection.push(dir);
105+
player.heldDirection.push(dir);
86106
} else if (cursors[dir].isUp && idx > -1) {
87-
scene.heldDirection.splice(idx, 1);
107+
player.heldDirection.splice(idx, 1);
88108
}
89109
});
90110
};

frontend/src/component/Info/Help.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ const Help = () => {
3131

3232
useEffect(() => {
3333
const openHelp = localStorage.getItem('openHelp');
34-
if (openHelp === 'open') {
34+
35+
if (openHelp !== 'close') {
3536
setIsShowModal(true);
3637
isSetOpen(true);
3738
}

frontend/src/component/Info/Users.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import users from '../../assets/icon/users.svg';
33
import { useEffect, useState } from 'react';
44
import { useRecoilValue } from 'recoil';
55
import { socketState } from '../../store/atom/socket';
6+
import { userType } from '../../types/types';
67

78
const Users = () => {
89
const socket = useRecoilValue(socketState);
910
const [userCnt, setUseCnt] = useState(0);
1011

1112
useEffect(() => {
12-
socket.on('userInitiated', (data: any) => {
13+
socket.on('userInitiated', (data: userType[]) => {
1314
setUseCnt(data.length);
1415
});
1516

frontend/src/component/Info/info.styled.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,14 @@ export const content = css`
117117
padding: 5px;
118118
119119
::-webkit-scrollbar {
120-
display: none;
120+
width: 10px;
121+
}
122+
123+
::-webkit-scrollbar-thumb {
124+
background: rgba(0, 0, 0, 0.1);
125+
background-clip: padding-box;
126+
border: 2px solid transparent;
127+
border-radius: 10px;
121128
}
122129
`;
123130

frontend/src/component/Sidebar/Chat/ChatList.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { useEffect, useState } from 'react';
55
import { useRecoilValue } from 'recoil';
66
import { socketState } from '../../../store/atom/socket';
77
import axios from 'axios';
8+
import { chatRoomType } from '../../../types/types';
89

910
const ChatList = ({ setChatTarget }: { setChatTarget: Function }) => {
1011
const socket = useRecoilValue(socketState);
11-
const [roomList, setRoomList] = useState<any[]>([]);
12+
const [roomList, setRoomList] = useState<chatRoomType[]>([]);
1213
const [isLoaded, setIsLoaded] = useState(false);
1314
const [isClose, setIsClose] = useState(false); // 애니메이션
1415

@@ -18,6 +19,7 @@ const ChatList = ({ setChatTarget }: { setChatTarget: Function }) => {
1819
try {
1920
const { data } = await axios('/api/chat/roomList');
2021

22+
console.log(data);
2123
setRoomList(() => data);
2224
setIsLoaded(true);
2325
} catch (e) {}
@@ -53,7 +55,7 @@ const ChatList = ({ setChatTarget }: { setChatTarget: Function }) => {
5355
<ul css={chatWrapper(isClose)} onClick={selectChatRoom}>
5456
{isLoaded &&
5557
(roomList.length ? (
56-
roomList.map((data: any) => (
58+
roomList.map(data => (
5759
<ChatRoom key={data.targetUserId} data={data} />
5860
))
5961
) : (

frontend/src/component/Sidebar/Chat/ChatMessage.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { useRecoilValue } from 'recoil';
22
import { userState } from '../../../store/atom/user';
3+
import { privateChatType } from '../../../types/types';
34
import * as style from './chat.styled';
45
import { calcTimeFromMs } from './util';
56

6-
const ChatMessage = ({ chat }: { chat: any }) => {
7+
const ChatMessage = ({ chat }: { chat: privateChatType }) => {
78
const user = useRecoilValue(userState);
89
const isSender = user.id === chat.senderId;
910

frontend/src/component/Sidebar/Chat/ChatRoom.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { chatRoomType } from '../../../types/types';
12
import Content from '../Content';
23
import { chatInfo, chatItemWrapper, message } from './chat.styled';
34
import { calcTimeFromMs } from './util';
45

5-
const ChatRoom = ({ data }: { data: any }) => {
6+
const ChatRoom = ({ data }: { data: chatRoomType }) => {
67
const { unReadCount, targetUserNickname, lastMsg } = data;
78

89
return (

0 commit comments

Comments
 (0)