Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
199 lines (168 loc) · 4.55 KB
/
Copy pathserver.js
File metadata and controls
199 lines (168 loc) · 4.55 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
198
199
const express = require('express')
const port = process.env.PORT || 3000
var app = require('express')()
var http = require('http').Server(app)
var io = require('socket.io')(http)
/*
* Serve /dist/ folder
*/
app.use(express.static(__dirname + '/dist'))
app.get(/.*/, (req, res) => {
res.sendFile(__dirname + '/dist/index.html')
})
http.listen(port, () => {
console.log(`Listening on port ${port}`)
})
/*
* Store connected clients etc.
* Do not use in production 🤪
*/
var clients = []
var counter = 0
/*
* Init Position of the Brick
*/
var position = {
x: 200,
y: 200
};
var player1Score = 0;
var player2Score = 0;
var player1selected = false;
var player2selected = false;
var currentQuestion = null;
var questions = [
{
"category":"General Knowledge",
"type":"multiple",
"difficulty":"easy",
"question":"Which sign of the zodiac is represented by the Crab?",
"correct_answer":"Cancer",
"incorrect_answers":[
"Libra",
"Virgo",
"Sagittarius"
]
},
{
"category":"General Knowledge",
"type":"multiple",
"difficulty":"easy",
"question":"What is the name of the Jewish New Year?",
"correct_answer":"Rosh Hashanah",
"incorrect_answers":[
"Elul",
"New Year",
"Succoss"
]
},
{
"category":"General Knowledge",
"type":"multiple",
"difficulty":"easy",
"question":"What is the Spanish word for "donkey"?",
"correct_answer":"Burro",
"incorrect_answers":[
"Caballo",
"Toro",
"Perro"
]
}
]
io.on('connection', (socket) => {
/*
* ✨ Handle new connected client
*/
console.log(`Client ${socket.id} connected to the server.`)
// Push new connected socket to socketList
clients.push({ id: socket.id })
// Emit the updated client list to *ALL* connected clients.
io.emit('update_clients', clients)
// Emit Brick Position
socket.emit("position", position);
socket.on("move", data => {
switch(data) {
case "left":
position.x -= 5;
io.emit("position", position);
break;
case "right":
position.x += 5;
io.emit("position", position);
break;
case "up":
position.y -= 5;
io.emit("position", position);
break;
case "down":
position.y += 5;
io.emit("position", position);
break;
}
})
// Emit the current counter *ONLY* to the new connected client.
// Refer to https://socket.io/docs/emit-cheatsheet/ for the difference
// of `io.emit` and `socket.emit`
socket.emit('update_counter', counter)
/*
* 👂 Listen to socket events emitted from vue components
*/
socket.on('next_question', () => {
//TODO: Fehler beim erstellen des allAnswer Arrays, es wird jedesmal die korrekte Lösung hinzugefügt...
currentQuestion = questions[Math.floor(Math.random() * questions.length)];
let allAnswers = []
allAnswers.push(currentQuestion.incorrect_answers)
allAnswers[0].push(currentQuestion.correct_answer)
currentQuestion.allAnswers = allAnswers[0];
io.emit('update_question', currentQuestion)
})
socket.emit('update_player1', player1selected)
socket.emit('update_player2', player2selected)
socket.on('select_player1', () => {
player1selected = true
socket.emit('update_player1', player1selected)
})
socket.on('select_player2', () => {
player2selected = true
socket.emit('update_player2', player2selected)
})
socket.emit('update_score1', player1Score)
socket.emit('update_score2', player2Score)
socket.on('increment_score1', () => {
player1Score ++
socket.emit('update_score1',player1Score)
})
socket.on('increment_score2', () => {
player2Score ++
socket.emit('update_score2',player2Score)
})
socket.on('reset_score1', () => {
player1Score = 0
socket.emit('update_score1',player1Score)
})
socket.on('reset_score2', () => {
player2Score = 0
socket.emit('update_score2',player2Score)
})
// Listen to increment_counter event, fired by `increment()` in 'Counter.vue'
socket.on('increment_counter', () => {
counter += 1
io.emit('update_counter', counter)
})
// Listen to reset_counter event, fired by `reset()` in 'Counter.vue'
socket.on('reset_counter', () => {
counter = 0
io.emit('update_counter', counter)
})
// Listen to disconnect event. 'disconnecting' is a reserved event,
// again refer to https://socket.io/docs/emit-cheatsheet/
socket.on('disconnecting', () => {
// Remove the disconnected client from the client list
clients = clients.filter((client) => {
return client.id != socket.id
})
// Emit the updated client list to all connected clients *EXCEPT* sender.
socket.broadcast.emit('update_clients', clients)
console.log(`Client ${socket.id} disconnected from the server.`)
})
})