This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·167 lines (139 loc) · 4.14 KB
/
app.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
/*** skatroom app.js ***/
/* express */
var express = require('express');
var app = express();
app.use(express.json());
app.use(express.urlencoded());
app.use(express.cookieParser('secretskatroomwowowow'));
app.use(express.static(__dirname + '/public'));
/* stall number to stall name enum */
var STALL_NUMBER_TO_NAME = {
1: 'chipotle',
2: 'taco bell',
3: 'arbys',
4: 'country kitchen buffet',
5: 'tgi fridays',
6: 'Applebees'
};
// index
app.get('/', function(req, resp) {
resp.sendfile(__dirname + '/views/index.html');
});
// dashboard (for desktop showing message activity)
app.get('/dashboard', function(req, resp) {
resp.sendfile(__dirname + '/views/dashboard.html');
});
// log in
app.get('/login/', function(req, resp) {
resp.sendfile(__dirname + '/views/login.html');
});
// log off
app.get('/logoff', function(req, resp) {
resp.clearCookie('skatroom');
resp.sendfile(__dirname + '/views/login.html');
});
// receive tp data - IF THIS DOESN'T WORK WITH MAX, TRY CHANGING APP.GET TO APP.POST ON NEXT LINE :-*
app.post('/post', function(req, resp) {
var tpData = {
stallNumber : req.param('stallNumber'),
tpAmount : req.param('tpAmount')
}
console.log('tp data received: ', tpData);
var stallName = STALL_NUMBER_TO_NAME[tpData.stallNumber];
var username = "";
connectedUsers.forEach(function(userData) {
if (userData.stall === stallName) {
username = userData.nickname;
}
else {
username = 'jenn testing skatroom (lol)';
}
});
//<username> used <tpAmount> sheets of toilet paper in stall <stall_name>
var message = {
nick: 'skatroom',
text: '<strong>' + username + '</strong> used ' + tpData.tpAmount + ' sheets of toilet paper in stall <strong>' + stallName + '</strong>',
timestamp: Date.now()
};
io.sockets.emit('message', { chat: message });
addToChatHistory(message);
//resp.writeHead(200, { 'Content-Type': 'application/json', "Access-Control-Allow-Origin":"*" });
//resp.write("{success: true}");
//resp.end();
});
// send history
app.get('/history', function(req, resp) {
console.log('history requested');
resp.send(chatHistory);
})
// receive login post data
app.post('/', function(req, resp) {
var data = {
nickname : req.body.nickname,
stall : req.body.stall.replace(/%20/g,' '),
authentic : true
};
dataJSON = JSON.stringify(data);
resp.cookie('skatroom', dataJSON);
resp.sendfile(__dirname + '/views/index.html');
});
/* socket.io */
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var connectedUsers = [];
var chatHistory = [];
var CHAT_BACKLOG_LENGTH = 10;
server.listen(4321);
io.sockets.on('connection', function (socket) {
io.sockets.emit('history', chatHistory);
console.log('connection made');
socket.on('send', function (data) {
console.log('data sent: ', data);
if ( data.chat ) {
// send messages or users
io.sockets.emit('message',data);
addToChatHistory(data.chat);
}
else if ( data.activeUser ) {
connectedUsers.push({
nickname: data.activeUser,
clientID: socket.id,
stall: data.userStall
});
io.sockets.emit('message', { updateRoom: connectedUsers });
}
});
socket.on('disconnect', function (data) {
// remove user from room list
var i = 0;
var removalIndex;
while ( i < connectedUsers.length ) {
if ( connectedUsers[i].clientID == socket.id ) {
removalIndex = i;
i = connectedUsers.length + 1;
}
else {
i++;
}
}
var leaveMessage = {
nick: 'skatroom',
color: 'gray',
text: connectedUsers[removalIndex].nickname + ' has left the room',
timestamp: Date.now()
};
addToChatHistory(leaveMessage);
// send message that user left
io.sockets.emit('message', { chat: leaveMessage });
// send new user list
connectedUsers.splice(removalIndex, 1);
io.sockets.emit('message', { updateRoom: connectedUsers });
});
});
/** helpers **/
function addToChatHistory(data) {
if (chatHistory.length >= CHAT_BACKLOG_LENGTH) {
chatHistory.splice(0,1);
}
chatHistory.push(data);
}