Skip to content

Commit 97750dd

Browse files
authored
Merge pull request #54 from boostcamp-2020/develop
2주차 스프린트
2 parents 4dda61c + 62a36e0 commit 97750dd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3278
-1159
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"eslint.format.enable": true,
3+
"editor.codeActionsOnSave": {
4+
"source.fixAll.eslint": true
5+
},
6+
}

package-lock.json

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/backend/.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[CORS]
2+
FRONTEND_ORIGIN=

src/backend/.eslintrc.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ module.exports = {
1919
},
2020
},
2121
rules: {
22-
'prettier/prettier': 'error',
22+
'prettier/prettier': [
23+
'error',
24+
{
25+
endOfLine: 'auto',
26+
},
27+
],
28+
'import/no-unresolved': 'off',
2329
},
2430
};

src/backend/app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from 'express';
22
import logger from 'morgan';
3+
import cors from 'cors';
34

45
import indexRouter from './routes/index';
56

@@ -8,6 +9,11 @@ const createApplication = () => {
89

910
app.use(logger('dev'));
1011
app.use(express.json());
12+
app.use(
13+
cors({
14+
origin: process.env.FRONTEND_ORIGIN,
15+
}),
16+
);
1117
app.use(express.urlencoded({ extended: false }));
1218

1319
app.use('/', indexRouter);

src/backend/game/Game.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import User from './User';
2+
3+
export default class Game {
4+
constructor(roomID) {
5+
this.roomID = roomID;
6+
this.users = new Map();
7+
this.status = {
8+
isPlaying: false,
9+
unusedCards: [],
10+
topic: '',
11+
turn: 0,
12+
};
13+
}
14+
15+
getRoomID() {
16+
return this.roomID;
17+
}
18+
19+
isPlaying() {
20+
return this.status.isPlaying;
21+
}
22+
23+
addUser({ socketID, nickname, color }) {
24+
const user = new User({ socketID, nickname, color });
25+
this.users.set(socketID, user);
26+
}
27+
28+
findUserInfo(socketID) {
29+
const user = this.users.get(socketID);
30+
return user ? user.getUserProfile() : false;
31+
}
32+
33+
findUserInfoAll(socketID) {
34+
return this.users.get(socketID).getUserInfo() || null;
35+
}
36+
}

src/backend/game/Games.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import generateRandom from '@utils/generateRandom';
2+
import Game from '@game/Game';
3+
4+
class Games {
5+
constructor() {
6+
this.roomGameMap = new Map();
7+
this.userRoomMap = new Map();
8+
}
9+
10+
isEnterableRoom(roomID) {
11+
const game = this.roomGameMap.get(roomID);
12+
if (!game) return false;
13+
if (game.isPlaying()) return false;
14+
return true;
15+
}
16+
17+
createGame() {
18+
const game = new Game();
19+
const roomID = generateRandom.code();
20+
this.roomGameMap.set(roomID, game);
21+
return roomID;
22+
}
23+
24+
enterUser({ socketID, roomID }) {
25+
const nickname = generateRandom.nickname();
26+
const color = generateRandom.color();
27+
this.userRoomMap.set(socketID, roomID);
28+
this.roomGameMap.get(roomID).addUser({ socketID, roomID, nickname, color });
29+
}
30+
31+
findRoomID(socketID) {
32+
return this.userRoomMap.get(socketID) || null;
33+
}
34+
35+
findGameBySocketID(socketID) {
36+
const roomID = this.userRoomMap.get(socketID);
37+
return this.roomGameMap.get(roomID) || false;
38+
}
39+
40+
findUserInfo(socketID) {
41+
const roomID = this.userRoomMap.get(socketID);
42+
if (!roomID) return false;
43+
const game = this.roomGameMap.get(roomID);
44+
return game.findUserInfo(socketID);
45+
}
46+
}
47+
48+
export default new Games();

src/backend/game/User.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class User {
2+
constructor({ socketID, nickname, color }) {
3+
this.socketID = socketID;
4+
this.nickname = nickname;
5+
this.color = color;
6+
this.turnID = 0;
7+
this.submittedCard = null;
8+
this.votedCard = null;
9+
this.isTeller = null;
10+
this.cards = [];
11+
this.score = 0;
12+
}
13+
14+
submitCard(cardID) {
15+
this.submittedCard = cardID;
16+
}
17+
18+
voteCard(cardID) {
19+
this.votedCard = cardID;
20+
}
21+
22+
addCard(cardID) {
23+
this.cards = [...this.cards, cardID];
24+
}
25+
26+
getUserInfo() {
27+
const {
28+
socketID,
29+
nickname,
30+
color,
31+
turnID,
32+
submittedCard,
33+
votedCard,
34+
isTeller,
35+
cards,
36+
score,
37+
} = this;
38+
39+
return {
40+
socketID,
41+
nickname,
42+
color,
43+
turnID,
44+
submittedCard,
45+
votedCard,
46+
isTeller,
47+
cards,
48+
score,
49+
};
50+
}
51+
52+
getUserProfile() {
53+
const { nickname, color } = this;
54+
return {
55+
nickname,
56+
color,
57+
};
58+
}
59+
}
60+
61+
export default User;

0 commit comments

Comments
 (0)