-
Notifications
You must be signed in to change notification settings - Fork 0
/
hipstermeetup.js
101 lines (86 loc) · 3.71 KB
/
hipstermeetup.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
var net = require('net');
var fs = require('fs');
var server = net.createServer();
var meetups = JSON.parse(fs.readFileSync('./hipster_meetup.json'));
server.on('connection', function(client) {
console.log('client connected');
client.write("Welcome to Hipster Meetup!\nType \'help\' for a list of available commands. \n");
client.write("\n");
client.setEncoding('utf8');
client.on('data', function(stringFromClient) {
var splitInput = stringFromClient.trim().split("|")
if (splitInput[0] === "admin"){
//checks whether an admin is trying to use the app
if (splitInput[1] === "1"){ //P@s$w0rd
// checks whether the admin has put in the correct password -- begin admin features if password is correct
var command = splitInput[2];
switch (command){
case 'list':
// console.log(meetups[0].attendees);
if (!meetups.attendees){
client.write("There are no attendees for the upcoming meetup. \n")
} else{
meetups.attendees.forEach(function(attendee){
client.write(attendee.name + ": " + attendee.email + "\n");
})
client.end();
}
break;
case 'meetup':
var topic = splitInput[5];
var date = splitInput[3];
var time = splitInput[4];
meetups.topic = topic;
meetups.date = date;
meetups.time = time;
fs.writeFile('./hipster_meetup.json', JSON.stringify(meetups), "utf8");
client.write("You have successfully added " + topic + " at " + time + " on " + date + "!\n");
client.end();
break;
case 'clear':
fs.writeFile('./hipster_meetup.json', "{}", "utf8");
client.write("You have cleared all previous meetups! Please add a new meetup. \n");
client.end();
break;
default:
client.write("You typed an invalid command. \n");
}
} else {
client.write("You have entered an incorrect password. Please try again. \n");
client.end();
}
} else {
//begin regular user features
var command = splitInput[0];
switch (command){
case 'list':
client.write("The next Hipster Meetup is: " + meetups.topic + " at " + meetups.time + " on " + meetups.date + "\n");
client.end();
break;
case 'headcount':
if (!meetups.attendees){
client.write("No hipsters have RSPV\'d for the next meetup. You could be the first! \n");
} else {
client.write("There are currently " + meetups.attendees.length + " hipsters confirmed for the next Hipster Meetup. \n")
}
client.end();
break;
case 'RSVP':
meetups.attendees.push({name: splitInput[1], email: splitInput[2]});
fs.writeFile('./hipster_meetup.json', JSON.stringify(meetups), "utf8");
client.write("Thanks, " + splitInput[1] + "! You have successfully RSVP\'d to the next Hipster Meetup!\n");
client.end();
break;
case 'help':
client.write("Help menu. Here are the available commands:\n \'list\' -- Displays the details for the upcoming Hipster Meetup \n \'headcount\' -- Displays the number of confirmed hipsters for the next meetup \n \'RSVP\' -- RSVP for the upcoming Hipster Meetup! (Put in your info like this: \'RSVP|{full name|{email address}\'')\n")
client.end();
default:
client.write("You typed an invalid command. Type \'help\' for a list of available inputs.\n")
client.end();
}
}
});
});
server.listen(8124, function() {
console.log('Listening on port 8124');
});