Updating datas in firebase database with js code returns unexpected loop

Viewed 72

Some weeks ago i started creating my own game using javascript and firebase, and i had some problems with updating datas. When i was creating a battle system for this game, i was trying to give players some benefits from winning fights. When player won a battle, i wanted to update his (for example) exp by adding exp and it worked, but when i added another updating data (when player won a fight, system was updating datas about fight wins, and data location about stats were in different location in database), it returns loop. JS code here

//battle_system.js

var opponent = nextBattle.getAttribute('mon-name');
var hp = 0;

 var getPlayer = firebase.database().ref().child('accounts/' + uid + '/character/char');
 var getEnemy = firebase.database().ref().child('Monsters/' + opponent);

                    getPlayer.on('value', (snapshot) => {
                       var getSnapshot = snapshot.val();


                         getEnemy.on('value', (snap) => {
                              var getSnap = snap.val();

                              var enemyHp = getSnap.hp;
                              var playerHp = getSnapshot.hp;

                              var enemy = {
                                  name: getSnap.name,
                                  hp: enemyHp,
                                  damage: getSnap.dmg,
                                  armor: getSnap.armor,
                                  lvl: getSnap.lvl
                              } 
                              var player = {
                                  name: getSnapshot.nick,
                                  hp: playerHp,
                                  damage: getSnapshot.dmg,
                                  armor: getSnapshot.armor,
                                  lvl: getSnapshot.lvl
                              }
                              var starCountRef = firebase.database().ref('accounts/' + uid + '/character/battles/stats/' + opponent);

                              btl_section.innerHTML = '';
                              starCountRef.on('value', (snapStat) => {

                                  var x = (snapStat.val().wins)+1;
                                  var y = (snapStat.val().loses)+1;

                                  while(player.hp > 0 && enemy.hp > 0){
                                      var MathPlayer = Math.random(),
                                          MathEnemy = Math.random();

                                      enemy.hp -= player.damage * MathPlayer;
                                      player.hp -= enemy.damage * MathEnemy;
                                      btl_section.innerHTML = btl_section.innerHTML + 'Enemy: ' + enemy.hp + '<br> Player: ' + player.hp + '<br>';

                                     if(player.hp < 0){
                                          firebase.database().ref('accounts/' + uid + '/character/battles/stats/' + opponent).update({
                                              loses : y
                                          });
                                      }else if(enemy.hp < 0) {
                                          console.log('win');
                                          firebase.database().ref('accounts/' + uid + '/character/battles/stats/' + opponent).update({
                                              wins : x
                                          });
                                          firebase.database().ref('accounts/' + uid + '/character/char').update({
                                              exp : (30 * (Math.random()*100))
                                          });
                                      }

                                 }

                            });
                         });

                    });
  //firebase structure
Monsters: {
  Freder : {
    armor: 0,
    dmg: 10,
    lvl: 1,
    hp: 100,
    name: 'Freder'
 }
},
accounts:{
   uid:{
    character:{
       battles:{
          stats: {
             wins: 0,
            loses: 0
          }
        },
        char:{
          exp: 0,
          lvl: 0,
          dmg: 12,
          hp: 100,
          armor: 0,
          nick: 'Nickname'
       }
    }

   }
 }

And now, i except that, when player win the battle, the exp and statistics about wins will be add without problems, but battle is looping. When i see a consol.log in browser console about winning game, i see hundreds of logs from result and after some seconds, game returns error "Uncaught RangeError: Maximum call stack size exceeded". And now i need help about this. How could i fix this?

0 Answers
Related