Skip to content

Commit 36d8183

Browse files
authored
Merge pull request #67 from boostcamp-2020/develop
GitHub Actions test
2 parents 3260314 + 72a8a9e commit 36d8183

File tree

11 files changed

+170
-86
lines changed

11 files changed

+170
-86
lines changed

.github/workflows/deploy.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# GitHub Actions 공식 documentation: https://docs.github.com/en/free-pro-team/actions
2+
# ssh에 shell script 실행하기: https://github.com/fifsky/ssh-action/blob/master/entrypoint.sh
3+
4+
name: Deploy master branch
5+
6+
on:
7+
push:
8+
branches: [master]
9+
10+
jobs:
11+
Deploy:
12+
runs-on: ubuntu-16.04
13+
env:
14+
DEPLOY_SCRIPT: shell.sh
15+
DEPLOY_KEY: deploy_key
16+
steps:
17+
- name: Make deploy script
18+
run: |
19+
cat << EOF > $DEPLOY_SCRIPT
20+
echo "=== pull master branch ==="
21+
cd ${{ secrets.SSH_WORKING_DIRECTORY }}/src
22+
git checkout master
23+
git fetch origin master
24+
BE_DIFF=$(git diff HEAD..origin/master --name-only -- src/backend | wc -l)
25+
FE_DIFF=$(git diff HEAD..origin/master --name-only -- src/frontend | wc -l)
26+
git pull origin master
27+
28+
if [ $BE_DIFF -ne 0 ] ; then
29+
echo "=== build backend ==="
30+
cd backend
31+
npm ci || exit 1
32+
npm run build || exit 1
33+
echo "=== restart backend server ==="
34+
forever stopall
35+
forever start ./dist/app.js
36+
cd ..
37+
fi
38+
39+
if [ $FE_DIFF -ne 0 ] ; then
40+
echo "=== build frontend ==="
41+
cd frontend
42+
npm ci || exit 1
43+
npm run build || exit 1
44+
echo "=== update nginx serve files ==="
45+
# rm -rf ${{ secrets.NGINX_DIRECTORY }}
46+
sudo cp ./dist/* ${{ secrets.NGINX_DIRECTORY }}
47+
cd ..
48+
fi
49+
50+
echo "=== All done🚀 ==="
51+
EOF
52+
53+
- name: Setting ssh agent and key
54+
run: |
55+
echo "${{ secrets.PRIVATE_KEY }}" > $DEPLOY_KEY
56+
chmod 600 $DEPLOY_KEY
57+
58+
- name: Run script on ssh
59+
run: ssh -i $DEPLOY_KEY -o StrictHostKeyChecking=no ${{ secrets.SSH_ORIGIN }} < $DEPLOY_SCRIPT
60+
61+
- name: Cleanup
62+
if: ${{ always() }}
63+
run: |
64+
rm -f $DEPLOY_SCRIPT
65+
rm -f $DEPLOY_KEY

src/backend/game/Game.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import generateRandom from '@utils/generateRandom';
2+
import GameList from '@game/GameList';
13
import User from './User';
24

5+
const MAX_PLAYER = 6;
6+
37
export default class Game {
48
constructor(roomID) {
59
this.roomID = roomID;
@@ -12,25 +16,34 @@ export default class Game {
1216
};
1317
}
1418

15-
getRoomID() {
16-
return this.roomID;
17-
}
18-
19-
isPlaying() {
20-
return this.status.isPlaying;
19+
isEnterable() {
20+
if (this.status.isPlaying || this.users.size >= MAX_PLAYER) return false;
21+
return true;
2122
}
2223

23-
addUser({ socketID, nickname, color }) {
24+
addUser({ socketID }) {
25+
const nickname = generateRandom.nickname();
26+
const color = generateRandom.color();
2427
const user = new User({ socketID, nickname, color });
28+
GameList.addID(socketID, this);
2529
this.users.set(socketID, user);
30+
return user;
2631
}
2732

28-
findUserInfo(socketID) {
29-
const user = this.users.get(socketID);
30-
return user ? user.getUserProfile() : false;
33+
removeUser({ socketID }) {
34+
this.users.delete(socketID);
35+
GameList.removeID(socketID);
36+
if (this.users.size < 1) {
37+
GameList.removeID(this.roomID);
38+
}
3139
}
3240

33-
findUserInfoAll(socketID) {
34-
return this.users.get(socketID).getUserInfo() || null;
41+
getUser(socketID) {
42+
if (!this.users.has(socketID)) {
43+
console.log(`getUser: socketID ${socketID} does not exist`);
44+
return null;
45+
}
46+
47+
return this.users.get(socketID);
3548
}
3649
}

src/backend/game/GameList.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import generateRandom from '@utils/generateRandom';
2+
import Game from '@game/Game';
3+
4+
class GameList {
5+
constructor() {
6+
// roomID -> game
7+
// socketID -> game
8+
this.games = new Map();
9+
}
10+
11+
addID(ID, game) {
12+
this.games.set(ID, game);
13+
}
14+
15+
removeID(ID) {
16+
this.games.delete(ID);
17+
}
18+
19+
createGame() {
20+
const roomID = generateRandom.roomID();
21+
const game = new Game(roomID);
22+
this.addID(roomID, game);
23+
return roomID;
24+
}
25+
26+
getGame(ID) {
27+
const game = this.games.get(ID);
28+
if (!game) {
29+
console.log(`Game.findGame: can not find ${ID}`);
30+
return null;
31+
}
32+
33+
return this.games.get(ID);
34+
}
35+
36+
hasGame(roomID) {
37+
return this.games.has(roomID);
38+
}
39+
}
40+
41+
export default new GameList();

src/backend/game/Games.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/backend/game/User.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class User {
4949
};
5050
}
5151

52-
getUserProfile() {
52+
getProfile() {
5353
const { nickname, color } = this;
5454
return {
5555
nickname,

src/backend/routes/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import express from 'express';
2-
import Games from '@game/Games';
2+
import GameList from '@game/GameList';
33

44
const router = express.Router();
55

66
const getRoomsRouter = (req, res) => {
77
const { roomID } = req.params;
8-
if (Games.isEnterableRoom(roomID)) {
8+
const game = GameList.getGame(roomID);
9+
if (game && game.isEnterable(roomID)) {
910
return res.status(200).json({});
1011
}
1112

@@ -16,7 +17,7 @@ router.get('/rooms', getRoomsRouter);
1617
router.get('/rooms/:roomID', getRoomsRouter);
1718

1819
router.post('/rooms', (req, res) => {
19-
const roomID = Games.createGame();
20+
const roomID = GameList.createGame();
2021
return res.status(200).json({ roomID });
2122
});
2223

src/backend/sockets/chat.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import Games from '@game/Games';
2-
31
function onSendChat({ message }) {
42
const socket = this;
5-
const roomID = Games.findRoomID(socket.id);
6-
if (!roomID) return;
7-
8-
const { nickname } = Games.findUserInfo(socket.id);
9-
socket.in(roomID).emit('send chat', { message, nickname });
3+
// game = socket.game
4+
const { game } = socket;
5+
const { nickname } = game.getUser(socket.id).getProfile();
6+
socket.in(game.roomID).emit('send chat', { message, nickname });
107
}
118

129
export default function onChat(socket) {

src/backend/sockets/exitRoom.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function exitRoom() {
2+
const socket = this;
3+
socket.game?.removeUser(socket.id);
4+
console.log(`user disconnected ${socket.id}`);
5+
}
6+
7+
export default function onWaitingRoom(socket) {
8+
socket.on('disconnect', exitRoom);
9+
}

src/backend/sockets/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import io from 'socket.io';
22
import onWaitingRoom from './waitingRoom';
33
import onChat from './chat';
4+
import exitRoom from './exitRoom';
45

56
const socketIO = io();
67

78
socketIO.on('connection', (socket) => {
8-
console.log('a user connected');
9+
console.log(`User connected ${socket.id}`);
910

1011
onWaitingRoom(socket);
1112
onChat(socket);
13+
exitRoom(socket);
1214
});
1315

1416
export default socketIO;

src/backend/sockets/waitingRoom.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
import Games from '@game/Games';
1+
import GameList from '@game/GameList';
22

33
function onJoinPlayer({ roomID }) {
44
const socket = this;
5-
if (!roomID || !Games.isEnterableRoom(roomID)) return;
5+
const game = GameList.getGame(roomID);
6+
if (!game || !game.isEnterable(roomID)) return;
67

7-
Games.enterUser({ socketID: socket.id, roomID });
8-
const userInfo = Games.findUserInfo(socket.id);
8+
// User enter the room
9+
socket.game = game;
10+
const user = game.addUser({ socketID: socket.id, roomID });
911
socket.join(roomID);
12+
13+
// only sending to the client
1014
socket.emit('enter room', {
11-
...userInfo,
15+
...user.getProfile(),
1216
roomID,
1317
players: [],
1418
});
19+
20+
// TODO send 'update player' to other players in the room
1521
}
1622

1723
export default function onWaitingRoom(socket) {

src/backend/utils/generateRandom.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1+
import GameList from '@game/GameList';
12
import nickname from './nickname.json';
23

3-
const randomStrings = new Set();
4-
54
const generateRandomString = () =>
65
Math.random().toString(36).substr(2, 5).toUpperCase();
76

8-
export default {
7+
const randomFunctions = {
98
nickname: () => {
109
const { adjective: adj, noun } = nickname;
1110
const randomAdj = adj[Math.floor(Math.random() * adj.length)];
1211
const randomNoun = noun[Math.floor(Math.random() * noun.length)];
1312
return `${randomAdj} ${randomNoun}`;
1413
},
1514
color: () => '#222222',
16-
code: () => {
17-
let randomString = generateRandomString();
18-
while (randomStrings.has(randomString)) {
19-
randomString = generateRandomString();
20-
}
21-
randomStrings.add(randomString);
15+
roomID: () => {
16+
const randomString = generateRandomString();
17+
if (GameList.hasGame(randomString)) return randomFunctions.roomID();
2218
return randomString;
2319
},
2420
};
21+
22+
export default randomFunctions;

0 commit comments

Comments
 (0)