-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
307 lines (275 loc) · 10.1 KB
/
main.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
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
const usersURL = "https://ameowsement-park.herokuapp.com/users"
const redDotURL = "https://ameowsement-park.herokuapp.com/red_dot_games"
const topDiv = document.querySelector('div#top-div')
const main = document.querySelector('div#main-container')
const sideNav = document.querySelector('div.sidenav')
const myBGM = document.querySelector('audio#BGM')
const scoreSound = document.querySelector('audio#dotScore')
const gamesObj = {'Catch the Dot': catchTheDotGameMenu, 'More Games to Come': moreGames}
let currentUser
document.addEventListener('DOMContentLoaded',()=>{
userLogin()
})
const userLogin = () =>{
renderLogo()
axios.get(usersURL)
const loginDiv = document.createElement('div')
const header = document.createElement('header')
const loginForm = document.createElement('form')
const userInput = document.createElement('input')
const submitBtn = document.createElement('button')
const br = document.createElement('br')
const musicDiv = document.createElement('div')
const music = document.createElement('i')
musicDiv.className = 'bottom-right-corner'
musicDiv.id = 'mute-control'
music.classList.add('fas', 'fa-volume-up', 'hover-enlarge')
music.addEventListener('click', (e)=>toggleMute(e))
musicDiv.append(music)
topDiv.append(musicDiv)
loginDiv.className = 'nav-bar-item'
userInput.type = 'text'
userInput.name = 'username'
userInput.placeholder = 'Enter Username here'
header.innerText = "What's your name?"
submitBtn.innerText = 'Login'
loginForm.addEventListener('submit', submitHandle )
loginForm.append(userInput,br, submitBtn)
loginDiv.appendChild(loginForm)
sideNav.append(header, loginDiv)
}
const submitHandle = (e) =>{
e.preventDefault()
e.target.style.display = 'none'
let userName = {
username: e.target[0].value
}
axios.post(usersURL, userName)
.then(r => {
currentUser = r.data
gameNav()
})
}
const gameNav = () =>{
main.innerHTML = ''
main.id = 'main-container'
myBGM.src = 'assets/audio/bathtime-funk-all-good-folks-main-version-01-16-689.mp3'
BGMplay()
renderLogo()
sideNav.style.display = 'block'
sideNav.innerHTML = ''
const greetingDiv = document.createElement('div')
const greeting = document.createElement('header')
const gamesDiv = document.createElement('div')
const gamesHeader = document.createElement('p')
const gamesUL = document.createElement('ul')
const leaderBoardDiv = document.createElement('div')
const highScore = document.createElement('p')
const mute = document.querySelector('div#mute-control')
greetingDiv.className ='hover-enlarge'
greeting.innerText = `Welcome, ${currentUser.username}`
greeting.id = 'greeting'
greeting.title = 'Click to edit user'
highScore.className = 'title'
highScore.innerText = 'High Score'
gamesDiv.className = 'nav-bar-item'
gamesHeader.innerText = 'Games Selection'
gamesHeader.className = 'title'
gamesUL.className = 'no-bullets'
leaderBoardDiv.className = 'nav-bar-item'
leaderBoardDiv.id = 'leaderboard'
leaderBoardDiv.style.display ='none'
mute.className = 'bottom-right-corner'
greetingDiv.addEventListener('click', getUser)
for (const game in gamesObj){
const li = document.createElement('li')
li.className = 'game-title'
li.className = 'hover-enlarge'
li.innerText = game
li.addEventListener('click', ()=>{
leaderBoardDiv.style.display = 'block'
leaderBoardDiv.innerHTML = ''
leaderBoardDiv.append(highScore)
switch (game){
case 'Catch the Dot':
renderLeaderBoard(`${redDotURL}/topscore`).then(leaderBoard => leaderBoardDiv.appendChild(leaderBoard))
break
case 'More Games to Come':
leaderBoardDiv.style.display = 'none'
break
}
const gameSelected = gamesObj[game]
gameSelected()
})
gamesUL.appendChild(li)
}
greetingDiv.append(greeting)
gamesDiv.append(gamesHeader, gamesUL)
sideNav.append(greetingDiv, gamesDiv, leaderBoardDiv)
}
const renderLogo = () =>{
const logoDiv = document.createElement('div')
const logo = document.createElement('img')
const phrase = document.createElement('p')
logoDiv.id = 'logo'
logo.src = 'assets/img/main_logo.png'
logo.alt = 'A-meow-sement Park Logo'
logo.title = 'A-MEOW-sement Park'
logo.width = '490'
logo.height = '300'
phrase.innerText = 'Where you can spend the day being a cat'
logoDiv.append(logo, phrase)
main.append(logoDiv)
}
const getUser = async() =>{
clearMainDiv()
const userNameArea = document.createElement('div')
const name = document.createElement('h1')
const editBtn = document.createElement('button')
const dotTableDiv = document.createElement('div')
const dotTable = document.createElement('table')
const header = document.createElement('header')
const titleTr = document.createElement('tr')
const scoreTh = document.createElement('th')
const dateTh = document.createElement('th')
const deleteTh = document.createElement('th')
const leaderBoard = document.querySelector('div#leaderboard')
name.innerText = `Username: ${currentUser.username}`
editBtn.innerText = 'Edit'
header.innerText = '<Catch The Dot> Record'
header.className = 'title'
dotTableDiv.className = 'main-sub-div'
scoreTh.innerText = 'Score'
dateTh.innerText = 'Date'
deleteTh.innerText = 'Delete'
leaderBoard.style.display = 'none'
editBtn.style.display = 'none'
userNameArea.append(name, editBtn)
titleTr.append(scoreTh, dateTh, deleteTh)
dotTable.append(header, titleTr)
dotTableDiv.append(dotTable)
editBtn.addEventListener('click', (e)=>updateForm(e))
let user = await axios.get(`${usersURL}/${currentUser.id}`)
for(let i = 0; i< user.data.redDotGames.length; i++){
const recordTr = document.createElement('tr')
const scoreTh = document.createElement('th')
const dateTh = document.createElement('th')
const deleteTh = document.createElement('th')
const deleteIcon = document.createElement('i')
deleteIcon.className ='hover-enlarge'
deleteIcon.id = user.data.redDotGames[i].id
scoreTh.innerText = user.data.redDotGames[i].score
dateTh.innerText = user.data.redDotGames[i].created_at.split('T')[0]
deleteIcon.classList.add('fas', 'fa-trash')
deleteIcon.addEventListener('click', (e)=>confirmPop(e))
deleteTh.append(deleteIcon)
recordTr.append(scoreTh, dateTh, deleteTh)
dotTable.append(recordTr)
}
main.append(userNameArea, dotTableDiv)
}
const destroyRecord = async(e, confirmation) => {
let sure = confirmation
let scoreData = await axios.get(`${redDotURL}/topscore`)
let topTen = scoreData.data.map(score => score.id)
let idNum = parseInt(e.target.id, 10)
console.log(idNum)
if(sure && !topTen.includes(idNum)){
axios.delete(`${redDotURL}/${e.target.id}`)
.then(()=>{
warningPop('Record Deleted')
e.target.parentElement.parentElement.remove()
})
} else if (sure && topTen.includes(idNum)){
warningPop('Cannot delete top 10 record')
}
}
const updateForm = (e) =>{
const userDiv = e.target.parentElement
const updateForm = document.createElement('form')
const input = document.createElement('input')
const submitBtn = document.createElement('button')
input.value = currentUser.username
input.name = 'username'
submitBtn.innerText = 'Update'
userDiv.innerHTML = ''
updateForm.addEventListener('submit', (e) =>updateUser(e))
updateForm.append(input, submitBtn)
userDiv.append(updateForm)
}
const updateUser = (e) =>{
e.preventDefault()
axios.patch(`${usersURL}/${currentUser.id}`, {
username: e.target.username.value
})
.then((user)=>{
console.log(user)
const greeting = document.querySelector('#greeting')
currentUser = user.data
warningPop('Username updated')
greeting.innerText = `Welcome, ${currentUser.username}`
getUser()
})
}
const BGMplay = () =>{
myBGM.loop = true
myBGM.volume = 0.08
myBGM.play()
}
const toggleMute = (e) =>{
if(myBGM.muted){
scoreSound.muted = !scoreSound.muted
myBGM.muted = !myBGM.muted
e.target.className = 'fas fa-volume-up hover-enlarge'
} else {
scoreSound.muted = !scoreSound.muted
myBGM.muted = !myBGM.muted
e.target.className = 'fas fa-volume-mute hover-enlarge'
}
}
const clearMainDiv = () =>{
main.innerHTML = ""
}
const confirmPop = (e) =>{
const bigDiv = document.createElement('div')
const smallDiv = document.createElement('div')
const br = document.createElement('br')
const yesBtn = document.createElement('button')
const noBtn = document.createElement('button')
let result
bigDiv.className = 'masking-div'
smallDiv.className = 'modal-like'
smallDiv.innerText = 'Are you sure?'
yesBtn.innerText = 'Yes'
noBtn.innerText = 'No'
yesBtn.addEventListener('click', ()=>{
result = true
removeItself(bigDiv)
destroyRecord(e, result)
})
noBtn.addEventListener('click', ()=>{
result = false
removeItself(bigDiv)
destroyRecord(e,result)
})
smallDiv.append(br, yesBtn, noBtn)
bigDiv.append(smallDiv)
topDiv.prepend(bigDiv)
}
const warningPop = (string) => {
const bigDiv = document.createElement('div')
const smallDiv = document.createElement('div')
const br = document.createElement('br')
const okBtn = document.createElement('button')
bigDiv.className = 'masking-div'
smallDiv.className = 'modal-like'
smallDiv.innerText = string
okBtn.innerText = 'OK'
okBtn.addEventListener('click', () => removeItself(bigDiv))
smallDiv.append(br, okBtn)
bigDiv.append(smallDiv)
topDiv.prepend(bigDiv)
}
const removeItself = (itself) =>{
itself.remove()
}