awacke1 commited on
Commit
6068085
Β·
verified Β·
1 Parent(s): d434610

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +446 -184
index.html CHANGED
@@ -3,128 +3,112 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Sam's Birthday - Favorites With Plinko Game Card πŸ“</title>
7
  <script src="https://cdn.tailwindcss.com"></script>
8
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
9
  <style>
10
  body {
11
  font-family: 'Inter', sans-serif;
12
- background-color: #f0f4f8; /* Light blue-gray background */
13
- }
14
- .game-title {
15
- color: #3730a3; /* Indigo-800 */
16
  }
 
17
  .question-card {
18
  background-color: #ffffff;
19
- border-radius: 12px; /* Slightly less rounded */
20
- box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06); /* Tailwind shadow-md */
21
- page-break-inside: avoid; /* Avoid breaking cards across printed pages */
22
- }
23
- .question-text-strong {
24
- color: #4338ca; /* Indigo-700 */
25
  }
26
- .answer-label {
27
- color: #3f3f46; /* Zinc-700 */
28
- }
29
- input[type="text"].answer-input {
30
- border: 1px solid #d1d5db; /* Gray-300 */
31
- padding: 0.5rem 0.75rem; /* p-2 px-3 */
32
- border-radius: 0.375rem; /* rounded-md */
33
  width: 100%;
34
  box-sizing: border-box;
35
  }
36
- input[type="text"].answer-input:focus {
37
  outline: none;
38
- border-color: #4f46e5; /* Indigo-600 */
39
  box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.3);
40
  }
41
  .correct-answer-section {
42
- background-color: #f0fdf4; /* Green-50 */
43
- border-left: 4px solid #22c55e; /* Green-500 */
44
  padding: 0.5rem 0.75rem;
45
- border-radius: 0.25rem; /* rounded-sm */
46
- }
47
- .correct-answer-text {
48
- color: #15803d; /* Green-700 */
49
  }
50
- .btn-toggle-answers {
51
- background-color: #4f46e5; /* Indigo-600 */
 
52
  color: white;
53
  transition: background-color 0.3s ease;
54
  }
55
- .btn-toggle-answers:hover {
56
- background-color: #4338ca; /* Indigo-700 */
 
57
  }
58
- .bonus-prompt-text {
59
- color: #6d28d9; /* Violet-700 */
60
  }
61
-
62
- /* Initially hide answer sections */
63
- .correct-answer-container.hidden-answers .correct-answer-section {
64
- display: none;
 
 
 
 
 
 
 
65
  }
66
 
67
- /* Print-specific styles */
 
 
 
 
 
68
  @media print {
69
- body {
70
- background-color: #ffffff; /* White background for printing */
71
- -webkit-print-color-adjust: exact; /* Ensures colors print in Chrome/Safari */
72
- print-color-adjust: exact; /* Standard */
73
- }
74
- .btn-toggle-answers, .print-hide {
75
- display: none !important; /* Hide buttons and other non-print elements */
76
- }
77
- .question-card {
78
- box-shadow: none; /* Remove shadow for printing */
79
- border: 1px solid #e5e7eb; /* Light border for cards */
80
- margin-bottom: 1rem; /* Space between cards on print */
81
- }
82
- .correct-answer-section {
83
- display: block !important; /* Always show answers when printing */
84
- background-color: #f9fafb !important; /* Lighter bg for print */
85
- border-left: 2px solid #6b7280 !important; /* Gray border for print */
86
- padding: 0.25rem 0.5rem;
87
- }
88
- .correct-answer-text {
89
- color: #1f2937 !important; /* Darker text for print */
90
- }
91
- input[type="text"].answer-input {
92
- border-bottom: 1px solid #9ca3af; /* Underline style for print */
93
- border-top: none;
94
- border-left: none;
95
- border-right: none;
96
- border-radius: 0;
97
- padding-left: 0;
98
- padding-right: 0;
99
- }
100
- input[type="text"].answer-input::placeholder {
101
- color: transparent; /* Hide placeholder on print */
102
- }
103
- .cards-container-grid {
104
- grid-template-columns: repeat(1, minmax(0, 1fr)) !important; /* Single column for print */
105
- }
106
- .page-container {
107
- padding: 0.5in !important; /* Adjust print margins */
108
- max-width: 100% !important;
109
- }
110
- .game-title, .footer-text {
111
- text-align: center;
112
- }
113
  }
114
  </style>
115
  </head>
116
  <body class="p-4 sm:p-6 md:p-8">
117
 
118
  <div id="pageContainer" class="page-container max-w-5xl mx-auto">
119
- <header class="text-center mb-8">
120
- <h1 class="game-title text-3xl sm:text-4xl font-bold">Sam's Birthday - Favorites With Plinko Game Card πŸ“</h1>
121
- <p class="text-gray-600 mt-2">How well do you know Sam? Fill in the answers below!</p>
122
  </header>
123
 
124
- <div class="text-center mb-8 print-hide">
125
- <button id="toggleAnswersBtn" class="btn-toggle-answers py-2 px-6 rounded-lg font-semibold text-lg">
 
 
 
 
 
 
 
 
 
126
  Reveal Answers
127
  </button>
 
 
 
128
  </div>
129
 
