@@ -307,6 +307,16 @@ const shop = document.getElementById("shop");
307307const lifeDisplay = document . getElementById ( "lifePoints" ) ;
308308const skillCountDisplay = document . getElementById ( "skillCount" ) ;
309309const loginBtn = document . getElementById ( "loginBtn" ) ;
310+ const subscriptionBtn = document . getElementById ( "subscriptionBtn" ) ;
311+ const popup = document . getElementById ( "popupWindow" ) ;
312+ const closeBtn = document . getElementById ( "closePopupBtn" ) ;
313+ const runFuncBtn = document . getElementById ( "runFuncBtn" ) ;
314+ const leftAd = document . getElementById ( "leftAd" ) ;
315+ const rightAd = document . getElementById ( "rightAd" ) ;
316+
317+ const continueGameBtn = document . getElementById ( "continueGameBtn" ) ;
318+
319+ const saved = localStorage . getItem ( "savedGame" ) ;
310320// ---------- Show question ----------
311321function showQuestion ( ) {
312322 const q = questions [ currentIndex ] ;
@@ -329,12 +339,14 @@ function checkAnswer(selected, correct) {
329339 skillCount += skillMultiplier ;
330340 message . textContent = "Correct! +1 skill!" ;
331341 saveStats ( ) ;
342+ saveGameToLocal ( ) ;
332343
333344 } else {
334345 wrongCount ++ ;
335346 lifePoints -- ;
336347 message . textContent = `Wrong! -1 Life Point!` ;
337348 saveStats ( ) ;
349+ saveGameToLocal ( ) ;
338350 }
339351
340352 correctDisplay . textContent = correctCount ;
@@ -455,29 +467,92 @@ continueBtn.onclick = () => {
455467}
456468
457469mainMenuBtn . onclick = ( ) => {
458- showWelcomeMessage ( ) ;
459470 panel . style . display = "none" ;
460- startBtn . style . display = "inline-block" ;
461471 shop . style . display = "none" ;
462472
463- currentIndex = 0 ;
464- correctCount = 0 ;
465- wrongCount = 0 ;
466- skillCount = 0 ;
467- lifePoints = 5 ;
468- skillMultiplier = 1 ;
469-
470- correctDisplay . textContent = correctCount ;
471- wrongDisplay . textContent = wrongCount ;
472- skillDisplay . textContent = skillCount ;
473- lifeDisplay . textContent = lifePoints ;
473+ document . getElementById ( "menuContainer" ) . style . display = "block" ;
474+ startBtn . style . display = "none" ;
475+ continueGameBtn . style . display = "inline-block" ;
474476} ;
475477
476478loginBtn . onclick = ( ) => {
477479 window . location . href = "LoginPage.html" ;
478480
479481}
480482
483+ subscriptionBtn . onclick = ( ) => {
484+ popup . classList . remove ( "hidden" ) ;
485+ }
486+
487+ closeBtn . onclick = ( ) => {
488+ popup . classList . add ( "hidden" ) ;
489+ } ;
490+
491+ runFuncBtn . onclick = ( ) => {
492+ adBanner . classList . add ( "hidden" ) ;
493+ leftAd . classList . add ( "hidden" ) ;
494+ rightAd . classList . add ( "hidden" ) ;
495+ subscriptionBtn . disabled = true ;
496+ subscriptionBtn . textContent = "(Subscribed)" ;
497+ subscriptionBtn . style . opacity = "0.5" ;
498+ popup . classList . add ( "hidden" ) ;
499+ } ;
500+
501+ continueGameBtn . onclick = ( ) => {
502+ try {
503+ const raw = localStorage . getItem ( 'savedGame' ) ;
504+ console . log ( 'Continue clicked. raw savedGame:' , raw ) ;
505+
506+ if ( ! raw ) {
507+ console . warn ( 'No saved game found in localStorage.' ) ;
508+ return ;
509+ }
510+
511+ const saved = JSON . parse ( raw ) ;
512+
513+ // Validate required keys
514+ if ( typeof saved . questionIndex !== 'number' ||
515+ typeof saved . correct !== 'number' ||
516+ typeof saved . wrong !== 'number' ||
517+ typeof saved . skill !== 'number' ||
518+ typeof saved . life !== 'number' ) {
519+ console . error ( 'Saved game missing required numeric fields:' , saved ) ;
520+ return ;
521+ }
522+
523+ // Restore stats safely
524+ currentIndex = saved . questionIndex ;
525+ correctCount = saved . correct ;
526+ wrongCount = saved . wrong ;
527+ skillCount = saved . skill ;
528+ lifePoints = saved . life ;
529+
530+ // Update UI numbers
531+ correctDisplay . textContent = correctCount ;
532+ wrongDisplay . textContent = wrongCount ;
533+ skillDisplay . textContent = skillCount ;
534+ lifeDisplay . textContent = lifePoints ;
535+
536+ // Make sure index is within bounds
537+ if ( currentIndex < 0 ) currentIndex = 0 ;
538+ if ( currentIndex >= questions . length ) {
539+ console . warn ( 'Saved questionIndex out of range, resetting to last question.' ) ;
540+ currentIndex = Math . max ( 0 , questions . length - 1 ) ;
541+ }
542+
543+ // showQuestion uses currentIndex global — call without args
544+ showQuestion ( ) ;
545+
546+ // Hide main-menu/continue and show panel
547+ startBtn . style . display = "none" ;
548+ continueGameBtn . style . display = "none" ;
549+ panel . style . display = "block" ;
550+
551+ console . log ( 'Game restored. currentIndex =' , currentIndex ) ;
552+ } catch ( err ) {
553+ console . error ( 'Error while restoring saved game:' , err ) ;
554+ }
555+ } ;
481556// --- Initialize display on page load ---
482557function initializeDisplay ( ) {
483558 correctDisplay . textContent = correctCount ;
@@ -523,3 +598,18 @@ async function saveStats() {
523598 } , { merge : true } ) ;
524599}
525600
601+ if ( saved ) {
602+ continueGameBtn . style . display = "inline-block" ;
603+ }
604+
605+ function saveGameToLocal ( ) {
606+ const payload = {
607+ questionIndex : currentIndex ,
608+ correct : correctCount ,
609+ wrong : wrongCount ,
610+ skill : skillCount ,
611+ life : lifePoints
612+ } ;
613+ localStorage . setItem ( 'savedGame' , JSON . stringify ( payload ) ) ;
614+ }
615+
0 commit comments