This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (54 loc) · 1.86 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
import express from 'express';
import { createServer } from 'node:http';
import { Server } from 'socket.io'
const app = express();
const server = createServer(app);
const io = new Server(server);
app.use(express.static('static'))
var users = []
var submissions = []
app.get('/', (req, res) => {
res.sendFile('./index.html', {root: '.'});
});
io.on('connection', (socket) => {
console.log('new user connected! userID: ' + socket.id);
const playerCount = users.length;
var newUser = new Object();
newUser.id = socket.id;
newUser.name = socket.id;
users[playerCount] = newUser;
console.log(users);
io.emit('player update', users);
socket.on('disconnect', () => {
console.log('user disconnected... :( ' + socket.id);
var targetUser = users.find((element) => element.id == socket.id)
users.splice(users.indexOf(targetUser), 1); //remove 1 item at index of id
io.emit('player update', users);
console.log(users);
});
});
io.on('connection', (socket) => {
console.log("name change watcher ready");
socket.on('name change', (pkg) => {
console.log("Name change: " + pkg.id + " changed name to " + pkg.name);
var targetIdx = users.findIndex((element) => element.id == pkg.id);
users[targetIdx] = pkg;
console.log("USERS!");
console.log(users);
io.emit('player update', users);
});
});
io.on('connection', (socket) => {
socket.on('ready up', (readyData) => {
console.log(readyData.senderName + " has readied up! (" + (submissions.length+1) + '/' + users.length + ')');
if (submissions.push(readyData) == users.length) {
console.log(submissions);
io.emit('results', (submissions));
console.log("All users ready, releasing results!");
submissions = []
}
});
});
server.listen(3000, () => {
console.log('server running at http://localhost:3000');
});