forked from rmmccar92/Confused-Gamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavoriteList.js
216 lines (177 loc) · 8.53 KB
/
favoriteList.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
var topTen = $(".top-10-games");
var savedListEl = $(".saved-list");
var listCount = 0
localStorageCount = localStorage.getItem("count");
pageLoad();
function pageLoad() {
if (localStorage.getItem("count") != null) {
listCount = localStorage.getItem("count")
}
else if (localStorage.getItem("count") !=0){
(!$(this).addClass("active"));
}
};
firstListItem()
function firstListItem() {
data = JSON.parse(localStorage.getItem('data'));
var guid3 = data.results[0].guid
// api call for title, genre, and description
var singleGameAPI = 'https://www.giantbomb.com/api/game/' + guid3 + '/?api_key=073c2f94ba69540e99d2b7e8b4cd3aebb2d9befb&format=jsonp';
$.ajax({
type: 'GET',
dataType: 'jsonp',
crossDomain: true,
jsonp: 'json_callback',
url: singleGameAPI
})
.then((data) => {
var titleValue = data.results.name;
var descValue = data.results.deck ? data.results.deck : "Sorry, no Giant Bomb users have written a description for " + titleValue + " yet!";
var genreValue = data.results.genres ? data.results.genres[0].name : "Sorry, a genre for " + titleValue + ", is not listed.";
var ratingValue = data.results.original_game_rating ? data.results.original_game_rating[0].name : titleValue + " rating not available";
var bombValue = data.results.site_detail_url;
var imageURL = data.results.image.medium_url;
$('#photoBox').empty()
console.log(imageURL);
var image = new Image();
image.src = imageURL;
document.getElementById('photoBox').appendChild(image);
title.innerHTML = titleValue;
gameDesc.innerHTML = descValue;
gameGenre.innerHTML = "Genre: " + genreValue;
gameRating.innerHTML = "Rating: " + ratingValue;
bombSite.innerHTML = `<a style="color: #4ddb4f" href=${bombValue} target="_blank">click here!</a>`;
})
}
var data = JSON.parse(localStorage.getItem('data'));
console.log("data: ", data);
topTenAppend()
function topTenAppend() {
for (i = 0; i < data.results.length; i++) {
var gameEl = "#game" + [i + 1]
topTen.append("<a href='#!'class='collection-item black darken-1 right-list' data-index='" + [i] + "' id = 'game" + [i + 1] + "'data-guid='" + data.results[i].guid + "'>" + data.results[i].name + "</a>")
var gameBtn = "<a class='btn-floating btn-small waves-effect waves-light deep-purple accent-3 game-" + [i + 1] + "-button list-button'><i class='material-icons'>add</i></a>"
$(gameEl).append(gameBtn);
$("#game1").addClass("active");
}
}
$(topTen).on('click', '.btn-small', function () {
var selection = $(this).parent().text().replace("add", "")
var guid = $(this).parent().attr("data-guid")
localStorage.setItem(selection, guid)
localStorage.setItem(listCount, selection);
var listItem = $(".saved-list");
listItem.append("<a href='#!'class='collection-item black darken-1 white-text'>" + selection + "</a>");
listCount++
localStorage.setItem("count", listCount);
})
// Clicking the text in the top ten list
$(topTen).on('click', 'a', function () {
var index = $(this).attr('data-index')
var guid = data.results[index].guid
console.log(guid)
$().css('color', 'yellow');
// api call for title, genre, and description
var singleGameAPI = 'https://www.giantbomb.com/api/game/' + guid + '/?api_key=073c2f94ba69540e99d2b7e8b4cd3aebb2d9befb&format=jsonp';
$.ajax({
type: 'GET',
dataType: 'jsonp',
crossDomain: true,
jsonp: 'json_callback',
url: singleGameAPI
})
.then((data) => {
var titleValue = data.results.name;
var descValue = data.results.deck ? data.results.deck : "Sorry, no Giant Bomb users have written a description for " + titleValue + " yet!";
var genreValue = data.results.genres ? data.results.genres[0].name : "Sorry, a genre for " + titleValue + ", is not listed.";
var ratingValue = data.results.original_game_rating ? data.results.original_game_rating[0].name : titleValue + " rating not available";
var bombValue = data.results.site_detail_url;
var imageURL = data.results.image.medium_url;
$('#photoBox').empty()
console.log(imageURL);
var image = new Image();
image.src = imageURL;
document.getElementById('photoBox').appendChild(image);
title.innerHTML = titleValue;
gameDesc.innerHTML = descValue;
gameGenre.innerHTML = "Genre: " + genreValue;
gameRating.innerHTML = "Rating: " + ratingValue;
bombSite.innerHTML = `<a style="color: #4ddb4f" href=${bombValue} target="_blank">click here!</a>`;
})
})
$(savedListEl).on('click', 'a', function () {
console.log($(this).attr('data-index'));
var index = $(this).attr('data-index');
var guid2 = localStorage.getItem($(this).text())
// api call for title, genre, and description
var singleGameAPI = 'https://www.giantbomb.com/api/game/' + guid2 + '/?api_key=073c2f94ba69540e99d2b7e8b4cd3aebb2d9befb&format=jsonp';
$.ajax({
type: 'GET',
dataType: 'jsonp',
crossDomain: true,
jsonp: 'json_callback',
url: singleGameAPI
})
.then((data) => {
var titleValue = data.results.name;
var descValue = data.results.deck ? data.results.deck : "Sorry, no Giant Bomb users have written a description for " + titleValue + " yet!";
var genreValue = data.results.genres ? data.results.genres[0].name : "Sorry, a genre for " + titleValue + ", is not listed.";
var ratingValue = data.results.original_game_rating ? data.results.original_game_rating[0].name : titleValue + " rating not available";
var bombValue = data.results.site_detail_url;
var imageURL = data.results.image.medium_url;
$('#photoBox').empty()
console.log(imageURL);
var image = new Image();
image.src = imageURL;
document.getElementById('photoBox').appendChild(image);
title.innerHTML = titleValue;
gameDesc.innerHTML = descValue;
gameGenre.innerHTML = "Genre: " + genreValue;
gameRating.innerHTML = "Rating: " + ratingValue;
bombSite.innerHTML = `<a style="color: #4ddb4f" href=${bombValue} target="_blank">click here!</a> `;
console.log(bombValue);
})
})
for (i = 0; i < localStorageCount; i++) {
var savedItem = localStorage.getItem(i);
var listItem = $(".saved-list").addClass("saved-games");
listItem.append("<a href='#!'class='collection-item black darken-1 white-text' data-guid='" + data.results[i].guid + "'>" + savedItem + "</a>");
}
// color change of active tab
$(topTen).on('click', 'a', function () {
if (!$(this).hasClass("active")) {
$("a.active").removeClass("active");
$(this).addClass("active");
}
})
var genreGame = 'strategy';
var f2playAPI = 'https://free-to-play-games-database.p.rapidapi.com/api/games?platform=browser&category=' + genreGame + '&sort-by=release-date'
fetch(f2playAPI, {
"method": "GET",
"headers": {
"x-rapidapi-key": "a0cca6994cmsh81f3b7fb76d08efp1175c4jsn19381bff6ee1",
"x-rapidapi-host": "free-to-play-games-database.p.rapidapi.com"
}
})
.then(function (response) {
return response.json();
})
.then(function (data) {
console.log(data)
//Loop over the data to generate a table, each table row will have a link to the repo url
for (var i = 0; i < 5; i++) {
// Creating elements, tablerow, tabledata, and anchor
var createTableRow = document.createElement('tr');
var tableData = document.createElement('td');
var link = document.createElement('a');
// Setting the text of link and the href of the link
link.textContent = data[i].freetogame_profile_url;
link.href = data[i].freetogame_profile_url;
link.setAttribute("target", "_blank");
// Appending the link to the tabledata and then appending the tabledata to the tablerow
// The tablerow then gets appended to the tablebody
tableData.appendChild(link);
createTableRow.appendChild(tableData);
repoTable.appendChild(createTableRow);
}
});