-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·179 lines (154 loc) · 4.69 KB
/
Copy pathapp.js
File metadata and controls
executable file
·179 lines (154 loc) · 4.69 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
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const helper = require('./helpers');
const port = process.env.PORT||3000
app.use(express.static('public'));
app.get('/', function(req, res) {
res.sendfile('index.html');
});
app.get('/player/', function(req, res) {
res.sendfile('player.html');
});
app.get('/host/', function(req, res) {
res.sendfile('host.html');
});
var users = {};
var connections = {};
var rooms = [ "" ];
var hosts = {};
io.on('connection', function(socket) {
/* All Clients */
add_to_users(socket);
/* End All Clients */
socket.on('disconnect', (data) => { disconnect(socket) });
/* / Host Function */
socket.on('create_room', (data) => { create_room(socket) });
/* End / Host Function */
/* / Players Function */
socket.on('create_player', (data) => { create_player(socket, data) });
/* End / Players Function */
/* /host/ Host Function */
socket.on('host_joined', (data) => { host_joined(socket) });
socket.on('round_reset', (data) => { round_reset(socket) });
socket.on('end_game', (data) => { end_game(socket) });
/* End /host/ Host Function */
/* /player/ Players Function */
socket.on('player_joined', (data) => { player_joined(socket) });
socket.on('player_buzzed', (data) => { player_buzzed(socket, data) });
socket.on('send_round_results', (data) => { send_round_results(socket, data) });
socket.on('player_connected', (data) => { player_connected(socket, data) });
/* End /player/ Players Function */
});
/* All Clients */
function add_to_users(socket) {
var user_info = user_obj(socket);
user_info = (user_info) ? user_info : { 'id': socket.id }
users[user_info['id']] = socket.id;
}
/* End All Clients */
/* / Host Function */
function create_room(socket) {
var room = "";
while ( room_exists(room) ) {
room = (Math.random() + 1).toString(36).substring(7).toLowerCase();
}
rooms.push(room);
socket.emit('host_room_response', {room});
}
/* End / Host Function */
/* / Players Function */
function create_player(socket, data) {
if (room_exists(data.room)) {
socket.emit('create_player_response');
}
}
/* End / Players Function */
/* /host/ Host Function */
function host_joined(socket) {
var user_info = user_obj(socket);
remove_host(user_info['id']);
hosts[user_info['room']] = user_info['id'];
join_room(socket);
}
function round_reset(socket) {
user_info = user_obj(socket);
io.in(user_info['room']).emit('round_reset');
}
function remove_host(host) {
if (helper.key_exists(hosts, host)) {
delete hosts[helper.get_key_by_value(hosts, host)];
}
}
function end_game(socket) {
user_info = user_obj(socket);
io.in(user_info['room']).emit('end_game');
remove_host(user_info['id']);
}
/* End /host/ Host Function */
/* /player/ Players Function */
function player_joined(socket) {
var user_info = user_obj(socket);
socket.join(user_info['room']);
if (room_exists(user_info['room'])) {
io.in(user_info['room']).emit('update_player_list', user_info);
} else {
socket.emit('end_game');
}
}
function player_buzzed(socket, data) {
var user_info = user_obj(socket);
user_info['time'] = data['time'];
io.in(user_info['room']).emit('player_buzzed', user_info);
}
function send_round_results(socket, data) {
data.forEach( (e, i) => {
socket.to( users[e['id']] ).emit('send_round_results', {i});
});
}
function player_connected(socket, data) {
user_info = user_obj(socket);
user_info['connected'] = data;
io.in(user_info['room']).emit('player_connected', user_info);
}
/* End /player/ Players Function */
/*
function connection(socket) {
var user_info = helper.str_obj(socket.handshake.headers.cookie);
if (user_info['id']) {
connections[socket.id] = user_info['id'];
}
}
*/
function clear_room(id) {
var key = helper.get_key_by_value(rooms, id);
}
function disconnect(socket) {
if (is_host(socket.id)) {
end_game(socket);
} else {
var user_info = user_obj(socket);
user_info['connected'] = false;
io.in(user_info['room']).emit('player_connected', user_info);
}
delete connections[socket.id];
}
/* Helper Functions */
function is_host(id) {
return helper.key_exists(rooms, id);
}
function user_obj(socket) {
return helper.str_obj(socket.handshake.headers.cookie);
}
function room_exists(room) {
return ( rooms.includes(room) ) ? true : false;
}
function join_room(socket) {
var user_info = helper.str_obj(socket.handshake.headers.cookie);
socket.join(user_info['room']);
}
/* End Helper Functions */
http.listen(port, function() {
console.log(`listening on localhost:${3000}`);
});