Guest User

hnoke

a guest
Oct 5th, 2010
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. /*
  2.  * This program is free software: you can redistribute it and/or modify it under
  3.  * the terms of the GNU General Public License as published by the Free Software
  4.  * Foundation, either version 3 of the License, or (at your option) any later
  5.  * version.
  6.  *
  7.  * This program is distributed in the hope that it will be useful, but WITHOUT
  8.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9.  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10.  * details.
  11.  *
  12.  * You should have received a copy of the GNU General Public License along with
  13.  * this program. If not, see <http://www.gnu.org/licenses/>.
  14.  */
  15. package com.l2jserver.gameserver.datatables;
  16.  
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.util.Map;
  22.  
  23. import javolution.util.FastMap;
  24.  
  25. import com.l2jserver.L2DatabaseFactory;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. import com.l2jserver.util.Rnd;
  28.  
  29.  
  30. /**
  31.  * @author hNoke
  32.  *
  33.  */
  34. public class PvPRewardsTable
  35. {
  36.     private Map<Integer, PvPRewardItem> _rewards;
  37.  
  38.     public static PvPRewardsTable getInstance()
  39.     {
  40.         return SingletonHolder._instance;
  41.     }
  42.    
  43.     public PvPRewardsTable()
  44.     {
  45.         load();
  46.     }
  47.    
  48.     public void rewardLastHit(L2PcInstance winner, L2PcInstance target)
  49.     {
  50.         int random = Rnd.get(100000);
  51.         int ammount;
  52.        
  53.         for(Map.Entry<Integer, PvPRewardItem> item : _rewards.entrySet())
  54.         {
  55.             if(random < item.getKey() && item.getValue().checkConsecutiveShits(winner))
  56.             {
  57.                 ammount = item.getValue().getAmmount(winner);
  58.                
  59.                 if(ammount > 0)
  60.                     winner.addItem("PvPReward", item.getValue().id, ammount, null, true);
  61.             }
  62.         }
  63.     }
  64.  
  65.     public class PvPRewardItem
  66.     {
  67.         public int id;
  68.         public int minAmmount;
  69.         public int maxAmmount;
  70.         public int chance;
  71.         public int pvpRequired;
  72.         public int levelRequired;
  73.         public int rewardAfter;
  74.        
  75.         public PvPRewardItem(int id, int minAmmount, int maxAmmount, int chance, int pvpRequired, int levelRequired, int rewardAfter)
  76.         {
  77.             this.id = id;
  78.             this.minAmmount = minAmmount;
  79.             this.maxAmmount = maxAmmount;
  80.             this.chance = chance;
  81.             this.pvpRequired = pvpRequired;
  82.             this.levelRequired = levelRequired;
  83.             this.rewardAfter = rewardAfter;
  84.         }
  85.        
  86.         public boolean checkConsecutiveShits(L2PcInstance player)
  87.         {
  88.             if(rewardAfter == 0)
  89.                 return true;
  90.            
  91.             if(player.getPvpKills() != 0 && player.getPvpKills() % rewardAfter == 0)
  92.                 return true;
  93.            
  94.             return false;
  95.         }
  96.        
  97.         public int getAmmount(L2PcInstance player)
  98.         {
  99.             if(player.getLevel() >= levelRequired && player.getPvpKills() >= pvpRequired)
  100.                 return Rnd.get(minAmmount, maxAmmount);
  101.             else
  102.                 return 0;
  103.         }
  104.     }
  105.  
  106.     private void load()
  107.     {
  108.         _rewards = new FastMap<Integer, PvPRewardItem>();
  109.        
  110.         Connection con = null;
  111.         try
  112.         {
  113.             con = L2DatabaseFactory.getInstance().getConnection();
  114.             PreparedStatement statement = con.prepareStatement("SELECT * FROM pvp_rewards");
  115.             ResultSet rset = statement.executeQuery();
  116.            
  117.             while (rset.next())
  118.             {
  119.                 _rewards.put(rset.getInt("chance"), new PvPRewardItem(rset.getInt("id"), rset.getInt("minAmmount"), rset.getInt("maxAmmount"), rset.getInt("chance"), rset.getInt("pvpsRequired"), rset.getInt("levelRequired"), rset.getInt("rewardAfter")));
  120.             }
  121.            
  122.             rset.close();
  123.             statement.close();
  124.         }
  125.  
  126.         catch (SQLException e)
  127.         {
  128.             e.printStackTrace();
  129.         }
  130.  
  131.         finally
  132.         {
  133.             try
  134.             {
  135.                 con.close();
  136.             }
  137.  
  138.             catch (Exception e)
  139.             {
  140.             }
  141.         }
  142.     }
  143.    
  144.     @SuppressWarnings("synthetic-access")
  145.     private static class SingletonHolder
  146.     {
  147.         protected static final PvPRewardsTable _instance = new PvPRewardsTable();
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment