@@ -10,6 +10,35 @@ let isGoingDown = true;
1010let lastPushUpTime = 0 ;
1111let pushUpCooldown = 1000 ;
1212
13+ let rewards = [
14+ { name : 'Extra TV time' , cost : 1 } ,
15+ { name : 'Favorite snack' , cost : 2 } ,
16+ { name : 'Movie night' , cost : 3 }
17+ ] ;
18+
19+ function loadTokens ( ) {
20+ const savedTokens = localStorage . getItem ( 'tokens' ) ;
21+ if ( savedTokens ) {
22+ tokenCount = parseInt ( savedTokens ) ;
23+ document . getElementById ( 'tokenCount' ) . textContent = tokenCount ;
24+ }
25+ }
26+
27+ function saveTokens ( ) {
28+ localStorage . setItem ( 'tokens' , tokenCount . toString ( ) ) ;
29+ }
30+
31+ function loadRewards ( ) {
32+ const savedRewards = localStorage . getItem ( 'rewards' ) ;
33+ if ( savedRewards ) {
34+ rewards = JSON . parse ( savedRewards ) ;
35+ }
36+ }
37+
38+ function saveRewards ( ) {
39+ localStorage . setItem ( 'rewards' , JSON . stringify ( rewards ) ) ;
40+ }
41+
1342async function setupCamera ( ) {
1443 video = document . getElementById ( 'video' ) ;
1544 const constraints = {
@@ -74,6 +103,7 @@ function updateStats() {
74103 if ( pushUpCount % pushUpsPerToken === 0 ) {
75104 tokenCount ++ ;
76105 document . getElementById ( 'tokenCount' ) . textContent = tokenCount ;
106+ saveTokens ( ) ;
77107 }
78108}
79109
@@ -95,15 +125,9 @@ document.getElementById('startStop').addEventListener('click', () => {
95125 }
96126} ) ;
97127
98- const rewards = [
99- { name : 'Extra TV time' , cost : 1 } ,
100- { name : 'Favorite snack' , cost : 2 } ,
101- { name : 'Movie night' , cost : 3 }
102- ] ;
103-
104128function displayRewards ( ) {
105129 const rewardsContainer = document . getElementById ( 'rewards' ) ;
106- rewardsContainer . innerHTML = '<h3 class="text-xl font-semibold mb-4">Rewards:</h3> ' ;
130+ rewardsContainer . innerHTML = '' ;
107131 rewards . forEach ( reward => {
108132 const button = document . createElement ( 'button' ) ;
109133 button . textContent = `${ reward . name } (${ reward . cost } tokens)` ;
@@ -117,13 +141,35 @@ function redeemReward(reward) {
117141 if ( tokenCount >= reward . cost ) {
118142 tokenCount -= reward . cost ;
119143 document . getElementById ( 'tokenCount' ) . textContent = tokenCount ;
144+ saveTokens ( ) ;
120145 alert ( `You've redeemed: ${ reward . name } ` ) ;
121146 } else {
122147 alert ( 'Not enough tokens!' ) ;
123148 }
124149}
125150
151+ document . getElementById ( 'editRewards' ) . addEventListener ( 'click' , ( ) => {
152+ const rewardEditor = document . getElementById ( 'rewardEditor' ) ;
153+ const rewardJSON = document . getElementById ( 'rewardJSON' ) ;
154+ rewardEditor . classList . toggle ( 'hidden' ) ;
155+ rewardJSON . value = JSON . stringify ( rewards , null , 2 ) ;
156+ } ) ;
157+
158+ document . getElementById ( 'saveRewards' ) . addEventListener ( 'click' , ( ) => {
159+ const rewardJSON = document . getElementById ( 'rewardJSON' ) ;
160+ try {
161+ rewards = JSON . parse ( rewardJSON . value ) ;
162+ saveRewards ( ) ;
163+ displayRewards ( ) ;
164+ document . getElementById ( 'rewardEditor' ) . classList . add ( 'hidden' ) ;
165+ } catch ( error ) {
166+ alert ( 'Invalid JSON. Please check your input.' ) ;
167+ }
168+ } ) ;
169+
126170async function init ( ) {
171+ loadTokens ( ) ;
172+ loadRewards ( ) ;
127173 await setupCamera ( ) ;
128174 displayRewards ( ) ;
129175}
0 commit comments