-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.js
More file actions
296 lines (251 loc) · 11 KB
/
Copy pathScript.js
File metadata and controls
296 lines (251 loc) · 11 KB
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
let games = [];
const library = document.querySelector(".library"); // Find library container
const genreSelect = document.getElementById('genre-filter');
const platformSelect = document.getElementById('platform-filter');
function setRatingFilled(container, i) {
[].forEach.call(container.children, function(sym, j) {
if (j <= i) {
sym.classList.add("rate-symbol-filled");
} else {
sym.classList.remove("rate-symbol-filled");
}
})
}
function renderLibraryCards() {
games.forEach((game) => {
const card = document.createElement("a");
card.classList.add("card");
card.href = "#";
card.dataset.id = game.game_id;
const img = game.cover_url;
card.innerHTML =
`<img src="${img}" alt="${game.game_title}">`;
library.appendChild(card); // Fill library container with cards, library is wrapper for all cards
card.addEventListener("click", (event) => {
event.preventDefault();
//if game is from API, add, else go to game details page when clicking, yes the logic sucks but it works ¯\(ツ)/¯
if (game.fromApi) {
window.location.href = `add_game.php?api_game_id=${game.game_id}`
}
else {
window.location.href = `game_details.php?game_id=${game.game_id}`;
}
});
})
}
//async function to load game data from database
async function loadGames() {
const response = await fetch('get_games.php');
games = await response.json();
renderLibraryCards();
const allGenres = [...new Set(games.flatMap(g => g.genre))].filter(Boolean);
const allPlatforms = [...new Set(games.flatMap(g => g.platform))].filter(Boolean);
allGenres.forEach(g => {
const option = document.createElement('option');
option.value = g;
option.textContent = g;
genreSelect.appendChild(option);
});
allPlatforms.forEach(p => {
const option = document.createElement('option');
option.value = p;
option.textContent = p;
platformSelect.appendChild(option);
});
filterCards("all");
restoreCardBorder();
}
//Renders cards from API results
function renderSearchCards(results) {
results.forEach((r) =>{
const card = document.createElement("a");
card.classList.add("card");
const title = r.name;
const img = r.background_image || 'https://placehold.co/222x168.475?text=' + encodeURIComponent(r.name);
card.innerHTML = `
<img src="${img}" alt="${title}">
<div class="card-title-container">
<span class="card-title">${title}</span>
<form method="get" action="add_game.php">
<input type="hidden" name="gid" value="${r.id}">
<input type="submit" class="card-add-btn" value="Add">
</form>
</div>
`;
library.appendChild(card);
});
}
document.addEventListener("DOMContentLoaded", () => {
//if API results exist, use them, else load from database
if (Array.isArray(window.apiResults) && window.apiResults.length) {
renderSearchCards(window.apiResults);
} else {
// otherwise (library.php), load from database
loadGames();
}
});
//functions to create keys and store them in localStorage for different collections
const KEY = "game_data"; //key for game data, just so we don't have to type it out every time we update the localStorage
const DATA = {
collections: { //Collections and platform hold arrays, they simply hold IDs of games that belong to each category
favorites: [],
wishlist: [], //favorites: [3, 12, 18, etc.]
completed: [] //each number is the ID associated with a game
}, //so if a game is added to favorites, add the game ID to the favorites array
rating: {}, //ratings stored as key value pair, gameID maps to rating value
};
//ngl I feel like properly storing things in this is going to be a nightmare but we got this guys we can do anything
// ok now to actually create the thing in localStorage
function initializeStorage() {
if (!localStorage.getItem(KEY)) { //if the key doesn't already exist in localStorage
localStorage.setItem(KEY, JSON.stringify(DATA)); //store the DATA key array thing in it
}
}
initializeStorage(); //call the function once when page loads
console.log('INIT data:', getData());
//ok so these 2 functions are how we put data in and get data out of localStorage.
//put data in so we can update user's collections and we pull data out so we can display it on the page.
function getData() {
return JSON.parse(localStorage.getItem(KEY));
}
function saveData(data) {
localStorage.setItem(KEY, JSON.stringify(data));
}
const get = getData(); //can use this inside functions to get copy of user's data from localStorage
//to actually start adding games to collections and stuff, we can do something like this inside another function:
get.collections.favorites.push(/*<gameID>*/);
saveData(get);
//this updates the local copy of the localStorage data, then saves it back to user's localStorage
//Now to actually make the filtering buttons work
const buttons = document.querySelectorAll("[data-filter]"); //select all buttons with data-filter attribute
function filterCards(filter) {
const cards = document.querySelectorAll(".card"); //select all game cards
const get = getData(); //get fresh copy of data from
const selectedGenre = genreSelect.value;
const selectedPlatform = platformSelect.value;
cards.forEach(card => {
const id = Number(card.dataset.id); //get the game ID from the card, convert from string to number
const game = games.find(g => g.game_id === id);
let visible = true;
switch (filter) {
case "Favorites":
visible = get.collections.favorites.includes(id);
break;
case "Wishlist":
visible = get.collections.wishlist.includes(id);
break;
case "Completed":
visible = get.collections.completed.includes(id);
break;
default:
visible = true;
}
if (selectedGenre !== "all" && !game.genre.includes(selectedGenre)) {
visible = false;
}
if (selectedPlatform !== "all" && !game.platform.includes(selectedPlatform)) {
visible = false;
}
card.style.display = visible ? "" : "none";
});
};
//now to add event listeners to buttons so they actually do something when clicked
buttons.forEach(button => {
button.addEventListener("click", () => {
const filter = button.dataset.filter; //get the filter type from button's data-filter attribute
filterCards(filter); //call filterCards function with the selected filter
});
});
genreSelect.addEventListener("change", () => {
const activeButton = document.querySelector("[data-filter][aria-pressed='true']") || { dataset: { filter: "all" } };
filterCards(activeButton.dataset.filter);
});
platformSelect.addEventListener("change", () => {
const activeButton = document.querySelector("[data-filter][aria-pressed='true']") || { dataset: { filter: "all" } };
filterCards(activeButton.dataset.filter);
})
//reset functions on page reload, ideally this would stay
//this should (hope) cycle cards through each collection when double clicked
//border color by collection in css file
//can't really have games in more than one though.. maybe later
/*
cards.forEach(card =>{
card.addEventListener("dblclick", (c1) =>{
c1.preventDefault() //don't really need but just incase
const gameId=Number(card.dataset.id); //get game id and data
const data = getData();
//variables to check where game is rn
const inFavorites = data.collections.favorites.includes(gameId);
const inWishlist = data.collections.wishlist.includes(gameId);
const inCompleted = data.collections.completed.includes(gameId);
//self explanatory me thinks
let currentstate = "none";
if (inFavorites) currentstate = "favorites";
else if (inWishlist) currentstate = "wishlist";
else if (inCompleted) currentstate = "completed";
switch (currentstate) {
case "none":
//not in any collection, add to favorites
data.collections.favorites.push(gameId);
card.classList.add("in-favorites");
break;
case "favorites":
//in favorites, move to wishlist
data.collections.favorites = data.collections.favorites.filter(id => id !== gameId);
data.collections.wishlist.push(gameId);
card.classList.remove("in-favorites");
card.classList.add("in-wishlist");
break;
case "wishlist":
//in wishlist, move to completed
data.collections.wishlist = data.collections.wishlist.filter(id => id !== gameId);
data.collections.completed.push(gameId);
card.classList.remove("in-wishlist");
card.classList.add("in-completed");
break;
case "completed":
//in completed, remove from all collections
data.collections.completed = data.collections.completed.filter(id => id !== gameId);
card.classList.remove("in-completed");
break;
}
saveData(data);
console.log("updated collections", data.collections);
});
});
*/
//works mostly ok but sometimes you need to hover off when changing multiple times, not important
function restoreCardBorder(){ //cards weren't keeping color on page refresh so this exists
const cards = document.querySelectorAll(".card"); //select all game cards
const data = getData();
cards.forEach(card=>{
const gameId=Number(card.dataset.id);
card.classList.remove("in-favorites", "in-wishlist", "in-completed"); //remove first
//check collection to see if card is in it then add class back
if (data.collections.favorites.includes(gameId)){
card.classList.add("in-favorites")
}
else if (data.collections.wishlist.includes(gameId)){
card.classList.add("in-wishlist")
}
else if (data.collections.completed.includes(gameId)){
card.classList.add("in-completed")
}
})
}
/* - not on main page no longer need here
function restoreRatings() {
const data = getData();
cards.forEach(card=>{
const gameId=Number(card.dataset.id);
let container = card.querySelector(".rate-container");
if (gameId in data.rating) {
setRatingFilled(container, data.rating[gameId] - 1);
}
});
}
*/
//filterCards("all"); moved to line 68
//restoreCardBorder(); moved to line 69
//keep ratings
//restoreRatings();