This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketServer.js
174 lines (166 loc) · 4.46 KB
/
socketServer.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
const EventEmitter = require('events')
const ticker = new EventEmitter()
const PORT = 8081
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server, { origins: '*:*'}),
path = require('path');
server.listen(PORT);
console.log("Socket Server started on "+PORT)
let names = {}
let votes = {}
let timer = {
totalTime: 0,
timeElapsed: 0,
running: false,
color: 'red'
}
let donationList = {}
setInterval(() => { ticker.emit('tick') }, 1000)
ticker.on('tick', function tick () {
if (!timer.running) return null
else if (timer.timeElapsed >= timer.totalTime) {
timer.running = false
timer.timeElapsed = 0
} else {
timer.timeElapsed++
}
emitTimer()
})
let page = '/'
let score = new Array(5).fill(0).map(() => ({red: 0, blue: 0}))
let slideNumber = 0
let donations = []
io.on('connection', function (socket) {
names[socket.id] = socket.id
emitNames()
socket.on('name', function (data) {
names[socket.id] = data.name
emitNames()
emitPage()
emitDonations()
emitScore()
})
socket.on('disconnect', function () {
delete names[socket.id]
emitNames()
})
socket.on('refresh', emitNames)
socket.on('pushDonation', function pushDonation (value) {
console.log(`Donation of $${value} recieved`)
donations.push({
amount: Number(value),
time: Date.now()
})
emitDonations()
})
socket.on('popDonation', function popDonation () {
donations.splice(-1)
emitDonations()
})
socket.on('requestToDonate', function requestToDonate () {
let name = names[socket.id]
donationList[name] = function release () {
delete donationList[name]
socket.emit('releaseDonation')
emitDonationList()
}
emitDonationList()
})
socket.on('releaseDonationTable', function releaseTable (table) {
if (table === null) {
for (table in donationList) {
donationList[table]()
}
} else {
donationList[table]()
}
})
socket.on('setColor', function setColor (data) {
timer.color = data.color
emitUpdateColor()
})
socket.on('setPage', function setPage (data) {
page = data.page
emitPage()
})
socket.on('startVote', function startVote () {
votes = {}
Object.values(names).filter((e) => e !== 'screen' && e !== 'admin' && e.length < 20).forEach(element => {
votes[element] = null
})
io.emit('votesOpened')
emitVoteResults()
socket.on('closeVote', function closeVote () {
emitVoteResults()
io.emit('votesClosed')
})
})
socket.on('setTimer', function setTimer (totalTime) {
timer.totalTime = totalTime
emitTimer()
})
socket.on('startTimer', function startTimer () {
timer.running = true
emitTimer()
})
socket.on('pauseTimer', function pauseTimer () {
timer.running = false
emitTimer()
})
socket.on('endTimer', function endTimer () {
timer.running = false
timer.timeElapsed = 0
emitTimer()
})
socket.on('vote', function (vote) {
let name = names[socket.id]
if (votes.hasOwnProperty(name)) {
votes[name] = Number(vote)
}
emitVoteResults()
})
socket.on('setSlideshow', function setSlideshow (data) {
slideNumber = Number(data)
emitslideShow()
})
socket.on('setScore', function setScore ([event, color, value]) {
console.log(`setting score of ${color} team for event ${event} from ${score[event][color]} to ${value}.`)
score[event][color] = value
emitScore()
})
socket.on('setFullScore', function setFullScore (newScore) {
score = newScore
emitScore()
})
})
function emitUpdateColor () {
io.emit('colorUpdate', {color: timer.color})
}
function emitNames (socket) {
io.emit('roster', { roster: names, page })
}
function emitPage () {
io.emit('pageUpdate', {page})
}
function emitScore () {
io.emit('scoreUpdate', score)
}
function emitslideShow () {
io.emit('slideshowUpdate', slideNumber)
}
function emitDonations () {
let total = donations.reduce((p, n) => p + n.amount, 0)
console.log(`Total donations is $${total}`)
io.emit('donationsUpdate', total)
}
function emitTimer () {
io.emit('timerUpdate', { timeElapsed: timer.timeElapsed, totalTime: timer.totalTime, isPlaying: timer.running })
}
function emitVoteResults () {
io.emit('voteResultsUpdate', votes)
}
function emitDonationList () {
io.emit('updateDonationList', Object.keys(donationList))
}