-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
218 lines (177 loc) · 6.35 KB
/
app.js
File metadata and controls
218 lines (177 loc) · 6.35 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"use strict";
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const path = require("path");
app.set('port', process.env.PORT || 3005);
app.use(express.static(__dirname + '/public'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/app.html'));
});
app.use((req, res) => {
res.type('text/plain');
res.status(404);
res.send('404 - Not Found');
});
app.use((err, req, res, next) => {
console.log(err);
res.type('text/plain');
res.status(500);
res.send('500 - Server Error');
});
// Store all rooms here
var currentRooms = [];
const addUniqueToArray = (array, thing) => {
if(array.indexOf(thing) === -1) return array.push(thing);
return array;
}
const getCurrentRoom = (rooms) => {
var currentRoom = '';
for (let key in rooms) {
if (key.slice(0, 2) === "ID") {
currentRoom = key;
}
}
if(currentRoom === '') {
currentRoom = "could not find room"
}
return currentRoom;
}
const getRooms = (socketRooms) => {
var roomsAndClients = socketRooms, rooms = {};
for (let key in roomsAndClients) {
var first3char = key.slice(0,3);
if (first3char === "ID-") {
rooms[key] = roomsAndClients[key];
}
}
return rooms;
}
const getOpenRooms = (rooms) => {
var openRooms = [];
for (let key in rooms) {
var occupants = rooms[key].length;
if (occupants < 2) {
openRooms.push(key);
}
}
return openRooms;
}
const getCurrentPlayers = (currentRoom, socket) => {
var currentPlayers = [], currentClients;
currentClients = io.sockets.adapter.rooms[currentRoom].sockets;
for (let key in currentClients) {
currentPlayers.push(key);
}
return currentPlayers;
}
// getOtherPlayer()
// gets the other players ID
const getOtherPlayer = (currentRoom, playerID, socket) => {
var currentPlayers = [], currentClients, playerSocketID;
playerSocketID = playerID;
console.log('excluding ', playerSocketID)
currentClients = io.sockets.adapter.rooms[currentRoom].sockets;
for (let key in currentClients) {
if(playerSocketID !== key) {
currentPlayers.push(key);
}
}
return currentPlayers[0];
}
const removeRoom = (rooms, roomID) => {
var rooms = rooms;
var roomIdIndex = rooms.indexOf(roomID);
rooms.splice(roomIdIndex, 1);
return rooms;
}
io.on('connection', (socket) => {
console.log("socket connection established: " + socket.id);
console.log("current open rooms are: ", currentRooms);
console.log("this current socket is in: ", socket.rooms)
socket.emit('gamelist:all', currentRooms);
socket.on('game:play', (data) => {
// only emit plays clients in the same room
var currentRoom = getCurrentRoom(socket.rooms);
io.to(currentRoom)
.emit('game:play', data);
});
socket.on('game:state', (data) => {
console.log('game state of', socket.id , 'is', data);
//broadcast the state to the other client connected in the same room
var gameState = {}
gameState.state = data;
var currentRoom = getCurrentRoom(socket.rooms);
socket.to(currentRoom)
.emit('game:state', gameState);
});
socket.on('connect:host', (gameID) => {
// WHAT DOES THIS EVEN DO MATE?
console.log('connecting to ' + gameID);
var rooms = getRooms(io.sockets.adapter.rooms);
console.log('rooms: ', rooms);
var openRooms = getOpenRooms(rooms); // instead of getting all rooms
console.log('openRooms: ', openRooms);
socket.join(gameID, () => {
if (Object.keys(rooms).length === 0) {
console.log('sending gameID')
addUniqueToArray(currentRooms, gameID);
io.emit('gamelist:added', [gameID]);
} else {
console.log('sending found ID');
addUniqueToArray(currentRooms, gameID);
io.emit('gamelist:added', openRooms);
}
})
});
socket.on('connect:join', (gameID) => {
socket.join(gameID, () => {
var currentRoom = getCurrentRoom(socket.rooms);
// console.log('connect:join - socket.rooms: ', socket.rooms);
// console.log('connect:join - io.rooms: ', io.sockets.adapter.rooms)
var joiningID = socket.id;
var hostID = getOtherPlayer(currentRoom, joiningID, socket);
socket.emit('player:host', hostID);
io.emit('gamelist:removed', removeRoom(currentRooms, gameID));
socket.to(currentRoom).emit('player:joined', joiningID);
});
});
socket.on('disconnect:game', function(gameID) {
socket.leave(gameID, () => {
console.log('leaving');
});
});
socket.on('disconnect', function () {
console.log('disconnected');
var rooms = getRooms(io.sockets.adapter.rooms);
var openRooms = getOpenRooms(rooms); // instead of getting all rooms
if(openRooms.length) {
openRooms.forEach((room) => {
// ADDINIG WHEN ANY PLAYER DISCONNECTS
io.emit('gamelist:added', openRooms);
var otherPlayer = getOtherPlayer(room, socket.id, socket)
console.log('getOtherPlayer: ', otherPlayer);
// Instead of looping through all rooms
// loop through the rooms the current socket is inside
console.log('checking if can get currentRooms: ', io.sockets.adapter.rooms[room].sockets)
if (otherPlayer) {
console.log('ADDING TO CURRENT ROOMS');
addUniqueToArray(currentRooms, room);
} else {
let currentRoomIndex = currentRooms.indexOf(room);
currentRooms.slice(currentRoomIndex, currentRoomIndex + 1);
console.log(currentRooms);
}
// IF THERE IS ONLY ONE PERSON LEFT WE SHOULD NOT PUSH
});
} else {
currentRooms = [];
}
console.log('rooms: ', rooms);
console.log('openRooms: ', openRooms);
});
});
http.listen(app.get('port'), () => {
console.log('express started on ' + app.get('port'));
});