-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
153 lines (124 loc) · 3.77 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
var globals = require('./globals.js');
var helpers = require('./helpers.js');
var votehelpers = require('./votehelpers.js');
var admincmds = require('./admincmds.js');
var displaycmds = require('./displaycmds.js');
var playercmds = require('./playercmds.js');
//check the various collections exist if not create them
globals.Presets.findOne({type: "love", name: "default"}).then((doc)=> {
if(doc == null)
{
console.log("creating default parameters");
globals.Presets.insert({name: "default", options: globals.LoveParameters});
}
});
helpers.loadSettings(); //make native
//check whether any existing users rejoin
setTimeout(function()
{
globals.UserData.find({connected: true}, '_id').then((docs)=>
{
if(docs != null)
{
for(var i = 0; i < docs.length; i++)
{
if(globals.sockets[docs[i]._id] == undefined) //no socket has been created
{
globals.UserData.update({_id: docs[i]._id}, {$set: {connected: false}});
}
}
}
});
},3000);
setInterval(function()
{
time = Date.now();
var k = Object.keys(globals.checkins);
for(var i = 0; i < k.length; i++)
{
var delta = time - globals.checkins[k[i]];
if(globals.DEBUG)console.log(k[i], delta);
if(delta > 20000) //20 secs = dormant
{
if(globals.DEBUG)console.log(k[i] + " is dormant");
try
{
if(helpers.validateId(k[i]))
{
globals.UserData.update({_id: k[i]},{$set: {connected: false}})
.catch((err)=>{
console.log("Error - checkins : " + err);
globals.checkins[k[i]];
});
delete globals.checkins[k[i]];
}
else
{
// delete globals.checkins[k[i]];
console.log(globals.checkins[k[i]]);
throw k[i] + " is not a valid id";
}
}
catch (e)
{
console.log("Error - checkins: " + e);
}
}
}
}, 5000);
//We define a route handler / that gets called when we hit our website home.
globals.app.use("/admin",express.static(__dirname + "/admin"));
globals.app.use("/style",express.static(__dirname + "/style"));
globals.app.use("/libs",express.static(__dirname + "/libs"));
globals.app.use("/player",express.static(__dirname + "/player"));
globals.app.use("/tests",express.static(__dirname + "/tests"));
globals.app.use("/display",express.static(__dirname + "/display"));
globals.app.use("/samples",express.static(__dirname + "/samples"));
globals.app.use("/images",express.static(__dirname + "/images"));
globals.app.use("/fonts",express.static(__dirname + "/fonts"));
//three types of user
globals.app.get('/admin', function(req, res){
res.sendFile(__dirname + '/admin/admin.html');
});
//
globals.app.get('/display', function(req, res){
res.sendFile(__dirname + '/display/display.html');
});
//
globals.app.get('/tests', function(req, res){
res.sendFile(__dirname + '/tests/tests.html');
});
//
globals.app.get('/', function(req, res){
res.sendFile(__dirname + '/player/player.html');
});
globals.admin.on('connection', admincmds.response);
globals.display.on('connection', displaycmds.response);
globals.players.on('connection', playercmds.response);
//We make the http server listen on port 3000.
http.listen(3000, function(){
console.log('listening on *:3000');
});
//////////////OSC LISTENER////////////////////////////////
globals.udpPort.on('message', (msg, rinfo) => {
if(msg.address == "/poll")
{
globals.display.emit('cmd', { type: 'update', id: msg.args[0], val: msg.args[1]});
}
if(msg.address == "/phraseComplete")
{
votehelpers.handlePhraseComplete(msg).catch((err)=>{
console.log(err);
});
}
if(msg.address == "/resumeVote") //TODO change name
{
globals.players.emit('cmd',{cmd: 'resume_vote'});
//allows other votes to happen
globals.currentConcludedVote = null;
}
if(msg.address == "/winSampleDone")
{
votehelpers.concludeDisplayAndPlayers();
}
});