-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
230 lines (196 loc) · 6.86 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
<!doctype html>
<html>
<head>
<title>Socket Game</title>
<script src="/socket.io/socket.io.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/cv/js/sender/v1/cast_sender.js"></script>
<script src="/gc.js"></script>
<link href='http://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet' type='text/css'>
<style>
html, body {margin:10px 0 0 0; padding:0; background:#2C3E50; color:#ECF0F1; font-family: 'Lato', sans-serif;}
.container {margin:0 auto; width:96%; text-align:center;}
.title {font-size:3em; color:#E74C3C; text-transform:capitalize;}
.main button {display: block; border:none; outline:none; padding:15px 30px; color:#ECF0F1; font-size:2em;}
.main button:hover {cursor: pointer;}
button.top {background: #2980B9; margin: 30px auto;}
button.bottom {background: #3498DB; margin: 0 auto;}
.initial-hide {display:none;}
input {text-transform:capitalize; margin: 0 0 20px 0; font-size: 2em; outline: none; border: none; width: 200px; color: #2C3E50; text-align: center;}
.game {text-align: center;}
.aa, .bb, .cc, .dd {margin:20px 0; width: 100%;}
.aa {background: #3498DB;}
.bb {background: #2ECC71;}
.cc {background: #E74C3C;}
.dd {background: #F1C40F;}
.disabled {background-color: #ECF0F1; color:#fff;}
</style>
</head>
<body>
<div class="container">
<h1 class="title">Welcome</h1>
<div class="main">
<div class="welcome">
<button class="top" data-route="host">New Game</button>
<button class="bottom" data-route="player">Join Game</button>
</div>
<div class="name initial-hide">
<input type="text" maxlength="8" id="username" placeholder="name" autocomplete="off">
<button class="bottom" style="display:none;" id="continue">Ready!</button>
</div>
<div class="lobby initial-hide">
<div class="message">Waiting on more players.<br>Other players: <span id="pc">0</span></div>
<button class="top" style="display:none;" id="start">Play</button>
</div>
<div class="game initial-hide">
<button class="aa disabled" data-ans="one">A</button>
<button class="bb disabled" data-ans="two">B</button>
<button class="cc disabled" data-ans="three">C</button>
<button class="dd disabled" data-ans="four">D</button>
</div>
</div>
</div>
<script>
var socket = io();
var player = {id: 0, name: "", role: ""};
var players = [];
var playerCount = function() {return players.length};
var status, answered;
var answerQueue = [];
$(document).ready(function(){
// welcome
$('.welcome button').on("click", function() {
player.role = $(this).data('route');
$('.welcome').hide();
$('.name').css({display: "block"});
});
// username
$("#username").on('keyup',function () {
if ($(this).val()) {
$('#continue').show();
}
else {
$('#continue').hide();
}
});
// lobby
$('#continue').on("click", function() {
player.name = $("#username").val();
$("#username").off('keyup');
$('.name').hide();
$('.title').text(player.name);
$('.lobby').css({display: "block"});
if(player.role === "host") {
createGame();
} else {
joinGame();
}
});
// game
$('#start').on('click', startGame());
});
// create game - this user is the host
function createGame() {
// generate user id
var d = new Date();
var t = d.getTime().toString().slice(-7);
player.id = t + "-" + player.name;
players.push(player);
// TODO: Connect to chromecast
castMessage({action: 'message', msg: 'Waiting for players'});
// listen for new players joining game
socket.on('setup', function(data){
//create new player
var newPlayer = {id: data.id, name: data.name, role: "player"};
players.push(newPlayer);
$('#pc').text(playerCount() - 1);
// confirm creation
socket.emit('setup complete', {id: newPlayer.id, playerCount: playerCount()});
//update chromecast
castMessage({action: 'players', msg: players});
// can we start?
if (playerCount() > 1) {
$('#start').fadeIn();
} else {
$('#start').fadeOut();
}
});
socket.on ('question answer', function(data) {
answerQueue.push(data);
});
}
function startGame() {
$('.lobby').hide();
$('.game').show();
if (player.role === 'host') {
socket.emit('start game', 'go');
castMessage({action: 'timer', msg: 'Starting game'});
var counter = 11;
var timer = setInterval(function() {
counter--;
if(counter <= 1) {
clearInterval(timer);
playGame();
} else {
var msg = 'Starting game in ' + counter;
castMessage({action: 'timer', msg: msg});
}
}, 1000);
}
socket.on('game question', function(data){
castMessage({action:'question', msg: 'Question ' + data.questionNumber, question: data.question, });
showQuestion(data);
});
}
function playGame(data) {
var question = questions[i];
socket.emit('game question', question);
}
function showQuestion(data) {
$('.title').text('Question ' + data.questionNumber);
enableButtons();
}
// join game - this user is a player
function joinGame() {
// generate user id
var d = new Date();
var t = d.getTime().toString().slice(-7);
player.id = t + "-" + player.name;
// send player data
socket.emit('setup', player);
// confirm and update player count
socket.on('setup complete', function(data){
if(data.id === player.id) {
status = 'connected';
$('#pc').text(data.playerCount - 1);
} else {
$('#pc').text(data.playerCount - 1);
}
});
// listen for game start
socket.on('start game', function() {
startGame();
});
}
function enableButtons() {
answered = false;
$('.game button').removeClass('disabled');
// listen for answer
$('.game button').on('click', function(){
if (!answered) { // only allow user to answer once
var ans = $(this).data('ans');
// send answer
socket.emit('question answer', {id: player.id, answer: ans});
answered = true;
disableButtons();
}
});
}
function disableButtons() {
$('.game button').addClass('disabled');
$('.game button').off('click');
}
</script>
<script src="/questions.js"></script>
</body>
</html>