130
  <div id="cardsContainer" class="correct-answer-container hidden-answers cards-container-grid grid grid-cols-1 md:grid-cols-2 gap-6">
@@ -136,142 +120,420 @@
136
  </footer>
137
  </div>
138
 
139
- <script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  const gameData = [
141
- { question: "Name Sam's all time favorite musical group? Hint: Their debut album was in 1986.", answer: "Beastie Boys" },
142
- { question: "What is Sam's favorite game? Hint: She grew up playing this game with her Grandma.", answer: "Yahtzee" },
143
  {
144
  question: "In her 20's, Sam lived briefly in which two states?",
145
- answer: ["California", "Texas"], // Array of parts for multi-input
146
- answerLabels: ["First State:", "Second State:"] // Labels for each input
147
  },
148
- { question: "Name Sam's longest Employer. Hint: Close to 20 years employment.", answer: "MSU", alternatives: ["Metropolitan State University"] },
149
  {
150
  question: "Sam holds a BA degree in _____, and Masters degree in _____?",
151
- answer: ["Political Science", "Public Administration"], // Array of parts for multi-input
152
- answerLabels: ["BA Degree:", "Masters Degree:"] // Labels for each input
153
  },
154
- { question: "What is Sam's biggest fear?", answer: "Snakes" },
155
- { question: "Name the first National Park that Sam and Aaron travelled to. Hint: It was during COVID.", answer: "Death Valley" },
156
- { question: "Sam and her BFF have matching tattoos which is the title of a song. What is the Paul McCartney song?", answer: "Let It Be" },
157
- { question: "What animal would Sam like to have as an indoor pet?", answer: "Raccoon" },
158
  {
159
  question: "If Sam won $1000 shopping spree at the store of her choice, which store would it be?",
160
- answer: "Menards", // Main answer for bonus question
161
- bonusPrompt: "Bonus: Why would she choose this store?",
162
- bonusAnswer: "Rebate"
163
  }
164
  ];
 
165
 
166
  const cardsContainerEl = document.getElementById('cardsContainer');
