-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.js
More file actions
368 lines (317 loc) · 14.5 KB
/
Copy pathcontrol.js
File metadata and controls
368 lines (317 loc) · 14.5 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
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
const STARTING_POINTS = 501;
let player1 = { name: "", score: STARTING_POINTS, legs: 0, sets: 0, country: "it", totalPointsScored: 0, dartsThrown: 0 };
let player2 = { name: "", score: STARTING_POINTS, legs: 0, sets: 0, country: "it", totalPointsScored: 0, dartsThrown: 0 };
let legsToWinSet = 3;
let setsToWinMatch = 2;
let customMessage = "";
let customMessageBottom = "";
let currentTurnPlayerId = 1;
let startingPlayerIdForLeg = 1; // Nuovo: Tieni traccia del giocatore che inizia il leg
const history = [];
function saveGameData() {
// Aggiorna le variabili globali con i valori correnti degli input prima di salvare
player1.name = document.getElementById("player1Name").value.trim();
player2.name = document.getElementById("player2Name").value.trim();
player1.country = document.getElementById("player1Country").value;
player2.country = document.getElementById("player2Country").value;
legsToWinSet = parseInt(document.getElementById("legsToWinSet").value) || 3;
setsToWinMatch = parseInt(document.getElementById("setsToWinMatch").value) || 2;
customMessage = document.getElementById("customText").value.trim();
customMessageBottom = document.getElementById("customTextBottom").value.trim();
const gameData = {
player1: player1,
player2: player2,
legsToWinSet: legsToWinSet,
setsToWinMatch: setsToWinMatch,
customMessage: customMessage,
customMessageBottom: customMessageBottom,
currentTurnPlayerId: currentTurnPlayerId,
startingPlayerIdForLeg: startingPlayerIdForLeg, // Salva anche questa variabile
history: history
};
localStorage.setItem('dartsGameData', JSON.stringify(gameData));
updateDisplay();
}
function loadGameData() {
const storedData = localStorage.getItem('dartsGameData');
if (storedData) {
const gameData = JSON.parse(storedData);
player1 = gameData.player1;
player2 = gameData.player2;
legsToWinSet = gameData.legsToWinSet;
setsToWinMatch = gameData.setsToWinMatch;
customMessage = gameData.customMessage;
customMessageBottom = gameData.customMessageBottom || "";
currentTurnPlayerId = gameData.currentTurnPlayerId || 1;
startingPlayerIdForLeg = gameData.startingPlayerIdForLeg || 1; // Carica la variabile
player1.totalPointsScored = player1.totalPointsScored || 0;
player1.dartsThrown = player1.dartsThrown || 0;
player2.totalPointsScored = player2.totalPointsScored || 0;
player2.dartsThrown = player2.dartsThrown || 0;
document.getElementById("player1Name").value = player1.name;
document.getElementById("player2Name").value = player2.name;
document.getElementById("legsToWinSet").value = legsToWinSet;
document.getElementById("setsToWinMatch").value = setsToWinMatch;
document.getElementById("customText").value = customMessage;
document.getElementById("customTextBottom").value = customMessageBottom;
document.getElementById("player1Country").value = player1.country || "it";
document.getElementById("player2Country").value = player2.country || "it";
if (player1.name !== "" && player2.name !== "") {
document.getElementById("setupSection").style.display = "none";
document.getElementById("gameArea").style.display = "flex";
}
}
updateDisplay();
}
function startGame() {
saveGameData();
player1.score = STARTING_POINTS;
player1.legs = 0;
player1.sets = 0;
player1.totalPointsScored = 0;
player1.dartsThrown = 0;
player2.score = STARTING_POINTS;
player2.legs = 0;
player2.sets = 0;
player2.totalPointsScored = 0;
player2.dartsThrown = 0;
history.length = 0;
currentTurnPlayerId = 1;
startingPlayerIdForLeg = 1; // All'inizio della partita, il giocatore 1 inizia
document.getElementById("winnerMessage").innerText = "";
document.getElementById("setupSection").style.display = "none";
document.getElementById("gameArea").style.display = "flex";
document.getElementById("scoreInput").focus();
saveGameData();
}
function updateDisplay() {
document.getElementById("player1Flag").src = `flags/${player1.country || 'it'}.svg`;
document.getElementById("player2Flag").src = `flags/${player2.country || 'it'}.svg`;
document.getElementById("player1DisplayName").innerText = player1.name;
document.getElementById("player2DisplayName").innerText = player2.name;
document.getElementById("player1LegsDisplay").innerText = player1.legs;
document.getElementById("player2LegsDisplay").innerText = player2.legs;
document.getElementById("player1SetsDisplay").innerText = player1.sets;
document.getElementById("player2SetsDisplay").innerText = player2.sets;
document.getElementById("player1ScoreDisplay").innerText = player1.score;
document.getElementById("player2ScoreDisplay").innerText = player2.score;
const avg1 = player1.dartsThrown > 0 ? ((player1.totalPointsScored / player1.dartsThrown) * 3).toFixed(2) : '0.00';
const avg2 = player2.dartsThrown > 0 ? ((player2.totalPointsScored / player2.dartsThrown) * 3).toFixed(2) : '0.00';
document.getElementById("player1AvgDisplay").innerText = avg1;
document.getElementById("player2AvgDisplay").innerText = avg2;
// Aggiorna l'indicatore di turno
if (currentTurnPlayerId === 1) {
document.getElementById("player1Card").classList.add("active");
document.getElementById("player2Card").classList.remove("active");
} else {
document.getElementById("player2Card").classList.add("active");
document.getElementById("player1Card").classList.remove("active");
}
checkMatchWinner();
}
function appendToInput(num) {
const input = document.getElementById("scoreInput");
if (input.value === "" && num === "0") return;
input.value += num;
}
function clearInput() {
document.getElementById("scoreInput").value = "";
}
function preventNonNumericInput(inputElement) {
inputElement.value = inputElement.value.replace(/[^0-9]/g, "");
}
function registerScore() {
const inputElement = document.getElementById("scoreInput");
const points = parseInt(inputElement.value);
if (isNaN(points) || points <= 0 || points > 180) {
alert("Inserisci un punteggio valido (un numero tra 1 e 180).");
clearInput();
return;
}
const impossibleScores = [179, 178, 176, 175, 173, 172, 169, 166, 163];
if (impossibleScores.includes(points)) {
alert("Bust! Il tiro non è valido per chiudere o lascia 1 punto.");
clearInput();
return;
}
const currentPlayer = currentTurnPlayerId === 1 ? player1 : player2;
const newScore = currentPlayer.score - points;
const prevPlayer1State = JSON.parse(JSON.stringify(player1));
const prevPlayer2State = JSON.parse(JSON.stringify(player2));
const prevTurnPlayerId = currentTurnPlayerId; // Salva il turno prima del cambio
const prevStartingPlayerIdForLeg = startingPlayerIdForLeg; // Salva il giocatore di inizio leg
currentPlayer.totalPointsScored += points;
currentPlayer.dartsThrown += 3;
if (newScore === 0) {
alert(`${currentPlayer.name} ha vinto il leg!`);
currentPlayer.legs++;
history.push({
player: prevTurnPlayerId,
points: points,
action: 'score',
legWon: true,
prevPlayer1State: prevPlayer1State,
prevPlayer2State: prevPlayer2State,
prevStartingPlayerIdForLeg: prevStartingPlayerIdForLeg
});
// Aggiungi una pausa per permettere all'utente di leggere l'alert
setTimeout(() => {
checkLegWinner();
}, 100);
} else if (newScore < 0 || newScore === 1) {
alert("Bust! Il tiro non è valido per chiudere o lascia 1 punto.");
history.push({
player: prevTurnPlayerId,
points: points,
action: 'bust',
prevPlayer1State: prevPlayer1State,
prevPlayer2State: prevPlayer2State,
prevStartingPlayerIdForLeg: prevStartingPlayerIdForLeg
});
currentPlayer.totalPointsScored -= points;
currentPlayer.dartsThrown -= 3;
} else {
currentPlayer.score = newScore;
history.push({
player: prevTurnPlayerId,
points: points,
action: 'score',
prevPlayer1State: prevPlayer1State,
prevPlayer2State: prevPlayer2State,
prevStartingPlayerIdForLeg: prevStartingPlayerIdForLeg
});
}
clearInput();
// Se un leg non è stato vinto, cambia il turno come al solito
if (newScore !== 0) {
changeTurn();
}
saveGameData();
}
function changeTurn() {
currentTurnPlayerId = currentTurnPlayerId === 1 ? 2 : 1;
document.getElementById("scoreInput").focus();
saveGameData();
}
// Funzione modificata per gestire la vittoria del set
function checkLegWinner() {
if (player1.legs === legsToWinSet) {
alert(`${player1.name} ha vinto il Set!`);
player1.sets++;
resetLegsForNewSet();
} else if (player2.legs === legsToWinSet) {
alert(`${player2.name} ha vinto il Set!`);
player2.sets++;
resetLegsForNewSet();
}
// Avvia un nuovo leg
startNewLeg();
}
// Funzione per iniziare un nuovo leg
function startNewLeg() {
player1.score = STARTING_POINTS;
player2.score = STARTING_POINTS;
player1.totalPointsScored = 0; player1.dartsThrown = 0;
player2.totalPointsScored = 0; player2.dartsThrown = 0;
history.length = 0;
// Alterna il giocatore che inizia il leg
startingPlayerIdForLeg = startingPlayerIdForLeg === 1 ? 2 : 1;
currentTurnPlayerId = startingPlayerIdForLeg;
saveGameData();
checkMatchWinner(); // Controlla se qualcuno ha vinto il match dopo il reset del leg
}
function resetLegsForNewSet() {
player1.legs = 0;
player2.legs = 0;
// La logica per il reset dei punteggi e delle statistiche è stata spostata in startNewLeg()
}
function checkMatchWinner() {
let winner = null;
if (player1.sets === setsToWinMatch) {
winner = player1.name;
} else if (player2.sets === setsToWinMatch) {
winner = player2.name;
}
if (winner) {
document.getElementById("winnerMessage").innerText = `${winner} vince la ${customMessage.toLowerCase() || 'partita'}! Complimenti!`;
disableGameControls(true);
} else {
document.getElementById("winnerMessage").innerText = "";
disableGameControls(false);
}
}
function disableGameControls(disable) {
document.getElementById("scoreInput").disabled = disable;
document.querySelectorAll(".numpad-and-actions-grid button").forEach(btn => btn.disabled = disable);
document.querySelectorAll(".game-controls button").forEach(btn => btn.disabled = disable);
}
function resetLeg() {
if (confirm("Sei sicuro di voler resettare il leg corrente?")) {
// La logica per il reset del leg è stata spostata in startNewLeg(), quindi la chiamiamo qui
startNewLeg();
saveGameData();
}
}
function resetMatch() {
if (confirm("Sei sicuro di voler resettare l'intera partita?")) {
player1 = { name: "", score: STARTING_POINTS, legs: 0, sets: 0, country: "it", totalPointsScored: 0, dartsThrown: 0 };
player2 = { name: "", score: STARTING_POINTS, legs: 0, sets: 0, country: "it", totalPointsScored: 0, dartsThrown: 0 };
legsToWinSet = 3;
setsToWinMatch = 2;
customMessage = "";
customMessageBottom = "";
currentTurnPlayerId = 1;
startingPlayerIdForLeg = 1; // Resetta anche il giocatore di inizio leg
history.length = 0;
document.getElementById("winnerMessage").innerText = "";
disableGameControls(false);
document.getElementById("player1Name").value = "";
document.getElementById("player2Name").value = "";
document.getElementById("legsToWinSet").value = 3;
document.getElementById("setsToWinMatch").value = 2;
document.getElementById("customText").value = "";
document.getElementById("customTextBottom").value = "";
document.getElementById("player1Country").value = "it";
document.getElementById("player2Country").value = "it";
document.getElementById("setupSection").style.display = "flex";
document.getElementById("gameArea").style.display = "none";
saveGameData();
}
}
function undoLastScore() {
if (history.length === 0) {
alert("Nessun tiro da annullare.");
return;
}
const lastAction = history.pop();
player1 = JSON.parse(JSON.stringify(lastAction.prevPlayer1State));
player2 = JSON.parse(JSON.stringify(lastAction.prevPlayer2State));
currentTurnPlayerId = lastAction.player;
startingPlayerIdForLeg = lastAction.prevStartingPlayerIdForLeg; // Ripristina anche il giocatore di inizio leg
saveGameData();
}
function changeStartingPlayerForLeg() {
if (confirm("Vuoi cambiare il giocatore che inizia il leg corrente?")) {
startingPlayerIdForLeg = startingPlayerIdForLeg === 1 ? 2 : 1;
currentTurnPlayerId = startingPlayerIdForLeg;
alert(`Il giocatore che inizia il prossimo turno è ora: ${currentTurnPlayerId === 1 ? player1.name : player2.name}`);
saveGameData();
}
}
document.getElementById("scoreInput").addEventListener("keydown", function(event) {
if (event.key === "Enter") {
event.preventDefault();
registerScore();
}
});
document.addEventListener('DOMContentLoaded', () => {
loadGameData();
document.getElementById("player1Name").addEventListener("input", saveGameData);
document.getElementById("player2Name").addEventListener("input", saveGameData);
document.getElementById("player1Country").addEventListener("change", saveGameData);
document.getElementById("player2Country").addEventListener("change", saveGameData);
document.getElementById("legsToWinSet").addEventListener("input", saveGameData);
document.getElementById("setsToWinMatch").addEventListener("input", saveGameData);
document.getElementById("customText").addEventListener("input", saveGameData);
document.getElementById("customTextBottom").addEventListener("input", saveGameData);
});