-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.html
258 lines (213 loc) · 8.98 KB
/
page.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
<!DOCTYPE html>
<html>
<head>
<title>Sockets Game!</title>
<meta charset='utf-8'>
<!-- Bootstrap dependencies -->
<link rel="stylesheet" type="text/css" href="./css/bootstrap.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="./js/bootstrap.js"></script>
<!-- ********************** -->
<!-- Socket dependencies -->
<script src="/socket.io/socket.io.js"></script>
<!-- ******************* -->
<style type="text/css">
.runButton {
height: 70px;
width: 100px;
margin-right: 10px;
}
.container {
width: 900px;
}
.userTrack {
margin-bottom: 10px;
}
.playersField {
width: 100%;
}
.playersField tr {
border-bottom: 1px solid lightgray;
}
.playersField tr td {
padding-top: 5px;
}
</style>
<script type="text/javascript">
var socket;
var user;
var raceStarted = false;
var socketInitialization = function(username){
var welcome = document.getElementById("welcome");
var allUsers = $("#users");
var progress = document.getElementById("progress");
var results = document.getElementById("results");
socket = io.connect('http://'+window.location.hostname+':3250');
socket.emit("userEnters", { 'username' : username });
socket.on("userAccepted", function(data){
user = data;
allUsers.append(buildUserTrack(user));
});
socket.on('updateUsers', function (data) {
for (var i=0; i < data.users.length; i++){
var userHomer = $("#user"+data.users[i].id);
if (userHomer.length > 0){
userHomer.animate({
"margin-left": data.users[i].clicks
}, 100);
}else{
// For table
allUsers.append(buildUserTrack(data.users[i]));
}
}
});
socket.on("removeUser", function(user) {
var userContent = $("#userContent"+user.id);
if (userContent.length > 0){
userContent.remove();
}
});
socket.on('update', function (data) {
progress.innerHTML = data.currentWidth;
progress.style.width = parseInt(data.currentWidth) + "px";
});
socket.on('win', function (data) {
raceStarted = false;
$("#winnerUsername").text(data.user.name);
$("#userWinModal").modal({
backdrop : 'static',
keyboard : false
});
});
socket.on('raceStarted', function(data){
showRaceCountModal();
});
socket.on('raceRestarted', function(){
raceStarted = false;
});
};
var buildUserTrack = function(user){
return '<tr id="userContent'+user.id+'"><td style="vertical-align:middle;"><span class="label label-success">'+user.name+'</span></td><td><div style="display:inline-block; width:273px;"><img id="user'+user.id+'" src="./img/run_homer_run.png" style="height:85px;"/></div><img src="./img/donut_transparent.png" style="height:85px; margin-left:-15px;"></td></tr>';
};
var showRaceCountModal = function(){
var raceCountModal = $("#raceCountModal");
raceCountModal.modal({
backdrop : 'static',
keyboard : false
});
var i = 5;
timer = setInterval(function(){
if (i>0){
$("#raceCount").text(i+"");
i = i-1;
}else{
clearInterval(timer);
$("#raceCount").text("Go go go!");
raceCountModal.modal('hide');
raceStarted = true;
generateKeyRace();
}
},1000);
};
var lastKeyRaceGenerated;
var generateKeyRace = function(){
// Calculate ASCII code for 'a' to 'z'
lastKeyRaceGenerated = Math.floor(Math.random() * (122 - 97 + 1)) + 97;
var keyRace = $("#keyRace");
keyRace.fadeOut(40);
keyRace.text(String.fromCharCode(lastKeyRaceGenerated));
keyRace.fadeIn(40);
};
$(document).keypress(function (eventObject){
if (raceStarted){
if (lastKeyRaceGenerated == eventObject.charCode){
sendClick();
generateKeyRace();
}
}
});
var sendClick = function(){
if (raceStarted)
socket.emit("click", user);
};
var startRace = function(){
socket.emit('startRace');
};
var restartRace = function(){
socket.emit('restartRace');
};
$(function(){
$('#usernameFormModal').modal({
backdrop: 'static',
keyboard: false
});
$("#usernameForm").submit(function(){
var usernameInput = $("#usernameInput");
socketInitialization(usernameInput.val());
$("#usernameFormModal").modal("hide");
// Stop the normal action
return false;
});
});
</script>
</head>
<body class="main">
<div id="raceCountModal" class="modal hide fade">
<div class="modal-body">
<h3>La carrera empieza en <span id="raceCount"></span></h3>
</div>
</div>
<div id="userWinModal" class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h1><span id="winnerUsername"></span> ganó la carrera!</h1>
</div>
<div style="text-align:center;" class="modal-body">
<img style="height:300px;" src="./img/homer-woohoo-big.jpg">
</div>
</div>
<div id="usernameFormModal" class="modal hide fade">
<div class="modal-header">
<h3>Bienvenido a Sockets Simpsons Game!</h3>
</div>
<div class="modal-body">
<form id="usernameForm">
<fieldset>
<label>Nick</label>
<input type="text" id="usernameInput" placeholder="Elige un nick original…" required>
</fieldset>
</form>
</div>
<div class="modal-footer">
<button onclick="$('#usernameForm').submit();" type="submit" class="btn">Jugar!</button>
</div>
</div>
<div class="container">
<div class="row-fluid">
<div class="span12 hero-unit well well-small">
<h2>Bienvenido a Sockets Simpsons Game!</h2>
<p>
<button onclick="startRace();" class="btn btn-primary">Empezar carrera!</button>
<button onclick="restartRace();" class="btn btn btn-danger">Reiniciar carrera!</button>
</p>
</div>
</div>
<div class="row-fluid">
<div style="padding-top: 4px; padding-bottom: 4px;" class="span2 well well-small">
<h4>Tecla: <span style="text-transform:uppercase;" id="keyRace"></span></h4>
</div>
<div style="padding-top: 4px; padding-bottom: 4px;" class="span9 well well-small">
<h5>Presiona la tecla que aparece para hacer correr a Homero</h5>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<table class="playersField">
<tbody id="users">
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>