-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
212 lines (184 loc) · 6.08 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
const ejs = require('ejs');
const bodyParser = require('body-parser');
const fs = require('fs');
// server stuff
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
// random name stuff
const randomWords = require('random-words');
const rita = require('rita');
// read data from the JSON file
let usersFile = fs.readFileSync('users.json');
let chatFile =fs.readFileSync('chat.json');
//this line parses the raw data to make it readable and interprets it as a JavaScript object
let usersData = JSON.parse(usersFile); // users data contains: {userid, username, avatar} of multiple users
let chatData = JSON.parse(chatFile); // chat data contains: {userid, username, avatar, message}
let currentId = usersData.length; // keep track of number of users signed up
// parse stuff from front end
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
////////////////// INDEX PAGE ///////////////////////
// the same page as login, chat
// if user authorized, send chat page
// if not, render log in
app.get('/', function(req, res){
res.render('login', {error: null});
});
app.post('/', function(req, res){
let username = req.body.username;
let password = req.body.password;
let userFound = false;
let userIndex = null;
// find if username exist
for (let i=0; i<usersData.length; i++){
// if exist, move on to check password
if (usersData[i].username === username){
userFound = true;
userIndex = i;
}
}
// found user after loop, compare password
if (userFound){
if (password === usersData[userIndex].password){
// move on to voting if everything matches!
res.render('chat', {userid: usersData[userIndex].userid, username: usersData[userIndex].username, avatar: usersData[userIndex].avatar});
}
else{
res.render('login', {error: 'Wrong password! Oops!'});
}
}
// no such username, render error
else{
res.render('login', {error: 'No username found! Try again!'});
}
});
///////////////// SIGN UP PAGE ///////////////////////
// writing to users files
// drawing to avatars
// linking to log in page
app.get('/signup', function(req, res){
// sign up
res.render('signup', {success: null, error: null, userid: null, username: null});
});
app.post('/signup', function(req, res){
let username = req.body.username;
let password = req.body.password;
let confirm_password = req.body.confirm_password;
let repeatUsername = false; // checking if username already exist
// check for validity
// check if username already exist
for (let i=0; i<usersData.length; i++){
if (usersData[i].username === username){
repeatUsername = true;
}
}
// if repeat, render error
if (repeatUsername){
res.render('signup', {success: null, error: 'Username already exists, try again!', userid: null, username: null});
}
// no repeat, move on to check password
else{
// check if passwords are empty
if (password===''){
res.render('signup', {success: null, error: "Password cannot be empty!", userid: null, username: null});
}
// check two passwords are the same
else if (password===confirm_password){
// password matched!
// assign an userid
// write a default avatar src
let userid = currentId;
usersData.push({userid: userid, username: username, password: password, avatar: "https://www.fillmurray.com/500/500"});
let finalUserData = JSON.stringify(usersData);
fs.writeFile('users.json', finalUserData, finished);
function finished(err) {
currentId += 1; // one more user!
console.log('file written: '+req.body);
// let user draw
res.render('signup', {success: "registered!", error: null, userid: userid, username: username});
}
}
// if two passwords are not the same
else{
res.render('signup', {success: null, error: "Passwords don't match, try again!", userid: null, username: null});
}
}
});
///////////////////////////////////////// Random Name Stuff ///////////////////////////////////////////////////////
function newName(){
// name grammar:
// 2 or 3 words, must be adj & noun
let wordCount = Math.round(Math.random())+2; // randomly generate 2 or 3
console.log(wordCount+' word name')
let name = '';
let wordsArr = [];
while (wordsArr.length < wordCount){
let word = randomWords();
if (rita.isNoun(word) || rita.isAdjective(word)){
wordsArr.push(word);
}
}
for (let i=0; i<wordsArr.length; i++){
name += capitalize(wordsArr[i]) +' ';
}
let newName = name.trim();
console.log(newName);
return newName;
}
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
////////////////////////////////////////// Socket.io ////////////////////////////////////////////
io.on('connection', function(socket){
console.log('a user connected');
socket.on('generate name', function(data){
data.newName = newName();
io.emit('generate name', data);
});
socket.on('send avatar', function(data){
io.emit('send avatar', data);
let userid = data.userid;
let avatar = data.avatar;
changeAvatar(userid, avatar);
});
/*
socket.on('get chat', function(data){
data.chatData = chatData;
io.emit('get chat', data);
})
*/
socket.on('send canvas', function(data){
let userid = data.userid;
let message = data.message;
let username = usersData[userid].username;
let avatar = usersData[userid].avatar;
// send new info to client side
data.username = username;
data.avatar = avatar;
//saveMessage(data); // save to chat data
io.emit('send canvas', data);
});
});
function changeAvatar(userid, avatar){
usersData[userid].avatar = avatar;
// write files
let finalUserData = JSON.stringify(usersData);
fs.writeFile('users.json', finalUserData, changed);
function changed(err) {
console.log('changed avatar for '+usersData[userid].username);
}
}
function saveMessage(data){
// write into chat file
chatData.push(data);
let finalChatData = JSON.stringify(chatData);
fs.writeFile('chat.json', finalChatData, updatedChat);
function updatedChat(err) {
console.log('saved a message from ' + data.username);
}
}
server.listen(3000, function(){
console.log('listening on port 3000');
});