-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.html
263 lines (252 loc) · 9.42 KB
/
game.html
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
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BuklakiTheGame - playing</title>
<meta name="description" content="BuklakiTheGame - gra na projekt zespolowy UWM.">
<meta name="keywords" content="Buklaki The Game">
<meta name="author" content="Kropka">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="#">
<link href="static/css/bootstrap.min.css" rel="stylesheet">
<link href="static/custom/global.css" rel="stylesheet">
<link href="static/custom/game.css" rel="stylesheet">
</head>
<body>
<div id="main-div">
<div id="basket-amount"><span></span></div>
<div id="status" class="menu curr-menu">
<div class="flex-center">
<span class="text-start">Oaza</span>
<div id="phases" class="text-center"></div>
<span class="text-end">Cel</span>
</div>
</div>
<div id="pause" class="menu">
<h1>Pauza</h1>
<h3><span><a href="#" data-action="play-game">Wróć do gry</a></span></h3>
<h3>
<span><a href="#" data-action="solve-and-finish">Rozwiąż i zakończ</a></span>
<div class="warning-sign">UWAGA! Wykluczenie z rankingu dla tej gry.</div>
</h3>
<h3>
<span><a href="#" data-action="back-to-menu">Przejdź do menu</a></span>
<div class="warning-sign">UWAGA! Utrata postępu.</div>
</h3>
</div>
<div id="gameover" class="menu">
<h1>Porażka!</h1>
<h3>
<span><a href="#" data-action="back-to-menu">Wróć do menu.</a></span>
</h3>
</div>
<div id="success" class="menu">
<h1>Sukces!</h1>
<h3>Osiągnąłeś wynik (liczba dni):</h3>
<h4><span id="days">9999</span></h4>
<h3><span><a href="#" data-action="confirm">Potwierdź.</a></span></h3>
<h3><span><a href="#" data-action="back-to-menu">Wróć do menu.</a></span></h3>
</div>
</div>
<script src="static/js/jquery-3.6.0.min.js"></script>
<script src="static/js/js.cookie.min.js"></script>
<script src="static/js/bootstrap.min.js"></script>
<script src="static/custom/global-utils.js"></script>
<script src="static/custom/trans-handler.js"></script>
<script>
const PHASE_SPAN = '<div class="phase"><span class="top"></span><span class="bottom"></span></div>';
let currPhase = 0;
let days = 0;
let generateSpans = (number) => {
$('.phase').remove();
for(let step=1; step<=number; step++){
$('#phases').append($(PHASE_SPAN).attr('data-step', step))
}
}
let getWaterAmount = () => {
return parseInt($('#basket-amount').find('span').text());
}
let setWaterAmount = (amount) => {
return $('#basket-amount').find('span').text(amount);
}
$(document).ready(() => {
LOGGER.success('BuklakiTheGame v0.001 initialized correctly!');
LOGGER.info('CURRENT STAGE: GAME');
let distance = 8;
if(Cookies.get('distance')) distance = Cookies.get('distance');
generateSpans(distance);
let basketAmount = 5;
if(Cookies.get('basket')) basketAmount = Cookies.get('basket');
let initialBasketAmount = basketAmount;
$('#basket-amount').find('span').text(basketAmount);
let musicPlaying = false;
let getBody = $('body');
getBody.on('click', (e) => {
if (!musicPlaying) {
let audio = new Audio('static/sound/main-ost.mp3');
audio.play();
audio.addEventListener('ended', function () {
this.currentTime = 0;
this.play();
}, false);
musicPlaying = true;
}
});
const saveScoreToBackend = () => {
$.ajax({
url: '/buklakithebackend/handle-record/',
type: 'POST',
data: {
'name': 'ABC',
'days': parseInt($('#days').text()),
'basket_amount': getWaterAmount(),
'distance': distance,
},
timeout: 1000,
async: false,
success: (data) => {
if(data['is_finished']) {
LOGGER.info('Sent score to backend system.')
return true;
}
return false;
},
fail: (jqXHR, textStatus, errorThrown) => {
LOGGER.error('Request got ignored.')
return false;
},
})
}
const handleChoiceSelect = () => {
let menuItem = $(jqueryGet('.curr-menu').find('h3')[getMenuPropertyIndex()]);
let action = menuItem.find('a').data('action');
switch (action){
case 'play-game':
switchMenus('#status');
break;
case 'confirm':
saveScoreToBackend();
window.location.href = 'main.html';
break;
case 'back-to-menu':
window.location.href = 'main.html';
break;
}
}
$('.menu').find('h1, h3').each((index, entry) => {
$(entry).find('a').each((index, anotherEntry) => {
$(anotherEntry).hover((e) => {
const currEntry = $(e.currentTarget);
$('.arrows').remove();
$('.menu-selected').each((index, item) => {$(item).removeClass('menu-selected')})
if(currEntry.data('arrows') !== 'no'){
currEntry.prepend($(ARROW_SPAN).text('--> ')).append($(ARROW_SPAN).text(' <--'))
}
currEntry.addClass('menu-selected');
})
$(anotherEntry).click((e) => {
handleChoiceSelect();
})
})
})
getBody.keydown((e) => {
let waterAmount = getWaterAmount();
let ignoreWindows = ['status', 'success'];
let currentMenu = $('.curr-menu');
if(currentMenu.attr('id') === 'status'){
if(checkIfPressed('RIGHT', e.keyCode)){
if(waterAmount >= 0){
currPhase += 1
let step = $(`[data-step=${currPhase}]`);
step.find('.top').text('X');
days += 1;
if(currPhase <= distance){
waterAmount -= 1;
setWaterAmount(waterAmount);
}
}
} else if(checkIfPressed('LEFT', e.keyCode)){
if(waterAmount < 0){
switchMenus('#gameover');
}
else if(waterAmount >= 0 && currPhase > 0){
waterAmount -= 1;
let step = $(`[data-step=${currPhase}]`);
step.find('.top').text('');
currPhase -= 1;
setWaterAmount(waterAmount);
days += 1;
}
} else if(checkIfPressed('DOWN', e.keyCode)) {
if(waterAmount > 1){
let step = $(`[data-step=${currPhase}]`);
let getHiddenBasketVal = parseInt(step.find('.bottom').text());
if(!getHiddenBasketVal) getHiddenBasketVal = 1;
else getHiddenBasketVal += 1;
step.find('.bottom').text(getHiddenBasketVal);
waterAmount -= 1;
setWaterAmount(waterAmount);
}
} else if(checkIfPressed('UP', e.keyCode)) {
let step = $(`[data-step=${currPhase}]`);
let getHiddenBasketVal = parseInt(step.find('.bottom').text());
if (getHiddenBasketVal && getHiddenBasketVal > 0 && getWaterAmount() < basketAmount) {
step.find('.bottom').text(getHiddenBasketVal - 1);
waterAmount += 1;
setWaterAmount(waterAmount)
}
}
}
if(checkIfPressed('ESC', e.keyCode)){
let excludeIds = ['gameover', 'success']
if(!excludeIds.includes(currentMenu.attr('id'))){
if(!$('#pause').hasClass('curr-menu'))
switchMenus('#pause');
else switchMenus('#status');
}
} else if(currentMenu.attr('id') !== 'status'){
let getMenuItems = $('.curr-menu').find('h3');
if(checkIfPressed('ENTER', e.keyCode)){
$('.menu-selected').click();
} else {
let menuSelected = $('.menu-selected');
let getIndex = 0;
if(!menuSelected.length){
let curr = $(getMenuItems[getIndex]);
if(curr.find('a').data('arrows') !== 'no') {
curr.find('a').prepend($(ARROW_SPAN).text('--> ')).append($(ARROW_SPAN).text(' <--'));
}
curr.addClass('menu-selected');
} else if(menuSelected.length){
getIndex = getMenuPropertyIndex();
if(checkIfPressed('UP', e.keyCode)) getIndex = (getIndex % getMenuItems.length) - 1;
else if(checkIfPressed('DOWN', e.keyCode)) getIndex = (getIndex % getMenuItems.length) + 1;
$('.arrows').remove();
if(getIndex < 0) getIndex = getMenuItems.length - 1;
else if(getIndex > getMenuItems.length - 1) getIndex = 0;
let curr = $(getMenuItems[getIndex]);
if(curr.find('a').data('arrows') !== 'no')
$(getMenuItems[getIndex]).find('a').prepend($(ARROW_SPAN).text('--> ')).append($(ARROW_SPAN).text(' <--'))
}
menuSelected.each((index, item) => {$(item).removeClass('menu-selected')})
$(getMenuItems[getIndex]).find('a').addClass('menu-selected');
}
}
let getGameover = $('#gameover');
if(currentMenu.attr('id') === 'status'){
if(!(getWaterAmount() >= 0) && currPhase <= distance && currPhase > 0 && !getGameover.hasClass('curr-menu')){
switchMenus('#gameover');
setWaterAmount(0);
} else if(!currPhase) setWaterAmount(initialBasketAmount);
else if(currPhase > distance && currentMenu.attr('id') !== 'gameover') {
$('#days').text(days);
switchMenus('#success');
}
}
});
})
</script>
</body>
</html>