forked from Tkim43/c918_hackathon2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
760 lines (639 loc) · 23.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/* information about jsdocs:
* param: http://usejsdoc.org/tags-param.html#examples
* returns: http://usejsdoc.org/tags-returns.html
*
/**
* Listen for the document to load and initialize the application
*/
$(document).ready(initializeApp);
/**
* Define all global variables here.
*/
var logos = {
"ATL": "nbaimages/ATL.png",
"BKN": "nbaimages/BKN.png",
"BOS": "nbaimages/BOS.png",
"CHA": "nbaimages/CHA.png",
"CHI": "nbaimages/CHI.png",
"CLE": "nbaimages/CLE.png",
"DAL": "nbaimages/DAL.png",
"DEN": "nbaimages/DEN.png",
"DET": "nbaimages/DET.png",
"GSW": "nbaimages/GSW.png",
"HOU": "nbaimages/HOU.png",
"IND": "nbaimages/IND.png",
"LAC": "nbaimages/LAC.png",
"LAL": "nbaimages/LAL.png",
"MIA": "nbaimages/MIA.png",
"MEM": "nbaimages/MEM.png",
"MIL": "nbaimages/MIL.png",
"MIN": "nbaimages/MIN.png",
"NOP": "nbaimages/NOP.png",
"NYK": "nbaimages/NYK.png",
"OKC": "nbaimages/OKC.png",
"ORL": "nbaimages/ORL.png",
"PHI": "nbaimages/PHI.png",
"PHX": "nbaimages/PHX.png",
"POR": "nbaimages/POR.png",
"SAC": "nbaimages/SAC.png",
"SAS": "nbaimages/SAS.png",
"TOR": "nbaimages/TOR.png",
"UTA": "nbaimages/UTA.png",
"WAS": "nbaimages/WAS.png"
};
var numCoord = {};
/***************************************************************************************************
* initializeApp
* @params {undefined} none
* @returns: {undefined} none
* initializes the application, including adding click handlers and pulling in any data from the server
*/
function initializeApp() {
getTwitterData();
getNBAData();
landing();
// playIntro();
// clickHandler();
}
/***************************************************************************************************
* initMap
* @params {undefined}
* @returns {undefined}
*
*/
function initMap(restArray) {
var bounds = new google.maps.LatLngBounds();
var map = new google.maps.Map(
document.getElementById('map'), { zoom: 10, center: numCoord });// areaOne needs to be the city we are searching
for (i = 0; i < restArray.length; i++) {
var icon = {
url: 'http://kindersay.com/files/images/basketball.png',
scaledSize: new google.maps.Size(40, 35)
};
var position = new google.maps.LatLng(restArray[i].latitude, restArray[i].longitude);
var url = restArray[i].url
var name = restArray[i].name
var content = `<a href=${url} target= "_blank"> ${name} </a>`
// var content = restArray[i].name
infoWindow = new google.maps.InfoWindow({ content: content });
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
icon: icon,
title: restArray[i].name,
infowindow: infoWindow
});
google.maps.event.addListener(marker, 'click', function () {
var name = this.infowindow.content
infoWindow.setContent(name)
infoWindow.open(map, this)
})
}
}
/***************************************************************************************************
* landing
* @params {undefined}
* @returns {undefined}
*
*/
function landing() {
document.getElementById('search-btn').addEventListener('click', function (e) {
e.preventDefault();
document.getElementById("loaderBackground").style.display = "block";
search_result();
$("#address").val('');
loader();
});
document.getElementById('nba').addEventListener('click', function () {
openPageNBA();
});
document.getElementById('nba1').addEventListener('click', function () {
if ($("#pageTwo").css('display') == 'none') {
$(".navbar-collapse").removeClass('in');
openPageNBA1();
}
else {
return
}
});
document.getElementById('eats').addEventListener('click', function () {
openPageEats();
});
document.getElementById('eats1').addEventListener('click', function () {
if ($("#pageThree").css('display') == 'none') {
$(".navbar-collapse").removeClass('in');
openPageEats1();
}
else {
return
}
});
document.getElementById('home2').addEventListener('click', function () {
if ($("#pageTwo").css('display') == 'none') {
$(".navbar-collapse").removeClass('in');
openPageHome2();
}
if ($("#pageThree").css('display') == 'none') {
$(".navbar-collapse").removeClass('in');
openPageHome3();
}
});
document.getElementById('home1').addEventListener('click', function () {
if ($("#pageTwo").css('display') == 'none') {
$(".navbar-collapse").removeClass('in');
openPageHome2();
}
if ($("#pageThree").css('display') == 'none') {
$(".navbar-collapse").removeClass('in');
openPageHome3();
}
});
var input = document.getElementById('address');
var options = {
types: ['(cities)'],
componentRestrictions: { country: 'USA' }
};
autocomplete = new google.maps.places.Autocomplete(input, options);
}
/***************************************************************************************************
* search_result -
* @param: two (geocoder, resultsMap)
* @return:
none
*/
function search_result() {
var geocoder = new google.maps.Geocoder();
var address = $("#address").val();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status === 'OK') {
var loc = results[0].geometry.location
numCoord = {
lat: loc.lat(),
lng: loc.lng()
};
getRestaurantInformation();
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
/***************************************************************************************************
* getTwitterData -
* @param:
* @returns:
* @calls:
*/
function getTwitterData() {
var ajaxObject = {
url: 'https://s-apis.learningfuze.com/hackathon/twitter/index.php',
dataType: 'json',
method: 'post',
data: {
action: 'user',
screen_name: 'nba',
get_timeline: 'true',
include_entities: 'true',
},
success: function (response) {
storeTwitterData(response);
}
};
$.ajax(ajaxObject)
}
/***************************************************************************************************
* storeTwitterData -
* @param:
* @return:
* @calls:
*/
function storeTwitterData(response) {
var twitterArray = response.info;
for (var i = 0; i < twitterArray.length; i++) {
var tweet = twitterArray[i].text;
var urlIndex = tweet.indexOf("http");
var tweetBox = $("<div>");
//If the tweet has a url at the end
if (urlIndex !== -1) {
var newTweetArray = tweet.split(" ");
var urlTweet = newTweetArray.pop();
var newTweet = newTweetArray.join(" ");
var hyperLink = $("<a>").text("more info").attr("href", urlTweet).attr("target", "_blank");
tweetBox.append(newTweet, hyperLink).addClass("tweetText");
}
else {
tweetBox.append(tweet).addClass("tweetText");
}
$(".tweetsFeed").append(tweetBox);
}
var nbaLogo = $("<img>").attr("src", "images/nbalogo.jpg");
var verifiedLogo = $("<img>").attr("src", "images/verified2.png");
var nbaHandleName = response.info[0].user.name;
$(".tweetLogo").append(nbaLogo);
$(".twitterHandle").append(nbaHandleName);
$(".twitterVerified").append(verifiedLogo);
}
/***************************************************************************************************
* getRestaurantInformation - clears out the form values based on inputIds variable
* @param {undefined} lat, long
* @return restaurant coordinates and restaurant information
* @calls initMap
*/
function getRestaurantInformation() {
var settings = {
"async": true,
"crossDomain": true,
"url": "https://danielpaschal.com/zamatoproxy.php",
"method": "GET",
dataType: 'json',
data: {
url: 'api/v2.1/search',
count: 10,
lat: numCoord.lat,
lon: numCoord.lng,
radius: 1000,
cuisines: 227,
q: "bar",
},
"headers": {
"user-key": "dd384e671b6ae1836ee2ff1a1829fdbc",
},
success: function (response) {
if(response.restaurants.length === 0 ){
emptyRestaurant()
}else{
if($(".emptyRestaurantContainer").css('display') == 'block'){
$(".emptyRestaurantContainer").toggle(".display");
$(".map").toggle(".display");
$(".restaurants").toggle(".display");
createRestaurantObj(response);
}else if($(".map").css('display') == 'block'){
createRestaurantObj(response);
}else{
$(".map").toggle(".display");
$(".restaurants").toggle(".display");
createRestaurantObj(response);
}
}
},
error: function (err) {
}
};
$.ajax(settings)
}
/***************************************************************************************************
* renderRestaurants - take in a object, dynamically create html elements with object values and append the elements
* into the restaurantSection
* @param object of restaurant info
*/
function renderRestaurants(restObj) {
var restaurantContainer = $("<div>").addClass("mainRestaurantContainer");
var infoContainer = $("<a>", {
class: "info",
href: restObj.url,
target: "_blank"
});
var nameContainer = $("<div>").addClass("restaurantName").text(restObj.name);
var cityContainer = $("<div>").addClass("city").text(restObj.city);
var addressContainer = $("<div>").addClass("address").text(restObj.address);
var rateContainer = $("<div>").addClass("rateSection");
var ratingContainer = $("<div>").addClass("rating label label-warning").text(restObj.rating);
var voteContainer = $("<div>").addClass("votes").text(restObj.votes + " reviews");
infoContainer.append(nameContainer, cityContainer, addressContainer);
rateContainer.append(ratingContainer, voteContainer);
restaurantContainer.append(infoContainer, rateContainer);
$(".restaurantSection").append(restaurantContainer);
}
/***************************************************************************************************
* createRestaurantObj - take in a object, create html elements from the values and then append the elements
* into the .student_list tbody
* @param object of restaurant info
*/
function createRestaurantObj(apiObj) {
$(".restaurantSection").empty();
var brewery = apiObj.restaurants;
var restaurantsArray = [];
for (var restaurantIndex = 0; restaurantIndex < brewery.length; restaurantIndex++) {
var restaurantObj = {};
var restLat = brewery[restaurantIndex].restaurant.location.latitude;
var restLong = brewery[restaurantIndex].restaurant.location.longitude;
var restName = brewery[restaurantIndex].restaurant.name;
var restAddress = brewery[restaurantIndex].restaurant.location.address;
var restPricing = brewery[restaurantIndex].restaurant.price_range;
var restRating = brewery[restaurantIndex].restaurant.user_rating.aggregate_rating;
var restCity = brewery[restaurantIndex].restaurant.cuisines;
var restRateCount = brewery[restaurantIndex].restaurant.user_rating.votes;
var restUrl = brewery[restaurantIndex].restaurant.url;
restaurantObj.latitude = restLat;
restaurantObj.longitude = restLong;
restaurantObj.name = restName;
restaurantObj.address = restAddress;
restaurantObj.pricing = restPricing;
restaurantObj.rating = restRating;
restaurantObj.city = restCity;
restaurantObj.votes = restRateCount;
restaurantObj.url = restUrl;
renderRestaurants(restaurantObj);
restaurantsArray.push(restaurantObj);
}
initMap(restaurantsArray);
}
/***************************************************************************************************
* FallbackScreen -
* @param: none
* @returns: object response from NBA API
*/
function emptyRestaurant(){
if($(".emptyRestaurantContainer").css('display') == 'block'){
return
}else if($(".map").css('display') == 'block'){
$(".emptyRestaurantContainer").toggle(".display");
$(".map").toggle(".display");
$(".restaurants").toggle(".display");
}else {
$(".emptyRestaurantContainer").toggle(".display");
}
}
/***************************************************************************************************
* getNBAData -
* @param: none
* @returns: object response from NBA API
*/
function getNBAData() {
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
// console.log(month, day, year)
if (day < 10) {
var date = `${year}${month}0${day}`
var nbaData = {
"async": true,
"crossDomain": true,
'dataType': 'json',
"url": `https://place.kim-chris.com/nba/${date}`,
"method": "GET",
};
$.ajax(nbaData).done(function (response) {
var nbaData = response.data;
updateNBAScores(nbaData);
getNBADataInterval();
})
}
if (month < 10) {
var date = `${year}0${month}${day}`
var nbaData = {
"async": true,
"crossDomain": true,
'dataType': 'json',
"url": `https://place.kim-chris.com/nba/${date}`,
"method": "GET",
};
$.ajax(nbaData).done(function (response) {
var nbaData = response.data;
updateNBAScores(nbaData);
getNBADataInterval();
})
}
else {
var date = `${year}0${month}${day}`
var nbaData = {
"url": `https://place.kim-chris.com/nba/${date}`,
"method": "GET",
};
$.ajax(nbaData).done(function (response) {
var nbaData = response.data;
updateNBAScores(nbaData);
getNBADataInterval();
})
}
}
/***************************************************************************************************
* getNBADataInterval -
* @param: none
* @returns: object response from NBA API
*/
function getNBADataInterval() {
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
setInterval(function () {
if (day < 10) {
var date = `${year}${month}0${day}`
var nbaData = {
"async": true,
"crossDomain": true,
'dataType': 'json',
"url": `https://place.kim-chris.com/nba/${date}`,
"method": "GET",
};
$.ajax(nbaData).done(function (response) {
var nbaData = response.data;
updateNBAScores(nbaData);
})
}
if (month < 10) {
var date = `${year}0${month}${day}`
var nbaData = {
"async": true,
"crossDomain": true,
'dataType': 'json',
"url": `https://place.kim-chris.com/nba/${date}`,
"method": "GET",
};
$.ajax(nbaData).done(function (response) {
var nbaData = response.data;
updateNBAScores(nbaData);
})
}
else {
var date = `${year}${month}${day}`
var nbaData = {
"url": `https://place.kim-chris.com/nba/${date}`,
"method": "GET",
};
$.ajax(nbaData).done(function (response) {
var nbaData = response.data;
updateNBAScores(nbaData);
})
}
}, 15000);
}
/***************************************************************************************************
* updateNBAScores -
* @param: nbaData
* @returns teamOne, teamTwo, gameInfo
*/
function updateNBAScores(nbaData) {
$("#gameSection").empty();
var numberGames = nbaData.numGames;
for (var i = 0; i < numberGames; i++) {
var teamName1 = nbaData.games[i].hTeam.triCode;
var teamName2 = nbaData.games[i].vTeam.triCode;
var teamScore1 = nbaData.games[i].hTeam.score;
var teamScore2 = nbaData.games[i].vTeam.score;
var teamImage1 = logos[teamName1];
var teamImage2 = logos[teamName2];
var gameFinal = nbaData.games[i].isGameActivated;
var quarter = nbaData.games[i].period.current;
var clock = nbaData.games[i].clock;
var startTime = nbaData.games[i].startTimeEastern;
var teamOne = formatTeamInfo(teamName1, teamScore1, teamImage1);
var teamTwo = formatTeamInfo(teamName2, teamScore2, teamImage2);
var gameInfo = {
quarter: quarter,
clock: clock,
startTime: startTime,
gameFinal: gameFinal,
};
generateScoreboard(teamOne, teamTwo, gameInfo);
}
}
/***************************************************************************************************
* formatTeamInfo -
* @param: tricode, score, teamImg
* @returns team Obj
*/
function formatTeamInfo(tricode, score, teamImg) {
var team = {
tricode: tricode,
score: score,
teamImg: teamImg
};
return team;
}
/***************************************************************************************************
* functionGenerateScoreboard -
* @param:
* @returns
*/
function generateScoreboard(teamOne, teamTwo, gameInfo) {
var scoreboard = $("<div>").addClass("scoreboard");
var homeTeam = $("<div>").addClass("team team-a");
var homeTeamLogo1 = $("<div>").addClass("team-logo");
var logo1 = $("<img>").attr("src", teamOne.teamImg).css("width", "50px").css("height", "50px");
var teamDetails1 = $("<div>").addClass("team-detail");
var teamNameandScore1 = $("<div>").addClass("team-nameandscore");
var teamName1 = $("<div>").addClass("homeTeamName").text(teamOne.tricode);
if (teamOne.score == "0") {
var teamScore1 = " "
}
else {
var teamScore1 = $("<div>").addClass("homeTeamScore").text(teamOne.score);
}
var space = $("<div>").addClass('space');
teamNameandScore1.append(teamName1, teamScore1);
teamDetails1.append(teamNameandScore1, space);
homeTeamLogo1.append(logo1);
homeTeam.append(homeTeamLogo1, teamDetails1);
scoreboard.append(homeTeam);
var awayTeam = $("<div>").addClass("team team-b");
var homeTeamLogo2 = $("<div>").addClass("team-logo");
var logo2 = $("<img>").attr("src", teamTwo.teamImg).css("width", "50px").css("height", "50px");
var teamDetails2 = $("<div>").addClass("team-detail");
var teamNameandScore2 = $("<div>").addClass("team-nameandscore");
var teamName2 = $("<div>").addClass("homeTeamName").text(teamTwo.tricode);
if (teamTwo.score == "0") {
var teamScore1 = " "
}
else {
var teamScore2 = $("<div>").addClass("homeTeamScore").text(teamOne.score);
}
var space = $("<div>").addClass('space');
if (teamScore2 == "0") {
var teamScore2 = " "
};
teamNameandScore2.append(teamName2, teamScore2);
teamDetails2.append(teamNameandScore2, space);
homeTeamLogo2.append(logo2);
awayTeam.append(homeTeamLogo2, teamDetails2);
scoreboard.append(awayTeam);
var quarter = gameInfo.quarter;
var timerContainer = $("<div>").addClass("timer-container");
var timer = $("<div>").addClass("timer");
var gameStart = gameInfo.gameFinal;
var clock = gameInfo.clock
if (gameStart == true && quarter > 4) {
var quarter1 = $("<div>").addClass("quarter").text("OT");
var timeLeft = $("<div>").addClass("timeleft").text(clock);
timerContainer.append(quarter1, timeLeft);
}
if (gameStart == true && quarter <= 4) {
var quarter = $("<div>").addClass("quarter").text(gameInfo.quarter);
var timeLeft = $("<div>").addClass("timeleft").text(clock);
timerContainer.append(quarter, timeLeft);
}
if (gameStart == true && quarter == 2 && clock == "0.0") {
var quarter = $("<div>").addClass("quarter").text(gameInfo.quarter);
var halftime = $("<div>").addClass("timeleft").text("HALF");
timerContainer.append(quarter, halftime);
}
if (gameStart == false && teamOne.score == 0) {
var startTime = $("<div>").addClass("timeleft").text(gameInfo.startTime);
var quarter2 = $("<div>").addClass("quarter").text("");
timerContainer.append(quarter2, startTime);
}
if (gameStart == false && teamOne.score > 0) {
var endTime = $("<div>").addClass("timeleft").text("FINAL");
var quarter3 = $("<div>").addClass("quarter").text("");
timerContainer.append(quarter3, endTime);
}
timer.append(timerContainer);
scoreboard.append(homeTeam, awayTeam, timer);
$(".gameSection").append(scoreboard);
}
/***************************************************************************************************
* openPage - switch between opening page and main page
* @param:
* @returns
*/
function openPageNBA() {
$(".navbar").toggle(".display");
$(".pageOne").toggle(".display");
$(".pageTwo").toggle(".display");
}
function openPageNBA1() {
if($(".emptyRestaurantContainer").css('display') == 'block'){
$(".emptyRestaurantContainer").toggle(".display");
}
$("#map").empty();
$(".restaurantSection").empty();
$(".pageTwo").toggle(".display");
$(".pageThree").toggle(".display");
}
function openPageEats() {
$(".navbar").toggle(".display");
$(".pageOne").toggle(".display");
$(".pageThree").toggle(".display");
}
function openPageEats1() {
$(".pageTwo").toggle(".display");
$(".pageThree").toggle(".display");
}
function openPageHome2() {
if($(".emptyRestaurantContainer").css('display') == 'block'){
$(".emptyRestaurantContainer").toggle(".display");
}
$("#map").empty();
$(".restaurantSection").empty();
$(".navbar").toggle(".display");
$(".pageOne").toggle(".display");
$(".pageThree").toggle(".display");
}
function openPageHome3() {
$(".navbar").toggle(".display");
$(".pageOne").toggle(".display");
$(".pageTwo").toggle(".display");
}
/***************************************************************************************************
* loading screen - loader after search function
* @param:
* @returns
*/
function loader() {
const loadingImg = setTimeout( hideLoader , 2000);
}
function hideLoader() {
document.getElementById("loaderBackground").style.display = "none"
}