forked from ChromeGaming/GameSphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
159 lines (135 loc) · 4.48 KB
/
script.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
// API Keys
let MOVIEDB_API_KEY = '';
let GIPHY_API_KEY = '';
while (MOVIEDB_API_KEY === '') MOVIEDB_API_KEY = prompt('Enter IMDB API Key - ');
while (GIPHY_API_KEY === '') GIPHY_API_KEY = prompt('Enter Giphy API Key - ');
// Global Variables
let moves = 0;
let moviesArray;
// Audio elements
const bgMusic = new Audio('./sounds/bgMusic.mp3');
bgMusic.volume = 0.3;
const correctAns = new Audio('./sounds/correct.mp3');
const gameOver = new Audio('./sounds/gameOver.wav');
const wrongAns = new Audio('./sounds/wrong.mp3');
async function getMoviesPage(pageNum) {
$.get(`https://api.themoviedb.org/3/movie/top_rated?api_key=${MOVIEDB_API_KEY}&page=${pageNum}`, response => {
const movieTitles = response.results.map(movie => movie.title);
moviesArray.push(...movieTitles);
});
}
// restore moviesArray each time getAllMovies is invoked (reset game);
async function getAllMovies() {
moviesArray = [];
for (let i = 1; i <= 5; i++) {
await getMoviesPage(i);
}
}
function randomElement(array) {
const length = array.length;
const randomIndex = Math.floor(Math.random() * length);
return array[randomIndex];
}
function getRandomMovies(array, movieAmount) {
let newArray = [];
for (let i = 0; i < movieAmount; i++) {
newArray.push(randomElement(array));
}
return newArray;
}
function prepareMoviesAndGifData(array) {
// 10 movies per set
const randomMovies = getRandomMovies(array, 10);
// 1 movie out of 10 selected - goes also to GIF request
const randomMovie = getRandomMovies(randomMovies, 1);
return [randomMovies, randomMovie[0]];
}
async function getAndRenderGif(movieTitle) {
$.get(`https://api.giphy.com/v1/gifs/search?api_key=${GIPHY_API_KEY}&q=${movieTitle}&limit=20&offset=0&rating=G&lang=en`, response => {
return response.data;
})
.then(gifs => {
const gif = $('#giphy');
gif.data('title', movieTitle);
gif.attr('src', randomElement(gifs.data).images['original'].url);
});
}
function filterAndShuffleMovies(array, movie) {
let movieList = [movie];
for (let i = 0; i < 3; i++) {
const movie = randomElement(array);
// push unique title, otherwise count again
!movieList.includes(movie) ? movieList.push(movie) : i--;
}
// returns randomly sorted movieList array
return movieList.sort(() => Math.random() - 0.5);
}
function renderMovieTitles(array) {
const movieList = $('#movies');
for (let i = 0; i < 4; i++) {
movieList.append(`<button class="movie ui button" style="background-color: rgba(255, 255, 255, 0.4)">${array[i]}</button>`);
}
}
function checkAnswer(htmlElement) {
const correntAnswer = $('#giphy').data('title');
const result = htmlElement.innerText === correntAnswer;
const answers = Number($('#answers').text());
moves++;
if (moves === 10) {
gameOver.play();
if (result) { $('#answers').text(answers + 1) }
$('#final-score').text($('#answers').text());
$('.ui.modal.end-game').modal('setting', 'closable', false).modal('show');
} else {
if (result) {
correctAns.play();
$('#answers').text(answers + 1);
$('.ui.modal.correct').modal('show');
} else {
wrongAns.play();
$('.ui.modal.incorrect').modal('show');
}
}
return result;
}
function renderAllComponents(array) {
const moviesData = prepareMoviesAndGifData(moviesArray);
const [tenMovies, oneMovie] = moviesData;
const filteredMovies = filterAndShuffleMovies(tenMovies, oneMovie);
renderMovieTitles(filteredMovies);
getAndRenderGif(oneMovie);
}
function updateComponents() {
$('.movie').remove();
}
function startGame() {
getAllMovies();
setTimeout(() => {
renderAllComponents(moviesArray);
$('#giphy').css('opacity', '1');
}, 1000);
}
function restartGame() {
moves = 0;
moviesArray = [];
$('#answers').text(moves);
updateComponents();
startGame();
}
$('#giphy').on('click', (event) => {
console.log($(event.target).data('title'));
});
$('#movies').on('click', '.movie', (event) => {
$('#giphy').css('opacity', '0');
const answer = checkAnswer(event.target);
updateComponents(answer);
renderAllComponents();
});
$('.actions').on('click', '.next', () => {
$('#giphy').css('opacity', '1');
});
$('#restart').on('click', restartGame);
document.addEventListener('click', () => {
bgMusic.play();
});
startGame();