Results 1 to 2 of 2

Thread: Want to make my code more efficient

  1. #1
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Want to make my code more efficient

    I have this part of my script which is pretty efficient at finding my target. I am trying to turn the second script, which is basically the same function, into an efficient script like the first one. I am learning javascript so I would like to make this a learning experience into writing optimized scripts.


    Code:
    if(thisPage=="The Hit List")
    {
    	var i, highestValue = boss.preferences.bounty_min, correspondingInput;
    	var inputs = document.evaluate("//input[@value='attack']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
    	for(i = inputs.snapshotLength - 1; i>=0; i--) 
    	{
    		checkVal = parseInt(inputs.snapshotItem(i).parentNode.parentNode.previousSibling.previousSibling.previousSibling.previousSibling.innerHTML.replace(/[\$,]/g, ""));
    		if (checkVal < boss.preferences.bounty_max &&  checkVal > boss.preferences.bounty_min && checkVal > highestValue) 
    		{
    			highestValue = checkVal;
    			correspondingInput = inputs.snapshotItem(i);
    			reload = false;
    		}	
    	}
    	if (correspondingInput) correspondingInput.click();
    }

    Code:
      if (Page.c_page=='hitlist')
        {
        var found = false;
        if (boss.jackpot_id)
          {
          var seek = Utils.getElementsByXPath('.//form[contains(@action,"http://apps.facebook.com/mobwars/fight/do.php")]');
          if (seek)
            {
            var x;
            for (it in seek)
              {
              var further = Utils.getElementsByXPath('.//input[contains(@name,"target_id")]',seek[it]);
              if (further)
                {
                if (further[0].value==boss.jackpot_id)
                  {
                  found = true;
                  break;
                  }
                }
              }
            }
          }
    
        if (!found)
          {
        	boss.jackpot_id = 0;
        	boss.jackpot = 0;
        	GM_setValue('boss', boss.toSource());
          }
    
        if (boss.preferences.hitlist_active && boss.stamina>0)
          {
          var content = document.getElementById('app8743457343_content');
          if (content) divs = content.getElementsByTagName('FORM');
          var target_acquired = 0;
          var first_target
    
          for (var i = 0; i < divs.length; i++)
            {
            var attack = divs[i].innerHTML.match(/attack/);
            if (!attack)
              {
              continue;
              }
            else
              {
              var highest = 0;
              var highest_target;
              var target_id = divs[i].getElementsByTagName('INPUT')[12].value;
              var bounty_id = divs[i].getElementsByTagName('INPUT')[13].value;
              var bounty = parseInt(dollars_to_int(divs[i].parentNode.parentNode.getElementsByTagName('TD')[2].innerHTML));
    
              var req1 = bounty > boss.preferences.bounty_min;
              var req2 = bounty < boss.preferences.bounty_max;
              var req3 = boss.preferences.bounty_min==0;
              var req4 = boss.preferences.bounty_max==0;
              var highest_target_id;
              var first_target_id;
    
              if ((req1 && req2) || (req1 && req4) || (req3 && req2) || (req3 && req4))
                {
            	  target_acquired++;
    
                // First Come First Serve
                if (boss.preferences.hitlist_order)
                  { // first in-range target acquired
                  first_target = 'http://apps.facebook.com/mobwars/fight/do.php?action=attack_bounty&target_id='+target_id+'&bounty_id='+bounty_id+'&from=/hitlist/index.php';
                  first_target_id = target_id;
                  break;
                  }
    
                // Aim For Highest Bounty
                if (bounty > highest)
            	    { // target with more bounty acquried
                  highest = bounty;
        	        highest_target = 'http://apps.facebook.com/mobwars/fight/do.php?action=attack_bounty&target_id='+target_id+'&bounty_id='+bounty_id+'&from=/hitlist/index.php';
        	        highest_target_id = target_id;
                  }
          	    }
              }
            }
    
          if (target_acquired>0)
            {
            target_acquired--;
            boss.evaded = 0;
            boss.jackpot++;
            boss.targets = target_acquired;
            GM_setValue('boss', boss.toSource());
            if (boss.preferences.snipes>0)
              {
            	for (var snipers=1;snipers<boss.preferences.snipes;snipers++)
            	  {
            	  GM_xmlhttpRequest({method:'GET',url:(boss.preferences.hitlist_order?first_target:highest_target)}); // GM ajax snipe!
            	  }
              }
            document.location.href = (boss.preferences.hitlist_order?first_target:highest_target);
            return;
      		  }
    
          boss.jackpot = 0;
          GM_setValue('boss', boss.toSource());
    
          if (boss.preferences.aggressive)
            {
            if (divs.length>0)
              {
              boss.evaded++;
              }
             else
             	{
            	boss.evaded = 0;
             	}
            GM_setValue('boss', boss.toSource());
            if (boss.targets>0 && boss.evaded<=5)
              {
              window.location.reload();
              }
            else
              {
              window.location.href = "http://apps.facebook.com/mobwars/"+(boss.preferences.hurl?"":"hitlist/"); 
              }
            }
         else
            {
            if (boss.preferences.passive_refresh) window.setTimeout(function() { window.location.href = "http://apps.facebook.com/mobwars/"+(boss.preferences.hurl?"":"hitlist/") }, norm_refresh);
            }
       	  }
        }
      }

  2. #2
    Join Date
    Dec 2008
    Location
    Nigeria
    Posts
    95
    Thanks
    3
    Thanked 8 Times in 8 Posts

    Default

    You can alwayz start your learning from javascriptkit.com, w3schools.com those are nice sites for beginners.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •