-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1022 lines (836 loc) · 33.6 KB
/
script.js
File metadata and controls
1022 lines (836 loc) · 33.6 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// S.A.C - Social Alignment Chart
let people = [];
let chart = null;
// Load people from Firestore on startup
async function loadPeople() {
try {
// Wait for Firebase to be ready
if (!window.firebaseReady) {
console.log('⏳ Waiting for Firebase to initialize...');
await new Promise(resolve => {
window.addEventListener('firebaseReady', resolve, { once: true });
});
}
console.log('📥 Loading people from Firestore...');
const querySnapshot = await window.db.collection('people').get();
// Clear existing people array
people = [];
for (const docSnap of querySnapshot.docs) {
const data = docSnap.data();
// Load image from stored data URL if available
if (data.imageDataUrl) {
const img = new Image();
img.src = data.imageDataUrl;
data.image = img;
} else {
const image = await fetchTwitterPFP(data.handle);
data.image = image;
}
// Store the document ID for potential deletion
data.id = docSnap.id;
people.push(data);
}
// Redraw chart after all people are loaded
const canvas = document.getElementById('burnChart');
if (canvas) {
const ctx = canvas.getContext('2d');
drawChart(ctx, canvas.width, canvas.height);
updatePersonList();
}
console.log(`✅ Loaded ${people.length} people from Firestore`);
} catch (error) {
console.error('❌ Error loading people from Firestore:', error);
}
}
// Save person to Firestore
async function savePerson(person) {
try {
// Wait for Firebase if not ready
if (!window.firebaseReady) {
console.log('⏳ Waiting for Firebase before saving...');
await new Promise(resolve => {
window.addEventListener('firebaseReady', resolve, { once: true });
});
}
// Create a copy without the image object but with imageDataUrl
const toSave = {
handle: person.handle,
foodScore: person.foodScore,
character: person.character,
socialScore: person.socialScore,
imageDataUrl: person.imageDataUrl,
timestamp: new Date().toISOString()
};
console.log('💾 Saving person to Firestore:', toSave);
const docRef = await window.db.collection('people').add(toSave);
console.log('✅ Person saved to Firestore with ID:', docRef.id);
return docRef.id;
} catch (error) {
console.error('❌ Error saving person to Firestore:', error);
console.error('Error details:', error.message);
throw error;
}
}
// Color palette - Bubbly and bold
const colors = {
primary: '#FF1493', // Hot pink
secondary: '#FF69B4', // Bright pink
tertiary: '#9B59B6', // Purple
accent1: '#E91E63', // Deep pink
accent2: '#F06292', // Medium pink
accent3: '#FCE4EC' // Light pink
};
// Initialize chart
function initChart() {
const canvas = document.getElementById('burnChart');
const ctx = canvas.getContext('2d');
// Set canvas size to full container width
const container = canvas.parentElement;
const isMobile = window.innerWidth <= 768;
if (isMobile) {
// On mobile, use full available width minus minimal padding
canvas.width = container.clientWidth - 10;
} else {
// On desktop, use full container width
canvas.width = container.clientWidth - 40; // Subtract padding
}
canvas.height = 700;
drawChart(ctx, canvas.width, canvas.height);
}
// Draw the chart
function drawChart(ctx, width, height) {
// Clear canvas
ctx.clearRect(0, 0, width, height);
// Set up padding (reduced for more chart space)
const isMobile = window.innerWidth <= 768;
const padding = isMobile ? 35 : 50;
const chartWidth = width - padding * 2;
const chartHeight = height - padding * 2;
// Draw background
ctx.fillStyle = colors.accent3;
ctx.fillRect(padding, padding, chartWidth, chartHeight);
// Grid lines removed - only axis lines will be drawn below
// Draw axes
ctx.strokeStyle = colors.primary;
ctx.lineWidth = 3;
// X-axis (center horizontal)
ctx.beginPath();
ctx.moveTo(padding, padding + chartHeight / 2);
ctx.lineTo(padding + chartWidth, padding + chartHeight / 2);
ctx.stroke();
// Y-axis (center vertical)
ctx.beginPath();
ctx.moveTo(padding + chartWidth / 2, padding);
ctx.lineTo(padding + chartWidth / 2, padding + chartHeight);
ctx.stroke();
// Draw labels at the end of each pink axis line
ctx.fillStyle = colors.primary; // Use pink color to match the axis
const labelFontSize = isMobile ? 9 : 11;
ctx.font = `bold ${labelFontSize}px Georgia`;
// X-axis labels - stacked vertically letter by letter
const letterSpacing = isMobile ? 10 : 12;
const xLabelOffset = isMobile ? 15 : 20;
// Left end of X-axis: "Dietary Restrictions" (stacked vertically)
ctx.textAlign = 'center';
const leftLabel = 'Dietary Restrictions';
const leftStartY = padding + chartHeight / 2 - (leftLabel.length * letterSpacing) / 2;
for (let i = 0; i < leftLabel.length; i++) {
ctx.fillText(leftLabel[i], padding - xLabelOffset, leftStartY + i * letterSpacing);
}
// Right end of X-axis: "Epicurean" (stacked vertically)
const rightLabel = 'Epicurean';
const rightStartY = padding + chartHeight / 2 - (rightLabel.length * letterSpacing) / 2;
for (let i = 0; i < rightLabel.length; i++) {
ctx.fillText(rightLabel[i], padding + chartWidth + xLabelOffset, rightStartY + i * letterSpacing);
}
// Y-axis labels - horizontal landscape, positioned at the ends of the vertical pink line
const yLabelOffset = isMobile ? 8 : 10;
// Top end of Y-axis: "Pristine" (horizontal)
ctx.textAlign = 'center';
ctx.fillText('Pristine', padding + chartWidth / 2, padding - yLabelOffset);
// Bottom end of Y-axis: "Cunt" (horizontal)
ctx.textAlign = 'center';
ctx.fillText('Cunt', padding + chartWidth / 2, padding + chartHeight + yLabelOffset + labelFontSize);
// Draw axis numbers with smaller font
const numberFontSize = isMobile ? 8 : 10;
ctx.font = `${numberFontSize}px Georgia`;
ctx.fillStyle = colors.tertiary;
// X-axis numbers
const xNumberY = isMobile ? height - 5 : height - 10;
ctx.fillText('0', padding, xNumberY);
ctx.fillText('50', padding + chartWidth / 2, xNumberY);
ctx.fillText('100', padding + chartWidth, xNumberY);
// Y-axis numbers
ctx.textAlign = 'right';
const yNumberOffset = isMobile ? 5 : 8;
ctx.fillText('100', padding - yNumberOffset, padding + 5);
ctx.fillText('50', padding - yNumberOffset, padding + chartHeight / 2 + 5);
ctx.fillText('0', padding - yNumberOffset, padding + chartHeight + 5);
// Draw people on chart with collision detection
console.log('Drawing', people.length, 'people on chart');
// Track occupied positions to detect overlaps
const positions = [];
const minDistance = isMobile ? 25 : 30; // Minimum distance between avatars (using isMobile from above)
people.forEach((person, index) => {
console.log(`Drawing person ${index}:`, person.handle, 'foodScore:', person.foodScore, 'character:', person.character);
// Calculate base position (0-100 scale)
let baseX = padding + (person.foodScore / 100) * chartWidth;
let baseY = padding + ((100 - person.character) / 100) * chartHeight;
let offsetX = 0;
let offsetY = 0;
let finalX = baseX;
let finalY = baseY;
// Check for collisions with existing positions
let collision = true;
let attempts = 0;
const maxAttempts = 30;
while (collision && attempts < maxAttempts) {
collision = false;
for (const pos of positions) {
const distance = Math.sqrt(Math.pow(finalX - pos.x, 2) + Math.pow(finalY - pos.y, 2));
if (distance < minDistance) {
collision = true;
// Offset in a spiral pattern for better distribution
const angle = (attempts / maxAttempts) * Math.PI * 4; // More rotations
const radius = minDistance * (1 + attempts / maxAttempts); // Expanding spiral
offsetX = Math.cos(angle) * radius;
offsetY = Math.sin(angle) * radius;
finalX = baseX + offsetX;
finalY = baseY + offsetY;
break;
}
}
attempts++;
}
// Store the final position
positions.push({ x: finalX, y: finalY });
// Draw with offset
drawPersonOnChart(ctx, person, padding, chartWidth, chartHeight, offsetX, offsetY);
});
}
// Draw person on chart
function drawPersonOnChart(ctx, person, padding, chartWidth, chartHeight, offsetX = 0, offsetY = 0) {
// Convert coordinates (0 to 100) to canvas coordinates
let x = padding + (person.foodScore / 100) * chartWidth;
let y = padding + ((100 - person.character) / 100) * chartHeight; // Inverted Y
// Apply offset to prevent overlapping
x += offsetX;
y += offsetY;
console.log(`Positioning ${person.handle} at x:${x}, y:${y}`);
// Smaller avatar size for less clutter
const isMobile = window.innerWidth <= 768;
const size = isMobile ? 20 : 25;
const radius = size / 2;
// Check if user is logged in or in view-only mode
const isLoggedIn = sessionStorage.getItem('authenticated');
const isViewOnly = sessionStorage.getItem('viewOnly');
// Draw profile picture if available AND (logged in OR view-only)
if (person.image && (isLoggedIn || isViewOnly)) {
const img = person.image;
// Draw circular clipping path
ctx.save();
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.closePath();
ctx.clip();
// Draw image
ctx.drawImage(img, x - radius, y - radius, size, size);
ctx.restore();
// Draw border around image
ctx.strokeStyle = getScoreColor(person.socialScore);
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.stroke();
} else {
// Draw pink circle placeholder (no pfp when not logged in)
ctx.fillStyle = colors.primary; // Hot pink
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fill();
// Only show colored border if logged in or view-only
if (isLoggedIn || isViewOnly) {
ctx.strokeStyle = getScoreColor(person.socialScore);
ctx.lineWidth = 2;
ctx.stroke();
}
}
// Don't draw handle or score on chart anymore - only show avatar
}
// Calculate social score based on position
function calculateSocialScore(foodScore, character) {
// Formula: weight character more heavily, add some food score factor
// Pristine character (high) and Epicurean (high food tolerance) = positive score
// Cunt character (low) and Dietary restrictions (low food tolerance) = negative score
const characterWeight = 0.7;
const foodWeight = 0.3;
const score = (character * characterWeight) + (foodScore * foodWeight);
return Math.round(score); // Already on 0-100 scale
}
// Get color based on score
function getScoreColor(score) {
if (score > 75) return colors.primary;
if (score > 50) return colors.secondary;
if (score > 25) return colors.tertiary;
return '#666';
}
// Load uploaded image file
async function loadImageFile(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = () => reject(new Error('Failed to load image'));
img.src = e.target.result;
};
reader.onerror = () => reject(new Error('Failed to read file'));
reader.readAsDataURL(file);
});
}
// Fetch Twitter profile picture (fallback if no image uploaded)
async function fetchTwitterPFP(handle) {
// Remove @ if present
const username = handle.replace('@', '');
try {
// Use a placeholder service
const placeholderUrl = `https://ui-avatars.com/api/?name=${encodeURIComponent(username)}&size=200&background=${colors.accent1.replace('#', '')}&color=fff&bold=true`;
// Create image element
const img = new Image();
img.crossOrigin = 'anonymous';
return new Promise((resolve, reject) => {
img.onload = () => resolve(img);
img.onerror = () => {
// Fallback to placeholder
resolve(null);
};
img.src = placeholderUrl;
});
} catch (error) {
console.error('Error fetching profile picture:', error);
return null;
}
}
// Check authentication status and show/hide overlay
function checkAuth() {
const overlay = document.getElementById('loginOverlay');
const userProfile = document.getElementById('userProfile');
const viewOnceButton = document.getElementById('viewOnceButton');
const isAuthenticated = sessionStorage.getItem('authenticated');
const isViewOnly = sessionStorage.getItem('viewOnly');
if (isAuthenticated) {
overlay.classList.add('hidden');
// Hide View Once button when fully authenticated
if (viewOnceButton) {
viewOnceButton.classList.add('hidden');
}
// Show user profile
if (userProfile) {
const userName = sessionStorage.getItem('username');
const userEmoji = sessionStorage.getItem('userEmoji');
if (userName && userEmoji) {
document.getElementById('userName').textContent = userName;
const emojiElement = document.getElementById('userEmoji');
// Check if userEmoji is a data URL (image) or emoji
if (userEmoji.startsWith('data:')) {
// It's an image
emojiElement.innerHTML = `<img src="${userEmoji}" style="width: 1.5em; height: 1.5em; border-radius: 50%; object-fit: cover;">`;
} else {
// It's an emoji
emojiElement.textContent = userEmoji;
}
userProfile.style.display = 'flex';
}
}
} else {
overlay.classList.remove('hidden');
if (userProfile) {
userProfile.style.display = 'none';
}
// Show View Once button when not authenticated
if (viewOnceButton && !isViewOnly) {
viewOnceButton.classList.remove('hidden');
}
}
}
// Handle logout
function handleLogout() {
sessionStorage.clear();
checkAuth();
// Redraw chart to hide pfps
const canvas = document.getElementById('burnChart');
const ctx = canvas.getContext('2d');
drawChart(ctx, canvas.width, canvas.height);
// Update person list to hide scores
updatePersonList();
}
// View-only mode variables
const VIEW_ONLY_PASSWORD = 'onlyratz';
let viewOnlyTimeout = null;
// Handle view-only access
function handleViewOnce() {
// Show the custom modal
document.getElementById('viewOnceModal').style.display = 'block';
// Focus the password input
setTimeout(() => {
document.getElementById('viewOncePassword').focus();
}, 100);
}
function closeViewOnceModal() {
document.getElementById('viewOnceModal').style.display = 'none';
document.getElementById('viewOncePassword').value = '';
}
function submitViewOncePassword() {
const password = document.getElementById('viewOncePassword').value;
if (password === VIEW_ONLY_PASSWORD) {
// Close modal
closeViewOnceModal();
// Set view-only session
sessionStorage.setItem('viewOnly', 'true');
sessionStorage.setItem('viewOnlyTime', Date.now().toString());
// Hide the View Once button
const viewOnceButton = document.getElementById('viewOnceButton');
if (viewOnceButton) {
viewOnceButton.classList.add('hidden');
}
// Redraw chart to show pfps
const canvas = document.getElementById('burnChart');
const ctx = canvas.getContext('2d');
drawChart(ctx, canvas.width, canvas.height);
// Update person list to show scores
updatePersonList();
// Set 15-second timeout
viewOnlyTimeout = setTimeout(() => {
handleViewOnlyExpire();
}, 15000);
alert('✅ View-only access granted for 15 seconds!');
} else {
alert('❌ Incorrect password!');
document.getElementById('viewOncePassword').value = '';
document.getElementById('viewOncePassword').focus();
}
}
// Handle view-only expiration
function handleViewOnlyExpire() {
sessionStorage.removeItem('viewOnly');
sessionStorage.removeItem('viewOnlyTime');
// Show the View Once button again
const viewOnceButton = document.getElementById('viewOnceButton');
if (viewOnceButton) {
viewOnceButton.classList.remove('hidden');
}
// Redraw chart to hide pfps
const canvas = document.getElementById('burnChart');
const ctx = canvas.getContext('2d');
drawChart(ctx, canvas.width, canvas.height);
// Update person list to hide scores
updatePersonList();
alert('⏰ View-only session expired. Click "View Once" to view again.');
}
// Handle login button click
document.addEventListener('DOMContentLoaded', function() {
const loginButton = document.getElementById('loginButton');
if (loginButton) {
loginButton.addEventListener('click', function() {
window.location.href = 'login.html';
});
}
const logoutButton = document.getElementById('logoutButton');
if (logoutButton) {
logoutButton.addEventListener('click', handleLogout);
}
const viewOnceButton = document.getElementById('viewOnceButton');
if (viewOnceButton) {
viewOnceButton.addEventListener('click', handleViewOnce);
}
const editProfileButton = document.getElementById('editProfileButton');
if (editProfileButton) {
editProfileButton.addEventListener('click', openEditProfileModal);
}
// Check auth on page load
checkAuth();
// Check auth periodically in case user logs in from another tab
setInterval(checkAuth, 1000);
});
// Add person to chart
async function addPerson() {
console.log('addPerson called');
const handleInput = document.getElementById('twitterHandle');
const profilePicInput = document.getElementById('profilePic');
const foodInput = document.getElementById('foodScore');
const characterInput = document.getElementById('character');
console.log('Inputs found:', {
handleInput: !!handleInput,
profilePicInput: !!profilePicInput,
foodInput: !!foodInput,
characterInput: !!characterInput
});
const handle = handleInput.value.trim();
const foodScore = parseInt(foodInput.value);
const character = parseInt(characterInput.value);
console.log('Values:', { handle, foodScore, character });
// Validation
if (!handle) {
alert('Please enter a Twitter/X handle!');
return;
}
if (isNaN(foodScore) || foodScore < 0 || foodScore > 100) {
alert('Food score must be between 0 and 100!');
return;
}
if (isNaN(character) || character < 0 || character > 100) {
alert('Character must be between 0 and 100!');
return;
}
// Calculate social score
const socialScore = calculateSocialScore(foodScore, character);
// Load profile picture
let image = null;
let imageDataUrl = null;
if (profilePicInput.files && profilePicInput.files[0]) {
try {
image = await loadImageFile(profilePicInput.files[0]);
imageDataUrl = image.src; // Store the data URL for localStorage
} catch (error) {
console.error('Error loading image:', error);
image = await fetchTwitterPFP(handle);
}
} else {
image = await fetchTwitterPFP(handle);
if (image) {
imageDataUrl = image.src;
}
}
// Create person object
const person = {
handle: handle.startsWith('@') ? handle : '@' + handle,
foodScore,
character,
socialScore,
image,
imageDataUrl // Store for localStorage
};
// Add to people array
people.push(person);
// Save to Firestore
try {
const docId = await savePerson(person);
person.id = docId; // Store the Firestore document ID
console.log('Person added successfully:', person.handle);
} catch (error) {
console.error('Failed to save person:', error);
alert('Failed to save person to database. Please try again.');
// Remove from local array if save failed
people.pop();
return;
}
// Redraw chart
const canvas = document.getElementById('burnChart');
const ctx = canvas.getContext('2d');
drawChart(ctx, canvas.width, canvas.height);
// Update person list
updatePersonList();
// Clear inputs
handleInput.value = '';
profilePicInput.value = '';
foodInput.value = '';
characterInput.value = '';
}
// Update person list display
function updatePersonList() {
const personList = document.getElementById('personList');
personList.innerHTML = '';
// Check if user is logged in or in view-only mode
const isLoggedIn = sessionStorage.getItem('authenticated');
const isViewOnly = sessionStorage.getItem('viewOnly');
people.forEach((person, index) => {
const card = document.createElement('div');
card.className = 'person-card';
// Make cards clickable for admins only (not view-only)
if (isLoggedIn) {
card.classList.add('clickable');
card.onclick = () => openEditScoreModal(index);
}
const pfp = document.createElement('img');
pfp.className = 'person-pfp';
if (person.imageDataUrl) {
pfp.src = person.imageDataUrl;
} else if (person.image) {
pfp.src = person.image.src;
} else {
pfp.src = `https://ui-avatars.com/api/?name=${encodeURIComponent(person.handle.replace('@', ''))}&size=100&background=${colors.accent1.replace('#', '')}&color=fff`;
}
const info = document.createElement('div');
info.className = 'person-info';
const handleDiv = document.createElement('div');
handleDiv.className = 'person-handle';
handleDiv.textContent = person.handle;
// Only show score if logged in or view-only
if (isLoggedIn || isViewOnly) {
const scoreDiv = document.createElement('div');
scoreDiv.className = 'person-score';
const scoreClass = person.socialScore >= 0 ? 'score-positive' : 'score-negative';
scoreDiv.innerHTML = `Score: <span class="${scoreClass}">${person.socialScore}</span>`;
info.appendChild(scoreDiv);
}
info.appendChild(handleDiv);
card.appendChild(pfp);
card.appendChild(info);
personList.appendChild(card);
});
}
// Edit score modal functions
let currentEditingIndex = null;
function openEditScoreModal(index) {
const isLoggedIn = sessionStorage.getItem('authenticated');
if (!isLoggedIn) return;
currentEditingIndex = index;
const person = people[index];
document.getElementById('editHandle').value = person.handle;
document.getElementById('editFoodScore').value = person.foodScore;
document.getElementById('editCharacterScore').value = person.character;
document.getElementById('editScoreModal').style.display = 'block';
}
function closeEditScoreModal() {
document.getElementById('editScoreModal').style.display = 'none';
currentEditingIndex = null;
}
async function saveEditedEntry() {
if (currentEditingIndex === null) return;
const person = people[currentEditingIndex];
const newFoodScore = parseInt(document.getElementById('editFoodScore').value);
const newCharacterScore = parseInt(document.getElementById('editCharacterScore').value);
// Validation
if (isNaN(newFoodScore) || newFoodScore < 0 || newFoodScore > 100) {
alert('Food score must be between 0 and 100!');
return;
}
if (isNaN(newCharacterScore) || newCharacterScore < 0 || newCharacterScore > 100) {
alert('Character score must be between 0 and 100!');
return;
}
// Update person
person.foodScore = newFoodScore;
person.character = newCharacterScore;
person.socialScore = calculateSocialScore(newFoodScore, newCharacterScore);
// Update in Firestore
try {
await window.db.collection('people').doc(person.id).update({
foodScore: person.foodScore,
character: person.character,
socialScore: person.socialScore
});
// Redraw chart
const canvas = document.getElementById('burnChart');
const ctx = canvas.getContext('2d');
drawChart(ctx, canvas.width, canvas.height);
// Update person list
updatePersonList();
closeEditScoreModal();
console.log('Entry updated successfully');
} catch (error) {
console.error('Error updating entry:', error);
alert('Failed to update entry. Please try again.');
}
}
async function deleteEntry() {
if (currentEditingIndex === null) return;
if (!confirm('Are you sure you want to delete this entry?')) {
return;
}
const person = people[currentEditingIndex];
// Delete from Firestore
try {
await window.db.collection('people').doc(person.id).delete();
// Remove from local array
people.splice(currentEditingIndex, 1);
// Redraw chart
const canvas = document.getElementById('burnChart');
const ctx = canvas.getContext('2d');
drawChart(ctx, canvas.width, canvas.height);
// Update person list
updatePersonList();
closeEditScoreModal();
console.log('Entry deleted successfully');
} catch (error) {
console.error('Error deleting entry:', error);
alert('Failed to delete entry. Please try again.');
}
}
// Close modal when clicking outside
window.addEventListener('click', function(event) {
const editScoreModal = document.getElementById('editScoreModal');
const editProfileModal = document.getElementById('editProfileModal');
const viewOnceModal = document.getElementById('viewOnceModal');
if (event.target === editScoreModal) {
closeEditScoreModal();
}
if (event.target === editProfileModal) {
closeEditProfileModal();
}
if (event.target === viewOnceModal) {
closeViewOnceModal();
}
});
// Edit Profile Modal Functions
function openEditProfileModal() {
const userId = sessionStorage.getItem('userId');
console.log('Opening edit profile modal, userId:', userId);
if (!userId) {
console.error('No userId found in sessionStorage');
alert('Error: No user ID found. Please log in again.');
return;
}
// Load board members from localStorage
const boardMembers = JSON.parse(localStorage.getItem('boardMembers') || '[]');
console.log('Board members from localStorage:', boardMembers);
const member = boardMembers.find(m => m.id === userId);
console.log('Found member:', member);
if (!member) {
console.error('Member not found for userId:', userId);
alert('Error: Profile data not found. Please log in again.');
return;
}
// Populate form
document.getElementById('editProfileName').value = member.name;
document.getElementById('editProfileRole').value = member.role;
document.getElementById('editProfileEmoji').value = member.emoji;
document.getElementById('editProfilePassword').value = member.password;
// Show preview
const previewEmoji = document.getElementById('profilePreviewEmoji');
const previewImage = document.getElementById('profilePreviewImage');
if (member.imageUrl) {
previewImage.src = member.imageUrl;
previewImage.style.display = 'block';
previewEmoji.style.display = 'none';
} else {
previewEmoji.textContent = member.emoji;
previewEmoji.style.display = 'block';
previewImage.style.display = 'none';
}
document.getElementById('editProfileModal').style.display = 'block';
}
function closeEditProfileModal() {
document.getElementById('editProfileModal').style.display = 'none';
}
async function saveEditedProfile() {
const userId = sessionStorage.getItem('userId');
if (!userId) return;
const newName = document.getElementById('editProfileName').value;
const newRole = document.getElementById('editProfileRole').value;
const newEmoji = document.getElementById('editProfileEmoji').value || '🐭';
const newPassword = document.getElementById('editProfilePassword').value;
if (!newName || !newRole || !newPassword) {
alert('Please fill in all required fields!');
return;
}
// Load board members
const boardMembers = JSON.parse(localStorage.getItem('boardMembers') || '[]');
const memberIndex = boardMembers.findIndex(m => m.id === userId);
if (memberIndex === -1) return;
// Update member data
boardMembers[memberIndex].name = newName;
boardMembers[memberIndex].role = newRole;
boardMembers[memberIndex].emoji = newEmoji;
boardMembers[memberIndex].password = newPassword;
// Handle profile picture upload
const fileInput = document.getElementById('editProfilePic');
if (fileInput.files && fileInput.files[0]) {
const reader = new FileReader();
reader.onload = function(e) {
boardMembers[memberIndex].imageUrl = e.target.result;
// Save to localStorage
localStorage.setItem('boardMembers', JSON.stringify(boardMembers));
// Update session storage
sessionStorage.setItem('username', newName);
sessionStorage.setItem('userRole', newRole);
sessionStorage.setItem('userEmoji', e.target.result);
// Update UI
updateUserProfileDisplay();
closeEditProfileModal();
alert('✅ Profile updated successfully!');
};
reader.readAsDataURL(fileInput.files[0]);
} else {
// Save without new image
localStorage.setItem('boardMembers', JSON.stringify(boardMembers));
// Update session storage
sessionStorage.setItem('username', newName);
sessionStorage.setItem('userRole', newRole);
sessionStorage.setItem('userEmoji', boardMembers[memberIndex].imageUrl || newEmoji);
// Update UI
updateUserProfileDisplay();
closeEditProfileModal();
alert('✅ Profile updated successfully!');
}
}
function updateUserProfileDisplay() {
const userName = sessionStorage.getItem('username');
const userEmoji = sessionStorage.getItem('userEmoji');
if (userName && userEmoji) {
document.getElementById('userName').textContent = userName;
const emojiElement = document.getElementById('userEmoji');
if (userEmoji.startsWith('data:')) {
emojiElement.innerHTML = `<img src="${userEmoji}" style="width: 1.5em; height: 1.5em; border-radius: 50%; object-fit: cover;">`;
} else {
emojiElement.textContent = userEmoji;
}
}
}
// Profile picture preview in edit modal
document.addEventListener('DOMContentLoaded', function() {
const profilePicInput = document.getElementById('editProfilePic');
if (profilePicInput) {
profilePicInput.addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(event) {
const previewImage = document.getElementById('profilePreviewImage');
const previewEmoji = document.getElementById('profilePreviewEmoji');
previewImage.src = event.target.result;
previewImage.style.display = 'block';
previewEmoji.style.display = 'none';
};
reader.readAsDataURL(file);
}
});
}
});
// Event listeners
document.getElementById('addPerson').addEventListener('click', addPerson);
// Allow Enter key to add person
document.getElementById('twitterHandle').addEventListener('keypress', (e) => {
if (e.key === 'Enter') addPerson();
});
document.getElementById('foodScore').addEventListener('keypress', (e) => {
if (e.key === 'Enter') addPerson();
});
document.getElementById('character').addEventListener('keypress', (e) => {
if (e.key === 'Enter') addPerson();
});
// Handle window resize
window.addEventListener('resize', () => {
const canvas = document.getElementById('burnChart');
const container = canvas.parentElement;
const isMobile = window.innerWidth <= 768;