-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
211 lines (186 loc) · 6.28 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
<head>
<title>family</title>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<link rel="apple-touch-icon" href="./icon-180.png" />
<link rel="icon" sizes="192x192" href="./icon-192.png" />
<link
href="https://fonts.googleapis.com/css?family=Roboto:400,700"
rel="stylesheet"
/>
</head>
<body>
<div id="loading">Loading...</div>
<div id="setup" class="hidden">
<input
type="text"
name="thing"
placeholder="thing"
autocorrect="off"
autocomplete="off"
autocapitalize="off"
autofill="off"
id="input"
/>
<button id="add-person" type="button">add</button>
<button id="start-game" type="button">
start <span id="count"></span>
</button>
</div>
<div id="game" class="hidden">
<ul id="names"></ul>
<button id="finish-game" type="button">done</button>
</div>
<style>
html {
font-size: 5vw;
font-family: 'Roboto', sans-serif;
box-sizing: border-box;
}
* {
font-size: inherit;
box-sizing: inherit;
}
* + * {
margin-top: 0.5em;
}
body {
margin: 2em;
}
@media only screen and (min-width: 47em) {
html {
font-size: 2vw;
}
body {
width: 50vw;
margin: 0 auto;
}
}
ul {
list-style-type: none;
padding: 0;
}
button {
display: block;
width: 100%;
border: none;
background-color: cadetblue;
color: white;
padding: 0.5em 1em;
}
input {
display: block;
width: 100%;
border-radius: 3px;
padding: 0.5em 1em;
}
.hidden {
display: none;
}
</style>
<script src="https://www.gstatic.com/firebasejs/5.6.0/firebase.js"></script>
<script>
// ==================
// Constants
// ==================
var ENTER_KEY = 13
var ELEMENTS = {
addPerson: document.getElementById('add-person'),
startGame: document.getElementById('start-game'),
finishGame: document.getElementById('finish-game'),
input: document.getElementById('input'),
setupDiv: document.getElementById('setup'),
gameDiv: document.getElementById('game'),
loadingDiv: document.getElementById('loading'),
names: document.getElementById('names'),
count: document.getElementById('count'),
}
var DATABASE = {}
// ==================
// Helpers
// ==================
function init() {
if (!window.firebase) return setTimeout(init, 50)
window.firebase.initializeApp({
apiKey: 'AIzaSyB2pZb5r-VI46v3a1AnLa8LMXmvOVvEAgM',
authDomain: 'wt-family.firebaseapp.com',
databaseURL: 'https://wt-family.firebaseio.com',
projectId: 'wt-family',
storageBucket: 'wt-family.appspot.com',
messagingSenderId: '653609965128',
})
DATABASE.names = window.firebase.database().ref('/names')
DATABASE.names.on('value', function(snap) {
const players = Object.values(snap.val() || []).length
ELEMENTS.count.innerHTML = `(${players} players)`
})
ELEMENTS.loadingDiv.className = 'hidden'
ELEMENTS.setupDiv.className = ''
setupListeners()
}
function startGame() {
addPerson().then(function() {
DATABASE.names.once('value').then(function(snap) {
var people = shuffle(Object.values(snap.val()) || [])
ELEMENTS.setupDiv.className = 'hidden'
ELEMENTS.gameDiv.className = ''
ELEMENTS.names.innerHTML = people
.map(function(name) {
return '<li>' + name + '</li>'
})
.join('\n')
DATABASE.names.set(null)
})
})
}
function endGame() {
ELEMENTS.setupDiv.className = ''
ELEMENTS.gameDiv.className = 'hidden'
}
function addPerson() {
if (ELEMENTS.input.value && ELEMENTS.input.value.length) {
var name = ELEMENTS.input.value
ELEMENTS.input.value = ''
return DATABASE.names.push(name)
}
return Promise.resolve()
}
function shuffle(originalArray) {
var array = originalArray.slice(0)
var currentIndex = array.length,
temporaryValue,
randomIndex
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
// And swap it with the current element.
temporaryValue = array[currentIndex]
array[currentIndex] = array[randomIndex]
array[randomIndex] = temporaryValue
}
return array
}
function setupListeners() {
ELEMENTS.addPerson.addEventListener('click', addPerson)
ELEMENTS.input.addEventListener('keypress', function(e) {
if (e.keyCode === ENTER_KEY) {
addPerson()
}
})
ELEMENTS.startGame.addEventListener('click', startGame)
ELEMENTS.finishGame.addEventListener('click', endGame)
}
// ==================
// Start
// ==================
init()
</script>
</body>