-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
456 lines (416 loc) Β· 21.8 KB
/
app.js
File metadata and controls
456 lines (416 loc) Β· 21.8 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
// βββ SOMARS Points Tracker Β· app.js ββββββββββββββββββββββββββββββββββββββββββ
// ββ State βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
let members = [];
let selectedMemberId = null; // member currently open in the add-points modal
let profilePageId = null; // member whose profile page is displayed
let isUnlocked = false; // true after the admin enters the correct password
// ββ Init ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function init() {
generateStars();
startFirestoreListener();
}
// ββ Firestore real-time listener ββββββββββββββββββββββββββββββββββββββββββββββ
function startFirestoreListener() {
showConnectionStatus("connecting");
db.collection("members")
.orderBy("id")
.onSnapshot(snapshot => {
if (snapshot.empty) {
seedDatabase();
} else {
members = snapshot.docs.map(doc => {
const firestoreData = doc.data();
// Profile info (name, icon, bio) always comes from data.js β the source of truth.
// Only "points" is stored in and read from Firestore.
// This means editing data.js immediately updates names/icons/bios on the site.
const profile = DEFAULT_MEMBERS.find(m => m.id === firestoreData.id);
return {
docId: doc.id,
id: firestoreData.id,
points: firestoreData.points ?? 0,
name: profile?.name || firestoreData.name || `Crew Member ${firestoreData.id}`,
icon: profile?.icon || firestoreData.icon || "π",
bio: profile?.bio || firestoreData.bio || "",
};
});
showConnectionStatus("live");
renderAll();
if (profilePageId !== null) renderProfilePage(profilePageId);
if (selectedMemberId !== null) {
const m = members.find(x => x.id === selectedMemberId);
if (m) document.getElementById("modal-member-pts").textContent =
m.points.toLocaleString() + " pts";
}
}
}, err => {
console.error("Firestore error:", err);
showConnectionStatus("error");
const saved = localStorage.getItem("somars_members_fallback");
if (saved) members = JSON.parse(saved);
renderAll();
});
}
// ββ Seed DB on first launch βββββββββββββββββββββββββββββββββββββββββββββββββββ
async function seedDatabase() {
showConnectionStatus("seeding");
const batch = db.batch();
DEFAULT_MEMBERS.forEach(m => {
const ref = db.collection("members").doc(String(m.id));
batch.set(ref, m);
});
await batch.commit();
}
// ββ Connection badge ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function showConnectionStatus(state) {
let badge = document.getElementById("conn-badge");
if (!badge) {
badge = document.createElement("div");
badge.id = "conn-badge";
badge.style.cssText = `
position:fixed;bottom:18px;right:18px;z-index:300;
padding:7px 14px;border-radius:20px;font-size:0.75rem;
font-weight:700;letter-spacing:0.5px;backdrop-filter:blur(8px);
border:1px solid;transition:all 0.4s;
`;
document.body.appendChild(badge);
}
const S = {
connecting: { text:"π‘ Connecting...", bg:"rgba(255,180,0,.15)", border:"#ffb400", color:"#ffb400" },
seeding: { text:"π± Setting up DB...", bg:"rgba(0,212,255,.15)", border:"#00d4ff", color:"#00d4ff" },
live: { text:"π’ Live β synced", bg:"rgba(0,232,120,.15)", border:"#00e878", color:"#00e878" },
error: { text:"β οΈ Offline (local)", bg:"rgba(255,80,80,.15)", border:"#ff5050", color:"#ff5050" },
}[state];
badge.textContent = S.text;
badge.style.background = S.bg;
badge.style.borderColor = S.border;
badge.style.color = S.color;
badge.style.opacity = "1";
if (state === "live") {
clearTimeout(badge._t);
badge._t = setTimeout(() => { badge.style.opacity = "0"; }, 4000);
}
}
// ββ Stars βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function generateStars() {
const c = document.getElementById("stars");
for (let i = 0; i < 180; i++) {
const s = document.createElement("div");
s.className = "star";
s.style.left = Math.random() * 100 + "vw";
s.style.top = Math.random() * 100 + "vh";
const sz = Math.random() * 2.5 + 0.5;
s.style.width = sz + "px";
s.style.height = sz + "px";
s.style.animationDelay = Math.random() * 4 + "s";
s.style.animationDuration = (Math.random() * 3 + 2) + "s";
c.appendChild(s);
}
}
// ββ Admin password modal ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// The password is stored in data.js as ADMIN_PASSWORD.
// Edit that value and push to GitHub to change the password.
function handleAdminNavClick() {
if (isUnlocked) {
lockAdmin();
} else {
openAdminModal();
}
}
function openAdminModal() {
const modal = document.getElementById("admin-modal");
document.getElementById("admin-username-input").value = "";
document.getElementById("admin-password-input").value = "";
document.getElementById("admin-error").style.display = "none";
modal.classList.add("open");
setTimeout(() => document.getElementById("admin-username-input").focus(), 80);
}
function closeAdminModal(e) {
if (e.target.id === "admin-modal") closeAdminModalDirect();
}
function closeAdminModalDirect() {
document.getElementById("admin-modal").classList.remove("open");
}
function submitAdminPassword() {
const user = document.getElementById("admin-username-input").value;
const pass = document.getElementById("admin-password-input").value;
if (user === ADMIN_USERNAME && pass === ADMIN_PASSWORD) {
isUnlocked = true;
closeAdminModalDirect();
updateAdminNavBtn();
renderAll();
} else {
const err = document.getElementById("admin-error");
err.textContent = "β Incorrect username or password. Try again.";
err.style.display = "block";
document.getElementById("admin-password-input").value = "";
document.getElementById("admin-username-input").focus();
}
}
function lockAdmin() {
isUnlocked = false;
updateAdminNavBtn();
renderAll();
closeModalDirect();
}
function updateAdminNavBtn() {
const btn = document.getElementById("admin-nav-btn");
if (!btn) return;
if (isUnlocked) {
btn.textContent = "π Admin (click to lock)";
btn.classList.add("unlocked");
} else {
btn.textContent = "π Edit Points";
btn.classList.remove("unlocked");
}
const ppBtn = document.getElementById("pp-add-pts-btn");
if (ppBtn) ppBtn.style.display = isUnlocked ? "inline-flex" : "none";
}
// Allow pressing Enter in either credential field to submit
document.addEventListener("DOMContentLoaded", () => {
const uInp = document.getElementById("admin-username-input");
const pInp = document.getElementById("admin-password-input");
if (uInp) uInp.addEventListener("keydown", e => { if (e.key === "Enter") document.getElementById("admin-password-input").focus(); });
if (pInp) pInp.addEventListener("keydown", e => { if (e.key === "Enter") submitAdminPassword(); });
});
// ββ πΌοΈ IMAGE RENDER POINT βββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Each member has an "icon" field in data.js. It can be:
// β’ An emoji string β rendered as a <span>
// β’ A file path β rendered as an <img> (e.g. "images/alice.jpg")
//
// To use a photo:
// 1. Add the image file to the images/ folder next to index.html
// 2. Set icon: "images/yourphoto.jpg" in that member's entry in data.js
// 3. Open a Pull Request β see README.md for full steps.
//
// The icon is rendered the same way EVERYWHERE the member appears:
// leaderboard, member grid, profile page, add-points modal header.
// Updating data.js once updates all instances automatically.
function renderMemberIcon(icon, cssClass = "") {
const isImg = icon && (icon.startsWith("images/") || /\.(jpg|jpeg|png|webp|gif|svg)$/i.test(icon));
return isImg
? `<img src="${escHtml(icon)}" class="member-img-icon ${cssClass}" alt="member photo"/>`
: `<span class="${cssClass}">${icon}</span>`;
}
// ββ Page routing ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function showPage(name) {
document.querySelectorAll(".page").forEach(p => p.classList.remove("active"));
document.querySelectorAll(".nav-btn").forEach(b => b.classList.remove("active"));
document.getElementById("page-" + name).classList.add("active");
// The profile page has no dedicated nav button β keep "Home Base" highlighted
const navId = "nav-" + (name === "profile" ? "home" : name);
const navEl = document.getElementById(navId);
if (navEl) navEl.classList.add("active");
renderAll();
}
// ββ Render all ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function renderAll() {
renderHomeStats();
renderMilestoneNodes();
renderLeaderboard();
renderAwardsFull();
renderGroupMilestonesFull();
}
// ββ Home stats ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function renderHomeStats() {
const total = members.reduce((s, m) => s + m.points, 0);
const maxPts = members.length ? Math.max(...members.map(m => m.points)) : 0;
document.getElementById("total-points").textContent = total.toLocaleString();
document.getElementById("active-members").textContent = members.length;
document.getElementById("awards-unlocked").textContent = AWARDS.filter(a => maxPts >= a.points).length;
const next = GROUP_MILESTONES.find(a => total < a.points);
document.getElementById("next-award-pts").textContent = next ? next.points.toLocaleString() : "ALL DONE π";
}
// ββ Group milestone nodes (home page) ββββββββββββββββββββββββββββββββββββββββ
function renderMilestoneNodes() {
const el = document.getElementById("group-milestone-nodes");
if (!el) return;
const total = members.reduce((s, m) => s + m.points, 0);
el.innerHTML = GROUP_MILESTONES.map(a => {
const done = total >= a.points;
const pct = Math.min(100, total > 0 ? Math.round((total / a.points) * 100) : 0);
return `
<div class="milestone-node ${done ? "node-done" : ""} ${a.special ? "node-special" : ""}">
<div class="node-icon">${renderMemberIcon(a.icon)}</div>
<div class="node-pts" style="color:${a.color}">${a.points.toLocaleString()}</div>
<div class="node-title">${a.title}</div>
<div class="node-bar-wrap"><div class="node-bar" style="width:${pct}%;background:${a.color}"></div></div>
<div class="node-pct">${done ? "β
Unlocked!" : pct + "%"}</div>
</div>`;
}).join("");
}
// ββ Leaderboard βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function renderLeaderboard() {
const sorted = [...members].sort((a, b) => b.points - a.points);
const el = document.getElementById("leaderboard");
el.innerHTML = sorted.map((m, i) => {
const medal = i === 0 ? "π₯" : i === 1 ? "π₯" : i === 2 ? "π₯" : `#${i+1}`;
const bar = Math.max(2, sorted[0].points > 0 ? Math.round((m.points / sorted[0].points) * 100) : 2);
return `
<div class="lb-row">
<span class="lb-rank">${medal}</span>
<span class="lb-icon">${renderMemberIcon(m.icon)}</span>
<span class="lb-name lb-name-link" onclick="openProfilePage(${m.id})">${escHtml(m.name)}</span>
<div class="lb-bar-wrap"><div class="lb-bar" style="width:${bar}%"></div></div>
<span class="lb-pts">${m.points.toLocaleString()} pts</span>
</div>`;
}).join("");
}
// ββ Individual awards (Awards Bay tab) βββββββββββββββββββββββββββββββββββββββ
function renderAwardsFull() {
const el = document.getElementById("awards-full");
if (!el) return;
const maxPts = members.length ? Math.max(...members.map(m => m.points)) : 0;
el.innerHTML = AWARDS.map(a => {
const done = maxPts >= a.points;
const pct = Math.min(100, maxPts > 0 ? Math.round((maxPts / a.points) * 100) : 0);
return `
<div class="award-full-card ${done ? "award-done" : ""} ${a.special ? "award-special" : ""}">
<div class="afc-left"><div class="afc-icon">${renderMemberIcon(a.icon)}</div></div>
<div class="afc-body">
<div class="afc-title">${a.title}
${a.special ? '<span class="special-badge">β SPECIAL</span>' : ""}
${done ? '<span class="unlocked-badge">β
Someone has this</span>' : ""}
</div>
<div class="afc-pts" style="color:${a.color}">${a.points.toLocaleString()} individual pts required</div>
<div class="afc-desc">${a.desc}</div>
<div class="afc-progress-wrap"><div class="afc-progress-bar" style="width:${pct}%;background:${a.color}"></div></div>
<div class="afc-pct">${done ? "Top member has this!" : `Top member at ${pct}%`}</div>
</div>
</div>`;
}).join("");
}
// ββ Group milestones (Awards Bay tab) βββββββββββββββββββββββββββββββββββββββββ
function renderGroupMilestonesFull() {
const el = document.getElementById("group-milestones-full");
if (!el) return;
const total = members.reduce((s, m) => s + m.points, 0);
el.innerHTML = GROUP_MILESTONES.map(a => {
const done = total >= a.points;
const pct = Math.min(100, total > 0 ? Math.round((total / a.points) * 100) : 0);
return `
<div class="award-full-card ${done ? "award-done" : ""} ${a.special ? "award-special" : ""}">
<div class="afc-left"><div class="afc-icon">${renderMemberIcon(a.icon)}</div></div>
<div class="afc-body">
<div class="afc-title">${a.title}
${a.special ? '<span class="special-badge">β SPECIAL</span>' : ""}
${done ? '<span class="unlocked-badge">β
UNLOCKED</span>' : ""}
</div>
<div class="afc-pts" style="color:${a.color}">${a.points.toLocaleString()} combined points required</div>
<div class="afc-desc">${a.desc}</div>
<div class="afc-progress-wrap"><div class="afc-progress-bar" style="width:${pct}%;background:${a.color}"></div></div>
<div class="afc-pct">${done ? "Complete! π" : `${pct}% β ${(a.points-total).toLocaleString()} pts to go`}</div>
</div>
</div>`;
}).join("");
}
// ββ Profile PAGE ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function openProfilePage(id) {
profilePageId = id;
renderProfilePage(id);
showPage("profile");
}
function renderProfilePage(id) {
const m = members.find(x => x.id === id);
if (!m) return;
// ββ Header ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// renderMemberIcon() uses the same "icon" field that is set in data.js.
// If that field is changed to an image path (e.g. "images/alice.jpg"),
// it automatically renders as <img> here AND everywhere else the member
// appears (leaderboard, member grid, points modal header).
document.getElementById("pp-icon").innerHTML = renderMemberIcon(m.icon, "pp-icon-inner");
document.getElementById("pp-name").textContent = m.name;
document.getElementById("pp-pts").textContent = m.points.toLocaleString() + " pts";
const award = [...AWARDS].reverse().find(a => m.points >= a.points);
document.getElementById("pp-rank").innerHTML = award
? `<span style="color:${award.color}">${renderMemberIcon(award.icon)} ${award.title}</span>`
: '<span style="color:#7a9ab8">π¦ No rank yet</span>';
// ββ Bio (read-only) ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// To update a bio, edit the "bio" field in data.js and submit a PR.
// See README.md β "Editing Your Profile".
document.getElementById("pp-bio-display").textContent =
m.bio || "No bio yet. Submit a PR on GitHub to add one β see README.md.";
// ββ "Add Points" button β only visible when admin is unlocked ββββββββββββ
const addBtn = document.getElementById("pp-add-pts-btn");
if (addBtn) addBtn.style.display = isUnlocked ? "inline-flex" : "none";
// ββ Personal awards progress βββββββββββββββββββββββββββββββββββββββββββββ
const awardsEl = document.getElementById("pp-awards");
awardsEl.innerHTML = AWARDS.map(a => {
const done = m.points >= a.points;
const pct = Math.min(100, m.points > 0 ? Math.round((m.points / a.points) * 100) : 0);
return `
<div class="profile-award-row ${done ? "done" : ""}">
<span class="pa-icon">${renderMemberIcon(a.icon)}</span>
<div class="pa-body">
<div class="pa-title" style="color:${a.color}">${a.title}
${done ? '<span class="unlocked-badge">β
</span>' : ""}
${a.special ? '<span class="special-badge">β</span>' : ""}
</div>
<div class="pa-bar-wrap"><div class="pa-bar" style="width:${pct}%;background:${a.color}"></div></div>
<div class="pa-pct">${done
? "Unlocked!"
: `${m.points.toLocaleString()} / ${a.points.toLocaleString()} pts (${pct}%)`}
</div>
</div>
</div>`;
}).join("");
}
// Opens the add-points modal from the profile page.
// Button is only visible when isUnlocked = true.
function openAddPointsForProfile() {
if (!isUnlocked) return;
if (profilePageId === null) return;
openMemberModal(profilePageId);
}
// ββ Add-Points modal ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function openMemberModal(id) {
if (!isUnlocked) { openAdminModal(); return; }
selectedMemberId = id;
const m = members.find(x => x.id === id);
document.getElementById("modal-member-icon").innerHTML = renderMemberIcon(m.icon);
document.getElementById("modal-member-name").textContent = m.name;
document.getElementById("modal-member-pts").textContent = m.points.toLocaleString() + " pts";
document.getElementById("custom-input").value = "";
document.getElementById("modal").classList.add("open");
}
function closeModal(e) {
if (e.target.id === "modal") closeModalDirect();
}
function closeModalDirect() {
document.getElementById("modal").classList.remove("open");
selectedMemberId = null;
}
// ββ Add / deduct points β Firestore ββββββββββββββββββββββββββββββββββββββββββ
async function addPoints(amount) {
if (!isUnlocked) return;
if (selectedMemberId === null) return;
const m = members.find(x => x.id === selectedMemberId);
const newPts = Math.max(0, m.points + amount);
m.points = newPts;
document.getElementById("modal-member-pts").textContent = newPts.toLocaleString() + " pts";
flashPts();
try {
await db.collection("members").doc(String(selectedMemberId)).update({ points: newPts });
localStorage.setItem("somars_members_fallback", JSON.stringify(members));
} catch (err) {
console.error("Failed to write points:", err);
showConnectionStatus("error");
}
}
function addCustomPoints() {
if (!isUnlocked) return;
const val = parseInt(document.getElementById("custom-input").value, 10);
if (isNaN(val) || val === 0) return;
addPoints(val);
document.getElementById("custom-input").value = "";
}
// ββ Visual feedback βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function flashPts() {
const el = document.getElementById("modal-member-pts");
el.classList.add("flash");
setTimeout(() => el.classList.remove("flash"), 400);
}
// ββ Utility βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function escHtml(str) {
return String(str).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");
}
document.addEventListener("DOMContentLoaded", init);