-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (218 loc) · 7.45 KB
/
index.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const WebSocket = require('ws')
const fs = require('fs');
let trackIndex = 0
let trackArray
let timerDefault = 600
let warningTimerDefault = 20
let timer = timerDefault
let warningTimer = warningTimerDefault
const wss = new WebSocket.Server({ port: 8080 })
let ws
let players = []
let chatlog = [];
let currentID = 1
fs.readdir('./tracks/', (err, files) => {
trackArray = files
shuffle(trackArray);
});
wss.on('connection', (socket, req) => {
ws = socket
ws.binaryType = 'arraybuffer'
ws.on('message', function message(data, isBinary) {
if (isBinary == true) {
// Nothing to see here...
} else {
const message = isBinary ? data : data.toString();
var responseJSON = JSON.parse(message)
// if (responseJSON.hasOwnProperty('username')) {
if (Object.keys(responseJSON)[0] == 'username') {
responseJSON.username = responseJSON.username.replaceAll('"', '')
responseJSON.username = responseJSON.username.replaceAll('{', '')
responseJSON.username = responseJSON.username.replaceAll('}', '')
responseJSON.username = responseJSON.username.replaceAll('`', '')
responseJSON.username = responseJSON.username.replaceAll(`'`, '')
if (responseJSON.username.length > 32 || responseJSON.username.length < 1) {
ws.close()
return
}
ws.send(`{ "ID" : "${currentID}"}`)
ws.send(`{ "chat" : ${JSON.stringify(chatlog)} }`)
players.push({username : responseJSON.username, time : 999999, IP : req.socket.remoteAddress, ID : currentID})
currentID = currentID + 1
sendTrack(ws)
updateLeaderboard()
console.log(`User ${responseJSON.username} has connected`)
}
if (Object.keys(responseJSON)[0] == 'replay') {
if (warningInterval) return
/* var buffer = Buffer.from(responseJSON.replay, 'base64'); // Ta-da
var view = new Uint8Array(buffer)
var dec1 = view[25]
var dec2 = view[24]
var timeWord = `${("00000000"+dec1.toString(2)).slice(-8)}${("00000000"+dec2.toString(2)).slice(-8)}`
var time = parseInt(timeWord, 2);
var matches = players.filter(obj => {
return obj.username === responseJSON.username
})
matches.forEach(function (currentValue, index, arr){
if (currentValue.IP == req.socket.remoteAddress) {
if (currentValue.ID == responseJSON.ID) {
if (currentValue.time > time) {
currentValue.time = time
}
}
}
}) */
var buffer = Buffer.from(responseJSON.replay, 'base64'); // Ta-da
var view = new Uint8Array(buffer)
var dec1 = view[51]
var dec2 = view[50]
let timeHundredths = dec1 * 1280 + dec2 * 5
let time = timeHundredths * 10
var matches = players.filter(obj => {
return obj.username === responseJSON.username
})
matches.forEach(function (currentValue, index, arr){
if (currentValue.IP == req.socket.remoteAddress) {
if (currentValue.ID == responseJSON.ID) {
if (currentValue.time > time) {
currentValue.time = time
console.log(`User ${responseJSON.username}'s new best time is ${time}`)
}
}
}
})
updateLeaderboard()
}
if (Object.keys(responseJSON)[0] == 'disconnect') {
var matches = players.filter(obj => {
return obj.username === responseJSON.username
})
matches.forEach(function (currentValue, index, arr){
if (currentValue.IP == req.socket.remoteAddress) {
if (currentValue.ID == responseJSON.ID) {
players.splice(players.indexOf(currentValue), 1)
console.log(`User ${responseJSON.username} has disconnected`)
}
}
})
updateLeaderboard()
}
if (Object.keys(responseJSON)[0] == 'message') {
chatlog.push(`${responseJSON.username}: ${responseJSON.message}`)
wss.broadcast(`{ "chat" : ${JSON.stringify(chatlog)} }`)
}
}
// Continue as before.
});
ws.on('close', function() {
// Code here
})
})
wss.broadcast = function(broadcastMessage) {
wss.clients.forEach(client => client.send(broadcastMessage));
};
function str2ab(str) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i=0, strLen=str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function updateLeaderboard() {
players.sort(function (a, b) {
return a.time - b.time;
});
var playersForClient = players.map(({ IP, ...item }) => item);
wss.broadcast(`{"leaderboard" : ${JSON.stringify(playersForClient)}}`)
}
function dec2bin(dec) {
return (dec >>> 0).toString(2);
}
function _base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
function shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
// Sends current track as Uint8Array ArrayBuffer to client
function sendTrack() {
let trackBase64 = fs.readFileSync(`./tracks/${trackArray[trackIndex]}`, null).toString('base64');
ws.send(`{ "track" : "${trackBase64}", "name" : "${trackArray[trackIndex]}" }`)
console.log(`Sent track "${trackArray[trackIndex]}"`)
}
function broadcastTrack() {
let trackBase64 = fs.readFileSync(`./tracks/${trackArray[trackIndex]}`, null).toString('base64');
wss.broadcast(`{ "track" : "${trackBase64}", "name" : "${trackArray[trackIndex]}" }`)
console.log(`Broadcasted track "${trackArray[trackIndex]}"`)
}
function sendWarning() {
wss.broadcast(`{ "warning" : "when the imposter is sus!" }`)
}
/* function intervalFunction() {
wss.clients.forEach(client => client.send(`{ "timer" : ${timer} }`));
timer--
if (timer === 0) {
trackIndex++
if (typeof trackArray[trackIndex] == 'undefined') {
trackIndex = 0
}
sendTrack()
players.forEach(function (element, index) {
element.time = 999999
})
updateLeaderboard()
timer = 420
}
} */
function intervalFunction() {
if (timer < 0) {
if (warningTimer < 0) {
clearInterval(warningInterval)
warningInterval = null
warningTimer = warningTimerDefault
trackIndex++
if (typeof trackArray[trackIndex] == 'undefined') {
trackIndex = 0
}
broadcastTrack()
players.forEach(function (element, index) {
element.time = 999999
})
updateLeaderboard()
timer = timerDefault
} else {
if (!warningInterval) {
sendWarning()
warningInterval = setInterval(warningIntervalFunction, 1000)
}
}
}
if (!warningInterval) {
wss.clients.forEach(client => client.send(`{ "timer" : ${timer} }`));
timer--
}
}
function warningIntervalFunction() {
wss.clients.forEach(client => client.send(`{ "timer" : ${warningTimer} }`));
warningTimer--
}
let interval = setInterval(intervalFunction, 1000)
let warningInterval