-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1433 lines (1288 loc) · 64.5 KB
/
script.js
File metadata and controls
1433 lines (1288 loc) · 64.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
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
// ═══════════════════════════════════════════════════════════════════════════════
// COOKIE CLICKER v3.1 — Improved Global Bakery
// ═══════════════════════════════════════════════════════════════════════════════
// ─── FIREBASE CONFIG ──────────────────────────────────────────────────────────
// HOW TO SET UP (free, works on GitHub Pages, no server needed):
// 1. Go to https://console.firebase.google.com
// 2. Create a project → Build → Realtime Database → Create Database
// 3. Choose "Start in TEST mode" (allows public read/write — fine for a game)
// 4. Copy the URL shown (e.g. https://my-game-abc12-default-rtdb.firebaseio.com)
// 5. Paste it below replacing "YOUR-APP-default-rtdb"
const FIREBASE_URL = "https://cookie-clicker-7cc1f-default-rtdb.firebaseio.com/"; // ← REPLACE THIS
const LB_PATH = "/leaderboard";
// ─── DEV LOG ──────────────────────────────────────────────────────────────────
const DEV_LOG = {
show: true,
version: "v3.1",
text: `v3.1 — Quality & Leaderboard Overhaul\n\n• Real Firebase global leaderboard — works on GitHub Pages!\n• Fixed: Cookie Curse now requires actual clicks to break (was auto-breaking)\n• Fixed: curseClickCount resets properly between curses\n• Leaderboard: shows 🟢 online dot, last-seen time, CPS stat\n• Export/Import save game added to Settings\n• Notifications queue so they don't overlap\n• Prestige confirmation shows your new multiplier before you commit\n• Meteor click window extended (3–5s based on upgrades)\n• Mobile layout fixes\n• Sync on tab focus restored\n\nHappy baking! 🍪`
};
// ─── CONSTANTS ────────────────────────────────────────────────────────────────
const SAVE_KEY = "cc_v100";
// ─── FORMAT ──────────────────────────────────────────────────────────────────
function fmt(n) {
if (!isFinite(n) || n < 0) n = 0;
if (n >= 1e18) return (n / 1e18).toFixed(2) + " Qi";
if (n >= 1e15) return (n / 1e15).toFixed(2) + " Qa";
if (n >= 1e12) return (n / 1e12).toFixed(2) + " T";
if (n >= 1e9) return (n / 1e9).toFixed(2) + " B";
if (n >= 1e6) return (n / 1e6).toFixed(2) + " M";
if (n >= 1e3) return (n / 1e3).toFixed(1) + " K";
return Math.floor(n).toLocaleString();
}
function fmtTime(ms) {
const s = Math.floor(ms / 1000);
if (s < 60) return s + "s ago";
const m = Math.floor(s / 60);
if (m < 60) return m + "m ago";
const h = Math.floor(m / 60);
if (h < 24) return h + "h ago";
return Math.floor(h / 24) + "d ago";
}
function esc(s) {
return String(s)
.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")
.replace(/"/g,""").replace(/'/g,"'");
}
// ─── BUILDINGS ───────────────────────────────────────────────────────────────
const BUILDINGS = [
{ key:"cursor", icon:"👆", name:"Cursor", baseCost:15, cps:0.3, cpcMult:0.05, desc:"Auto-clicks the cookie for you." },
{ key:"grandma", icon:"👵", name:"Grandma", baseCost:100, cps:1.5, cpcMult:0.05, desc:"A gentle grandma baking cookies." },
{ key:"bakery", icon:"🧁", name:"Bakery", baseCost:500, cps:5, cpcMult:0.05, desc:"A small local bakery, smells amazing." },
{ key:"farm", icon:"🌾", name:"Cookie Farm", baseCost:1100, cps:8, cpcMult:0.04, desc:"Grows cookie plants from magic seeds." },
{ key:"mine", icon:"⛏️", name:"Chocolate Mine", baseCost:12000, cps:25, cpcMult:0.04, desc:"Mines raw chocolate chips underground." },
{ key:"factory", icon:"🏭", name:"Factory", baseCost:130000, cps:100, cpcMult:0.03, desc:"Mass-produces cookies around the clock." },
{ key:"bank", icon:"🏦", name:"Cookie Bank", baseCost:1400000, cps:400, cpcMult:0.03, desc:"Invests cookies to yield passive returns." },
{ key:"temple", icon:"🔮", name:"Cookie Temple", baseCost:20000000, cps:1500, cpcMult:0.02, desc:"Prays to the cookie gods for daily gifts." },
{ key:"wizard", icon:"🧙", name:"Wizard Tower", baseCost:330000000, cps:5000, cpcMult:0.02, desc:"Conjures cookies from thin air." },
{ key:"chocriver", icon:"🍫", name:"Chocolate River", baseCost:2000000000, cps:12000, cpcMult:0.02, desc:"A river of pure liquid chocolate." },
{ key:"shipment", icon:"🚀", name:"Shipment", baseCost:5100000000, cps:18000, cpcMult:0.02, desc:"Imports cookies from the Cookie Planet." },
{ key:"alchemy", icon:"⚗️", name:"Alchemy Lab", baseCost:75000000000, cps:55000, cpcMult:0.01, desc:"Transmutes gold into delicious cookies." },
{ key:"portal", icon:"🌀", name:"Portal", baseCost:1e12, cps:180000, cpcMult:0.01, desc:"Opens rifts to cookie-rich dimensions." },
{ key:"timemachine", icon:"⏱️", name:"Time Machine", baseCost:14e12, cps:550000, cpcMult:0.01, desc:"Brings cookies from the future." },
{ key:"antimatter", icon:"🌌", name:"Antimatter Condenser",baseCost:1.7e14, cps:1200000, cpcMult:0.01, desc:"Condenses matter into pure cookie energy." },
{ key:"prism", icon:"🌈", name:"Prism", baseCost:2.1e14, cps:1800000, cpcMult:0.01, desc:"Converts pure light into cookies." },
{ key:"chancemaker", icon:"🍀", name:"Chancemaker", baseCost:3.3e15, cps:6000000, cpcMult:0.01, desc:"Harnesses luck to generate cookies." },
{ key:"cookiedim", icon:"🪐", name:"Cookie Dimension", baseCost:5.1e16, cps:22000000, cpcMult:0.01, desc:"An entire dimension made of cookies." },
{ key:"idleverse", icon:"🌠", name:"Idleverse", baseCost:7.5e17, cps:80000000, cpcMult:0.01, desc:"A parallel universe dedicated to baking." },
];
// ─── UPGRADES ────────────────────────────────────────────────────────────────
const UPGRADES = [
{ id:1, icon:"⚡", name:"Nimble Fingers", bonus:5, cpsBonus:0.005, baseCost:50, costMult:2.0 },
{ id:2, icon:"🔥", name:"Hot Oven", bonus:30, cpsBonus:0.01, baseCost:500, costMult:2.2 },
{ id:3, icon:"✨", name:"Magic Dough", bonus:150, cpsBonus:0.02, baseCost:5000, costMult:2.5 },
{ id:4, icon:"🤖", name:"Robot Baker", bonus:800, cpsBonus:0.03, baseCost:50000, costMult:2.8 },
{ id:5, icon:"🌟", name:"Cookie God", bonus:5000, cpsBonus:0.05, baseCost:600000, costMult:3.0 },
{ id:6, icon:"🪄", name:"Reality Bender", bonus:35000, cpsBonus:0.08, baseCost:10000000, costMult:3.5 },
{ id:7, icon:"💎", name:"Diamond Oven", bonus:200000, cpsBonus:0.12, baseCost:500000000, costMult:4.0 },
{ id:8, icon:"🌀", name:"Quantum Mixer", bonus:1000000, cpsBonus:0.18, baseCost:25000000000, costMult:4.5 },
];
// ─── SPECIAL ITEMS ────────────────────────────────────────────────────────────
const SPECIALS = [
{
id:"golden_cursor", icon:"✨", name:"Golden Cursor", style:"special-gold", cost:5000,
desc:"+10% global CPC permanently.",
effect: s => { s.spec_cpc_bonus = (s.spec_cpc_bonus||0) + 0.10; }
},
{
id:"grandma_army", icon:"👵", name:"Grandma Army", style:"special-glow", cost:50000,
desc:"Each grandma produces +50% more.",
effect: s => { s.spec_grandma_mult = (s.spec_grandma_mult||1) * 1.5; }
},
{
id:"lucky_cookie", icon:"🍀", name:"Fortune Cookie", style:"special-green", cost:200000,
desc:"Golden cookies appear 2× as often and give bigger bonuses.",
effect: s => { s.spec_golden_freq=(s.spec_golden_freq||1)*0.5; s.spec_golden_bonus=(s.spec_golden_bonus||1)*2; }
},
{
id:"turbo_click", icon:"⚡", name:"Turbo Fingers", style:"special-gold", cost:1000000,
desc:"5% chance per click to trigger a mini-frenzy (3× for 5s).",
effect: s => { s.spec_turbo_click = true; }
},
{
id:"offline_boost", icon:"💤", name:"Lazy Baker", style:"special-glow", cost:5000000,
desc:"Earn 50% of your CPS while closed (up to 4h).",
effect: s => { s.spec_offline = true; }
},
{
id:"cookie_storm", icon:"🌪️", name:"Cookie Storm", style:"special-glow", cost:25000000,
desc:"Instantly gain 30 minutes of your CPS.",
repeatable: true,
effect: s => {
const bonus = calcCPS(s) * 1800;
s.score += bonus; s.life += bonus;
showNotif("🌪️ Cookie Storm! +" + fmt(bonus) + " cookies!");
}
},
{
id:"sugar_rush", icon:"🍬", name:"Sugar Rush", style:"special-gold", cost:100000000,
desc:"Permanently adds 25% of CPS to click power.",
effect: s => { s.spec_cps_to_cpc = (s.spec_cps_to_cpc||0) + 0.25; }
},
{
id:"oracle_cookie", icon:"🔮", name:"Oracle Cookie", style:"special-glow", cost:1000000000,
desc:"Prestige bonus multiplied by 1.5×.",
effect: s => { s.spec_prestige_mult=(s.spec_prestige_mult||1)*1.5; recalcPrestige(s); }
},
{
id:"quantum_bakery", icon:"🌌", name:"Quantum Bakery", style:"special-glow", cost:100000000000,
desc:"+100% global CPS across all timelines.",
effect: s => { s.spec_global_cps = (s.spec_global_cps||1) * 2; }
},
{
id:"infinity_chip", icon:"♾️", name:"Infinity Chip", style:"special-gold", cost:1e15,
desc:"Every 60s, gain a bonus equal to 120s of CPS.",
effect: s => { s.spec_infinity = true; startInfinityTimer(); }
},
{
id:"meteor_magnet", icon:"☄️", name:"Meteor Magnet", style:"special-gold", cost:3000000,
desc:"Meteors appear 2× as often, stay longer, and give double cookies.",
effect: s => { s.spec_meteor_freq=(s.spec_meteor_freq||1)*0.5; s.spec_meteor_bonus=(s.spec_meteor_bonus||1)*2; }
},
{
id:"curse_ward", icon:"🛡️", name:"Curse Ward", style:"special-green", cost:8000000,
desc:"Cookie Curse lasts 50% shorter.",
effect: s => { s.spec_curse_ward = true; }
},
{
id:"chain_master", icon:"🔗", name:"Chain Master", style:"special-gold", cost:500000,
desc:"Click combo decays 30% slower.",
effect: s => { s.spec_chain_slow = true; }
},
];
// ─── ACHIEVEMENTS ─────────────────────────────────────────────────────────────
const ACH = [
{ id:"b100", icon:"🍪", name:"First Batch", cond: s => s.life >= 100 },
{ id:"b1k", icon:"🍪", name:"Cookie Amateur", cond: s => s.life >= 1e3 },
{ id:"b10k", icon:"🍪", name:"Cookie Baker", cond: s => s.life >= 1e4 },
{ id:"b100k", icon:"🍪", name:"Cookie Chef", cond: s => s.life >= 1e5 },
{ id:"b1m", icon:"🍪", name:"Cookie Master", cond: s => s.life >= 1e6 },
{ id:"b100m", icon:"🍪", name:"Cookie Tycoon", cond: s => s.life >= 1e8 },
{ id:"b1b", icon:"🍪", name:"Cookie Legend", cond: s => s.life >= 1e9 },
{ id:"b1t", icon:"🍪", name:"Cookie Deity", cond: s => s.life >= 1e12 },
{ id:"b1qa", icon:"🍪", name:"Cookie Universe", cond: s => s.life >= 1e15 },
{ id:"bld1", icon:"🏗️", name:"First Building", cond: s => totalBld(s) >= 1 },
{ id:"bld10", icon:"🏗️", name:"Workforce", cond: s => totalBld(s) >= 10 },
{ id:"bld50", icon:"🏗️", name:"Empire", cond: s => totalBld(s) >= 50 },
{ id:"bld100", icon:"🏗️", name:"Megacorp", cond: s => totalBld(s) >= 100 },
{ id:"bld200", icon:"🏗️", name:"Industrial God", cond: s => totalBld(s) >= 200 },
{ id:"bld500", icon:"🏗️", name:"The Bakening", cond: s => totalBld(s) >= 500 },
{ id:"allbld", icon:"🌐", name:"Diversified", cond: s => BUILDINGS.every(b => (s.bld[b.key]||0) >= 1) },
{ id:"cps10", icon:"⏱️", name:"Passive Income", cond: s => calcCPS(s) >= 10 },
{ id:"cps100", icon:"⏱️", name:"Cookie Factory", cond: s => calcCPS(s) >= 100 },
{ id:"cps1k", icon:"⏱️", name:"Industrial Baker", cond: s => calcCPS(s) >= 1000 },
{ id:"cps10k", icon:"⏱️", name:"Cookie Titan", cond: s => calcCPS(s) >= 10000 },
{ id:"cps1m", icon:"⏱️", name:"Cosmic Baker", cond: s => calcCPS(s) >= 1000000 },
{ id:"upg1", icon:"⚡", name:"Upgraded!", cond: s => s.upgrades.some(v => v > 0) },
{ id:"upg_all", icon:"⚡", name:"Min-Maxer", cond: s => s.upgrades.every(v => v > 0) },
{ id:"upg10", icon:"⚡", name:"Power User", cond: s => s.upgrades.reduce((a,b)=>a+b,0) >= 10 },
{ id:"pres1", icon:"✨", name:"Born Again", cond: s => s.prestige >= 1 },
{ id:"pres5", icon:"✨", name:"Veteran Baker", cond: s => s.prestige >= 5 },
{ id:"pres10", icon:"✨", name:"Eternal Baker", cond: s => s.prestige >= 10 },
{ id:"cpc1k", icon:"👆", name:"Power Click", cond: () => calcCPC() >= 1000 },
{ id:"cpc1m", icon:"👆", name:"Mega Click", cond: () => calcCPC() >= 1e6 },
{ id:"cpc1b", icon:"👆", name:"Galaxy Click", cond: () => calcCPC() >= 1e9 },
{ id:"clk100", icon:"👆", name:"Dedicated", cond: s => s.totalClicks >= 100 },
{ id:"clk1k", icon:"👆", name:"Clicker", cond: s => s.totalClicks >= 1000 },
{ id:"clk10k", icon:"👆", name:"Click Addict", cond: s => s.totalClicks >= 10000 },
{ id:"clk100k", icon:"👆", name:"Carpal Tunnel", cond: s => s.totalClicks >= 100000},
{ id:"spec1", icon:"🎁", name:"Special Treatment", cond: s => Object.keys(s.specials||{}).length >= 1 },
{ id:"spec5", icon:"🎁", name:"Special Collector", cond: s => Object.keys(s.specials||{}).length >= 5 },
{ id:"golden1", icon:"🌟", name:"Lucky!", cond: s => (s.goldenCollected||0) >= 1 },
{ id:"golden10",icon:"🌟", name:"Fortune Favors You", cond: s => (s.goldenCollected||0) >= 10 },
{ id:"golden50",icon:"🌟", name:"Golden Touch", cond: s => (s.goldenCollected||0) >= 50 },
{ id:"frenzy1", icon:"🔥", name:"On Fire!", cond: s => (s.frenziesHad||0) >= 1 },
{ id:"frenzy5", icon:"🔥", name:"Frenzy Master", cond: s => (s.frenziesHad||0) >= 5 },
{ id:"meteor1", icon:"☄️", name:"Meteorite!", cond: s => (s.meteorsCollected||0) >= 1 },
{ id:"meteor10",icon:"☄️", name:"Meteor Shower", cond: s => (s.meteorsCollected||0) >= 10 },
{ id:"bliz1", icon:"❄️", name:"First Snowfall", cond: s => (s.blizzardsHad||0) >= 1 },
{ id:"combo5", icon:"🔗", name:"Combo Starter", cond: s => (s.maxCombo||0) >= 5 },
{ id:"combo20", icon:"🔗", name:"Combo Master", cond: s => (s.maxCombo||0) >= 20 },
{ id:"curse1", icon:"💀", name:"Cursed!", cond: s => (s.cursesHad||0) >= 1 },
{ id:"curse5", icon:"💀", name:"Curse Survivor", cond: s => (s.cursesHad||0) >= 5 },
{ id:"midnight",icon:"🌙", name:"Night Baker", cond: s => (s.midnightBonus||0) >= 1 },
];
// ─── PATCH NOTES ─────────────────────────────────────────────────────────────
const PATCH_NOTES = [
{
version:"v3.1", date:"Quality & Leaderboard Fix",
text:"Real Firebase global leaderboard (GitHub Pages compatible). Fixed curse-break requiring actual clicks. Export/Import save. Queued notifications. Better prestige confirm dialog."
},
{
version:"v3.0", date:"The Global Bakery Update",
text:"Global leaderboard. Bakery naming. Meteor Cookies, Cookie Blizzard, Cookie Curse, Midnight Bonus. Chain click combos. 8 new achievements."
},
{
version:"v2.0", date:"The Big Bakery Update",
text:"Golden Cookies, Frenzy, Special Items, 6 new buildings, 8 upgrades, mobile nav, offline earnings."
},
{
version:"v1.0", date:"Launch",
text:"Initial release! 14 buildings, 6 upgrades, 24 achievements, prestige system."
}
];
// ─── STATE ───────────────────────────────────────────────────────────────────
let S = {
score:0, life:0,
upgrades: new Array(UPGRADES.length).fill(0),
bld:{}, ach:{}, specials:{},
prestige:0, pmult:1,
totalClicks:0,
goldenCollected:0, meteorsCollected:0,
frenziesHad:0, blizzardsHad:0, cursesHad:0,
midnightBonus:0, maxCombo:0,
lastSave: Date.now(),
spec_cpc_bonus:0, spec_grandma_mult:1,
spec_golden_freq:1, spec_golden_bonus:1,
spec_global_cps:1, spec_cps_to_cpc:0,
spec_prestige_mult:1,
spec_turbo_click:false, spec_offline:false, spec_infinity:false,
spec_meteor_freq:1, spec_meteor_bonus:1,
spec_curse_ward:false, spec_chain_slow:false,
};
BUILDINGS.forEach(b => { S.bld[b.key] = 0; });
// ─── AUDIO ───────────────────────────────────────────────────────────────────
let AUDIO = { sfxOn:true, sfxVol:0.6 };
let audioCtx = null;
let audioUnlocked = false;
function initAudio() {
if (audioUnlocked) return;
audioUnlocked = true;
try { audioCtx = new (window.AudioContext || window.webkitAudioContext)(); } catch(e) {}
}
function getAudioCtx() {
if (!audioCtx) return null;
if (audioCtx.state === "suspended") audioCtx.resume().catch(()=>{});
return audioCtx;
}
function toggleSFX() {
AUDIO.sfxOn = !AUDIO.sfxOn;
const btn = document.getElementById("sfx-toggle");
if (btn) { btn.textContent = AUDIO.sfxOn ? "ON" : "OFF"; btn.classList.toggle("off", !AUDIO.sfxOn); }
saveAudioPrefs();
}
function setSFXVol(v) {
AUDIO.sfxVol = v / 100;
const valEl = document.getElementById("sfx-vol-val");
if (valEl) valEl.textContent = v + "%";
saveAudioPrefs();
}
function saveAudioPrefs() {
try { localStorage.setItem("cc_audio", JSON.stringify(AUDIO)); } catch(e) {}
}
function loadAudioPrefs() {
try {
const raw = localStorage.getItem("cc_audio");
if (raw) {
const p = JSON.parse(raw);
if (typeof p.sfxOn === "boolean") AUDIO.sfxOn = p.sfxOn;
if (typeof p.sfxVol === "number") AUDIO.sfxVol = p.sfxVol;
}
} catch(e) {}
const sToggle = document.getElementById("sfx-toggle");
const sVol = document.getElementById("sfx-vol");
const sVal = document.getElementById("sfx-vol-val");
if (sToggle) { sToggle.textContent = AUDIO.sfxOn?"ON":"OFF"; sToggle.classList.toggle("off",!AUDIO.sfxOn); }
const svPct = Math.round(AUDIO.sfxVol * 100);
if (sVol) sVol.value = svPct;
if (sVal) sVal.textContent = svPct + "%";
}
function playSFX(type) {
if (!AUDIO.sfxOn) return;
const ctx = getAudioCtx();
if (!ctx) return;
const vol = AUDIO.sfxVol;
try {
const gain = ctx.createGain();
gain.connect(ctx.destination);
const now = ctx.currentTime;
if (type === "click") {
const osc = ctx.createOscillator(); osc.type = "sine";
osc.frequency.setValueAtTime(600, now);
osc.frequency.linearRampToValueAtTime(900, now+0.04);
gain.gain.setValueAtTime(vol*0.15, now); gain.gain.linearRampToValueAtTime(0, now+0.08);
osc.connect(gain); osc.start(now); osc.stop(now+0.1);
} else if (type === "buy") {
[0,0.07].forEach((t,i) => {
const o = ctx.createOscillator(); o.type="sine"; o.frequency.value=440+i*220;
gain.gain.setValueAtTime(vol*0.12,now+t); gain.gain.linearRampToValueAtTime(0,now+t+0.12);
o.connect(gain); o.start(now+t); o.stop(now+t+0.15);
});
} else if (type === "upgrade") {
[0,0.1,0.2].forEach((t,i) => {
const o = ctx.createOscillator(); o.type="triangle"; o.frequency.value=523+i*262;
gain.gain.setValueAtTime(vol*0.13,now+t); gain.gain.linearRampToValueAtTime(0,now+t+0.15);
o.connect(gain); o.start(now+t); o.stop(now+t+0.2);
});
} else if (type === "golden") {
const osc = ctx.createOscillator(); osc.type="sine";
osc.frequency.setValueAtTime(880,now); osc.frequency.linearRampToValueAtTime(1760,now+0.3);
gain.gain.setValueAtTime(vol*0.2,now); gain.gain.linearRampToValueAtTime(0,now+0.4);
osc.connect(gain); osc.start(now); osc.stop(now+0.45);
} else if (type === "meteor") {
[0,0.1,0.2].forEach((t,i) => {
const o = ctx.createOscillator(); o.type="sawtooth"; o.frequency.value=300-i*80;
gain.gain.setValueAtTime(vol*0.18,now+t); gain.gain.linearRampToValueAtTime(0,now+t+0.2);
o.connect(gain); o.start(now+t); o.stop(now+t+0.25);
});
} else if (type === "curse") {
const osc = ctx.createOscillator(); osc.type="sawtooth";
osc.frequency.setValueAtTime(200,now); osc.frequency.linearRampToValueAtTime(80,now+0.5);
gain.gain.setValueAtTime(vol*0.15,now); gain.gain.linearRampToValueAtTime(0,now+0.55);
osc.connect(gain); osc.start(now); osc.stop(now+0.6);
} else if (type === "combo") {
const osc = ctx.createOscillator(); osc.type="sine";
const pitch = Math.min(440 + comboCount * 25, 1200);
osc.frequency.setValueAtTime(pitch,now); osc.frequency.linearRampToValueAtTime(pitch*2,now+0.06);
gain.gain.setValueAtTime(vol*0.1,now); gain.gain.linearRampToValueAtTime(0,now+0.1);
osc.connect(gain); osc.start(now); osc.stop(now+0.12);
}
} catch(e) {}
}
// ─── PLAYER IDENTITY ─────────────────────────────────────────────────────────
let bakeryName = "";
let playerId = "";
function generateId() {
return Math.random().toString(36).substr(2,10) + Date.now().toString(36);
}
function initPlayerIdentity() {
bakeryName = localStorage.getItem("cc_bakery_name") || "";
playerId = localStorage.getItem("cc_player_id") || generateId();
localStorage.setItem("cc_player_id", playerId);
}
// ─── BAKERY NAME MODAL ────────────────────────────────────────────────────────
function showBakeryNameModal() {
const modal = document.getElementById("bakery-name-modal");
if (modal) modal.classList.remove("hidden");
const input = document.getElementById("bakery-name-input");
if (!input) return;
input.focus();
const charEl = document.getElementById("bakery-name-char");
input.oninput = () => { if (charEl) charEl.textContent = 28 - input.value.length; };
input.onkeydown = e => { if (e.key === "Enter") confirmBakeryName(); };
}
function confirmBakeryName() {
const input = document.getElementById("bakery-name-input");
const raw = input ? input.value.trim() : "";
bakeryName = sanitizeName(raw) || "Anonymous Bakery";
localStorage.setItem("cc_bakery_name", bakeryName);
document.getElementById("bakery-name-modal")?.classList.add("hidden");
updateBakeryLogoName();
initAudio();
showDevlog();
setTimeout(() => syncLeaderboard(), 1500);
}
function sanitizeName(str) {
return str.replace(/[<>"'&]/g,"").substring(0,28).trim();
}
function updateBakeryLogoName() {
const el = document.getElementById("bakery-logo-name");
if (el) el.textContent = bakeryName || "My Bakery";
const ni = document.getElementById("lb-name-input");
if (ni) ni.value = bakeryName;
}
// ─── EVENT STATE ──────────────────────────────────────────────────────────────
let buyQty = 1;
let frenzyActive = false;
let frenzyMult = 1;
let frenzyTimeoutId = null;
let goldenVisible = false;
let goldenTimeoutId = null;
let meteorVisible = false;
let meteorTimeoutId = null;
let blizzardActive = false;
let blizzardTimeoutId = null;
let blizzardIntervalId= null;
let curseActive = false;
let curseTimeoutId = null;
let curseClickCount = 0; // ← Only incremented by real player clicks (bug fixed)
let infinityIntervalId= null;
// ─── CHAIN COMBO ─────────────────────────────────────────────────────────────
let comboCount = 0;
let comboTimer = null;
function getComboMult() {
if (comboCount <= 1) return 1;
return Math.min(1 + (comboCount - 1) * 0.1, 5);
}
function updateCombo() {
comboCount++;
if (comboCount > (S.maxCombo||0)) S.maxCombo = comboCount;
clearTimeout(comboTimer);
comboTimer = setTimeout(() => { comboCount = 0; }, S.spec_chain_slow ? 1300 : 1000);
if (comboCount >= 5) playSFX("combo");
}
// ─── CORE CALCULATIONS ───────────────────────────────────────────────────────
function totalBld(s) {
return BUILDINGS.reduce((t,b) => t + (s.bld[b.key]||0), 0);
}
function calcCPS(s) {
let raw = BUILDINGS.reduce((t,b) => {
let cps = b.cps;
if (b.key === "grandma") cps *= (s.spec_grandma_mult||1);
return t + (s.bld[b.key]||0) * cps;
}, 0);
let result = raw * s.pmult * (s.spec_global_cps||1);
if (curseActive) result *= 0.5;
return result;
}
function calcCPC() {
const cps = calcCPS(S);
let flatBonus=0, cpsScale=0, bldBoost=0;
UPGRADES.forEach((u,i) => {
flatBonus += (S.upgrades[i]||0) * u.bonus;
cpsScale += (S.upgrades[i]||0) * u.cpsBonus;
});
BUILDINGS.forEach(b => { bldBoost += (S.bld[b.key]||0) * b.cps * b.cpcMult; });
const cpsToCpc = cps * (S.spec_cps_to_cpc||0);
const base = (1 + flatBonus + cps * cpsScale + bldBoost + cpsToCpc) * S.pmult;
return base * (1 + (S.spec_cpc_bonus||0));
}
function getUpgCost(i) {
return Math.floor(UPGRADES[i].baseCost * Math.pow(UPGRADES[i].costMult, S.upgrades[i]||0));
}
function getBldCostN(key, n) {
const b = BUILDINGS.find(x => x.key === key);
return Math.floor(b.baseCost * Math.pow(1.15, (S.bld[key]||0) + n));
}
function getBldCostBulk(key, qty) {
let total = 0;
for (let i=0; i<qty; i++) total += getBldCostN(key, i);
return total;
}
function maxAffordable(key) {
let count=0, total=0;
while (count < 2000) {
const next = getBldCostN(key, count);
if (total + next > S.score) break;
total += next; count++;
}
return count;
}
function recalcPrestige(s) {
s.pmult = Math.pow(1.05, s.prestige) * (s.spec_prestige_mult||1);
}
// ─── PURCHASING ───────────────────────────────────────────────────────────────
function setBuyQty(qty, btn) {
buyQty = qty;
document.querySelectorAll(".qty-btn").forEach(b => b.classList.remove("active"));
if (btn) btn.classList.add("active");
renderBuildings();
}
function buyBld(key) {
let purchased = 0;
if (buyQty === "max") {
while (true) {
const cost = getBldCostN(key, 0);
if (S.score < cost) break;
S.score -= cost; S.bld[key]=(S.bld[key]||0)+1; purchased++;
}
} else {
const qty = parseInt(buyQty);
const cost = getBldCostBulk(key, qty);
if (S.score < cost) return;
S.score -= cost; S.bld[key]=(S.bld[key]||0)+qty; purchased=qty;
}
if (purchased > 0) { playSFX("buy"); save(); scheduleDisplay(); }
}
function addUpgrade(idx) {
const i = idx - 1;
const cost = getUpgCost(i);
if (S.score < cost) return;
S.score -= cost; S.upgrades[i]=(S.upgrades[i]||0)+1;
playSFX("upgrade"); save(); scheduleDisplay();
}
function buySpecial(id) {
const sp = SPECIALS.find(s => s.id === id);
if (!sp || (!sp.repeatable && S.specials[id]) || S.score < sp.cost) return;
S.score -= sp.cost;
if (!sp.repeatable) S.specials[id] = true;
sp.effect(S);
playSFX("upgrade"); save(); scheduleDisplay(); renderSpecials();
}
// ─── CLICK HANDLING ──────────────────────────────────────────────────────────
let lastTouchTime = 0;
function handleClickTouch(e) {
e.preventDefault();
const now = Date.now();
if (now - lastTouchTime < 40) return;
lastTouchTime = now;
doClick(e.touches ? e.touches[0] : e);
initAudio();
}
function handleClick(e) {
if (Date.now() - lastTouchTime < 100) return;
doClick(e);
initAudio();
}
function doClick(e) {
updateCombo();
const combo = getComboMult();
const frenzyM = frenzyActive ? frenzyMult : 1;
let cpc = calcCPC() * frenzyM * combo;
if (S._clickStormBonus && S._clickStormEnd > Date.now()) cpc += S._clickStormBonus;
S.score += cpc; S.life += cpc;
S.totalClicks = (S.totalClicks||0) + 1;
const cls = frenzyActive ? "frenzy-float" : (combo >= 3 ? "golden-float" : "");
if (e) spawnFloat(e.clientX, e.clientY, cpc, cls);
if (comboCount >= 3) spawnComboLabel(e ? e.clientX : window.innerWidth/2, e ? e.clientY : window.innerHeight/2);
const btn = document.getElementById("cookie-btn");
if (btn) { btn.classList.remove("clicked"); void btn.offsetWidth; btn.classList.add("clicked"); }
// ✅ FIXED: Curse break only by actual clicks (removed from passive tick)
if (curseActive) {
curseClickCount++;
const needed = 20;
// Show progress hint every 5 clicks
if (curseClickCount % 5 === 0 && curseClickCount < needed) {
showNotif(`💀 Curse: ${curseClickCount}/${needed} clicks to break!`);
}
if (curseClickCount >= needed) {
curseClickCount = 0;
clearTimeout(curseTimeoutId);
curseActive = false;
playSFX("upgrade");
showNotif("💪 Curse broken! CPS restored!");
scheduleCurse();
}
}
if (S.spec_turbo_click && Math.random() < 0.05) {
startFrenzy(3, 5000, "⚡ Turbo Click! 3× for 5s!");
}
playSFX("click");
scheduleDisplay();
}
function spawnFloat(x, y, n, cls) {
const el = document.createElement("div");
el.className = "float-txt" + (cls ? " " + cls : "");
el.textContent = "+" + fmt(n);
el.style.left = (x - 20 + (Math.random()*30-15)) + "px";
el.style.top = (y - 12) + "px";
document.body.appendChild(el);
el.addEventListener("animationend", () => el.remove(), { once:true });
}
function spawnComboLabel(x, y) {
const el = document.createElement("div");
el.className = "float-txt golden-float combo-label";
el.textContent = `${comboCount}× COMBO`;
el.style.left = (x - 30 + Math.random()*20) + "px";
el.style.top = (y - 30) + "px";
document.body.appendChild(el);
el.addEventListener("animationend", () => el.remove(), { once:true });
}
// ─── FRENZY ──────────────────────────────────────────────────────────────────
function startFrenzy(mult, duration, label) {
frenzyActive = true;
frenzyMult = mult;
S.frenziesHad = (S.frenziesHad||0) + 1;
const banner = document.getElementById("frenzy-banner");
const text = document.getElementById("frenzy-text");
const prog = document.getElementById("frenzy-progress");
if (banner) banner.classList.remove("hidden");
if (text) text.textContent = label || ("🔥 Frenzy! " + mult + "× cookies!");
if (prog) {
prog.style.transition = "none";
prog.style.width = "100%";
requestAnimationFrame(() => {
prog.style.transition = "width " + (duration/1000) + "s linear";
prog.style.width = "0%";
});
}
clearTimeout(frenzyTimeoutId);
frenzyTimeoutId = setTimeout(() => {
frenzyActive = false; frenzyMult = 1;
if (banner) banner.classList.add("hidden");
}, duration);
}
// ─── GOLDEN COOKIE ────────────────────────────────────────────────────────────
function scheduleGolden() {
if (goldenVisible) return;
const delay = 30000 * (S.spec_golden_freq||1) * (0.5 + Math.random());
setTimeout(spawnGolden, delay);
}
function spawnGolden() {
if (goldenVisible) return;
goldenVisible = true;
const el = document.getElementById("golden-cookie");
if (!el) { goldenVisible=false; scheduleGolden(); return; }
const pad = 80;
el.style.left = (pad + Math.random()*(window.innerWidth -pad*2-70)) + "px";
el.style.top = (pad + Math.random()*(window.innerHeight-pad*2-100)) + "px";
el.classList.remove("hidden");
clearTimeout(goldenTimeoutId);
goldenTimeoutId = setTimeout(() => {
goldenVisible = false; el.classList.add("hidden"); scheduleGolden();
}, 15000);
}
function collectGolden() {
if (!goldenVisible) return;
goldenVisible = false;
clearTimeout(goldenTimeoutId);
document.getElementById("golden-cookie")?.classList.add("hidden");
S.goldenCollected = (S.goldenCollected||0) + 1;
const bonus = S.spec_golden_bonus || 1;
const roll = Math.random();
if (roll < 0.4) {
const gained = calcCPS(S) * 900 * bonus;
S.score += gained; S.life += gained;
showNotif("🌟 Lucky! +" + fmt(gained) + " cookies!");
spawnFloat(window.innerWidth/2, window.innerHeight/2, gained, "golden-float");
} else if (roll < 0.7) {
const mult = Math.max(7, Math.floor(7 * bonus));
startFrenzy(mult, 30000, "🌟 Golden Frenzy! " + mult + "× for 30s!");
showNotif("🌟 Golden Frenzy! " + mult + "×!");
} else {
const clickBonus = Math.floor(777 * bonus);
S._clickStormBonus = clickBonus;
S._clickStormEnd = Date.now() + 13000;
showNotif("🌟 Click Storm! +" + fmt(clickBonus) + " per click for 13s!");
setTimeout(() => { S._clickStormBonus=0; S._clickStormEnd=0; }, 13000);
}
playSFX("golden");
scheduleGolden();
}
// ─── METEOR ──────────────────────────────────────────────────────────────────
function scheduleMeteor() {
if (meteorVisible) return;
const delay = 120000 * (S.spec_meteor_freq||1) * (0.5 + Math.random());
setTimeout(spawnMeteor, delay);
}
function spawnMeteor() {
if (meteorVisible) return;
meteorVisible = true;
const el = document.getElementById("meteor-cookie");
if (!el) { meteorVisible=false; scheduleMeteor(); return; }
el.style.left = (Math.random() * window.innerWidth * 0.5) + "px";
el.style.top = (-60 + Math.random()*40) + "px";
el.classList.remove("hidden");
// Extended click window with meteor magnet
const windowMs = (S.spec_meteor_bonus||1) > 1 ? 5000 : 3000;
clearTimeout(meteorTimeoutId);
meteorTimeoutId = setTimeout(() => {
meteorVisible=false; el.classList.add("hidden"); scheduleMeteor();
}, windowMs);
}
function collectMeteor() {
if (!meteorVisible) return;
meteorVisible = false;
clearTimeout(meteorTimeoutId);
document.getElementById("meteor-cookie")?.classList.add("hidden");
S.meteorsCollected = (S.meteorsCollected||0) + 1;
const gained = calcCPS(S) * 3600 * (S.spec_meteor_bonus||1);
S.score += gained; S.life += gained;
playSFX("meteor");
showNotif("☄️ Meteor! +" + fmt(gained) + " cookies (1hr CPS)!");
spawnFloat(window.innerWidth*0.3, window.innerHeight*0.3, gained, "meteor-float");
scheduleMeteor();
}
// ─── BLIZZARD ─────────────────────────────────────────────────────────────────
function scheduleBlizzard() {
setTimeout(startBlizzard, 180000 + Math.random()*180000);
}
function startBlizzard() {
if (blizzardActive || frenzyActive) { scheduleBlizzard(); return; }
blizzardActive = true;
S.blizzardsHad = (S.blizzardsHad||0) + 1;
const dur = 20000;
const banner = document.getElementById("blizzard-banner");
const text = document.getElementById("blizzard-text");
const prog = document.getElementById("blizzard-progress");
if (banner) banner.classList.remove("hidden");
if (text) text.textContent = "❄️ Cookie Blizzard! Cookies are raining!";
if (prog) {
prog.style.transition = "none"; prog.style.width = "100%";
requestAnimationFrame(() => { prog.style.transition="width "+(dur/1000)+"s linear"; prog.style.width="0%"; });
}
showNotif("❄️ Cookie Blizzard started!");
blizzardIntervalId = setInterval(() => {
const rain = calcCPS(S) * (2 + Math.random()*3);
S.score += rain; S.life += rain;
spawnFloat(80+Math.random()*(window.innerWidth-160), 80+Math.random()*(window.innerHeight*0.5), rain, "blizzard-float");
}, 1000);
clearTimeout(blizzardTimeoutId);
blizzardTimeoutId = setTimeout(() => {
blizzardActive = false;
clearInterval(blizzardIntervalId);
document.getElementById("blizzard-banner")?.classList.add("hidden");
scheduleBlizzard();
}, dur);
}
// ─── CURSE ───────────────────────────────────────────────────────────────────
function scheduleCurse() {
if (calcCPS(S) < 10) { setTimeout(scheduleCurse, 60000); return; }
setTimeout(startCurse, 240000 + Math.random()*240000);
}
function startCurse() {
if (curseActive) { scheduleCurse(); return; }
curseActive = true;
curseClickCount = 0; // ← reset on each new curse
S.cursesHad = (S.cursesHad||0) + 1;
const dur = S.spec_curse_ward ? 15000 : 30000;
playSFX("curse");
showNotif(`💀 Cookie Curse! CPS halved for ${dur/1000}s — click 20× to break it!`);
if (S.spec_curse_ward) showNotif("🛡️ Curse Ward: duration halved!");
clearTimeout(curseTimeoutId);
curseTimeoutId = setTimeout(() => {
curseActive = false;
curseClickCount = 0;
showNotif("✨ Curse lifted! CPS restored.");
scheduleCurse();
}, dur);
}
// ─── MIDNIGHT BONUS ──────────────────────────────────────────────────────────
function checkMidnightBonus() {
const hour = new Date().getHours();
if (hour < 0 || hour >= 2) return;
const key = "cc_midnight_" + new Date().toDateString();
if (localStorage.getItem(key)) return;
localStorage.setItem(key, "1");
S.midnightBonus = (S.midnightBonus||0) + 1;
const bonus = calcCPS(S) * 7200;
S.score += bonus; S.life += bonus;
playSFX("golden");
showNotif("🌙 Midnight Bonus! +" + fmt(bonus) + " cookies!");
}
// ─── INFINITY CHIP ────────────────────────────────────────────────────────────
function startInfinityTimer() {
if (infinityIntervalId) return;
infinityIntervalId = setInterval(() => {
if (!S.spec_infinity) { clearInterval(infinityIntervalId); infinityIntervalId=null; return; }
const bonus = calcCPS(S) * 120;
S.score += bonus; S.life += bonus;
showNotif("♾️ Infinity Chip: +" + fmt(bonus) + " cookies!");
}, 60000);
}
// ─── PRESTIGE ─────────────────────────────────────────────────────────────────
function doPrestige() {
if (S.life < 1e9) return;
const nextMult = (Math.pow(1.05, S.prestige+1) * (S.spec_prestige_mult||1)).toFixed(3);
if (!confirm(`Prestige #${S.prestige+1}?\n\nNew global multiplier: ${nextMult}×\n\nCookies, buildings & upgrades reset.\nSpecial items & achievements kept.`)) return;
S.prestige++;
recalcPrestige(S);
S.score = 0; S.life = 0;
S.upgrades = new Array(UPGRADES.length).fill(0);
BUILDINGS.forEach(b => { S.bld[b.key] = 0; });
playSFX("upgrade");
showNotif(`✨ Prestige! ${S.pmult.toFixed(3)}× global bonus active!`);
save(); scheduleDisplay();
}
// ─── TABS ─────────────────────────────────────────────────────────────────────
function showTab(name, el) {
document.querySelectorAll(".pane").forEach(p => p.classList.remove("active"));
document.querySelectorAll(".tab").forEach(t => t.classList.remove("active"));
document.getElementById("tab-" + name)?.classList.add("active");
el.classList.add("active");
if (name === "leaderboard") loadLeaderboard();
}
// ─── MOBILE NAV ───────────────────────────────────────────────────────────────
function mobileShowPanel(panel, btn) {
document.querySelectorAll(".mnav-btn").forEach(b => b.classList.remove("active"));
if (btn) btn.classList.add("active");
const left = document.getElementById("left-panel");
const right = document.getElementById("right-panel");
if (panel === "bake") {
if (left) { left.style.display="flex"; left.classList.add("mobile-active"); }
if (right) { right.style.display="none"; right.classList.remove("mobile-active"); }
} else {
if (left) { left.style.display="none"; left.classList.remove("mobile-active"); }
if (right) { right.style.display="flex"; right.classList.add("mobile-active"); }
const tabMap = { more:"achievements", buildings:"buildings", upgrades:"upgrades", leaderboard:"leaderboard" };
const tabBtn = document.getElementById("tab-btn-" + (tabMap[panel]||panel));
if (tabBtn) showTab(tabMap[panel]||panel, tabBtn);
}
}
// ─── SETTINGS ─────────────────────────────────────────────────────────────────
function openSettings() {
document.getElementById("settings-modal")?.classList.remove("hidden");
const ni = document.getElementById("lb-name-input");
if (ni) ni.value = bakeryName;
}
function closeSettings() {
document.getElementById("settings-modal")?.classList.add("hidden");
}
function saveLeaderboardName() {
const input = document.getElementById("lb-name-input");
if (!input) return;
const val = sanitizeName(input.value.trim()) || "Anonymous Bakery";
bakeryName = val;
localStorage.setItem("cc_bakery_name", val);
updateBakeryLogoName();
showNotif("✅ Bakery name saved: " + val);
setTimeout(() => syncLeaderboard(), 500);
}
// ─── EXPORT / IMPORT ──────────────────────────────────────────────────────────
function exportSave() {
try {
const data = localStorage.getItem(SAVE_KEY) || "{}";
const encoded = btoa(unescape(encodeURIComponent(data)));
const el = document.getElementById("save-export-box");
if (el) { el.value = encoded; el.select(); document.execCommand("copy"); }
showNotif("📋 Save exported & copied to clipboard!");
} catch(e) { showNotif("❌ Export failed."); }
}
function importSave() {
const el = document.getElementById("save-export-box");
if (!el || !el.value.trim()) { showNotif("⚠️ Paste your save code first."); return; }
try {
const decoded = decodeURIComponent(escape(atob(el.value.trim())));
JSON.parse(decoded); // validate — throws if invalid
localStorage.setItem(SAVE_KEY, decoded);
showNotif("✅ Save imported! Reloading...");
setTimeout(() => location.reload(), 1200);
} catch(e) { showNotif("❌ Invalid save code. Check your paste."); }
}
// ─── GLOBAL LEADERBOARD (Firebase Realtime Database REST API) ─────────────────
// No SDK needed — works on any static host including GitHub Pages!
// Free tier: 1GB storage, 10GB/month transfer (plenty for a game leaderboard)
let lbCache = null;
let lbLastFetch = 0;
let lbSyncing = false;
function isFirebaseReady() {
return FIREBASE_URL && !FIREBASE_URL.includes("YOUR-APP");
}
async function syncLeaderboard() {
if (lbSyncing) return;
if (!isFirebaseReady()) { syncLocalLeaderboard(); return; }
lbSyncing = true;
try {
// Write our entry using PUT (idempotent, keyed by playerId)
const entry = {
id: playerId,
name: bakeryName || "Anonymous Bakery",
score: Math.floor(S.life),
prestige: S.prestige || 0,
buildings: totalBld(S),
cps: Math.floor(calcCPS(S)),
ts: Date.now(),
};
await fetch(`${FIREBASE_URL}${LB_PATH}/${playerId}.json`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(entry),
});
// Read top 100 sorted by score descending
const res = await fetch(`${FIREBASE_URL}${LB_PATH}.json?orderBy=%22score%22&limitToLast=100`);
const data = res.ok ? await res.json() : null;
lbCache = data
? Object.values(data).filter(e=>e&&isFinite(e.score)).sort((a,b)=>b.score-a.score).slice(0,50)
: [];
lbLastFetch = Date.now();
if (document.getElementById("tab-leaderboard")?.classList.contains("active")) {
renderLeaderboard(lbCache);
}
} catch(e) {
console.warn("[LB] Firebase error:", e.message);
syncLocalLeaderboard();
} finally {
lbSyncing = false;
}
}
function syncLocalLeaderboard() {
try {
let scores = JSON.parse(localStorage.getItem("cc_leaderboard") || "[]");
const entry = { id:playerId, name:bakeryName||"Anonymous Bakery", score:Math.floor(S.life), prestige:S.prestige||0, buildings:totalBld(S), cps:Math.floor(calcCPS(S)), ts:Date.now() };
const idx = scores.findIndex(s=>s.id===playerId);
if (idx>=0) scores[idx]=entry; else scores.push(entry);
scores.sort((a,b)=>b.score-a.score);
if (scores.length>100) scores=scores.slice(0,100);
localStorage.setItem("cc_leaderboard", JSON.stringify(scores));
lbCache=scores; lbLastFetch=Date.now();
} catch(e) {}
}
async function loadLeaderboard() {
const listEl = document.getElementById("lb-list");
const loadEl = document.getElementById("lb-loading");
if (!listEl) return;
if (lbCache && Date.now()-lbLastFetch < 15000) { renderLeaderboard(lbCache); return; }
if (loadEl) loadEl.classList.remove("hidden");
listEl.innerHTML = "";
if (!isFirebaseReady()) {
if (loadEl) loadEl.classList.add("hidden");
listEl.innerHTML = `
<div class="lb-setup-msg">
<div style="font-size:2rem">🔥</div>
<div style="font-weight:700;margin:6px 0">Set up Firebase for Global Scores</div>
<div style="font-size:0.78rem;opacity:0.7;line-height:1.6">
1. Visit <strong>console.firebase.google.com</strong><br>