-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
216 lines (156 loc) · 5.19 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
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
// Setup basic express server
var express = require('express');
var app = express();
var fs = require('fs');
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 3000;
var loopLimit = 0;
server.listen(port, function () {
console.log('Server listening at port %d', port);
fs.writeFile(__dirname + '/start.log', 'started');
});
// Routing
app.use(express.static(__dirname));
// Entire gameCollection Object holds all games and info
var gameCollection = new function() {
this.totalGameCount = 0,
this.gameList = []
};
function buildGame(socket) {
var gameObject = {};
gameObject.id = (Math.random()+1).toString(36).slice(2, 18);
gameObject.playerOne = socket.username;
gameObject.playerTwo = null;
gameCollection.totalGameCount ++;
gameCollection.gameList.push({gameObject});
console.log("Game Created by "+ socket.username + " w/ " + gameObject.id);
io.emit('gameCreated', {
username: socket.username,
gameId: gameObject.id
});
}
function killGame(socket) {
var notInGame = true;
for(var i = 0; i < gameCollection.totalGameCount; i++){
var gameId = gameCollection.gameList[i]['gameObject']['id']
var plyr1Tmp = gameCollection.gameList[i]['gameObject']['playerOne'];
var plyr2Tmp = gameCollection.gameList[i]['gameObject']['playerTwo'];
if (plyr1Tmp == socket.username){
--gameCollection.totalGameCount;
console.log("Destroy Game "+ gameId + "!");
gameCollection.gameList.splice(i, 1);
console.log(gameCollection.gameList);
socket.emit('leftGame', { gameId: gameId });
io.emit('gameDestroyed', {gameId: gameId, gameOwner: socket.username });
notInGame = false;
}
else if (plyr2Tmp == socket.username) {
gameCollection.gameList[i]['gameObject']['playerTwo'] = null;
console.log(socket.username + " has left " + gameId);
socket.emit('leftGame', { gameId: gameId });
console.log(gameCollection.gameList[i]['gameObject']);
notInGame = false;
}
}
if (notInGame == true){
socket.emit('notInGame');
}
}
function gameSeeker (socket) {
++loopLimit;
if (( gameCollection.totalGameCount == 0) || (loopLimit >= 20)) {
buildGame(socket);
loopLimit = 0;
} else {
var rndPick = Math.floor(Math.random() * gameCollection.totalGameCount);
if (gameCollection.gameList[rndPick]['gameObject']['playerTwo'] == null)
{
gameCollection.gameList[rndPick]['gameObject']['playerTwo'] = socket.username;
socket.emit('joinSuccess', {
gameId: gameCollection.gameList[rndPick]['gameObject']['id'] });
console.log( socket.username + " has been added to: " + gameCollection.gameList[rndPick]['gameObject']['id']);
} else {
gameSeeker(socket);
}
}
}
// Chatroom
var numUsers = 0;
io.on('connection', function (socket) {
var addedUser = false;
// when the client emits 'new message', this listens and executes
socket.on('new message', function (data) {
// we tell the client to execute 'new message'
socket.broadcast.emit('new message', {
username: socket.username,
message: data
});
});
// when the client emits 'add user', this listens and executes
socket.on('add user', function (username) {
if (addedUser) return;
// we store the username in the socket session for this client
socket.username = username;
++numUsers;
addedUser = true;
socket.emit('login', {
numUsers: numUsers
});
// echo globally (all clients) that a person has connected
socket.broadcast.emit('user joined', {
username: socket.username,
numUsers: numUsers
});
});
// when the client emits 'typing', we broadcast it to others
socket.on('typing', function () {
socket.broadcast.emit('typing', {
username: socket.username
});
});
// when the client emits 'stop typing', we broadcast it to others
socket.on('stop typing', function () {
socket.broadcast.emit('stop typing', {
username: socket.username
});
});
// when the user disconnects.. perform this
socket.on('disconnect', function () {
if (addedUser) {
--numUsers;
killGame(socket);
// echo globally that this client has left
socket.broadcast.emit('user left', {
username: socket.username,
numUsers: numUsers
});
}
});
socket.on('joinGame', function (){
console.log(socket.username + " wants to join a game");
var alreadyInGame = false;
for(var i = 0; i < gameCollection.totalGameCount; i++){
var plyr1Tmp = gameCollection.gameList[i]['gameObject']['playerOne'];
var plyr2Tmp = gameCollection.gameList[i]['gameObject']['playerTwo'];
if (plyr1Tmp == socket.username || plyr2Tmp == socket.username){
alreadyInGame = true;
console.log(socket.username + " already has a Game!");
socket.emit('alreadyJoined', {
gameId: gameCollection.gameList[i]['gameObject']['id']
});
}
}
if (alreadyInGame == false){
gameSeeker(socket);
}
});
socket.on('leaveGame', function() {
if (gameCollection.totalGameCount == 0){
socket.emit('notInGame');
}
else {
killGame(socket);
}
});
});