-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
440 lines (389 loc) · 15.5 KB
/
main.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Variables to track dice state
/*let playerDice=[]; // i.e the dice player chose to keep
let playerScore=[];
let rollCount=0;
let numFilledRowScore=0;*/
let transformValues= [
[0, 35], [-5, 45], [0, 40], [5, 45], [0, 35]
];
const playerContainer= document.querySelector(".savedDiceDisplay");
const rollButton= document.querySelector(".reroll-button");
const diceElements= document.querySelectorAll(".dice");
const scoreTableCells=document.querySelectorAll(".cell");
function rollDice() {
fetch('http://localhost:8000/yatzy.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'action=rollDice'
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// Update UI with rolled dice values
displayDice(data.randomDice, data.rollCount, data.playerDice); // Update displayDice function to handle this
console.log('Dice rolled:', data.randomDice);
console.log('Roll count:', data.rollCount);
// Check roll count from server response to enable/disable roll button
if (data.rollCount >= 2) {
rollButton.disabled = true;
rollButton.removeEventListener('click', rollDice); // Remove event listener
console.log('No more rolls allowed until score is entered.');
}
})
.catch(error => console.error('Error rolling dice:', error));
}
rollButton.addEventListener('click', function() {
if (!rollButton.disabled) {
rollDice(); // Call rollDice function to initiate a dice roll
}
});
function displayDice(dice, rollCount, playerDice) {
const playArea= document.querySelector(".DisplayOfRolling");
const diceContainer = document.querySelector(".savedDiceDisplay");
//let numDice= diceContainer.children.length;
diceElements.forEach( function(diceElement, index){
if (diceElement.classList.contains("active") || rollCount ==1){
resetDicePositions(); // cuz only 2 rolls allowed
const x = transformValues[index][0];// back to intial positions
const y = transformValues[index][1];
setTimeout(function(){
changeDiePosition(diceElement, x, y);
changeDiceFaces(dice, rollCount, playerDice);
writeTempValuesInScoreTable(playerDice);
if (rollCount == 2) {
rollButton.disabled = true;
rollButton.style.opacity = 0.5;
console.log('No more rolls allowed. Enter a score First');
writeTempValuesInScoreTable(playerDice);
}
},500)
}
});
}
function resetDicePositions() {
diceElements.forEach(function(diceElement) {
diceElement.style.transform = "none";
});
}
function changeDiePosition(diceElement,x,y){
let angle=135*Math.floor(Math.random()*10);
let diceRollDirection = 1;
angle=135*Math.floor(Math.random()*10);
diceElement.style.transform=
"translateX("+
x+"vw) translateY("+diceRollDirection*y+
"vh) rotate(" + angle + "deg)";
}
function changeDiceFaces(randomDice, rollCount, playerDice) {
for (let i=0; i < diceElements.length;i++) {
if(rollCount ===1) diceElements[i].classList.add("active");// no more rolls alllowed, all should be active, could return em back to position here
if(diceElements[i].classList.contains("active")) {
playerDice[i]=randomDice[i];
let face = diceElements[i].getElementsByClassName("face")[0];
face.src="docs/design_system/images/dice"+randomDice[i]+".png";
}
}
updatePlayerDice(playerDice);
}
function updatePlayerDice(playerDice) {
fetch('http://localhost:8000/yatzy.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'action=updatePlayerDice&randomDice=' + JSON.stringify(playerDice)
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log('Player dice updated on server:', data.playerDice);
})
.catch(error => console.error('Error updating player dice:', error));
}
function resetDiceFaces() {
for (let i=0;i<diceElements.length;i++){
let face = diceElements[i].getElementsByClassName("face")[0];
diceElements[i].classList.remove("active");
let diceNumber=i+1;
face.src="docs/design_system/images/dice"+diceNumber+".png";
}
}
function fetchRollCount() {
fetch('http://localhost:8000/yatzy.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'action=getRollCount'
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(data => {
const rollCount = parseInt(data); // Parse the response as an integer
console.log('Current roll count:', rollCount);
})
.catch(error => console.error('Error fetching roll count:', error));
}
//Dice Elements'Events listeners
diceElements.forEach(function(diceElement,index){
diceElement.addEventListener("click",function(){
if(fetchRollCount()==0) return;
diceElement.classList.toggle("active");
if(!diceElement.classList.contains("active")){
diceElement.style.transform="none";
}
else {
const diceNumber=diceElement.id.charAt(3);
const x = transformValues[diceNumber-1][0];
const y = transformValues[diceNumber-1][1];
changeDiePosition(diceElement,x,y);
}
})
})
/*function writeTempValuesInScoreTable(dice) {
let scoreTable= [];
scoreTable= playerScore.slice();
//onlyPossibleRow="blank";
let yatzyScore= calculateYatzy(dice);
const yatziElement= document.getElementById("yatzy"); //12.5 not n use
if (scoreTable[0] === undefined) {
let onesScore = calculateOnes(dice);
console.log("ones", onesScore);
document.getElementById("Ones").innerHTML = onesScore;
}
if (scoreTable[1] === undefined) {
let twosScore = calculateTwos(dice);
document.getElementById("Twos").innerHTML = twosScore;
}
if (scoreTable[2] === undefined) {
let threesScore = calculateThrees(dice);
document.getElementById("Threes").innerHTML = threesScore;
}
if (scoreTable[3] === undefined) {
let foursScore = calculateFours(dice);
document.getElementById("Fours").innerHTML = foursScore;
}
if (scoreTable[4] === undefined) {
let fivesScore = calculateFives(dice);
document.getElementById("Fives").innerHTML = fivesScore;
}
if (scoreTable[5] === undefined) {
let sixesScore = calculateSixes(dice);
document.getElementById("Sixes").innerHTML = sixesScore;
}
// Implement logic for Lower Section categories //12; skipped 6? yes cuz sum? no no need
if (scoreTable[6] === undefined) {
//console.log("got in?");
//console.log("table", scoreTable);
scoreTable[6] = calculateOnePair(dice);
document.getElementById("OnePair").innerHTML = scoreTable[6];
}
if (scoreTable[7] === undefined) {
scoreTable[7] = calculateTwoPair(dice);
document.getElementById("TwoPair").innerHTML = scoreTable[7];
}
if (scoreTable[8] === undefined) {
scoreTable[8] = calculateThreeOfAKind(dice);
//console.log("3ofK", calculateThreeOfAKind(dice));
document.getElementById("ThreeOfKind").innerHTML = scoreTable[8];
}
if (scoreTable[9] === undefined) {
scoreTable[9] = calculateFourOfAKind(dice);
document.getElementById("FourOfKind").innerHTML = scoreTable[9];
}
if (scoreTable[10] === undefined) {
scoreTable[10] = calculateSmallStraight(dice);
document.getElementById("smallStraight").innerHTML = scoreTable[10];
}
if (scoreTable[11] === undefined) {
scoreTable[11] = calculateLargeStraight(dice);
document.getElementById("LargeStraight").innerHTML = scoreTable[11];
}
if (scoreTable[12] === undefined) {
scoreTable[12] = calculateFullHouse(dice);
document.getElementById("FullHouse").innerHTML = scoreTable[12];
}
if (scoreTable[13] === undefined) {
scoreTable[13] = calculateChance(dice);
document.getElementById("chance").innerHTML = scoreTable[13];
}
if (scoreTable[14] === undefined) {
scoreTable[14] = yatzyScore; // Assign Yatzy score directly here
document.getElementById("yatzy").innerHTML = scoreTable[14];
}
console.log(scoreTable);
// Update playerScore with the calculated scores
//playerScore = scoreTable; //change 12
// Update UI or any other logic based on scoring
//console.log("Scores updated:", playerScore);
// can disable button gere as welll.
////12.1
}*/
function writeTempValuesInScoreTable() {
fetch('http://localhost:8000/yatzy.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded', // Adjusted to form-urlencoded
},
body: 'action=calculateScores', // No need to send dice data anymore
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text(); // Expecting HTML/text response
})
.then(htmlResponse => {
//console.log('HTML Response:', htmlResponse);
const response = JSON.parse(htmlResponse);
Object.keys(response).forEach(category => {
const element = document.getElementById(category);
if (element) {
element.innerHTML = response[category];
}
});
//console.log('Score table updated on client');
})
.catch(error => console.error('Error updating score table:', error));
}
scoreTableCells.forEach(function(cell){
cell.addEventListener("click",onCellClick);
});
function onCellClick(){
const id = this.getAttribute("id");
const yatzyScore = parseInt(document.getElementById("yatzy").innerHTML);
if( fetchRollCount()==0 || id===null ) return;
//playerScore[row-1]=parseInt(this.innerHTML);
fetch('http://localhost:8000/yatzy.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
action: 'enterScore',
id: id,
score: this.innerHTML, // Send updated player scores to PHP
yatzy: yatzyScore
}).toString()
})
//.then(response => response.json())
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log('JSON Response:', data);
Sum1.innerHTML = data.upperSectionScore;
bonus1.innerHTML = data.bonusScore;
//total1.innerHTML = data.totalScore;
console.log("this is total", data.totalScore);
this.removeEventListener("click",onCellClick);
this.style.color="green";
this.style.cursor= "default";
Sum1.style.color="green";
bonus1.style.color="green";
//total1.style.color="green";
// Enable roll button for next turn
rollButton.disabled = false;
rollButton.style.opacity = 1;
updateScoreTable(data.gameState.playerScore);
resetDiceFaces();
if(data.gameState.numFilledRowScore==15) {
calculateEndGameScore();
//console.log("print end");
return;
}
})
.catch(error => {
console.error('Error:', error);
});
}
function calculateEndGameScore() {
/*let playerTotal=parseInt(document.getElementById("total1").innerHTML);//12.1 does totalproper
const endGameMessage= "End of Game. Your total score is" + playerTotal;*/
endGameMessage= "End of Game Round. You can play another round after closing this window! \n Your total score for this round was: ";
resetDicePositions();
// Display modal with end game message
const modal = document.getElementById("endGameModal");
const messageElement = document.getElementById("endGameMessage");
const closeModal = document.getElementById("closeModal");
// Fetch leaderboard data
fetch('http://localhost:8000/yatzy.php?action=getLeaderboard')// get request
.then(response => response.json())
.then(data => {
endGameMessage+= data.total;
// Construct leaderboard message
let leaderboardMessage = "<h3>Leaderboard -- Top Scores:</h3><ul>";
data.top_scores.forEach((score, index) => {
leaderboardMessage += `<li>#${index + 1}: ${score}</li>`;
});
leaderboardMessage += "</ul>";
// Set end game message and leaderboard in modal
messageElement.innerHTML = endGameMessage + leaderboardMessage;
modal.style.display = "block";
})
.catch(error => {
console.error('Error fetching leaderboard:', error);
// Fallback message if fetch fails
messageElement.textContent = endGameMessage + " (Leaderboard data could not be retrieved)";
modal.style.display = "block";
});
// Close modal when close button is clicked
closeModal.addEventListener("click", function() {
modal.style.display = "none";
});
updateScoringTable();
}
function updateScoreTable(playerScore) {
//console.log(playerScore);
// Convert playerScore object to an array of objects for easier iteration
let scoreTable = Object.keys(playerScore).map(key => {
return { category: key, score: playerScore[key] };
});
console.log("score table after player score copy?",scoreTable);
// Clear all score cells initially
let scoreCells = document.querySelectorAll('[data-column="1"]');
scoreCells.forEach(cell => {
let category = cell.getAttribute('id');
let scoreObj = scoreTable.find(item => item.category === category);
//console.log("score obj found?", scoreObj);
if (scoreObj) {
cell.innerHTML = scoreObj.score; // Update cell with score
} else {
cell.innerHTML = ''; // Clear cell if category score is not defined
}
});
}
function updateScoringTable() {
// Clear all score cells initially
let scoreCells = document.querySelectorAll('[data-column="1"]');
scoreCells.forEach(cell => {
cell.innerHTML = '';
cell.style.color="white";
cell.style.cursor = 'pointer';
// Restore event listener for the new round.
cell.addEventListener('click', onCellClick);
});
Sum1.style.color="white";
bonus1.style.color="white";
total1.style.color="white";
Sum1.innerHTML = '';
bonus1.innerHTML = '';
total1.innerHTML = '';
}