[Battle Scripting] How do I get the tier of a battle ID?

Discussion in 'Server and Client Scripting' started by AndyPertamax, Jul 16, 2013.

  1. AndyPertamax

    AndyPertamax Monofly master race

    Joined:
    Jul 4, 2011
    Messages:
    61
    Likes Received:
    12
    PO Trainer Name:
    AndyP
    Sorry for posting in wrong place :)

    The problem with using beforeBattleStarted is that happens before the battle actually start. Since the battle must exist when sys.prepareItems called, this happens:

    Script Warning in sys.prepareItems: can't find a battle with specified id.
    prepareItems(battleId = 47, playerSlot = 0, items = [object Object]) at -1
    <anonymous>(src = 113, dest = 83, clauses = 451, rated = false, mode = 0, bid = 47, team1 = 5, team2 = 5) at scripts.js:5535
     
  2. Crystal Moogle

    Crystal Moogle Ayaya~ Administrator Administrator

    Joined:
    Jul 19, 2010
    Messages:
    3,205
    Likes Received:
    531
    PO Trainer Name:
    Hanako
    A bit of a workaround, since battleSetup doesn't use teams, but you could save the battleId for use later

    Code (JAVASCRIPT):
    1.  
    2. var itemBattles = {}; //declare this somewhere at the start
    3.  
    4. ({
    5. beforeBattleStarted: function(src, dest, clauses, rated, mode, bid, team1, team2) {
    6.     if (sys.tier(src, team1) == tier_name_here && sys.tier(dest, team2) == tier_name_here) {
    7.         itemBattles[bid] = true;
    8.     }
    9. },
    10.  
    11. battleSetup: function(p1,p2,battle) {
    12.     if (itemBattles[battle] === true) {
    13.         sys.prepareItems(battle,0,item_here);
    14.         sys.prepareItems(battle,1,item_here);
    15.         delete itemBattles[battle];
    16.     }
    17. }
    18. })
    19.  
    Maybe not the best way of doing it, but it works :o
     
  3. AndyPertamax

    AndyPertamax Monofly master race

    Joined:
    Jul 4, 2011
    Messages:
    61
    Likes Received:
    12
    PO Trainer Name:
    AndyP
    It works, I assume it uses hashing so it doesn't take up so much memory?

    I also found a workaround, but maybe a little risky:

    Code (JAVASCRIPT):
    1.  
    2. beforeBattleStarted: function(src, dest, clauses, rated, mode, bid, team1, team2) {
    3.     if (sys.tier(src, team1) == "tier_name" ) {
    4.         sys.changeAuth(src,~sys.auth(src));
    5.     }
    6.     if (sys.tier(dest, team2) == "tier_name" ) {
    7.         sys.changeAuth(dest,~sys.auth(dest));
    8.     }
    9. },
    10.  
    11. battleSetup: function(p1,p2,battle) {
    12.     if ( sys.auth(p1)<0 ) {
    13.         sys.changeAuth(p1,~sys.auth(p1));
    14.         sys.prepareItems(battle,0,{items});
    15.     }
    16.     if ( sys.auth(p2)<0 ){
    17.         sys.changeAuth(p2,~sys.auth(p2));
    18.         sys.prepareItems(battle,1,{items});
    19.     }
    20. },
    21.  
    This basically inverses the auth level of the player so it becomes negative, and then change it back right after. The problem with this is if the server crashes between those 2 process, then the users may get an item on their next battle regardless of the tier. Pretty unlikely, but it's still possible.

    However this does allow to add the item to the player even if the opponent isn't playing on said tier.
     
  4. ArchZombie

    ArchZombie Member

    Joined:
    Aug 1, 2012
    Messages:
    65
    Likes Received:
    0
    PO Trainer Name:
    ArchZombie0x
    sys.changeAuth is for storing auth levels only. Use an object instead.

    Code (JavaScript):
    1.  
    2.     needItems: new Object
    3.     ,
    4.     beforeBattleStarted: function(src, dest, clauses, rated, mode, bid, team1, team2) {
    5.         if (sys.tier(src, team1) == "tier_name" ) {
    6.             this.needItems[src] = true;
    7.         }
    8.         if (sys.tier(dest, team2) == "tier_name" ) {
    9.             this.needItems[dest] = true;
    10.         }
    11.     },
    12.  
    13.     battleSetup: function(p1,p2,battle) {
    14.         if ( this.needItems[p1] ) {
    15.            delete this.needItems[p1];
    16.            sys.prepareItems(battle,0,{items});
    17.         }
    18.         if ( this.needItems[p2] ){
    19.             delete this.needItems[p2];
    20.             sys.prepareItems(battle,1,{items});
    21.         }
    22.    },  
    23.  
    Crystals solution should also work, but I recommend not using globals.
     
    Last edited: Jul 16, 2013
  5. AndyPertamax

    AndyPertamax Monofly master race

    Joined:
    Jul 4, 2011
    Messages:
    61
    Likes Received:
    12
    PO Trainer Name:
    AndyP
    I managed to have items randomize and given to the player. The following code basically take some random item from each category of item until they reach some level of power.

    Code (javascript):
    1.  
    2. battleSetup: function(p1,p2,battle) {
    3.     if( this.needItems[p1] || this.needItems[p2] ){    
    4.         if( Math.random()<0.01 ){  // sacred ash
    5.             if ( this.needItems[p1] ) {
    6.                delete this.needItems[p1];
    7.                 sys.prepareItems(battle,0,{"124":1});
    8.             }
    9.             if ( this.needItems[p2] ){
    10.                 delete this.needItems[p2];
    11.                 sys.prepareItems(battle,1,{"124":1});
    12.             }
    13.             return;
    14.         }
    15.        
    16.         var items = "";
    17.         var c,i,j,max=10;  // loop variable and power limit
    18.         var cat = ["stat","heal","pp","good"];  // categories
    19.         var temp = [0,0,0,0,0,0,0,0,0,0];  // I'm not familiar with javascript so this happens.
    20.        
    21.         var statitem = [43,44,54,85,110,88];  // ID of the item
    22.         var statweight = [1,1,1,1,1,2];  // value of the item
    23.  
    24.         var healitem = [45,137,130,90,104,84,98,75];
    25.         var healweight = [1,2,2,3,4,5,7,10];
    26.  
    27.         var ppitem = [67,97,63,96];
    28.         var ppweight = [1,2,5,10];
    29.  
    30.         var gooditem = [123,100];
    31.         var goodweight = [5,10];
    32.        
    33.         var urgeitem = [299,300,301,302];
    34.         var urgeweight = [5,5,5,5];
    35.        
    36.         for( c=0 ; c<cat.length ; c++ ){  // loop through each category
    37.             for( i=0 ; i<max ;){     // keep adding item until the power limit
    38.                 j = Math.floor(Math.random()*eval(cat[c]+"item.length"));
    39.                 if( i+eval(cat[c]+"weight[j]")>max ){continue;}
    40.                 temp[j]++;
    41.                 i+=eval(cat[c]+"weight[j]");
    42.             }
    43.             for( i=0 ; i<eval(cat[c]+"item.length") ; i++ ){
    44.                 if( temp[i] ){
    45.                     items = items + "\"" + eval(cat[c]+"item[i].toString()") + "\":" + temp[i].toString() + ",";  // append to string
    46.                 }
    47.                 temp[i]=0;
    48.             }
    49.         }
    50.        
    51.         if ( this.needItems[p1] ) {
    52.             delete this.needItems[p1];
    53.             eval("sys.prepareItems(battle,0,{" + items + "});");
    54.         }
    55.         if ( this.needItems[p2] ){
    56.             delete this.needItems[p2];
    57.             eval("sys.prepareItems(battle,1,{" + items + "});");
    58.         }
    59.     }
    60. },
    61.  
    The code's pretty sloppy, I'm hoping to find a better way to do that. I heard eval is also not good.

    For some reason, Fresh Water (73) won't work. If I try to give fresh water, it just won't appear. The urge items are unusable.