-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
323 lines (211 loc) · 8.5 KB
/
index.html
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Kickster</title>
<link rel="stylesheet" href="main.css">
<link href='http://fonts.googleapis.com/css?family=Lobster' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Lato:100,300,400,700,900' rel='stylesheet' type='text/css'>
<script src="https://cdn.socket.io/socket.io-1.3.4.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
var colorArray = ["#0AC2D2", "#FDC162", "#60D7A9", "#E91E63", "#b554c6"];
var randomNum = Math.floor((Math.random() * colorArray.length));
var userColor = colorArray[randomNum];
$("body").css("background-color", userColor);
$("#message").css("background-color", userColor);
$("#messageSection").css("background-color", userColor);
$("#header").css("background-color", userColor);
////////////////////
//GLOBAL VARIABLES//
////////////////////
var socket = io.connect('http://localhost:3000');
var userCount = [];
var userObj = {};
var timeout;
//////////////
//CONNECTION//
//////////////
//start connection
socket.on("connect", function() {
console.log("connected");
// window.addEventListener("keydown", function(e) {
// if (e.keyCode == 13) sendMsg();
// });
});
$('#start').click(function() {
var nameValue = $('#name').val();
var name;
if (nameValue=="") {
var nameArray = ["anonymous cow", "anonymous duck", "anonymous chicken", "anonymous blueberry", "anonymous carrot"];
var randomName = Math.floor((Math.random() * nameArray.length));
name = nameArray[randomName];
var id = socket.id;
userCount.push(id);
console.log(userCount.length);
socket.emit('joinRoom', {name: name, id: id, length: userCount.length});
$('#name').empty();
} else {
name = nameValue;
var id = socket.id;
userCount.push(id);
console.log(userCount.length);
socket.emit('joinRoom', {name: name, id: id, length: userCount.length});
$('#name').empty();
}
});
socket.on('join', function(userObj) {
if (userObj[socket.id]) {
if (userObj[socket.id].length < 4) {
$("#startButtons").css('display', 'none');
$("#chat").css("display", "block");
$("#header").append($("<p></p>").html("Please wait for all chatty cathies..."));
} else if (userObj[socket.id].length == 4) {
$("#startButtons").hide();
$("#chat").css("display", "block");
socket.emit('ready', userObj);
console.log(userObj[socket.id].name);
console.log(userObj[socket.id].length);
console.log(userObj[socket.id].leader);
}
} else {
//to get here for testing - try to get into full room
$("#slogan").empty();
$("#slogan").html("Sorry, the room is full! Try again?");
$("#start").empty();
$("#start").html("Try Again").addClass("redo");
$('#logo').attr("src", "images/full.svg");
//REFRESH PAGE
$(".redo").click(function() {
console.log("refresh");
window.location.href = 'index.html';
});
}
});
//////////
//TIMERS//
//////////
function counter() {
socket.emit('reset');
socket.on('resetReturn', function(userObj) {
timeout = setTimeout(showButtons, 30000, userObj);
})
}
//////////////
//GAME START//
//////////////
socket.on('ready', function(userObj) {
// GAME STUFF
socket.emit(console.log("ALL GAME NOW PLEASE"));
$("#messageSection").css("display", "block");
$("#header").empty();
counter();
});
//////////////
//GROUP CHAT//
//////////////
function sendMsg() {
var id = socket.id;
var msg = $('#message').val();
var msgPack = {id: id, msg: msg};
socket.emit('chat message', msgPack);
$('#message').val('');
return false;
}
socket.on('chat', function(msgDeliver){
$('#allMessages').append($('<div></div>').addClass('messageDiv').text(msgDeliver.name + ": " + msgDeliver.msg));
$("#allMessages").scrollTop($("#allMessages")[0].scrollHeight);
});
$('#send').click(function() {sendMsg()});
///////////////
//LEADER KICK//
///////////////
function showButtons(userObj) {
console.log('buttons?');
clearTimeout(timeout);
kick();
function kick() {
var newCount = 0;
for (var number in userObj) {
newCount ++;
}
console.log(newCount);
$("#header").empty();
if (userObj[socket.id].leader == true) {
if (newCount > 1) {
for (var number in userObj) {
if (userObj[number].leader == false) {
$('#header').append($("<div></div>").addClass('button kickerButton').text(userObj[number].name).attr('id', userObj[number].id));
}
}
$('.kickerButton').click(function() {
nameID = this.id;
$('#header').empty();
socket.emit('kickUser', nameID);
setTimeout(counter, 1000);
})
}
else {
console.log("GAME OVER");
//to get here for testing- kick everyone out of the room
$('#header').empty();
$('#chat').css("display", 'none');
$("#startButtons").css('display', 'block');
$("#logo").attr("src", "images/sad.svg");
$("#title").html("THATS IT!");
$
$("#slogan").html("You\'ve kicked everyone out! Are you a little lonely now?");
$("#nameInput").remove();
// $('#startButtons').append("<div id='refresh' class='button'> Try Again </div>");
// //REFRESH PAGE
setTimeout(lastRefresh, 5000);
function lastRefresh() {
window.location.href = 'index.html';
}
}
} else {
$('#header').append($("<div></div>").addClass('The kickster could kick you out at any time...'));
//GAME OVER
}
}
}
socket.on('kickFinished', function(userObj, kickedID) {
console.log("KICK FINISH");
if (userObj[socket.id].kicked == true) {
location.replace("kicked.html");
} else if (userObj[socket.id].kicked == false) {
$('#allMessages').append($("<div></div>").text(userObj[kickedID].name+" has been kicked!"));
console.log(userObj[kickedID].name+" has been kicked!");
}
});
////////////////////////
//DISCONNECTION NOTICE//
////////////////////////
socket.on('exitNotify', function(data) {
console.log(data);
})
});
</script>
</head>
<body>
<div id="startButtons">
<img id="logo" src="images/kick.svg"/>
<h1 id="title">KICKSTER</h1>
<p id="slogan">If you're lucky enough to be the kickster, you get to kick your friends out of the chat!<p/>
<div id="nameInput">
<input type="text" id="name" maxlength="20" placeholder="Name" required autofocus/>
<button class="button" id="start"> Start </button>
</div>
</div>
<div id="chat">
<div id="header">
</div>
<div id="allMessages"></div>
<div id="messageSection">
<textarea id="message" maxlength="200" placeholder="Your Message" required></textarea>
<div class="button" id="send"> Send </div>
</div>
</div>
</body>
</html>