167
- const toggleAnswersBtn = document.getElementById('toggleAnswersBtn');
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
- function renderCards() {
170
- cardsContainerEl.innerHTML = ''; // Clear existing cards
171
 
172
  gameData.forEach((item, index) => {
173
  const card = document.createElement('div');
174
  card.className = 'question-card p-5 sm:p-6 flex flex-col';
 
 
175
 
176
- let cardInnerHTML = '';
177
-
178
- // Condition for multi-part non-bonus questions (e.g., Q3, Q5)
179
- if (Array.isArray(item.answer) && item.answerLabels && !item.bonusPrompt) {
180
- cardInnerHTML = `
181
- <div class="flex-grow">
182
- <h3 class="text-lg font-semibold question-text-strong mb-2">
183
- <span class="question-number">${index + 1}.</span>
184
- ${item.question}
185
- </h3>
186
- <div class="mb-3 mt-3">
187
- <label for="answer-part1-${index}" class="block text-sm font-medium answer-label mb-1">${item.answerLabels[0]}</label>
188
- <input type="text" id="answer-part1-${index}" name="answer-part1-${index}" class="answer-input w-full" placeholder="${item.answerLabels[0].replace(':', '...')}">
189
- </div>
190
- <div class="mb-3">
191
- <label for="answer-part2-${index}" class="block text-sm font-medium answer-label mb-1">${item.answerLabels[1]}</label>
192
- <input type="text" id="answer-part2-${index}" name="answer-part2-${index}" class="answer-input w-full" placeholder="${item.answerLabels[1].replace(':', '...')}">
193
  </div>
194
  </div>
195
- <div class="mt-auto">
196
- <div class="correct-answer-section mb-2">
197
- <p class="text-xs font-medium text-gray-600">Correct Answer (${item.answerLabels[0].replace(':', '')}):</p>
198
- <p class="correct-answer-text font-semibold">${item.answer[0]}</p>
199
- </div>
200
- <div class="correct-answer-section">
201
- <p class="text-xs font-medium text-gray-600">Correct Answer (${item.answerLabels[1].replace(':', '')}):</p>
202
- <p class="correct-answer-text font-semibold">${item.answer[1]}</p>
203
  </div>
 
 
 
 
 
204
  </div>
205
- `;
206
- }
207
- // Condition for bonus questions (e.g., Q10)
208
- else if (item.bonusPrompt) {
209
- // 'item.answer' is the main answer for the bonus question
210
- const mainAnswerForBonusDisplay = item.answer;
211
- cardInnerHTML = `
212
- <div class="flex-grow">
213
- <h3 class="text-lg font-semibold question-text-strong mb-2">
214
- <span class="question-number">${index + 1}.</span>
215
- ${item.question}
216
- </h3>
217
- <div class="mb-3 mt-3">
218
- <label for="answer-main-${index}" class="block text-sm font-medium answer-label mb-1">Your Answer (Main):</label>
219
- <input type="text" id="answer-main-${index}" name="answer-main-${index}" class="answer-input w-full" placeholder="Main answer...">
220
- </div>
221
- <p class="text-sm font-semibold bonus-prompt-text mt-4 mb-1">${item.bonusPrompt}</p>
222
- <div class="mb-3">
223
- <label for="answer-bonus-${index}" class="block text-sm font-medium answer-label mb-1">Your Answer (Bonus):</label>
224
- <input type="text" id="answer-bonus-${index}" name="answer-bonus-${index}" class="answer-input w-full" placeholder="Bonus answer...">
225
  </div>
226
  </div>
227
- <div class="mt-auto">
228
- <div class="correct-answer-section mb-2">
229
- <p class="text-xs font-medium text-gray-600">Correct Answer (Main):</p>
230
- <p class="correct-answer-text font-semibold">${mainAnswerForBonusDisplay}</p>
231
- </div>
232
- <div class="correct-answer-section">
233
- <p class="text-xs font-medium text-gray-600">Correct Answer (Bonus):</p>
234
- <p class="correct-answer-text font-semibold">${item.bonusAnswer}</p>
235
  </div>
 
 
 
 
 
236
  </div>
237
- `;
238
- }
239
- // Default for single-answer questions
240
- else {
241
- const singleAnswerDisplay = item.answer; // Assuming item.answer is a string here
242
- cardInnerHTML = `
243
- <div class="flex-grow">
244
- <h3 class="text-lg font-semibold question-text-strong mb-2">
245
- <span class="question-number">${index + 1}.</span>
246
- ${item.question}
247
- </h3>
248
- <div class="mb-3 mt-3">
249
- <label for="answer-${index}" class="block text-sm font-medium answer-label mb-1">Your Answer:</label>
250
- <input type="text" id="answer-${index}" name="answer-${index}" class="answer-input w-full" placeholder="Type your answer here...">
251
  </div>
252
- </div>
253
- <div class="correct-answer-section mt-auto">
 
254
  <p class="text-xs font-medium text-gray-600">Correct Answer:</p>
255
- <p class="correct-answer-text font-semibold">${singleAnswerDisplay}</p>
256
- </div>
257
- `;
258
  }
259
- card.innerHTML = cardInnerHTML;
 
 
 
 
 
 
 
 
 
 
 
260
  cardsContainerEl.appendChild(card);
261
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
  }
263
 
264
- toggleAnswersBtn.addEventListener('click', () => {
265
- cardsContainerEl.classList.toggle('hidden-answers');
266
- if (cardsContainerEl.classList.contains('hidden-answers')) {
267
- toggleAnswersBtn.textContent = 'Reveal Answers';
268
  } else {
269
- toggleAnswersBtn.textContent = 'Hide Answers';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  }
 
271
  });
 
 
 
 
272
 
273
- // Initial render of cards
274
- renderCards();
275
  </script>
276
  </body>
277
  </html>
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Sam's Birthday - Interactive Quiz πŸ“</title>
7
  <script src="https://cdn.tailwindcss.com"></script>
8
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
9
  <style>
10
  body {
11
  font-family: 'Inter', sans-serif;
12
+ background-color: #f0f4f8;
 
 
 
13
  }
14
+ .game-title { color: #3730a3; }
15
  .question-card {
16
  background-color: #ffffff;
17
+ border-radius: 12px;
18
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
19
+ page-break-inside: avoid;
 
 
 
20
  }
21
+ .question-text-strong { color: #4338ca; }
22
+ .answer-label { color: #3f3f46; }
23
+ input[type="text"].answer-input, input[type="text"].name-input {
24
+ border: 1px solid #d1d5db;
25
+ padding: 0.5rem 0.75rem;
26
+ border-radius: 0.375rem;
 
27
  width: 100%;
28
  box-sizing: border-box;
29
  }
30
+ input[type="text"].answer-input:focus, input[type="text"].name-input:focus {
31
  outline: none;
32
+ border-color: #4f46e5;
33
  box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.3);
34
  }
35
  .correct-answer-section {
36
+ background-color: #f0fdf4;
37
+ border-left: 4px solid #22c55e;
38
  padding: 0.5rem 0.75rem;
39
+ border-radius: 0.25rem;
40
+ margin-top: 0.5rem; /* Space between answer and its key */
 
 
41
  }
42
+ .correct-answer-text { color: #15803d; }
43
+ .btn-action {
44
+ background-color: #4f46e5;
45
  color: white;
46
  transition: background-color 0.3s ease;
47
  }
48
+ .btn-action:hover { background-color: #4338ca; }
49
+ .btn-save {
50
+ background-color: #10b981; /* Emerald-500 */
51
  }
52
+ .btn-save:hover {
53
+ background-color: #059669; /* Emerald-600 */
54
  }
55
+ .feedback-emoji {
56
+ font-size: 1.25rem; /* Make emojis a bit bigger */
57
+ margin-left: 0.5rem;
58
+ }
59
+ .score-display {
60
+ background-color: #e0e7ff; /* Indigo-100 */
61
+ color: #3730a3; /* Indigo-800 */
62
+ padding: 0.75rem 1rem;
63
+ border-radius: 8px;
64
+ font-weight: 600;
65
+ text-align: center;
66
  }
67
 
68
+ /* Initially hide answer sections and feedback emojis */
69
+ .correct-answer-container.hidden-answers .correct-answer-section { display: none; }
70
+ .feedback-emoji { display: none; }
71
+ .correct-answer-container.checked-answers .feedback-emoji { display: inline-block; }
72
+
73
+
74
  @media print {
75
+ body { background-color: #ffffff; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
76
+ .btn-action, .btn-save, .print-hide, #nameInputSection, #scoreDisplaySection { display: none !important; }
77
+ .question-card { box-shadow: none; border: 1px solid #e5e7eb; margin-bottom: 1rem; }
78
+ .correct-answer-section { display: block !important; background-color: #f9fafb !important; border-left: 2px solid #6b7280 !important; padding: 0.25rem 0.5rem; }
79
+ .correct-answer-text { color: #1f2937 !important; }
80
+ input[type="text"].answer-input { border-bottom: 1px solid #9ca3af; border-top: none; border-left: none; border-right: none; border-radius: 0; padding-left: 0; padding-right: 0; }
81
+ input[type="text"].answer-input::placeholder { color: transparent; }
82
+ .cards-container-grid { grid-template-columns: repeat(1, minmax(0, 1fr)) !important; }
83
+ .page-container { padding: 0.5in !important; max-width: 100% !important; }
84
+ .game-title, .footer-text { text-align: center; }
85
+ .feedback-emoji { display: inline-block !important; } /* Show emojis on print if answers are checked */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  }
87
  </style>
88
  </head>
89
  <body class="p-4 sm:p-6 md:p-8">
90
 
91
  <div id="pageContainer" class="page-container max-w-5xl mx-auto">
92
+ <header class="text-center mb-6">
93
+ <h1 class="game-title text-3xl sm:text-4xl font-bold">Sam's Birthday - Interactive Quiz πŸ“</h1>
 
94
  </header>
95
 
96
+ <div id="nameInputSection" class="mb-6 max-w-md mx-auto">
97
+ <label for="playerNameInput" class="block text-sm font-medium text-gray-700 mb-1">Player Name:</label>
98
+ <input type="text" id="playerNameInput" class="name-input" placeholder="Enter your name (e.g., Aaron)">
99
+ </div>
100
+
101
+ <div id="scoreDisplaySection" class="mb-6 text-center" style="display: none;">
102
+ <p id="scoreText" class="score-display">Score: 0 / 13 (0%)</p>
103
+ </div>
104
+
105
+ <div class="text-center mb-8 space-x-4 print-hide">
106
+ <button id="checkAnswersBtn" class="btn-action py-2 px-6 rounded-lg font-semibold text-lg">
107
  Reveal Answers
108
  </button>
109
+ <button id="saveProgressBtn" class="btn-action btn-save py-2 px-6 rounded-lg font-semibold text-lg">
110
+ Save Progress
111
+ </button>
112
  </div>
113
 
114
  <div id="cardsContainer" class="correct-answer-container hidden-answers cards-container-grid grid grid-cols-1 md:grid-cols-2 gap-6">
 
120
  </footer>
121
  </div>
122
 
123
+ <script type="module">
124
+ // Firebase Imports
125
+ import { initializeApp } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-app.js";
126
+ import { getAuth, signInAnonymously, onAuthStateChanged, signInWithCustomToken } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-auth.js";
127
+ import { getFirestore, doc, setDoc, getDoc, serverTimestamp, updateDoc } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-firestore.js";
128
+
129
+ // IMPORTANT: Replace with your actual Firebase config
130
+ const firebaseConfig = typeof __firebase_config !== 'undefined' ? JSON.parse(__firebase_config) : {
131
+ apiKey: "YOUR_API_KEY",
132
+ authDomain: "YOUR_AUTH_DOMAIN",
133
+ projectId: "YOUR_PROJECT_ID",
134
+ storageBucket: "YOUR_STORAGE_BUCKET",
135
+ messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
136
+ appId: "YOUR_APP_ID"
137
+ };
138
+
139
+ const appId = typeof __app_id !== 'undefined' ? __app_id : 'sams-birthday-quiz-default';
140
+
141
+ // Initialize Firebase
142
+ const app = initializeApp(firebaseConfig);
143
+ const auth = getAuth(app);
144
+ const db = getFirestore(app);
145
+
146
+ let userId = null;
147
+ let gameDocRef = null;
148
+ let hasUserTyped = false; // Track if user has typed anything
149
+
150
  const gameData = [
151
+ { question: "Name Sam's all time favorite musical group? Hint: Their debut album was in 1986.", answer: "Beastie Boys", id: "q1" },
152
+ { question: "What is Sam's favorite game? Hint: She grew up playing this game with her Grandma.", answer: "Yahtzee", id: "q2" },
153
  {
154
  question: "In her 20's, Sam lived briefly in which two states?",
155
+ answer: ["California", "Texas"], answerLabels: ["First State:", "Second State:"], id: "q3", parts: 2
 
156
  },
157
+ { question: "Name Sam's longest Employer. Hint: Close to 20 years employment.", answer: "MSU", alternatives: ["Metropolitan State University"], id: "q4" },
158
  {
159
  question: "Sam holds a BA degree in _____, and Masters degree in _____?",
160
+ answer: ["Political Science", "Public Administration"], answerLabels: ["BA Degree:", "Masters Degree:"], id: "q5", parts: 2
 
161
  },
162
+ { question: "What is Sam's biggest fear?", answer: "Snakes", id: "q6" },
163
+ { question: "Name the first National Park that Sam and Aaron travelled to. Hint: It was during COVID.", answer: "Death Valley", alternatives: ["Death Valley National Park"], id: "q7" },
164
+ { question: "Sam and her BFF have matching tattoos which is the title of a song. What is the Paul McCartney song?", answer: "Let It Be", id: "q8" },
165
+ { question: "What animal would Sam like to have as an indoor pet?", answer: "Raccoon", id: "q9" },
166
  {
167
  question: "If Sam won $1000 shopping spree at the store of her choice, which store would it be?",
168
+ answer: "Menards", bonusPrompt: "Bonus: Why would she choose this store?", bonusAnswer: "Rebate", id: "q10", parts: 1 // Main answer is 1 part, bonus is separate
 
 
169
  }
170
  ];
171
+ const TOTAL_POSSIBLE_POINTS = 13; // 9 single + 2 for q3 + 2 for q5 = 13 (bonus is one of these)
172
 
173
  const cardsContainerEl = document.getElementById('cardsContainer');
174
+ const checkAnswersBtn = document.getElementById('checkAnswersBtn');
175
+ const saveProgressBtn = document.getElementById('saveProgressBtn');
176
+ const playerNameInputEl = document.getElementById('playerNameInput');
177
+ const scoreTextEl = document.getElementById('scoreText');
178
+ const scoreDisplaySectionEl = document.getElementById('scoreDisplaySection');
179
+
180
+ function normalizeAnswer(answer) {
181
+ return answer.trim().toLowerCase();
182
+ }
183
+
184
+ function renderCards(savedAnswers = {}, savedFeedback = {}) {
185
+ cardsContainerEl.innerHTML = '';
186
+ hasUserTyped = Object.keys(savedAnswers).length > 0 || !!playerNameInputEl.value;
187
 
 
 
188
 
189
  gameData.forEach((item, index) => {
190
  const card = document.createElement('div');
191
  card.className = 'question-card p-5 sm:p-6 flex flex-col';
192
+ let inputFieldsHTML = '';
193
+ let answerSectionsHTML = '';
194
 
195
+ if (item.parts === 2 && item.answerLabels) { // Multi-part questions (Q3, Q5)
196
+ inputFieldsHTML = `
197
+ <div class="mb-3 mt-3">
198
+ <label for="${item.id}-part1" class="block text-sm font-medium answer-label mb-1">${item.answerLabels[0]}</label>
199
+ <div class="flex items-center">
200
+ <input type="text" id="${item.id}-part1" data-question-id="${item.id}" data-part-index="0" class="answer-input w-full" placeholder="${item.answerLabels[0].replace(':', '...')}" value="${savedAnswers[`${item.id}-part1`] || ''}">
201
+ <span id="feedback-${item.id}-part1" class="feedback-emoji">${savedFeedback[`${item.id}-part1`] || ''}</span>
 
 
 
 
 
 
 
 
 
 
202
  </div>
203
  </div>
204
+ <div class="mb-3">
205
+ <label for="${item.id}-part2" class="block text-sm font-medium answer-label mb-1">${item.answerLabels[1]}</label>
206
+ <div class="flex items-center">
207
+ <input type="text" id="${item.id}-part2" data-question-id="${item.id}" data-part-index="1" class="answer-input w-full" placeholder="${item.answerLabels[1].replace(':', '...')}" value="${savedAnswers[`${item.id}-part2`] || ''}">
208
+ <span id="feedback-${item.id}-part2" class="feedback-emoji">${savedFeedback[`${item.id}-part2`] || ''}</span>
 
 
 
209
  </div>
210
+ </div>`;
211
+ answerSectionsHTML = `
212
+ <div class="correct-answer-section mb-2">
213
+ <p class="text-xs font-medium text-gray-600">Correct Answer (${item.answerLabels[0].replace(':', '')}):</p>
214
+ <p class="correct-answer-text font-semibold">${item.answer[0]}</p>
215
  </div>
216
+ <div class="correct-answer-section">
217
+ <p class="text-xs font-medium text-gray-600">Correct Answer (${item.answerLabels[1].replace(':', '')}):</p>
218
+ <p class="correct-answer-text font-semibold">${item.answer[1]}</p>
219
+ </div>`;
220
+ } else if (item.bonusPrompt) { // Bonus question (Q10)
221
+ inputFieldsHTML = `
222
+ <div class="mb-3 mt-3">
223
+ <label for="${item.id}-main" class="block text-sm font-medium answer-label mb-1">Your Answer (Main):</label>
224
+ <div class="flex items-center">
225
+ <input type="text" id="${item.id}-main" data-question-id="${item.id}" data-part-type="main" class="answer-input w-full" placeholder="Main answer..." value="${savedAnswers[`${item.id}-main`] || ''}">
226
+ <span id="feedback-${item.id}-main" class="feedback-emoji">${savedFeedback[`${item.id}-main`] || ''}</span>
 
 
 
 
 
 
 
 
 
227
  </div>
228
  </div>
229
+ <p class="text-sm font-semibold bonus-prompt-text mt-4 mb-1">${item.bonusPrompt}</p>
230
+ <div class="mb-3">
231
+ <label for="${item.id}-bonus" class="block text-sm font-medium answer-label mb-1">Your Answer (Bonus):</label>
232
+ <div class="flex items-center">
233
+ <input type="text" id="${item.id}-bonus" data-question-id="${item.id}" data-part-type="bonus" class="answer-input w-full" placeholder="Bonus answer..." value="${savedAnswers[`${item.id}-bonus`] || ''}">
234
+ <span id="feedback-${item.id}-bonus" class="feedback-emoji">${savedFeedback[`${item.id}-bonus`] || ''}</span>
 
 
235
  </div>
236
+ </div>`;
237
+ answerSectionsHTML = `
238
+ <div class="correct-answer-section mb-2">
239
+ <p class="text-xs font-medium text-gray-600">Correct Answer (Main):</p>
240
+ <p class="correct-answer-text font-semibold">${item.answer}</p>
241
  </div>
242
+ <div class="correct-answer-section">
243
+ <p class="text-xs font-medium text-gray-600">Correct Answer (Bonus):</p>
244
+ <p class="correct-answer-text font-semibold">${item.bonusAnswer}</p>
245
+ </div>`;
246
+ } else { // Single-part questions
247
+ inputFieldsHTML = `
248
+ <div class="mb-3 mt-3">
249
+ <label for="${item.id}" class="block text-sm font-medium answer-label mb-1">Your Answer:</label>
250
+ <div class="flex items-center">
251
+ <input type="text" id="${item.id}" data-question-id="${item.id}" class="answer-input w-full" placeholder="Type your answer here..." value="${savedAnswers[item.id] || ''}">
252
+ <span id="feedback-${item.id}" class="feedback-emoji">${savedFeedback[item.id] || ''}</span>
 
 
 
253
  </div>
254
+ </div>`;
255
+ answerSectionsHTML = `
256
+ <div class="correct-answer-section">
257
  <p class="text-xs font-medium text-gray-600">Correct Answer:</p>
258
+ <p class="correct-answer-text font-semibold">${item.answer}</p>
259
+ </div>`;
 
260
  }
261
+
262
+ card.innerHTML = `
263
+ <div class="flex-grow">
264
+ <h3 class="text-lg font-semibold question-text-strong mb-2">
265
+ <span class="question-number">${index + 1}.</span>
266
+ ${item.question}
267
+ </h3>
268
+ ${inputFieldsHTML}
269
+ </div>
270
+ <div class="mt-auto answer-key-container">
271
+ ${answerSectionsHTML}
272
+ </div>`;
273
  cardsContainerEl.appendChild(card);
274
  });
275
+ updateButtonStates();
276
+ attachInputListeners(); // Attach listeners after rendering
277
+ if (Object.keys(savedFeedback).length > 0) { // If feedback exists, means answers were checked
278
+ cardsContainerEl.classList.remove('hidden-answers');
279
+ cardsContainerEl.classList.add('checked-answers');
280
+ checkAnswersBtn.textContent = 'Answers Checked';
281
+ } else {
282
+ cardsContainerEl.classList.add('hidden-answers');
283
+ cardsContainerEl.classList.remove('checked-answers');
284
+ }
285
+ }
286
+
287
+ function attachInputListeners() {
288
+ document.querySelectorAll('.answer-input, #playerNameInput').forEach(input => {
289
+ input.addEventListener('input', () => {
290
+ hasUserTyped = true;
291
+ updateButtonStates();
292
+ });
293
+ });
294
  }
295
 
296
+ function updateButtonStates() {
297
+ if (hasUserTyped || playerNameInputEl.value.trim() !== "") {
298
+ checkAnswersBtn.textContent = 'Check Answers with Key';
 
299
  } else {
300
+ checkAnswersBtn.textContent = 'Reveal Answers';
301
+ }
302
+ // If answers are already checked, reflect that
303
+ if (cardsContainerEl.classList.contains('checked-answers') && !cardsContainerEl.classList.contains('hidden-answers')) {
304
+ checkAnswersBtn.textContent = 'Answers Checked';
305
+ }
306
+ }
307
+
308
+
309
+ function collectAnswers() {
310
+ const userAnswers = {};
311
+ document.querySelectorAll('.answer-input').forEach(input => {
312
+ const questionId = input.dataset.questionId;
313
+ if (input.dataset.partIndex) { // Multi-part (Q3, Q5)
314
+ userAnswers[`${questionId}-part${parseInt(input.dataset.partIndex) + 1}`] = input.value;
315
+ } else if (input.dataset.partType) { // Bonus (Q10)
316
+ userAnswers[`${questionId}-${input.dataset.partType}`] = input.value;
317
+ } else { // Single part
318
+ userAnswers[questionId] = input.value;
319
+ }
320
+ });
321
+ return userAnswers;
322
+ }
323
+
324
+ function handleCheckAnswers() {
325
+ cardsContainerEl.classList.remove('hidden-answers');
326
+ cardsContainerEl.classList.add('checked-answers'); // To show emojis
327
+
328
+ let currentScore = 0;
329
+ const userAnswers = collectAnswers();
330
+ const feedbackEmojis = {};
331
+
332
+ gameData.forEach(item => {
333
+ if (item.parts === 2) { // Q3, Q5
334
+ for (let i = 0; i < 2; i++) {
335
+ const userAnswer = normalizeAnswer(userAnswers[`${item.id}-part${i + 1}`] || '');
336
+ const correctAnswer = normalizeAnswer(item.answer[i]);
337
+ if (userAnswer === correctAnswer) {
338
+ currentScore++;
339
+ feedbackEmojis[`${item.id}-part${i+1}`] = 'βœ…';
340
+ } else {
341
+ feedbackEmojis[`${item.id}-part${i+1}`] = '❌';
342
+ }
343
+ document.getElementById(`feedback-${item.id}-part${i+1}`).textContent = feedbackEmojis[`${item.id}-part${i+1}`];
344
+ }
345
+ } else if (item.bonusPrompt) { // Q10
346
+ const mainUserAnswer = normalizeAnswer(userAnswers[`${item.id}-main`] || '');
347
+ const mainCorrectAnswer = normalizeAnswer(item.answer);
348
+ let mainCorrect = false;
349
+ if (mainUserAnswer === mainCorrectAnswer || (item.alternatives && item.alternatives.map(normalizeAnswer).includes(mainUserAnswer))) {
350
+ currentScore++;
351
+ feedbackEmojis[`${item.id}-main`] = 'βœ…';
352
+ mainCorrect = true;
353
+ } else {
354
+ feedbackEmojis[`${item.id}-main`] = '❌';
355
+ }
356
+ document.getElementById(`feedback-${item.id}-main`).textContent = feedbackEmojis[`${item.id}-main`];
357
+
358
+ // Only score bonus if main was correct (optional rule, implementing as strict for now)
359
+ // if (mainCorrect) {
360
+ const bonusUserAnswer = normalizeAnswer(userAnswers[`${item.id}-bonus`] || '');
361
+ const bonusCorrectAnswer = normalizeAnswer(item.bonusAnswer);
362
+ if (bonusUserAnswer === bonusCorrectAnswer) {
363
+ currentScore++; //This makes it 13 points total
364
+ feedbackEmojis[`${item.id}-bonus`] = 'βœ…';
365
+ } else {
366
+ feedbackEmojis[`${item.id}-bonus`] = '❌';
367
+ }
368
+ // } else {
369
+ // feedbackEmojis[`${item.id}-bonus`] = 'βž–'; // Indicate bonus not attempted or main was wrong
370
+ // }
371
+ document.getElementById(`feedback-${item.id}-bonus`).textContent = feedbackEmojis[`${item.id}-bonus`];
372
+
373
+ } else { // Single answer questions
374
+ const userAnswer = normalizeAnswer(userAnswers[item.id] || '');
375
+ const correctAnswer = normalizeAnswer(item.answer);
376
+ if (userAnswer === correctAnswer || (item.alternatives && item.alternatives.map(normalizeAnswer).includes(userAnswer))) {
377
+ currentScore++;
378
+ feedbackEmojis[item.id] = 'βœ…';
379
+ } else {
380
+ feedbackEmojis[item.id] = '❌';
381
+ }
382
+ document.getElementById(`feedback-${item.id}`).textContent = feedbackEmojis[item.id];
383
+ }
384
+ });
385
+
386
+ const percentage = TOTAL_POSSIBLE_POINTS > 0 ? Math.round((currentScore / TOTAL_POSSIBLE_POINTS) * 100) : 0;
387
+ scoreTextEl.textContent = `Score: ${currentScore} / ${TOTAL_POSSIBLE_POINTS} (${percentage}%)`;
388
+ scoreDisplaySectionEl.style.display = 'block';
389
+ checkAnswersBtn.textContent = 'Answers Checked';
390
+ hasUserTyped = true; // Ensure button state remains "checked"
391
+
392
+ saveGameState(true, currentScore, percentage, feedbackEmojis); // Save with checked state
393
+ }
394
+
395
+ async function saveGameState(isChecked = false, score = 0, percentage = 0, feedback = {}) {
396
+ if (!userId || !gameDocRef) {
397
+ console.warn("User not authenticated or gameDocRef not set. Cannot save yet. Will retry on auth.");
398
+ return;
399
+ }
400
+ const playerName = playerNameInputEl.value.trim() || "Anonymous Player";
401
+ const answers = collectAnswers();
402
+
403
+ const gameState = {
404
+ playerName: playerName,
405
+ answers: answers,
406
+ lastUpdated: serverTimestamp(),
407
+ isChecked: isChecked, // New field to indicate if answers were checked
408
+ score: isChecked ? score : null,
409
+ percentage: isChecked ? percentage : null,
410
+ feedback: isChecked ? feedback : {}
411
+ };
412
+
413
+ try {
414
+ await setDoc(gameDocRef, gameState, { merge: true });
415
+ console.log("Game state saved for player:", playerName);
416
+ } catch (error) {
417
+ console.error("Error saving game state: ", error);
418
+ }
419
+ }
420
+
421
+ async function loadGameState() {
422
+ if (!userId) {
423
+ console.warn("User ID not available for loading game state.");
424
+ renderCards(); // Render empty cards if no user ID
425
+ updateButtonStates();
426
+ return;
427
+ }
428
+ // Corrected path: collection "samsFavoritesGameData", document "userGameState"
429
+ gameDocRef = doc(db, "artifacts", appId, "users", userId, "samsFavoritesGameData", "userGameState");
430
+ console.log("Attempting to load game state from:", gameDocRef.path);
431
+
432
+
433
+ try {
434
+ const docSnap = await getDoc(gameDocRef);
435
+ if (docSnap.exists()) {
436
+ const data = docSnap.data();
437
+ playerNameInputEl.value = data.playerName || 'Aaron'; // Default to Aaron if no name
438
+ renderCards(data.answers || {}, data.feedback || {});
439
+ if (data.isChecked) {
440
+ scoreTextEl.textContent = `Score: ${data.score || 0} / ${TOTAL_POSSIBLE_POINTS} (${data.percentage || 0}%)`;
441
+ scoreDisplaySectionEl.style.display = 'block';
442
+ cardsContainerEl.classList.remove('hidden-answers');
443
+ cardsContainerEl.classList.add('checked-answers');
444
+ checkAnswersBtn.textContent = 'Answers Checked';
445
+ hasUserTyped = true; // If data loaded, assume interaction
446
+ }
447
+ } else {
448
+ console.log("No saved game state found. Starting fresh.");
449
+ playerNameInputEl.value = 'Aaron'; // Default for new users
450
+ renderCards(); // Render empty cards
451
+ }
452
+ } catch (error) {
453
+ console.error("Error loading game state: ", error);
454
+ renderCards(); // Render empty cards on error
455
+ }
456
+ updateButtonStates();
457
+ }
458
+
459
+ // Authentication and Initialization
460
+ onAuthStateChanged(auth, async (user) => {
461
+ if (user) {
462
+ userId = user.uid;
463
+ console.log("User authenticated with UID:", userId);
464
+ // Define gameDocRef here, now that userId is available
465
+ gameDocRef = doc(db, "artifacts", appId, "users", userId, "samsFavoritesGameData", "userGameState");
466
+ await loadGameState();
467
+ } else {
468
+ // User is signed out or not yet signed in. Try custom token or anonymous.
469
+ console.log("No user signed in. Attempting sign-in.");
470
+ if (typeof __initial_auth_token !== 'undefined' && __initial_auth_token) {
471
+ try {
472
+ await signInWithCustomToken(auth, __initial_auth_token);
473
+ console.log("Signed in with custom token.");
474
+ // onAuthStateChanged will trigger again with the new user.
475
+ } catch (error) {
476
+ console.error("Error signing in with custom token, falling back to anonymous:", error);
477
+ await signInAnonymously(auth).catch(anonError => console.error("Anonymous sign-in also failed:", anonError));
478
+ }
479
+ } else {
480
+ try {
481
+ await signInAnonymously(auth);
482
+ console.log("Signed in anonymously.");
483
+ // onAuthStateChanged will trigger again with the new user.
484
+ } catch (error) {
485
+ console.error("Error signing in anonymously: ", error);
486
+ renderCards();
487
+ updateButtonStates();
488
+ }
489
+ }
490
+ }
491
+ });
492
+
493
+ checkAnswersBtn.addEventListener('click', () => {
494
+ if (checkAnswersBtn.textContent === 'Answers Checked') return; // Don't re-check
495
+
496
+ if (cardsContainerEl.classList.contains('hidden-answers') && !hasUserTyped) {
497
+ // If revealing answers without typing anything
498
+ cardsContainerEl.classList.remove('hidden-answers');
499
+ checkAnswersBtn.textContent = 'Check Answers with Key'; // Prompt to type then check
500
+ } else {
501
+ handleCheckAnswers();
502
+ }
503
+ });
504
+
505
+ saveProgressBtn.addEventListener('click', () => {
506
+ // Determine if answers have been checked to pass correct state to saveGameState
507
+ const isChecked = cardsContainerEl.classList.contains('checked-answers') && !cardsContainerEl.classList.contains('hidden-answers');
508
+ let score = 0;
509
+ let percentage = 0;
510
+ let feedback = {};
511
+
512
+ if (isChecked) {
513
+ // If checked, parse score from display or re-calculate (safer to re-calculate if needed)
514
+ // For simplicity, assume scoreTextEl is up-to-date if isChecked is true
515
+ const scoreMatch = scoreTextEl.textContent.match(/Score: (\d+) \/ \d+ \((\d+)%\)/);
516
+ if (scoreMatch) {
517
+ score = parseInt(scoreMatch[1]);
518
+ percentage = parseInt(scoreMatch[2]);
519
+ }
520
+ // Collect feedback emojis again
521
+ document.querySelectorAll('.feedback-emoji').forEach(el => {
522
+ const idParts = el.id.split('-'); // e.g., feedback-q1 or feedback-q3-part1
523
+ let key = idParts[1];
524
+ if (idParts.length > 2 && (idParts[2] === 'part1' || idParts[2] === 'part2' || idParts[2] === 'main' || idParts[2] === 'bonus')) {
525
+ key += `-${idParts[2]}`;
526
+ }
527
+ if(el.textContent && el.textContent !== 'βž–') feedback[key] = el.textContent;
528
+ });
529
  }
530
+ saveGameState(isChecked, score, percentage, feedback);
531
  });
532
+
533
+ // Initial setup
534
+ // renderCards(); // Render empty cards initially, loadGameState will populate if data exists
535
+ // updateButtonStates(); // Called within loadGameState or auth callback
536
 
 
 
537
  </script>
538
  </body>
539
  </html>