-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
131 lines (120 loc) · 4.96 KB
/
script.js
File metadata and controls
131 lines (120 loc) · 4.96 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
const API_HOST = "free-to-play-games-database.p.rapidapi.com";
const API_KEY = "63cfcd3668msh8999dab45fbfaf8p1941efjsn92b4b1bf298d";
let allGames = [];
// Fetch game list
function fetchGames() {
$.ajax({
url: "https://free-to-play-games-database.p.rapidapi.com/api/games",
method: "GET",
headers: {
"x-rapidapi-host": API_HOST,
"x-rapidapi-key": API_KEY
},
success: function (games) {
allGames = games;
displayGames(games);
},
error: function (err) {
console.error("Error fetching games:", err);
}
});
}
// Display game list
function displayGames(games) {
let gameListHtml = "";
games.forEach(game => {
gameListHtml += `
<div class="col-md-4">
<div class="card mb-3">
<img src="${game.thumbnail}" class="card-img-top w-100" alt="${game.title}">
<div class="card-body">
<h5 class="card-title">${game.title}</h5>
<p class="card-text" style="display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;">
${game.short_description}
</p>
<button class="btn btn-dark" onclick="fetchGameDetails(${game.id})" data-toggle="modal" data-target="#gameModal">View Details</button>
</div>
</div>
</div>
`;
});
$("#game-list").html(gameListHtml);
}
// Fetch game details
function fetchGameDetails(gameId) {
$.ajax({
url: `https://free-to-play-games-database.p.rapidapi.com/api/game?id=${gameId}`,
method: "GET",
headers: {
"x-rapidapi-host": API_HOST,
"x-rapidapi-key": API_KEY
},
success: function (game) {
let gameDetailsHtml = `
<div class="row">
<div class="col-md-4">
<img src="${game.thumbnail}" class="img-fluid w-100 mb-3" alt="${game.title}">
<a href="${game.game_url}" target="_blank" class="btn btn-success btn-block">Play Now</a>
</div>
<div class="col-md-8">
<h3>${game.title} <small class="badge badge-success">${game.status}</small></h3>
<p><strong>Genre:</strong> ${game.genre}</p>
<p><strong>Platform:</strong> ${game.platform}</p>
<p><strong>Publisher:</strong> ${game.publisher}</p>
<p><strong>Developer:</strong> ${game.developer}</p>
<p><strong>Release Date:</strong> ${game.release_date}</p>
<p><strong>Description:</strong> ${game.description}</p>
</div>
</div>
<h5 class="mt-4">Minimum System Requirements</h5>
<ul>
<li><strong>OS:</strong> ${game.minimum_system_requirements?.os || "N/A"}</li>
<li><strong>Processor:</strong> ${game.minimum_system_requirements?.processor || "N/A"}</li>
<li><strong>Memory:</strong> ${game.minimum_system_requirements?.memory || "N/A"}</li>
<li><strong>Graphics:</strong> ${game.minimum_system_requirements?.graphics || "N/A"}</li>
<li><strong>Storage:</strong> ${game.minimum_system_requirements?.storage || "N/A"}</li>
</ul>
<h5 class="mt-4">Screenshots</h5>
<div class="row">
${game.screenshots?.map(screenshot => `
<div class="col-md-4">
<img src="${screenshot.image}" class="img-fluid mb-2" alt="Screenshot">
</div>
`).join("") || "<p>No screenshots available.</p>"}
</div>
`;
$("#game-details").html(gameDetailsHtml);
},
error: function (err) {
console.error("Error fetching game details:", err);
}
});
}
// Search dan filter
function searchDanFilterGame() {
let query = $("#input-search").val().toLowerCase();
let selectedGenre = $("#genre-filter").val();
let filteredGames = allGames.filter(game =>
game.title.toLowerCase().includes(query) &&
(selectedGenre === "" || game.genre === selectedGenre)
);
if (filteredGames.length === 0) {
$("#game-list").html(`
<div class="col-12 text-center">
<h3 class="text-muted mt-5">Sorry, Game not available</h3>
</div>
`);
} else {
displayGames(filteredGames);
}
}
// Event listeners
$("#button-search").click(searchDanFilterGame);
$("#input-search").on("keypress", function (event) {
if (event.keyCode === 13) {
searchDanFilterGame();
}
});
$("#genre-filter").change(searchDanFilterGame);
// Load games
$(document).ready(fetchGames);