forked from LeoBadeaux/majorityrules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
197 lines (167 loc) · 5.43 KB
/
server.js
File metadata and controls
197 lines (167 loc) · 5.43 KB
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
const express = require('express')
const fs = require('fs')
//Remove when pushed to production
/**/const WebSocket = require('ws');
/**/const wss = new WebSocket.Server({ port: 8085 });
const app = express()
// set the view engine to ejs
app.set('view engine', 'ejs');
app.use(express.static('public'));
// set the view engine to ejs
app.set('json spaces', 2);
// index page
app.get('/', function(req, res) {
res.render('home');
});
app.get('/admin', function(req, res) {
res.render('admin');
});
// Api Routes
app.get('/api/schedule', function(req, res) {
res.sendFile(__dirname + '/private/schedule.json');
});
app.get('/api/config', function(req, res) {
res.sendFile(__dirname + '/private/config.json');
});
app.get('/api/admin/config', function(req, res) {
res.sendFile(__dirname + '/private/adminConfig.json');
});
app.get('/api/users/me', function(req, res) {
res.sendFile(__dirname + '/private/me.json');
});
app.get('/api/leaderboard', function(req, res) {
res.sendFile(__dirname + '/private/leaderboard.json');
});
app.listen(8080);
console.log('Server is listening on port 8080');
//Got lazy and didn't want to mess with a secondary server via a diffrent file
//So I decided to add the socket to this file
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
//console.log(data);
}
});
};
wss.on('connection', (ws) => {
ws.on('message', (data) => {
//console.log('data received \n ' + data)
wss.clients.forEach(function(client) {
client.send(data.toString());
});
})
})
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
//console.log(data);
}
});
};
//when a client connects, console.log a message
wss.on('connection', function connection(ws) {
fs.readFile(__dirname + '/private/schedule.json', 'utf8', function (err, data) {
if (err) throw err;
var schedule = JSON.parse(data);
wss.broadcast(JSON.stringify({
type: "gameStatus",
prize: (schedule.prizeCents/100).toLocaleString('en-US', { style: 'currency', currency: schedule.currency }),
prizeCents: schedule.prizeCents,
prizePoints: schedule.prizePoints,
isWinnerTakeAll: schedule.isWinnerTakeAll,
playingState: "pregame",
questionInfo: {
current: 0,
total: 12,
},
vipGameInfo: {
prizeCents: 0,
prizePoints: 0,
},
extraLife: false,
eraser: false,
vip: false,
"ts": Date.now(),
}));
});
});
wss.broadcast(JSON.stringify({
"type": "gameStatus",
"prize": 0,
"prizeCents": 0,
"prizePoints": 0,
"playingState": "pregame",
"ts": Date.now(),
}));
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
var msg = JSON.parse(message);
try {
if (msg.type == "question") {
incrementSeconds();
wss.broadcast(JSON.stringify({
type: "question",
question: msg.text.toString(),
totalQuestionCount: 12,
currentQuestion: 5,
isPrizeQuestion: false,
totalTimeMs: msg.totalTimeMs,
totalTimeLeftMs: msg.totalTimeMs,
ts: new Date().toISOString(),
c: 1
}));
}
}
catch (e) {
console.log(e);
}
});
});
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
//console.log('received: %s', message);
var msg = JSON.parse(message);
if (msg.type == "prizeReveal") {
wss.broadcast(JSON.stringify({
type: "dynamicPotAnimation",
ts: isoDate,
currency: msg.prizeInfo.currency,
currentPrizeCents: Math.floor(parseFloat(msg.prizeInfo.prizeStr.replace("$", "").replace(",", "")) * 100),
currentPrizePoints: 0,
sent: isoDate,
c: 1
}));
}
});
});
var seconds = 0;
function incrementSeconds() {
seconds += Math.floor(Math.random() * 734) + 1;;
//console.log(seconds);
}
var cancel = setInterval(incrementSeconds, 2000);
function broadcastStats() {
let counter = 1;
while (counter <= 10) {
//console.log(counter);
counter++;
}
wss.broadcast(JSON.stringify({
type: "broadcastStats",
ts: Date.now(),
statusMessage: "",
viewerCounts: {
connected: wss.clients.size,
playing: wss.clients.size,
watching: 0,
},
c: 1,
sent: Date.now()
}));
}
setInterval(broadcastStats, 5000);
wss.on('error', function(error) {
console.log(error);
});