-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·146 lines (126 loc) · 3.91 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
/* LOAD ALL DEPENDENCIES
----------------------------------------- */
//google calander api key AIzaSyB12EjnA2KlxqiDxHzXfmwzcz7bz8nLO4U
const express = require('express')
const path = require('path')
const session = require('express-session')
const bodyParser = require('body-parser')
const app = express()
const WebSocket = require('ws')
const http = require('http')
const hexRgb = require('hex-rgb')
require('dotenv').config();
const port = process.env.PORT || 3000;
/* MONGODB CONFIGURATION
----------------------------------------- */
const MongoClient = require("mongodb").MongoClient;
const dbConfig = process.env.MONGODB_URI;
MongoClient.connect(dbConfig, (err, database) => {
if (err) return console.log(err)
db = database
});
/* SESSIONS CONFIGURATION
----------------------------------------- */
app.use(session({
secret: "JA1d82JHYF9?nsdfDF635MuHe#ksd",
resave: false,
saveUninitialized: true
}));
/* BODY-PARSER FOR READING POST REQUESTS
----------------------------------------- */
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
/* Routes
----------------------------------------- */
const indexRouter = require('./routes/index.js')
const accountRouter = require('./routes/account.js')
const islandsRouter = require('./routes/islands.js')
const dashboardRouter = require('./routes/dashboard.js');
app
.set('view engine', 'ejs')
.use(express.static('public'))
.use('/', indexRouter)
.use('/account', accountRouter)
.use('/islands', islandsRouter)
.use('/dashboard', dashboardRouter)
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use('/', indexRouter)
app.use('/account', accountRouter)
const server = http.createServer(app);
const ws = new WebSocket.Server({
server
});
ws.on('connection', socketConnectionMade);
server.listen(port, () => {
console.log('Started server on http://localhost:' + port)
})
function socketConnectionMade(socket) {
socket.on('message', function(message) {
ws.clients.forEach(function(client) {
client.send(message);
})
handleMessage(JSON.parse(message))
})
}
let lastSender = ''
function handleMessage(message) {
const islandCollection = db.collection('islands');
const userCollection = db.collection('users');
// find user info based on boxId
const boxId = JSON.stringify(message.boxId)
userCollection.findOne({
boxId: boxId
}, function(err, user) {
'found user'
if (user.type == 'junior') {
console.log('type = junior')
lastSender = user
// find island where user is a junior of
islandCollection.find({}, {}).toArray(function(err, islands) {
islands.forEach(function(island) {
island.juniors.forEach(function(junior) {
if (junior == user.username) {
const senior = island.senior
// find user info of senior of island
userCollection.findOne({
username: senior
}, function(err, foundSenior) {
ws.clients.forEach(function(client) {
client.send(
JSON.stringify({
type: 'changecolor',
r: hexRgb(user.color)[0],
g: hexRgb(user.color)[1],
b: hexRgb(user.color)[2],
recipient: foundSenior.boxId,
recipientType: 'senior'
})
);
})
});
}
})
});
})
}
// send response from senior to client
if (user.type == 'senior') {
console.log('type = senior')
ws.clients.forEach(function(client) {
client.send(
JSON.stringify({
type: 'changecolor',
r: message.r,
g: message.g,
b: message.b,
recipient: lastSender.boxId,
recipientType: 'junior'
})
);
})
}
});
}