-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
112 lines (90 loc) · 2.3 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
const express = require('express')
const app = express()
const http = require("http").createServer(app)
const io = require("socket.io")(http)
const port = 3000
var gamesList = []
function setRoom(socket) {
var rooms = Object.keys(socket.rooms)
var sendTo = ""
for (var room of rooms) {
if (room != socket.id) {
sendTo = room
break
}
}
socket.room = sendTo
}
io.on("connection", function(socket) {
socket.on("createGame", function(name) {
gamesList.push(name)
io.emit("addToGameList", name)
})
socket.on("requestGamesList", function() {
socket.emit("showGamesList", gamesList.slice(0, 100))
})
socket.on("joinRoom", function(room) {
socket.join(room)
})
socket.on("updateCanvas", function(history) {
if (!socket.room) {
setRoom(socket)
}
socket.to(socket.room).emit("updateCanvas", history)
})
socket.on("sendMsg", function(msg) {
if (!socket.room) {
setRoom(socket)
}
io.to(socket.room).emit("newMsg", {
name: socket.name,
message: msg
})
})
socket.on("setName", function(name) {
socket.name = name
})
socket.on("joined", function(name) {
if (!socket.room) {
setRoom(socket)
}
io.to(socket.room).emit("joined", name)
})
socket.on("newRound", function() {
var room = io.sockets.adapter.rooms[socket.room]
var keys = Object.keys(room.sockets)
console.log(room)
if (room.length == 1) {
room.drawIdx = 0
}else if (socket.id == keys[room.drawIdx]) {
room.drawIdx++
room.drawIdx = room.drawIdx % keys.length
}
room.corrects = 0
io.to(keys[room.drawIdx]).emit("setDrawer")
})
socket.on("notDrawer", function() {
if (!socket.room) {
setRoom(socket)
}
socket.to(socket.room).emit("notDrawer")
})
socket.on("setAns", function(ans) {
socket.to(socket.room).emit("setAns", ans)
})
socket.on("correct", function() {
io.to(socket.room).emit("newMsg", {
name: `${socket.name} has answered correctly`,
message: ""
})
var room = io.sockets.adapter.rooms[socket.room]
room.corrects++
if (room.corrects == room.length - 1) {
io.to(socket.room).emit("callNewRound")
}
})
})
app.use(express.static(__dirname + "/public"))
http.listen(port, function() {
console.log("Running")
})