-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
287 lines (283 loc) · 7.69 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'use strict';
const express = require('express');
const app = express();
const cookieParser = require('cookie-parser');
const uuid = require('uuid');
const words = require('./words.js'); // Import the words array from word.js
const PORT = process.env.PORT || 3000;
app.use(cookieParser());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
// Object to connect sessions to usernames
const sessions = {};
// Object to connect usernames to game state
const games = {};
// Allowlist of valid characters for username
const allowedUsername = /^[a-zA-Z0-9_]+$/;
function maskWord(word, guesses) {
if (!word) {
return "";
}
const maskedWord = word
.split("")
.map((letter) => (guesses.includes(letter) ? letter : "_"))
.map((letter) => (letter === " " ? " " : "_"))
.join(" ");
return maskedWord.trim();
}
function createGame(username) {
if (games[username]) {
return games[username];
}
const wordList = [...words];
const word = wordList.splice(Math.floor(Math.random() * wordList.length), 1)[0];
const guesses = [];
const game = {
word: word,
guesses: guesses,
incorrectGuesses: 0,
wordList: wordList,
previousGuessCorrect: null,
};
games[username] = game;
return game;
}
// Home page
app.get('/', (req, res) => {
const { sid } = req.cookies;
if (sid && sessions[sid]) {
// If user has a valid session id, show game state
const username = sessions[sid];
const game = games[username] || createGame(username);
let message = '';
if (game.previousGuessCorrect === true) {
message = 'Congratulations, you won!';
} else if (game.previousGuessCorrect === false) {
message = `Incorrect guess. The secret word was "${game.word}".`;
} else if (game.previousGuessCorrect === null && game.incorrectGuesses > 0) {
message = `Invalid guess "${game.currentGuess}".`;
} else if (game.incorrectGuesses === 6) {
message = `Game over. The secret word was "${game.word}".`;
} else if (game.incorrectGuesses > 0) {
message = `${6 - game.incorrectGuesses} guesses remaining.`;
}
const maskedWord = maskWord(game.word, game.guesses);
const previousGuesses = game.guesses.join(", ");
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Guess Game</title>
<link rel="stylesheet" type="text/css" href="/css/styles.css">
</head>
<body>
<header>
<h1>Game:</h1>
</header>
<main>
<p>Welcome, ${username}!</p>
<p>${maskedWord}</p>
<p>${message}</p>
<form method="POST" action="/guess">
<label for="guess">Guess a letter:</label>
<input type="text" id="guess" name="guess" pattern="[A-Za-z]{5}" maxlength="5" required>
<button type="submit">Guess</button>
</form>
<p>Previous guesses: ${previousGuesses}</p>
<form method="POST" action="/logout">
<button type="submit">Log out</button>
</form>
</main>
<footer>
We really don't care about your privacy,see our <a href="privacy.html">Privacy Policy</a>
</footer>
</body>
</html>
`);
} else {
// If user does not have a valid session id, show login page
res.send(
`
<!DOCTYPE html>
<html>
<head>
<title>Login page</title>
<link rel="stylesheet" type="text/css" href="/css/styles.css">
</head>
<body>
<header>
<h1>Game</h1>
</header>
<main>
<form method="POST" action="/login">
<label for="username">Enter a username:</label>
<input type="text" id="username" name="username" required>
<button type="submit">Register</button>
<button type="submit">Log in</button>
</form>
</main>
<footer>
We really don't care about your privacy,see our <a href="privacy.html">Privacy Policy</a>
</footer>
</body>
</html>
`);
}
});
// 登录路由
app.post('/login', (req, res) => {
const { username } = req.body;
// Check if username is valid
if (!allowedUsername.test(username) || username === 'dog') {
res.send(
`
<!DOCTYPE html>
<html>
<head>
<title>My App - Login Page</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<header>
<h1>Invalid username</h1>
<header>
<main>
<form method="POST" action="/login">
<p>Invalid username. Please enter a valid username.</p>
<label>Username:</label>
<input type="text" name="username" />
<button type="submit">Register</button>
<button type="submit">Login</button>
</form>
</main>
<footer>
We really don't care about your privacy,see our <a href="privacy.html">Privacy Policy</a>
</footer>
</body>
</html>
` );
return;
}
// Check if user already has a session
const { sid } = req.cookies;
if (sid && sessions[sid]) {
res.redirect('/');
return;
}
// Create a new session
const newSid = uuid.v4();
sessions[newSid] = username;
res.cookie('sid', newSid);
res.redirect('/');
});
// 登出路由
app.post('/logout', (req, res) => {
// 查找当前会话,并将其从会话存储中删除
const sid = req.cookies.sid;
if (sid) {
delete sessions[sid];
}
res.clearCookie('sid'); // 从cookie中删除sid
res.redirect('/');
});
// 进行guess的路由
app.post('/guess', (req, res) => {
const { sid } = req.cookies;
const username = sessions[sid];
const game = games[username];
const guess = req.body.guess;
// Only accept guess if user is logged in
if (!sid || !sessions[sid]) {
res.redirect('/');
return;
}
// Only accept guess if it hasn't been made before
if (game.guesses.includes(guess)) {
game.previousGuessCorrect = null;
res.redirect('/');
return;
}
game.guesses.push(guess);
if (game.word.includes(guess)) {
game.previousGuessCorrect = true;
} else {
game.incorrectGuesses += 1;
game.previousGuessCorrect = false;
}
// Check if the user has won
if (maskWord(game.word, game.guesses).replace(/\s/g, '') === game.word) {
res.cookie('sid', '', { maxAge: -1 });
delete games[username];
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>My App - Game</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<header>
<h1>Game page</h1>
<p>Congratulations, ${username}! You won!</p>
</header>
<main>
<form method="POST" action="/newgame">
<button type="submit">New Game</button>
</form>
<form method="POST" action="/logout">
<button type="submit">Logout</button>
</form>
</main>
<footer>
We really don't care about your privacy,see our <a href="privacy.html">Privacy Policy</a>
</footer>
</body>
</html>
`);
return;
}
// Check if the user has lost
if (game.incorrectGuesses === 6) {
res.cookie('sid', '', { maxAge: -1 });
delete games[username];
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>My App - Game</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<header>
<h1>Game page</h1>
<p>Sorry, ${username}, you lost. The secret word was "${game.word}".</p>
</header>
<main>
<form method="POST" action="/newgame">
<button type="submit">New Game</button>
</form>
<form method="POST" action="/logout">
<button type="submit">Logout</button>
</form>
</main>
<footer>
We really don't care about your privacy,see our <a href="privacy.html">Privacy Policy</a>
</footer>
</body>
</html>
`);
return;
}
res.redirect('/');
});
// Endpoint to create new game
app.post('/newgame', (req, res) => {
const { sid } = req.cookies;
const username = sessions[sid];
games[username] = createGame(username);
res.redirect('/');
});
app.listen(PORT, () => console.log(`Listening on http://localhost:${PORT}`));