-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
256 lines (226 loc) · 7.44 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const app = require('express')();
const server = require('http').createServer(app);
const { Server } = require('socket.io');
const secretCodes = require('./db/secretCode.json').word;
/**
* Create socket io and setting cors for accessing from different url
*/
const io = new Server(server, {
cors: {
origin: 'http://localhost:8080',
methods: ['GET', 'POST'],
},
});
/**
* Constant object for indicating game stage
* @constant
* @type {string}
*/
const GAME_STAGE = {
PENDING: 'pending',
BEGINNING: 'beginning',
DAY: 'day',
NIGHT: 'night',
};
/**
* Constant object for indicating game status
* @constant
* @type {number}
*/
const GAME_STATUS_VALUE = {
CIVIL_WIN: 0,
MAFIA_WIN: 1,
MAFIA_NUM: 1,
CITIZEN: true,
MAFIA: false,
};
/**
* Constant number in catsInfo data
* @type {number}
*/
const CATS_NUMBER = 5;
/**
* Get random number in the range of 0 ~ maxNumber-1 (helper function)
* @param {number}
* @returns {number}
*/
const getRandomNumber = maxNumber => Math.floor(Math.random() * maxNumber);
/**
* Closure for cats data
* @returns {functions}
*/
const catsData = (() => {
const checkUserIndices = Array(5).fill(0);
const catsInfos = [
{ nickName: '오드아이', catImageUrl: './images/cats/cat2.png', jailCatImageUrl: './images/cats/cat2_jail.png' },
{ nickName: '삼색이', catImageUrl: './images/cats/cat3.png', jailCatImageUrl: './images/cats/cat3_jail.png' },
{ nickName: '샴', catImageUrl: './images/cats/cat5.png', jailCatImageUrl: './images/cats/cat5_jail.png' },
{ nickName: '고등어', catImageUrl: './images/cats/cat1.png', jailCatImageUrl: './images/cats/cat1_jail.png' },
{ nickName: '치즈', catImageUrl: './images/cats/cat4.png', jailCatImageUrl: './images/cats/cat4_jail.png' },
];
return {
getRandomCatInfo() {
// TODO
// checkUserIndices에 값을 설정하는 것과, 랜덤 유저 인덱스를 얻는것과, 캣의 정보를 리턴해주는 3가지 역할을 하고있음
let randomUserIndex = getRandomNumber(CATS_NUMBER);
while (checkUserIndices[randomUserIndex] !== 0) randomUserIndex = getRandomNumber(CATS_NUMBER);
checkUserIndices[randomUserIndex] = 1;
return catsInfos[randomUserIndex];
},
getCatsInfos() {
return catsInfos;
},
getCheckUserIndices() {
return checkUserIndices;
},
initializeCheckUserIndices() {
checkUserIndices.fill(0);
},
};
})();
/**
* Closure for game information
* @returns {functions}
*/
const gameInfo = (() => {
let currentUsers = [];
let citizens = [];
let mafia = [];
let jailCat = [];
let voteStatus = [];
let secretCode = '';
return {
getCurrentUsers() {
return currentUsers;
},
getCitizens() {
return citizens;
},
getMafia() {
return mafia;
},
getJailCat() {
return jailCat;
},
getVoteStatus() {
return voteStatus;
},
getSecretCode() {
return secretCode;
},
setJailCat(newJailCat) {
jailCat = [...jailCat, newJailCat];
},
setVoteStatus(newVoteStatus) {
voteStatus = newVoteStatus;
},
setGameStatus(randomIndex) {
citizens = [...currentUsers];
mafia = citizens[randomIndex];
citizens.splice(randomIndex, 1);
secretCode = secretCodes[getRandomNumber(secretCodes.length)];
},
add(socketId) {
// TODO
// add가 여러가지 역할해서 분리하는게 좋아보임
const catInfo = catsData.getRandomCatInfo();
currentUsers = [...currentUsers, { ...catInfo, socketId }];
return catInfo;
},
delete(disconnectedSocketId, catName) {
const emptyUserIndex = catsData.getCatsInfos().findIndex(catInfo => catInfo.nickName === catName);
catsData.getCheckUserIndices()[emptyUserIndex] = 0;
currentUsers = currentUsers.filter(user => user.socketId !== disconnectedSocketId);
},
initializegameInfo() {
currentUsers = [];
citizens = [];
mafia = [];
jailCat = [];
},
};
})();
/**
* Reset all game data
*/
const gameReset = () => {
catsData.initializeCheckUserIndices();
gameInfo.initializegameInfo();
};
/**
* Socket connect
*/
io.on('connection', socket => {
if (gameInfo.getCurrentUsers().length === 5) {
io.to(socket.id).emit('full room');
} else {
const catInfo = gameInfo.add(socket.id);
const { nickName, catImageUrl, jailCatImageUrl } = catInfo;
io.to(socket.id).emit('user update', [nickName, catImageUrl]);
io.emit('current users', gameInfo.getCurrentUsers());
socket.on('chat message', msg => {
io.emit('chat message', [nickName, catImageUrl, jailCatImageUrl, msg, socket.id]);
});
if (gameInfo.getCurrentUsers().length === 5) {
gameInfo.setGameStatus(getRandomNumber(CATS_NUMBER));
io.emit('change gameState', GAME_STAGE.BEGINNING, gameInfo.getCitizens().length, GAME_STATUS_VALUE.MAFIA_NUM);
setTimeout(() => {
gameInfo.getCitizens().forEach(civil => {
io.to(civil.socketId).emit('get secret-code', gameInfo.getSecretCode(), GAME_STATUS_VALUE.CITIZEN);
});
io.to(gameInfo.getMafia().socketId).emit('get mafia-code', '', GAME_STATUS_VALUE.MAFIA);
io.emit('change gameState', GAME_STAGE.DAY);
}, 6000);
}
const getMaxNum = nums => nums.reduce((acc, curr) => Math.max(acc, curr), nums[0]);
socket.on('day vote', selected => {
gameInfo.setVoteStatus([...gameInfo.getVoteStatus(), selected]);
if (gameInfo.getVoteStatus().length === gameInfo.getCurrentUsers().length - gameInfo.getJailCat().length) {
const voteCounts = new Map();
gameInfo.getVoteStatus().forEach(result => voteCounts.set(result, voteCounts.get(result) + 1 || 1));
const maxVote = getMaxNum([...voteCounts.values()]);
const isDraw = gameInfo.getVoteStatus().every(vote => !vote)
? true
: [...voteCounts.values()].filter(voteCount => voteCount === maxVote).length > 1;
const mostVoted = isDraw ? null : [...voteCounts.keys()].find(name => voteCounts.get(name) === maxVote);
const voteResult = [
mostVoted,
isDraw ? null : catsData.getCatsInfos().find(catInfo => catInfo.nickName === mostVoted).jailCatImageUrl,
];
if (!isDraw && mostVoted === gameInfo.getMafia().nickName) {
io.emit('game result', GAME_STATUS_VALUE.CIVIL_WIN, gameInfo.getMafia().nickName);
gameReset();
} else if (!isDraw) {
io.emit('vote result', voteResult);
gameInfo.setJailCat(mostVoted);
if (gameInfo.getCitizens().length - gameInfo.getJailCat().length < 3) {
io.emit('game result', GAME_STATUS_VALUE.MAFIA_WIN, gameInfo.getMafia().nickName);
gameReset();
} else {
io.emit('change gameState', GAME_STAGE.NIGHT);
}
} else {
io.emit('change gameState', GAME_STAGE.NIGHT);
}
gameInfo.setVoteStatus([]);
}
});
socket.on('night vote', selected => {
if (selected) {
gameInfo.setJailCat(selected);
io.emit('vote result', [
selected,
catsData.getCatsInfos().find(catInfo => catInfo.nickName === selected).jailCatImageUrl,
]);
}
io.emit('change gameState', GAME_STAGE.DAY);
});
socket.on('disconnect', () => {
gameInfo.delete(socket.id, nickName);
io.emit('user disconnect', gameInfo.getCurrentUsers());
});
}
});
server.listen(3000, () => {
console.log('listening on *:3000');
});