-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
454 lines (404 loc) · 18 KB
/
script.js
File metadata and controls
454 lines (404 loc) · 18 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// --- START OF REFACTORED script.js ---
// =================================================================
// MODEL: Centralized Application State
// =================================================================
// This is the single source of truth for the entire application.
const appState = {
currentGameMode: null, // '8-ball', '9-ball', '10-ball', '14.1 Continuous', etc.
standardGame: {
player1Name: 'Home',
player2Name: 'Guest',
player1Score: 0,
player2Score: 0,
},
continuousGame: {
player1Name: 'Player 1',
player2Name: 'Player 2',
currentScoreP1: 0,
highRunP1: 0,
inningsP1: 0,
currentScoreP2: 0,
highRunP2: 0,
inningsP2: 0,
ballsRemaining: 15,
currentInningBallsPotted: 0,
history: [],
rackHistory: [],
inningHistory: [],
currentPlayer: 1,
gameMode: 1, // 1 for Trainer, 2 for 2-Player
},
};
// =================================================================
// VIEW: UI Update Function
// =================================================================
// This single function reads from the 'appState' model and updates the DOM.
function updateUI() {
// Show/hide main containers
const gameSelection = document.getElementById('game-selection');
const playerInput = document.getElementById('player-input');
const scoreboard = document.getElementById('scoreboard');
const gameScreen = document.getElementById('game-screen');
gameSelection.style.display = 'none';
playerInput.style.display = 'none';
scoreboard.style.display = 'none';
gameScreen.style.display = 'none';
if (!appState.currentGameMode) {
gameSelection.style.display = 'flex';
} else if (appState.currentGameMode === 'player-input') {
playerInput.style.display = 'block';
} else if (['8-ball', '9-ball', '10-ball'].includes(appState.currentGameMode)) {
scoreboard.style.display = 'block';
document.getElementById('standard-scoreboard').style.display = 'flex';
document.getElementById('game-title').textContent = appState.currentGameMode;
document.getElementById('player1-name').textContent = appState.standardGame.player1Name;
document.getElementById('player2-name').textContent = appState.standardGame.player2Name;
document.getElementById('player1-score').textContent = appState.standardGame.player1Score;
document.getElementById('player2-score').textContent = appState.standardGame.player2Score;
} else if (['14.1 Continuous', '14.1 Continuous Trainer'].includes(appState.currentGameMode)) {
gameScreen.style.display = 'flex';
// Note: 14.1 screen elements are updated within their own logic for now, but could be moved here.
document.getElementById('player1-name-continuous').value = appState.continuousGame.player1Name;
document.getElementById('player2-name-continuous').value = appState.continuousGame.player2Name;
document.getElementById('current-score-p1').textContent = appState.continuousGame.currentScoreP1;
document.getElementById('high-run-p1').textContent = appState.continuousGame.highRunP1;
document.getElementById('innings-p1').textContent = appState.continuousGame.inningsP1;
document.getElementById('current-score-p2').textContent = appState.continuousGame.currentScoreP2;
document.getElementById('high-run-p2').textContent = appState.continuousGame.highRunP2;
document.getElementById('innings-p2').textContent = appState.continuousGame.inningsP2;
document.getElementById('balls-on-table').textContent = appState.continuousGame.ballsRemaining;
document.getElementById('current-player').textContent = appState.continuousGame.currentPlayer;
newRackBtn.disabled = false;
}
}
// =================================================================
// CONTROLLER: Application Logic and Event Handlers
// =================================================================
function selectGameMode(mode) {
appState.currentGameMode = mode;
if (mode === '14.1 Continuous' || mode === '14.1 Continuous Trainer') {
showGameScreen(mode); // This also resets the game state
} else if (mode) {
// Show player input for unhandled modes, or go straight to game
startGame();
} else {
updateUI();
}
}
document.addEventListener('DOMContentLoaded', function() {
loadGameHistory();
initializeGame();
updateUI(); // Initial render based on default state
});
function startGame() {
if (!appState.currentGameMode) return;
if (['8-ball', '9-ball', '10-ball'].includes(appState.currentGameMode)) {
// For simple games, reset scores and names directly
appState.standardGame.player1Name = document.getElementById('player1').value || 'Home';
appState.standardGame.player2Name = document.getElementById('player2').value || 'Guest';
appState.standardGame.player1Score = 0;
appState.standardGame.player2Score = 0;
}
// For other games, player input might be handled differently, but we still update the UI
updateUI();
}
function incrementScore(player) {
if (player === 1) {
appState.standardGame.player1Score++;
} else {
appState.standardGame.player2Score++;
}
updateUI();
}
function decrementScore(player) {
if (player === 1) {
appState.standardGame.player1Score = Math.max(0, appState.standardGame.player1Score - 1);
} else {
appState.standardGame.player2Score = Math.max(0, appState.standardGame.player2Score - 1);
}
updateUI();
}
function endGame() {
saveGameToHistory();
resetGameState(appState.currentGameMode);
appState.currentGameMode = null;
loadGameHistory();
updateUI();
}
// --- 14.1 Continuous Game Functions (Now using appState.continuousGame) ---
const initializeGame = () => {
createRemainingBallsButtons();
};
const showGameScreen = (mode) => {
appState.currentGameMode = mode;
appState.continuousGame.gameMode = (mode === '14.1 Continuous') ? 2 : 1;
const playerNameInputP2 = document.getElementById('player2-name-continuous');
const currentScoreDisplayP2 = document.getElementById('current-score-p2');
const highRunDisplayP2 = document.getElementById('high-run-p2');
const inningsDisplayP2 = document.getElementById('innings-p2');
const currentPlayerDisplayParent = document.getElementById('current-player-display');
const isTrainer = mode === '14.1 Continuous Trainer';
playerNameInputP2.parentElement.style.display = isTrainer ? 'none' : 'block';
currentScoreDisplayP2.parentElement.style.display = isTrainer ? 'none' : 'block';
highRunDisplayP2.parentElement.style.display = isTrainer ? 'none' : 'block';
inningsDisplayP2.parentElement.style.display = isTrainer ? 'none' : 'block';
currentPlayerDisplayParent.style.display = isTrainer ? 'none' : 'block';
resetGame(false); // Resets the continuous game state
};
const loadGameHistory = () => {
const history = JSON.parse(localStorage.getItem('gameHistory')) || [];
const historyList = document.getElementById('history-list');
historyList.innerHTML = ''; // Clear the list
if (history.length === 0) {
historyList.innerHTML = '<li>No games played yet.</li>';
} else {
history.slice().reverse().forEach((game) => {
const listItem = document.createElement('li');
let text = `<strong>${game.mode}:</strong> `;
if (game.mode === '14.1 Continuous' || game.mode === '14.1 Continuous Trainer') {
text += `${game.date} - <strong>${game.player1}</strong> (Innings: ${game.inningsP1}, Potted: ${game.ballsPottedP1})`;
if (game.mode === '14.1 Continuous') {
text += `; <strong>${game.player2}</strong> (Innings: ${game.inningsP2}, Potted: ${game.ballsPottedP2})`;
}
} else {
text += `${game.player1} ${game.score1} - ${game.score2} ${game.player2}`;
}
listItem.innerHTML = text;
historyList.appendChild(listItem);
});
}
};
const saveGameToHistory = () => {
const history = JSON.parse(localStorage.getItem('gameHistory')) || [];
const today = new Date();
const dateString = today.toLocaleDateString();
const timeString = today.toLocaleTimeString();
let gameData = { mode: appState.currentGameMode };
if (['14.1 Continuous', '14.1 Continuous Trainer'].includes(appState.currentGameMode)) {
Object.assign(gameData, {
date: `${dateString} ${timeString}`,
player1: appState.continuousGame.player1Name,
player2: appState.continuousGame.gameMode === 2 ? appState.continuousGame.player2Name : null,
inningsP1: appState.continuousGame.inningsP1 - 1,
ballsPottedP1: appState.continuousGame.currentScoreP1,
inningsP2: appState.continuousGame.gameMode === 2 ? appState.continuousGame.inningsP2 - 1 : null,
ballsPottedP2: appState.continuousGame.gameMode === 2 ? appState.continuousGame.currentScoreP2 : null,
});
} else {
Object.assign(gameData, {
player1: appState.standardGame.player1Name,
player2: appState.standardGame.player2Name,
score1: appState.standardGame.player1Score,
score2: appState.standardGame.player2Score,
});
}
history.push(gameData);
localStorage.setItem('gameHistory', JSON.stringify(history));
};
function resetGameState(mode) {
if (['14.1 Continuous', '14.1 Continuous Trainer'].includes(mode)) {
const continuousState = appState.continuousGame;
continuousState.currentScoreP1 = 0;
continuousState.highRunP1 = 0;
continuousState.inningsP1 = 1;
continuousState.currentScoreP2 = 0;
continuousState.highRunP2 = 0;
continuousState.inningsP2 = 1;
continuousState.ballsRemaining = 15;
continuousState.currentInningBallsPotted = 0;
continuousState.history = [];
continuousState.rackHistory = [];
continuousState.inningHistory = [];
continuousState.currentPlayer = 1;
} else {
const standardState = appState.standardGame;
standardState.player1Score = 0;
standardState.player2Score = 0;
}
}
const clearGameHistory = () => {
localStorage.removeItem('gameHistory');
loadGameHistory();
};
const exportGameHistory = () => {
const history = localStorage.getItem('gameHistory') || '[]';
const blob = new Blob([history], { type: "application/json" });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.download = 'game_history.json';
link.href = url;
link.click();
URL.revokeObjectURL(url);
}
const importGameHistory = (event) => {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
try {
const importedHistory = JSON.parse(e.target.result);
if (!Array.isArray(importedHistory)) throw new Error("Not an array");
localStorage.setItem('gameHistory', JSON.stringify(importedHistory));
loadGameHistory();
} catch (error) {
alert("Invalid file format. Please select a valid JSON file.");
}
};
reader.readAsText(file);
}
// --- 14.1 Game Functions ---
const ballButtonsContainer = document.querySelector('.ball-buttons');
const newRackBtn = document.getElementById('new-rack');
const inningDetailsTable = document.getElementById('inning-details');
const inningTableContainer = document.querySelector('.inning-table-container');
const createRemainingBallsButtons = () => {
ballButtonsContainer.innerHTML = '';
for (let i = 1; i <= 15; i++) {
const button = document.createElement('button');
button.textContent = i;
button.addEventListener('click', () => setRemainingBalls(i));
ballButtonsContainer.appendChild(button);
}
};
const setRemainingBalls = (balls) => {
if (balls > appState.continuousGame.ballsRemaining) {
alert('Invalid number of balls remaining.');
return;
}
updateInning(balls);
};
const updateInning = (ballsRemaining) => {
const cg = appState.continuousGame; // Shortcut for continuous game state
const ballsPotted = cg.ballsRemaining - ballsRemaining;
cg.currentInningBallsPotted = ballsPotted;
if (cg.gameMode === 1 || cg.currentPlayer === 1) {
cg.currentScoreP1 += ballsPotted;
cg.highRunP1 = Math.max(cg.highRunP1, ballsPotted);
} else {
cg.currentScoreP2 += ballsPotted;
cg.highRunP2 = Math.max(cg.highRunP2, ballsPotted);
}
cg.ballsRemaining = ballsRemaining;
updateInningTable(ballsPotted);
cg.history.push({ type: 'inning', player: cg.currentPlayer, ballsPotted });
updateInnings(cg.currentPlayer);
switchPlayer();
updateUI();
};
const handleNewRack = () => {
const cg = appState.continuousGame;
cg.rackHistory.push({index: cg.history.length, player: cg.currentPlayer});
cg.ballsRemaining += 14;
updateInningTable(14);
cg.history.push({ type: 'rack' });
updateUI();
};
const handleFoul = () => {
const cg = appState.continuousGame;
if (cg.gameMode === 1 || cg.currentPlayer === 1) {
cg.currentScoreP1 = Math.max(0, cg.currentScoreP1 - 1);
} else {
cg.currentScoreP2 = Math.max(0, cg.currentScoreP2 - 1);
}
updateInnings(cg.currentPlayer);
updateInningTable('Foul');
cg.history.push({ type: 'foul', player: cg.currentPlayer });
switchPlayer();
updateUI();
};
const handleSafety = () => {
const cg = appState.continuousGame;
updateInnings(cg.currentPlayer);
updateInningTable('Safety');
cg.history.push({ type: 'safety', player: cg.currentPlayer});
switchPlayer();
updateUI();
};
const undoLastAction = () => {
// This function is complex and requires careful state rollback.
// For this example, we'll keep it simple. A full implementation
// would involve a more detailed history state object.
console.warn("Undo is complex and not fully implemented in this refactor.");
// A proper undo would revert appState based on the last action in cg.history.
};
const endGameContinuous = () => {
// Using a simple confirm dialog for now
if (confirm('Are you sure you want to end the current game?')) {
endGame();
}
};
const resetGame = (saveToHistory = true) => {
if (saveToHistory && appState.currentGameMode) {
saveGameToHistory();
}
resetGameState(appState.currentGameMode);
clearInningTable();
updateInningTable();
updateUI();
};
const clearInningTable = () => {
inningDetailsTable.innerHTML = '';
};
const updateInningTable = (ballsPotted) => {
const cg = appState.continuousGame;
let currentInning = 0;
if (typeof ballsPotted !== 'undefined') {
currentInning = (cg.gameMode === 1) ? cg.inningsP1 : (cg.currentPlayer === 1 ? cg.inningsP1 : cg.inningsP2);
cg.inningHistory.push({
inning: currentInning,
player: cg.gameMode === 1 ? 1 : cg.currentPlayer,
ballsPotted: ballsPotted,
score: (cg.gameMode === 1 || cg.currentPlayer === 1) ? cg.currentScoreP1 : cg.currentScoreP2
});
}
clearInningTable();
cg.inningHistory.forEach(inning => {
const row = inningDetailsTable.insertRow();
row.innerHTML = `<td>${inning.inning}</td><td>${inning.player}</td><td>${inning.ballsPotted}</td><td>${inning.score}</td>`;
});
inningTableContainer.scrollTop = inningTableContainer.scrollHeight;
};
const switchPlayer = () => {
if (appState.continuousGame.gameMode === 2) {
appState.continuousGame.currentPlayer = appState.continuousGame.currentPlayer === 1 ? 2 : 1;
}
};
const updateInnings = (player) => {
const cg = appState.continuousGame;
if (cg.gameMode === 1) {
cg.inningsP1++;
} else if (player === 1) {
cg.inningsP1++;
} else if (player === 2) {
cg.inningsP2++;
}
};
// --- Event Listeners ---
document.getElementById('clear-history').addEventListener('click', clearGameHistory);
document.getElementById('export-history').addEventListener('click', exportGameHistory);
document.getElementById('import-history-button').addEventListener('click', () => document.getElementById('import-history').click());
document.getElementById('import-history').addEventListener('change', importGameHistory);
document.getElementById('new-rack').addEventListener('click', handleNewRack);
document.getElementById('foul').addEventListener('click', handleFoul);
document.getElementById('safety').addEventListener('click', handleSafety);
document.getElementById('undo').addEventListener('click', undoLastAction);
document.getElementById('reset').addEventListener('click', () => resetGame(true));
document.getElementById('end-game').addEventListener('click', endGameContinuous);
// Close buttons to return to main menu
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', () => {
if (appState.currentGameMode) {
// Ask for confirmation if in a game
if (['8-ball', '9-ball', '10-ball', '14.1 Continuous', '14.1 Continuous Trainer'].includes(appState.currentGameMode)) {
if (confirm("Are you sure you want to end this game and return to the menu? The result will not be saved.")) {
resetGameState(appState.currentGameMode);
appState.currentGameMode = null;
updateUI();
}
} else {
appState.currentGameMode = null;
updateUI();
}
}
});
});