tobaJK

aCis Coupons + Redeem manager

Jul 19th, 2017
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 37.78 KB | None | 0 0
  1. ### Eclipse Workspace Patch 1.0
  2. #P aCis_gameserver
  3. Index: java/net/sf/l2j/custom/CouponManager.java
  4. ===================================================================
  5. --- java/net/sf/l2j/custom/CouponManager.java   (nonexistent)
  6. +++ java/net/sf/l2j/custom/CouponManager.java   (working copy)
  7. @@ -0,0 +1,143 @@
  8. +package net.sf.l2j.custom;
  9. +
  10. +import java.sql.Connection;
  11. +import java.sql.PreparedStatement;
  12. +import java.sql.ResultSet;
  13. +import java.util.Set;
  14. +import java.util.concurrent.ConcurrentHashMap;
  15. +import java.util.logging.Level;
  16. +import java.util.logging.Logger;
  17. +
  18. +import net.sf.l2j.L2DatabaseFactory;
  19. +import net.sf.l2j.gameserver.idfactory.IdFactory;
  20. +import net.sf.l2j.gameserver.model.actor.instance.Player;
  21. +
  22. +/**
  23. + * @author Melron
  24. + */
  25. +public class CouponManager
  26. +{
  27. +   private final static Logger _log = Logger.getLogger(CouponManager.class.getName());
  28. +   private static Set<Coupon> coupons = ConcurrentHashMap.newKeySet();
  29. +  
  30. +   public static void loadCoupons()
  31. +   {
  32. +       try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  33. +       {
  34. +           PreparedStatement statement = con.prepareStatement("SELECT owner_objid,coupon_id, coupon_category, grade FROM coupons");
  35. +           ResultSet cpns = statement.executeQuery();
  36. +          
  37. +           while (cpns.next())
  38. +           {
  39. +               Coupon coupon = new Coupon();
  40. +               coupon.setOwnerObjectId(cpns.getInt("owner_objid"));
  41. +               coupon.setId(cpns.getInt("coupon_id"));
  42. +               coupon.setCategory(cpns.getString("coupon_category").toUpperCase());
  43. +               coupon.setGradeType(cpns.getInt("grade"));
  44. +               if (!coupons.contains(coupon))
  45. +                   coupons.add(coupon);
  46. +           }
  47. +           cpns.close();
  48. +           statement.close();
  49. +       }
  50. +       catch (Exception e)
  51. +       {
  52. +           _log.log(Level.WARNING, "Could not restore coupons: " + e.getMessage(), e);
  53. +       }
  54. +   }
  55. +  
  56. +   /**
  57. +    * @return
  58. +    */
  59. +   public static int getCouponsSize()
  60. +   {
  61. +       return coupons.size();
  62. +   }
  63. +  
  64. +   public static void setCoupons(Player player)
  65. +   {
  66. +       if (coupons.isEmpty() || player == null)
  67. +           return;
  68. +       for (Coupon cpn : coupons)
  69. +       {
  70. +           if (cpn.getOwnerObjectId() == player.getObjectId())
  71. +               player.setCoupon(cpn);
  72. +       }
  73. +   }
  74. +  
  75. +   public static Coupon generateCoupon(String category, Player player)
  76. +   {
  77. +       if (player == null)
  78. +       {
  79. +           System.out.println("Could not generate new coupon ID! Player = null!");
  80. +           return null;
  81. +       }
  82. +      
  83. +       Coupon coupon = new Coupon();
  84. +       coupon.setCategory(category);
  85. +       coupon.setId(IdFactory.getInstance().getNextId());
  86. +       coupon.setOwnerObjectId(player.getObjectId());
  87. +      
  88. +       coupon.setGradeType(Math.max(0, player.getSkillLevel(239)));
  89. +      
  90. +       if (!storeNewCoupon(coupon))
  91. +       {
  92. +           IdFactory.getInstance().releaseId(coupon.getId());
  93. +           return null;
  94. +       }
  95. +      
  96. +       coupons.add(coupon);
  97. +       player.setCoupon(coupon);
  98. +      
  99. +       return coupon;
  100. +      
  101. +   }
  102. +  
  103. +   /**
  104. +    * @param coupon
  105. +    * @return success
  106. +    */
  107. +   private static boolean storeNewCoupon(Coupon coupon)
  108. +   {
  109. +       try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  110. +       {
  111. +           PreparedStatement statement = con.prepareStatement("INSERT INTO coupons (owner_objid,coupon_id,coupon_category,grade) VALUES (?,?,?,?)");
  112. +          
  113. +           statement.setInt(1, coupon.getOwnerObjectId());
  114. +           statement.setInt(2, coupon.getId());
  115. +           statement.setString(3, coupon.getCategory().toUpperCase());
  116. +           statement.setInt(4, coupon.getGradeType());
  117. +           statement.execute();
  118. +           statement.close();
  119. +       }
  120. +       catch (Exception e)
  121. +       {
  122. +           _log.warning("could not save Coupon for objectId :" + coupon.getOwnerObjectId() + "." + e);
  123. +           return false;
  124. +       }
  125. +       return true;
  126. +   }
  127. +  
  128. +  
  129. +   /**
  130. +    * @param coupon
  131. +    */
  132. +   public static void deleteCoupon(Coupon coupon)
  133. +   {
  134. +       try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  135. +       {
  136. +           try (PreparedStatement ps = con.prepareStatement("DELETE FROM coupons where coupon_id = ?"))
  137. +           {
  138. +               ps.setInt(1, coupon.getId());
  139. +               ps.executeUpdate();
  140. +           }
  141. +           IdFactory.getInstance().releaseId(coupon.getId());
  142. +           coupons.remove(coupon);
  143. +       }
  144. +       catch (Exception e)
  145. +       {
  146. +           _log.log(Level.WARNING, "Could not delete coupon: ", e);
  147. +       }
  148. +      
  149. +   }
  150. +}
  151. Index: java/net/sf/l2j/custom/Coupon.java
  152. ===================================================================
  153. --- java/net/sf/l2j/custom/Coupon.java  (nonexistent)
  154. +++ java/net/sf/l2j/custom/Coupon.java  (working copy)
  155. @@ -0,0 +1,185 @@
  156. +package net.sf.l2j.custom;
  157. +
  158. +import java.util.ArrayList;
  159. +import java.util.Collections;
  160. +import java.util.List;
  161. +
  162. +import net.sf.l2j.commons.lang.StringUtil;
  163. +
  164. +import net.sf.l2j.gameserver.model.holder.IntIntHolder;
  165. +
  166. +/**
  167. + * @author Melron
  168. + */
  169. +
  170. +public class Coupon
  171. +{
  172. +   public enum GradeType
  173. +   {
  174. +       NG(1),
  175. +       D(2),
  176. +       C(3),
  177. +       B(4),
  178. +       A(5),
  179. +       S(6);
  180. +      
  181. +       private int multiplier;
  182. +      
  183. +       GradeType(int gradeMultiplier)
  184. +       {
  185. +           this.multiplier = gradeMultiplier;
  186. +       }
  187. +      
  188. +       int getGradeMultiplier()
  189. +       {
  190. +           return multiplier;
  191. +       }
  192. +   }
  193. +  
  194. +   private GradeType gradeType;
  195. +   private int couponId;
  196. +   private String couponCategory = "";
  197. +   private int ownerObjectId;
  198. +   private List<IntIntHolder> Rewards = new ArrayList<>();
  199. +  
  200. +   public String getName()
  201. +   {
  202. +       StringBuilder sb = new StringBuilder();
  203. +       StringUtil.append(sb, couponCategory, " Coupon - Grade [", gradeType.name(), "]");
  204. +       return sb.toString();
  205. +   }
  206. +  
  207. +   public String getCategory()
  208. +   {
  209. +       return couponCategory;
  210. +   }
  211. +  
  212. +   public void setId(int val)
  213. +   {
  214. +       couponId = val;
  215. +   }
  216. +  
  217. +   public int getId()
  218. +   {
  219. +       return couponId;
  220. +   }
  221. +  
  222. +   public void setCategory(String val)
  223. +   {
  224. +       couponCategory = val;
  225. +       if (couponCategory == null)
  226. +           couponCategory = "";
  227. +   }
  228. +  
  229. +   public int getOwnerObjectId()
  230. +   {
  231. +       return ownerObjectId;
  232. +   }
  233. +  
  234. +   public void setOwnerObjectId(int objId)
  235. +   {
  236. +       ownerObjectId = objId;
  237. +   }
  238. +  
  239. +   /**
  240. +    * @return
  241. +    */
  242. +   public List<IntIntHolder> getRewards()
  243. +   {
  244. +       final String Silver = "SILVER";
  245. +       final String Gold = "GOLD";
  246. +       final String Platinum = "PLATINUM";
  247. +       Rewards.clear();
  248. +       if (couponCategory.equalsIgnoreCase(Silver))
  249. +       {
  250. +           Rewards.add(new IntIntHolder(8762, 2)); // 1
  251. +           Rewards.add(new IntIntHolder(8762, 7)); // 2
  252. +           Rewards.add(new IntIntHolder(6622, 4)); // 3
  253. +           Rewards.add(new IntIntHolder(6622, 7)); // 4
  254. +           Rewards.add(new IntIntHolder(6578, 10)); // 5
  255. +           Rewards.add(new IntIntHolder(6578, 1)); // 6
  256. +           Rewards.add(new IntIntHolder(57, 1000000000));// 7
  257. +           Rewards.add(new IntIntHolder(6577, 5)); // 8
  258. +           Rewards.add(new IntIntHolder(6577, 20)); // 9
  259. +           Rewards.add(new IntIntHolder(6673, 15)); // 10
  260. +          
  261. +           Rewards.add(new IntIntHolder(8762, 9)); // 11
  262. +           Rewards.add(new IntIntHolder(8762, 4)); // 12
  263. +           Rewards.add(new IntIntHolder(6622, 6)); // 13
  264. +           Rewards.add(new IntIntHolder(6622, 9)); // 14
  265. +           Rewards.add(new IntIntHolder(6578, 12)); // 15
  266. +           Rewards.add(new IntIntHolder(6578, 33)); // 16
  267. +           Rewards.add(new IntIntHolder(57, 500000));// 17
  268. +           Rewards.add(new IntIntHolder(6577, 12)); // 18
  269. +           Rewards.add(new IntIntHolder(6577, 3)); // 19
  270. +           Rewards.add(new IntIntHolder(6673, 50)); // 20
  271. +       }
  272. +       else if (couponCategory.equalsIgnoreCase(Gold))
  273. +       {
  274. +           Rewards.add(new IntIntHolder(8762, 2)); // 1
  275. +           Rewards.add(new IntIntHolder(8762, 7)); // 2
  276. +           Rewards.add(new IntIntHolder(6622, 4)); // 3
  277. +           Rewards.add(new IntIntHolder(6622, 7)); // 4
  278. +           Rewards.add(new IntIntHolder(6578, 10)); // 5
  279. +           Rewards.add(new IntIntHolder(6578, 1)); // 6
  280. +           Rewards.add(new IntIntHolder(57, 1000000000));// 7
  281. +           Rewards.add(new IntIntHolder(6577, 5)); // 8
  282. +           Rewards.add(new IntIntHolder(6577, 20)); // 9
  283. +           Rewards.add(new IntIntHolder(6673, 15)); // 10
  284. +          
  285. +           Rewards.add(new IntIntHolder(8762, 9)); // 11
  286. +           Rewards.add(new IntIntHolder(8762, 4)); // 12
  287. +           Rewards.add(new IntIntHolder(6622, 6)); // 13
  288. +           Rewards.add(new IntIntHolder(6622, 9)); // 14
  289. +           Rewards.add(new IntIntHolder(6578, 12)); // 15
  290. +           Rewards.add(new IntIntHolder(6578, 33)); // 16
  291. +           Rewards.add(new IntIntHolder(57, 500000));// 17
  292. +           Rewards.add(new IntIntHolder(6577, 12)); // 18
  293. +           Rewards.add(new IntIntHolder(6577, 3)); // 19
  294. +           Rewards.add(new IntIntHolder(6673, 50)); // 20
  295. +       }
  296. +       else if (couponCategory.equalsIgnoreCase(Platinum))
  297. +       {
  298. +           Rewards.add(new IntIntHolder(8762, 2)); // 1
  299. +           Rewards.add(new IntIntHolder(8762, 7)); // 2
  300. +           Rewards.add(new IntIntHolder(6622, 4)); // 3
  301. +           Rewards.add(new IntIntHolder(6622, 7)); // 4
  302. +           Rewards.add(new IntIntHolder(6578, 10)); // 5
  303. +           Rewards.add(new IntIntHolder(6578, 1)); // 6
  304. +           Rewards.add(new IntIntHolder(57, 1000000000));// 7
  305. +           Rewards.add(new IntIntHolder(6577, 5)); // 8
  306. +           Rewards.add(new IntIntHolder(6577, 20)); // 9
  307. +           Rewards.add(new IntIntHolder(6673, 15)); // 10
  308. +          
  309. +           Rewards.add(new IntIntHolder(8762, 9)); // 11
  310. +           Rewards.add(new IntIntHolder(8762, 4)); // 12
  311. +           Rewards.add(new IntIntHolder(6622, 6)); // 13
  312. +           Rewards.add(new IntIntHolder(6622, 9)); // 14
  313. +           Rewards.add(new IntIntHolder(6578, 12)); // 15
  314. +           Rewards.add(new IntIntHolder(6578, 33)); // 16
  315. +           Rewards.add(new IntIntHolder(57, 500000));// 17
  316. +           Rewards.add(new IntIntHolder(6577, 12)); // 18
  317. +           Rewards.add(new IntIntHolder(6577, 3)); // 19
  318. +           Rewards.add(new IntIntHolder(6673, 50)); // 20
  319. +       }
  320. +       else
  321. +           return Collections.emptyList();
  322. +       Collections.shuffle(Rewards);
  323. +       return Rewards;
  324. +   }
  325. +  
  326. +   public void setGradeType(int ordinal)
  327. +   {
  328. +       gradeType = GradeType.values()[ordinal];
  329. +   }
  330. +  
  331. +   public int getGradeMultiplier()
  332. +   {
  333. +       return gradeType.getGradeMultiplier();
  334. +   }
  335. +  
  336. +   public int getGradeType()
  337. +   {
  338. +       return gradeType.ordinal();
  339. +   }
  340. +}
  341. Index: java/net/sf/l2j/Config.java
  342. ===================================================================
  343. --- java/net/sf/l2j/Config.java (revision 3)
  344. +++ java/net/sf/l2j/Config.java (working copy)
  345. @@ -461,6 +461,10 @@
  346.     public static boolean STORE_SKILL_COOLTIME;
  347.     public static int BUFFS_MAX_AMOUNT;
  348.    
  349. +   /** coupons */
  350. +   public static int COUPONS_LIMIT_PER_DAY;
  351. +   public static int[] COUPONS_RESET_LIMIT_AT = new int[2];
  352. +  
  353.     // --------------------------------------------------
  354.     // Sieges
  355.     // --------------------------------------------------
  356. @@ -1154,6 +1158,15 @@
  357.        
  358.         BUFFS_MAX_AMOUNT = players.getProperty("MaxBuffsAmount", 20);
  359.         STORE_SKILL_COOLTIME = players.getProperty("StoreSkillCooltime", true);
  360. +      
  361. +       COUPONS_LIMIT_PER_DAY = players.getProperty("CouponsLimit", 0);
  362. +      
  363. +       String[] Time = players.getProperty("ResetCouponsLimitAt", (String[]) null, ":");
  364. +       if (Time != null)
  365. +       {
  366. +           COUPONS_RESET_LIMIT_AT[0] = Integer.parseInt(Time[0]);
  367. +           COUPONS_RESET_LIMIT_AT[1] = Integer.parseInt(Time[1]);
  368. +       }
  369.     }
  370.    
  371.     /**
  372. Index: java/net/sf/l2j/gameserver/idfactory/IdFactory.java
  373. ===================================================================
  374. --- java/net/sf/l2j/gameserver/idfactory/IdFactory.java (revision 3)
  375. +++ java/net/sf/l2j/gameserver/idfactory/IdFactory.java (working copy)
  376. @@ -34,6 +34,10 @@
  377.         {
  378.             "items_on_ground",
  379.             "object_id"
  380. +       },
  381. +       {
  382. +           "coupons",
  383. +           "coupon_id"
  384.         }
  385.     };
  386.    
  387. Index: java/net/sf/l2j/gameserver/network/clientpackets/RequestBypassToServer.java
  388. ===================================================================
  389. --- java/net/sf/l2j/gameserver/network/clientpackets/RequestBypassToServer.java (revision 3)
  390. +++ java/net/sf/l2j/gameserver/network/clientpackets/RequestBypassToServer.java (working copy)
  391. @@ -1,11 +1,18 @@
  392.  package net.sf.l2j.gameserver.network.clientpackets;
  393.  
  394. +import java.util.List;
  395.  import java.util.StringTokenizer;
  396.  import java.util.logging.Level;
  397.  import java.util.logging.Logger;
  398.  
  399. +import net.sf.l2j.commons.lang.StringUtil;
  400. +import net.sf.l2j.commons.math.MathUtil;
  401. +import net.sf.l2j.commons.random.Rnd;
  402. +
  403.  import net.sf.l2j.Config;
  404. +import net.sf.l2j.custom.Coupon;
  405.  import net.sf.l2j.gameserver.communitybbs.CommunityBoard;
  406. +import net.sf.l2j.gameserver.data.ItemTable;
  407.  import net.sf.l2j.gameserver.data.xml.AdminData;
  408.  import net.sf.l2j.gameserver.handler.AdminCommandHandler;
  409.  import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
  410. @@ -15,6 +22,8 @@
  411.  import net.sf.l2j.gameserver.model.actor.instance.OlympiadManagerNpc;
  412.  import net.sf.l2j.gameserver.model.actor.instance.Player;
  413.  import net.sf.l2j.gameserver.model.entity.Hero;
  414. +import net.sf.l2j.gameserver.model.holder.IntIntHolder;
  415. +import net.sf.l2j.gameserver.model.item.instance.ItemInstance;
  416.  import net.sf.l2j.gameserver.model.olympiad.OlympiadManager;
  417.  import net.sf.l2j.gameserver.network.FloodProtectors;
  418.  import net.sf.l2j.gameserver.network.FloodProtectors.Action;
  419. @@ -169,6 +178,87 @@
  420.                 final int arenaId = Integer.parseInt(_command.substring(12).trim());
  421.                 activeChar.enterOlympiadObserverMode(arenaId);
  422.             }
  423. +           else if (_command.startsWith("list_"))
  424. +           {
  425. +               StringTokenizer st = new StringTokenizer(_command, "_");
  426. +               st.nextToken();
  427. +               int page;
  428. +               try
  429. +               {
  430. +                   page = Integer.parseInt(st.nextToken());
  431. +               }
  432. +               catch (NumberFormatException e)
  433. +               {
  434. +                   return;
  435. +               }
  436. +              
  437. +               showCoupons(activeChar, page);
  438. +              
  439. +           }
  440. +           else if (_command.startsWith("cpn_"))
  441. +           {
  442. +               StringTokenizer st = new StringTokenizer(_command, "_");
  443. +               st.nextToken();
  444. +              
  445. +               if (!st.hasMoreTokens())
  446. +                   return;
  447. +              
  448. +               String nextCommand = st.nextToken();
  449. +               int nextElement;
  450. +               int page = 1;
  451. +              
  452. +               try
  453. +               {
  454. +                   nextElement = Integer.parseInt(st.nextToken());
  455. +                   if (st.hasMoreTokens())
  456. +                       page = Integer.parseInt(st.nextToken());
  457. +               }
  458. +               catch (NumberFormatException e)
  459. +               {
  460. +                   return;
  461. +               }
  462. +              
  463. +               if (nextCommand.isEmpty())
  464. +                   return;
  465. +              
  466. +               Coupon coupon = activeChar.getCouponById(nextElement);
  467. +              
  468. +               if (coupon == null)
  469. +                   return;
  470. +              
  471. +               if (nextCommand.startsWith("list"))
  472. +               {
  473. +                   showCategoryReward(activeChar, coupon, 1);
  474. +               }
  475. +              
  476. +               else if (nextCommand.startsWith("redeem"))
  477. +               {
  478. +                   if (!activeChar.canUseCoupon())
  479. +                   {
  480. +                       activeChar.sendMessage("You have reached the daily limit of the coupons you can use. The limit will be lifted at " + (Config.COUPONS_RESET_LIMIT_AT[0] == 0 ? "00" : Config.COUPONS_RESET_LIMIT_AT[0]) + ":" + (Config.COUPONS_RESET_LIMIT_AT[1] == 0 ? "00" : Config.COUPONS_RESET_LIMIT_AT[1]) + ".");
  481. +                       return;
  482. +                   }
  483. +                   final int size = coupon.getRewards().size();
  484. +                   IntIntHolder reward = coupon.getRewards().get(Rnd.get(size));
  485. +                   final int multiplier = coupon.getGradeMultiplier();
  486. +                   ItemInstance item = ItemTable.getInstance().createItem("Redeem", reward.getId(), reward.getValue() * multiplier, activeChar, null);
  487. +                   activeChar.addItem("RedeemReward", item, null, true);
  488. +                   activeChar.increaseCouponsUsed();
  489. +                   activeChar.removeCoupon(coupon);
  490. +                   activeChar.sendMessage("Coupon " + coupon.getName() + " have been redeemed.");
  491. +                   showCoupons(activeChar, 1);
  492. +               }
  493. +               else if (nextCommand.startsWith("del"))
  494. +               {
  495. +                   activeChar.removeCoupon(coupon);
  496. +                   activeChar.sendMessage("Coupon: " + coupon.getName() + " have been successfully deleted");
  497. +                   showCoupons(activeChar, 1);
  498. +               }
  499. +               else if (nextCommand.startsWith("page"))
  500. +                   showCategoryReward(activeChar, coupon, page);
  501. +               else
  502. +                   return;
  503. +           }
  504.         }
  505.         catch (Exception e)
  506.         {
  507. @@ -176,6 +266,97 @@
  508.         }
  509.     }
  510.    
  511. +   /**
  512. +    * @param activeChar
  513. +    * @param page
  514. +    */
  515. +   private static void showCoupons(Player activeChar, int page)
  516. +   {
  517. +       List<Coupon> coupons = activeChar.getCoupons();
  518. +       // Load static Htm.
  519. +       final NpcHtmlMessage html = new NpcHtmlMessage(0);
  520. +       html.setFile("data/html/custom/couponsList.htm");
  521. +      
  522. +       if (coupons.isEmpty())
  523. +       {
  524. +           html.replace("%coupons%", "<tr><td>You havent any coupons available.</td></tr>");
  525. +           html.replace("%pages%", "");
  526. +           activeChar.sendPacket(html);
  527. +           return;
  528. +       }
  529. +      
  530. +       final int max = MathUtil.countPagesNumber(coupons.size(), 15);
  531. +      
  532. +       coupons = coupons.subList((page - 1) * 15, Math.min(page * 15, coupons.size()));
  533. +      
  534. +       // Generate data.
  535. +       final StringBuilder sb = new StringBuilder(2000);
  536. +      
  537. +       for (Coupon cpn : coupons)
  538. +           StringUtil.append(sb, "<tr><td><a action=\"bypass cpn_list_", cpn.getId(), "\">", cpn.getName(), "</a></td><td width=35></td><td><a action=\"bypass cpn_redeem_", cpn.getId(), "_", page, "\"><FONT COLOR=\"LEVEL\">Redeem</FONT></a></td><td><a action=\"bypass cpn_del_", cpn.getId(), "_", page, "\">", "<FONT COLOR=\"FF9988\">Remove</font></a></td></tr>");
  539. +      
  540. +       html.replace("%coupons%", sb.toString());
  541. +      
  542. +       // Cleanup the sb.
  543. +       sb.setLength(0);
  544. +      
  545. +       // End of table, open a new table for pages system.
  546. +       for (int i = 0; i < max; i++)
  547. +       {
  548. +           final int pagenr = i + 1;
  549. +           if (page == pagenr)
  550. +               StringUtil.append(sb, pagenr, "&nbsp;");
  551. +           else
  552. +               StringUtil.append(sb, "<a action=\"bypass list_", pagenr, "\">", pagenr, "</a>&nbsp;");
  553. +       }
  554. +      
  555. +       html.replace("%pages%", sb.toString());
  556. +       activeChar.sendPacket(html);
  557. +   }
  558. +  
  559. +   private static void showCategoryReward(Player activeChar, Coupon coupon, int page)
  560. +   {
  561. +      
  562. +       // Load static Htm.
  563. +       final NpcHtmlMessage html = new NpcHtmlMessage(0);
  564. +       html.setFile("data/html/custom/couponsRewards.htm");
  565. +      
  566. +       List<IntIntHolder> rewards = coupon.getRewards();
  567. +      
  568. +       final int max = MathUtil.countPagesNumber(rewards.size(), 15);
  569. +      
  570. +       rewards = rewards.subList((page - 1) * 15, Math.min(page * 15, rewards.size()));
  571. +      
  572. +       // Generate data.
  573. +       final StringBuilder sb = new StringBuilder();
  574. +      
  575. +       for (IntIntHolder reward : rewards)
  576. +       {
  577. +           String itemName = ItemTable.getInstance().getTemplate(reward.getId()).getName();
  578. +           final int multiplier = coupon.getGradeMultiplier();
  579. +           StringUtil.append(sb, "<tr><td><font color=\"LEVEL\">", itemName, "</font></td><td><font color=\"33cc33\">x", reward.getValue() * multiplier, "</font></td></tr>");
  580. +       }
  581. +      
  582. +       html.replace("%coupons%", sb.toString());
  583. +      
  584. +       // Cleanup the sb.
  585. +       sb.setLength(0);
  586. +      
  587. +       // End of table, open a new table for pages system.
  588. +       for (int i = 0; i < max; i++)
  589. +       {
  590. +           final int pagenr = i + 1;
  591. +           if (page == pagenr)
  592. +               StringUtil.append(sb, pagenr, "&nbsp;");
  593. +           else
  594. +               StringUtil.append(sb, "<a action=\"bypass cpn_page_", coupon.getId(), "_", pagenr, "\">", pagenr, "</a>&nbsp;");
  595. +       }
  596. +      
  597. +       html.replace("%pages%", sb.length() == 0 ? "" : sb.toString());
  598. +       html.replace("%couponName%", coupon.getName() + " Rewards!");
  599. +       activeChar.sendPacket(html);
  600. +   }
  601. +  
  602.     private static void playerHelp(Player activeChar, String path)
  603.     {
  604.         if (path.indexOf("..") != -1)
  605.  
  606. Index: java/net/sf/l2j/gameserver/handler/usercommandhandlers/Coupons.java
  607. ===================================================================
  608. --- java/net/sf/l2j/gameserver/handler/usercommandhandlers/Coupons.java (nonexistent)
  609. +++ java/net/sf/l2j/gameserver/handler/usercommandhandlers/Coupons.java (working copy)
  610. @@ -0,0 +1,77 @@
  611. +package net.sf.l2j.gameserver.handler.usercommandhandlers;
  612. +
  613. +import java.util.List;
  614. +
  615. +import net.sf.l2j.commons.lang.StringUtil;
  616. +import net.sf.l2j.commons.math.MathUtil;
  617. +
  618. +import net.sf.l2j.custom.Coupon;
  619. +import net.sf.l2j.gameserver.handler.IUserCommandHandler;
  620. +import net.sf.l2j.gameserver.model.actor.instance.Player;
  621. +import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  622. +
  623. +public class Coupons implements IUserCommandHandler
  624. +{
  625. +   private static final int[] COMMAND_IDS =
  626. +   {
  627. +       114
  628. +   };
  629. +  
  630. +   @Override
  631. +   public boolean useUserCommand(int id, Player activeChar)
  632. +   {
  633. +       showCoupons(activeChar, 1);
  634. +       return true;
  635. +   }
  636. +  
  637. +   @Override
  638. +   public int[] getUserCommandList()
  639. +   {
  640. +       return COMMAND_IDS;
  641. +   }
  642. +  
  643. +   private static void showCoupons(Player activeChar, int page)
  644. +   {
  645. +       List<Coupon> coupons = activeChar.getCoupons();
  646. +       // Load static Htm.
  647. +       final NpcHtmlMessage html = new NpcHtmlMessage(0);
  648. +       html.setFile("data/html/custom/couponsList.htm");
  649. +      
  650. +       if (coupons.isEmpty())
  651. +       {
  652. +           html.replace("%coupons%", "<tr><td>You havent any coupons available.</td></tr>");
  653. +           html.replace("%pages%", "");
  654. +           activeChar.sendPacket(html);
  655. +           return;
  656. +       }
  657. +      
  658. +       final int max = MathUtil.countPagesNumber(coupons.size(), 15);
  659. +      
  660. +       coupons = coupons.subList((page - 1) * 15, Math.min(page * 15, coupons.size()));
  661. +      
  662. +       // Generate data.
  663. +       final StringBuilder sb = new StringBuilder(2000);
  664. +      
  665. +       for (Coupon cpn : coupons)
  666. +           StringUtil.append(sb, "<tr><td><a action=\"bypass cpn_list_", cpn.getId(), "\">", cpn.getName(), "</a></td><td width=35></td><td><a action=\"bypass cpn_redeem_", cpn.getId(), "_", page, "\"><FONT COLOR=\"LEVEL\">Redeem</FONT></a></td><td><a action=\"bypass cpn_del_", cpn.getId(), "_", page, "\">", "<FONT COLOR=\"FF9988\">Remove</font></a></td></tr>");
  667. +      
  668. +       html.replace("%coupons%", sb.toString());
  669. +      
  670. +       // Cleanup the sb.
  671. +       sb.setLength(0);
  672. +      
  673. +       // End of table, open a new table for pages system.
  674. +       for (int i = 0; i < max; i++)
  675. +       {
  676. +           final int pagenr = i + 1;
  677. +           if (page == pagenr)
  678. +               StringUtil.append(sb, pagenr, "&nbsp;");
  679. +           else
  680. +               StringUtil.append(sb, "<a action=\"bypass list_", pagenr, "\">", pagenr, "</a>&nbsp;");
  681. +       }
  682. +      
  683. +       html.replace("%pages%", sb.toString());
  684. +       activeChar.sendPacket(html);
  685. +   }
  686. +  
  687. +}
  688. \ No newline at end of file
  689.  
  690. Index: java/net/sf/l2j/gameserver/GameServer.java
  691. ===================================================================
  692. --- java/net/sf/l2j/gameserver/GameServer.java  (revision 3)
  693. +++ java/net/sf/l2j/gameserver/GameServer.java  (working copy)
  694. @@ -18,6 +18,7 @@
  695.  
  696.  import net.sf.l2j.Config;
  697.  import net.sf.l2j.L2DatabaseFactory;
  698. +import net.sf.l2j.custom.CouponManager;
  699.  import net.sf.l2j.gameserver.cache.CrestCache;
  700.  import net.sf.l2j.gameserver.cache.HtmCache;
  701.  import net.sf.l2j.gameserver.communitybbs.Manager.ForumsBBSManager;
  702. @@ -94,6 +95,7 @@
  703.  import net.sf.l2j.gameserver.network.L2GamePacketHandler;
  704.  import net.sf.l2j.gameserver.scripting.ScriptManager;
  705.  import net.sf.l2j.gameserver.taskmanager.AttackStanceTaskManager;
  706. +import net.sf.l2j.gameserver.taskmanager.CouponsTaskManager;
  707.  import net.sf.l2j.gameserver.taskmanager.DecayTaskManager;
  708.  import net.sf.l2j.gameserver.taskmanager.GameTimeTaskManager;
  709.  import net.sf.l2j.gameserver.taskmanager.ItemsOnGroundTaskManager;
  710. @@ -216,6 +218,7 @@
  711.         ItemsOnGroundTaskManager.getInstance();
  712.         MovementTaskManager.getInstance();
  713.         PvpFlagTaskManager.getInstance();
  714. +       CouponsTaskManager.getInstance();
  715.         RandomAnimationTaskManager.getInstance();
  716.         ShadowItemTaskManager.getInstance();
  717.         WaterTaskManager.getInstance();
  718. @@ -281,6 +284,10 @@
  719.         _log.config("SkillHandler: Loaded " + SkillHandler.getInstance().size() + " handlers.");
  720.         _log.config("UserCommandHandler: Loaded " + UserCommandHandler.getInstance().size() + " handlers.");
  721.        
  722. +       StringUtil.printSection("Coupons");
  723. +       CouponManager.loadCoupons();
  724. +       _log.info("Loadded " + CouponManager.getCouponsSize() + " Coupons.");
  725. +      
  726.         StringUtil.printSection("System");
  727.         Runtime.getRuntime().addShutdownHook(Shutdown.getInstance());
  728.         ForumsBBSManager.getInstance();
  729. Index: java/net/sf/l2j/gameserver/handler/UserCommandHandler.java
  730. ===================================================================
  731. --- java/net/sf/l2j/gameserver/handler/UserCommandHandler.java  (revision 3)
  732. +++ java/net/sf/l2j/gameserver/handler/UserCommandHandler.java  (working copy)
  733. @@ -16,6 +16,7 @@
  734.  import net.sf.l2j.gameserver.handler.usercommandhandlers.PartyInfo;
  735.  import net.sf.l2j.gameserver.handler.usercommandhandlers.SiegeStatus;
  736.  import net.sf.l2j.gameserver.handler.usercommandhandlers.Time;
  737. +import net.sf.l2j.gameserver.handler.usercommandhandlers.Coupons;
  738.  
  739.  public class UserCommandHandler
  740.  {
  741. @@ -41,6 +42,7 @@
  742.         registerUserCommandHandler(new PartyInfo());
  743.         registerUserCommandHandler(new SiegeStatus());
  744.         registerUserCommandHandler(new Time());
  745. +       registerUserCommandHandler(new Coupons());
  746.     }
  747.    
  748.     public void registerUserCommandHandler(IUserCommandHandler handler)
  749. Index: java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminAdmin.java
  750. ===================================================================
  751. --- java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminAdmin.java (revision 3)
  752. +++ java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminAdmin.java (working copy)
  753. @@ -5,6 +5,8 @@
  754.  import net.sf.l2j.commons.lang.StringUtil;
  755.  
  756.  import net.sf.l2j.Config;
  757. +import net.sf.l2j.custom.Coupon;
  758. +import net.sf.l2j.custom.CouponManager;
  759.  import net.sf.l2j.gameserver.cache.CrestCache;
  760.  import net.sf.l2j.gameserver.cache.HtmCache;
  761.  import net.sf.l2j.gameserver.data.DoorTable;
  762. @@ -24,6 +26,7 @@
  763.  import net.sf.l2j.gameserver.model.actor.Creature;
  764.  import net.sf.l2j.gameserver.model.actor.instance.Player;
  765.  import net.sf.l2j.gameserver.network.SystemMessageId;
  766. +import net.sf.l2j.gameserver.network.serverpackets.ExShowScreenMessage;
  767.  
  768.  /**
  769.   * This class handles following admin commands:
  770. @@ -50,7 +53,8 @@
  771.         "admin_kill",
  772.         "admin_silence",
  773.         "admin_tradeoff",
  774. -       "admin_reload"
  775. +       "admin_reload",
  776. +       "admin_setcoupon"
  777.     };
  778.    
  779.     @Override
  780. @@ -58,10 +62,60 @@
  781.     {
  782.         if (command.startsWith("admin_admin"))
  783.             showMainPage(activeChar, command);
  784. +       else if (command.startsWith("admin_setcoupon"))
  785. +       {
  786. +           final String SILVER = "silver";
  787. +           final String GOLD = "gold";
  788. +           final String PLATINUM = "platinum";
  789. +          
  790. +           StringTokenizer st = new StringTokenizer(command, " ");
  791. +           st.nextToken();
  792. +           if (!st.hasMoreTokens())
  793. +           {
  794. +               activeChar.sendMessage("//setcoupon {category} (categories : silver/gold/platinum)");
  795. +               return false;
  796. +           }
  797. +           String category = st.nextToken();
  798. +           if (!category.equalsIgnoreCase(SILVER) && !category.equalsIgnoreCase(GOLD) && !category.equalsIgnoreCase(PLATINUM))
  799. +           {
  800. +               activeChar.sendMessage("//setcoupon {category} (categories : silver/gold/platinum)");
  801. +               return false;
  802. +           }
  803. +           if (activeChar.getTarget() == null)
  804. +           {
  805. +               activeChar.sendMessage("select a target first");
  806. +               return false;
  807. +           }
  808. +           Player target = null;
  809. +          
  810. +           if (activeChar.getTarget() instanceof Player)
  811. +               target = (Player) activeChar.getTarget();
  812. +          
  813. +           if (target != null)
  814. +           {
  815. +               Coupon coupon = CouponManager.generateCoupon(category.toUpperCase(), target);
  816. +               if (coupon != null)
  817. +               {
  818. +                   String exMessage = "New Coupon is now available! " + coupon.getName() + " is now activated!";
  819. +                   target.sendPacket(new ExShowScreenMessage(1, 0, 2, false, 1, 0, 0, false, 4000, false, exMessage));
  820. +               }
  821. +           }
  822. +           else
  823. +           {
  824. +               activeChar.sendMessage("Player instance allowed");
  825. +               return false;
  826. +           }
  827. +          
  828. +       }
  829.         else if (command.startsWith("admin_gmlist"))
  830.             activeChar.sendMessage((AdminData.getInstance().showOrHideGm(activeChar)) ? "Removed from GMList." : "Registered into GMList.");
  831.  
  832. Index: java/net/sf/l2j/gameserver/model/actor/instance/Player.java
  833. ===================================================================
  834. --- java/net/sf/l2j/gameserver/model/actor/instance/Player.java (revision 3)
  835. +++ java/net/sf/l2j/gameserver/model/actor/instance/Player.java (working copy)
  836. @@ -27,6 +27,8 @@
  837.  
  838.  import net.sf.l2j.Config;
  839.  import net.sf.l2j.L2DatabaseFactory;
  840. +import net.sf.l2j.custom.Coupon;
  841. +import net.sf.l2j.custom.CouponManager;
  842.  import net.sf.l2j.gameserver.LoginServerThread;
  843.  import net.sf.l2j.gameserver.communitybbs.BB.Forum;
  844.  import net.sf.l2j.gameserver.communitybbs.Manager.ForumsBBSManager;
  845. @@ -47,6 +49,7 @@
  846.  import net.sf.l2j.gameserver.handler.IItemHandler;
  847.  import net.sf.l2j.gameserver.handler.ItemHandler;
  848.  import net.sf.l2j.gameserver.handler.admincommandhandlers.AdminEditChar;
  849. +import net.sf.l2j.gameserver.idfactory.IdFactory;
  850.  import net.sf.l2j.gameserver.instancemanager.CastleManager;
  851.  import net.sf.l2j.gameserver.instancemanager.CoupleManager;
  852.  import net.sf.l2j.gameserver.instancemanager.CursedWeaponsManager;
  853. @@ -309,8 +312,8 @@
  854.     private static final String DELETE_SKILL_SAVE = "DELETE FROM character_skills_save WHERE char_obj_id=? AND class_index=?";
  855.    
  856.     private static final String INSERT_CHARACTER = "INSERT INTO characters (account_name,obj_Id,char_name,level,maxHp,curHp,maxCp,curCp,maxMp,curMp,face,hairStyle,hairColor,sex,exp,sp,karma,pvpkills,pkkills,clanid,race,classid,deletetime,cancraft,title,accesslevel,online,isin7sdungeon,clan_privs,wantspeace,base_class,nobless,power_grade,last_recom_date) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
  857. -   private static final String UPDATE_CHARACTER = "UPDATE characters SET level=?,maxHp=?,curHp=?,maxCp=?,curCp=?,maxMp=?,curMp=?,face=?,hairStyle=?,hairColor=?,sex=?,heading=?,x=?,y=?,z=?,exp=?,expBeforeDeath=?,sp=?,karma=?,pvpkills=?,pkkills=?,rec_have=?,rec_left=?,clanid=?,race=?,classid=?,deletetime=?,title=?,accesslevel=?,online=?,isin7sdungeon=?,clan_privs=?,wantspeace=?,base_class=?,onlinetime=?,punish_level=?,punish_timer=?,nobless=?,power_grade=?,subpledge=?,last_recom_date=?,lvl_joined_academy=?,apprentice=?,sponsor=?,varka_ketra_ally=?,clan_join_expiry_time=?,clan_create_expiry_time=?,char_name=?,death_penalty_level=? WHERE obj_id=?";
  858. -   private static final String RESTORE_CHARACTER = "SELECT account_name, obj_Id, char_name, level, maxHp, curHp, maxCp, curCp, maxMp, curMp, face, hairStyle, hairColor, sex, heading, x, y, z, exp, expBeforeDeath, sp, karma, pvpkills, pkkills, clanid, race, classid, deletetime, cancraft, title, rec_have, rec_left, accesslevel, online, char_slot, lastAccess, clan_privs, wantspeace, base_class, onlinetime, isin7sdungeon, punish_level, punish_timer, nobless, power_grade, subpledge, last_recom_date, lvl_joined_academy, apprentice, sponsor, varka_ketra_ally,clan_join_expiry_time,clan_create_expiry_time,death_penalty_level FROM characters WHERE obj_id=?";
  859. +   private static final String UPDATE_CHARACTER = "UPDATE characters SET level=?,maxHp=?,curHp=?,maxCp=?,curCp=?,maxMp=?,curMp=?,face=?,hairStyle=?,hairColor=?,sex=?,heading=?,x=?,y=?,z=?,exp=?,expBeforeDeath=?,sp=?,karma=?,pvpkills=?,pkkills=?,rec_have=?,rec_left=?,clanid=?,race=?,classid=?,deletetime=?,title=?,accesslevel=?,online=?,isin7sdungeon=?,clan_privs=?,wantspeace=?,base_class=?,onlinetime=?,punish_level=?,punish_timer=?,nobless=?,power_grade=?,subpledge=?,last_recom_date=?,lvl_joined_academy=?,apprentice=?,sponsor=?,varka_ketra_ally=?,clan_join_expiry_time=?,clan_create_expiry_time=?,char_name=?,death_penalty_level=?,couponsused=? WHERE obj_id=?";
  860. +   private static final String RESTORE_CHARACTER = "SELECT account_name, obj_Id, char_name, level, maxHp, curHp, maxCp, curCp, maxMp, curMp, face, hairStyle, hairColor, sex, heading, x, y, z, exp, expBeforeDeath, sp, karma, pvpkills, pkkills, clanid, race, classid, deletetime, cancraft, title, rec_have, rec_left, accesslevel, online, char_slot, lastAccess, clan_privs, wantspeace, base_class, onlinetime, isin7sdungeon, punish_level, punish_timer, nobless, power_grade, subpledge, last_recom_date, lvl_joined_academy, apprentice, sponsor, varka_ketra_ally,clan_join_expiry_time,clan_create_expiry_time,death_penalty_level,couponsused FROM characters WHERE obj_id=?";
  861.    
  862.     private static final String RESTORE_CHAR_SUBCLASSES = "SELECT class_id,exp,sp,level,class_index FROM character_subclasses WHERE char_obj_id=? ORDER BY class_index ASC";
  863.     private static final String ADD_CHAR_SUBCLASS = "INSERT INTO character_subclasses (char_obj_id,class_id,exp,sp,level,class_index) VALUES (?,?,?,?,?,?)";
  864. @@ -5386,6 +5389,8 @@
  865.                
  866.                 player.setDeathPenaltyBuffLevel(rset.getInt("death_penalty_level"));
  867.                
  868. +               player.setCouponsUsed(rset.getInt("couponsused"));
  869. +              
  870.                 // Set the x,y,z position of the Player and make it invisible
  871.                 player.getPosition().set(rset.getInt("x"), rset.getInt("y"), rset.getInt("z"));
  872.                
  873. @@ -5727,7 +5732,8 @@
  874.             statement.setLong(47, getClanCreateExpiryTime());
  875.             statement.setString(48, getName());
  876.             statement.setLong(49, getDeathPenaltyBuffLevel());
  877. -           statement.setInt(50, getObjectId());
  878. +           statement.setInt(50, getCouponsUsed());
  879. +           statement.setInt(51, getObjectId());
  880.            
  881.             statement.execute();
  882.             statement.close();
  883. @@ -10536,4 +10542,108 @@
  884.             }
  885.         }
  886.     }
  887. +  
  888. +   private List<Coupon> coupons = new ArrayList<>();
  889. +  
  890. +   public void setCoupon(Coupon _coupon)
  891. +   {
  892. +       if (_coupon == null)
  893. +           return;
  894. +       if (!coupons.contains(_coupon))
  895. +           coupons.add(_coupon);
  896. +   }
  897. +  
  898. +   public List<Coupon> getCoupons()
  899. +   {
  900. +       return coupons;
  901. +   }
  902. +  
  903. +   /**
  904. +    * @param couponName
  905. +    * @return
  906. +    */
  907. +   public Coupon getCouponsByName(String couponName)
  908. +   {
  909. +       for (Coupon cpn : coupons)
  910. +       {
  911. +           if (cpn.getName().equalsIgnoreCase(couponName))
  912. +               return cpn;
  913. +       }
  914. +       return null;
  915. +   }
  916. +  
  917. +   public Coupon getCouponById(int couponId)
  918. +   {
  919. +       for (Coupon cpn : coupons)
  920. +       {
  921. +           if (cpn.getId() == couponId)
  922. +               return cpn;
  923. +       }
  924. +       return null;
  925. +   }
  926. +  
  927. +   /**
  928. +    * @param coupon
  929. +    */
  930. +   public void removeCoupon(Coupon coupon)
  931. +   {
  932. +       if (coupon == null)
  933. +           return;
  934. +      
  935. +       if (coupons.remove(coupon))
  936. +       {
  937. +           IdFactory.getInstance().releaseId(coupon.getId());
  938. +           CouponManager.deleteCoupon(coupon);
  939. +       }
  940. +       else
  941. +           System.out.println("Cannot remove coupon from character " + this.getName());
  942. +   }
  943. +  
  944. +   private int couponsUsed = 0;
  945. +  
  946. +   public int getCouponsUsed()
  947. +   {
  948. +       return couponsUsed;
  949. +   }
  950. +  
  951. +   public boolean canUseCoupon()
  952. +   {
  953. +       return Config.COUPONS_LIMIT_PER_DAY == 0 ? true : couponsUsed < Config.COUPONS_LIMIT_PER_DAY;
  954. +   }
  955. +  
  956. +   public void increaseCouponsUsed()
  957. +   {
  958. +       if (Config.COUPONS_LIMIT_PER_DAY > 0)
  959. +       {
  960. +           couponsUsed++;
  961. +           storeCouponUsed();
  962. +       }
  963. +   }
  964. +  
  965. +   public void resetCouponsUsed()
  966. +   {
  967. +       couponsUsed = 0;
  968. +       sendMessage("Your coupons limit are now over.");
  969. +   }
  970. +  
  971. +   public void setCouponsUsed(int val)
  972. +   {
  973. +       couponsUsed = val;
  974. +   }
  975. +  
  976. +   public void storeCouponUsed()
  977. +   {
  978. +       try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  979. +       {
  980. +           PreparedStatement statement = con.prepareStatement("UPDATE characters SET couponsused=? WHERE obj_id=?");
  981. +           statement.setInt(1, getCouponsUsed());
  982. +           statement.setInt(2, getObjectId());
  983. +           statement.execute();
  984. +           statement.close();
  985. +       }
  986. +       catch (Exception e)
  987. +       {
  988. +           _log.warning("Error could not store char skills: " + e);
  989. +       }
  990. +   }
  991.  }
  992. \ No newline at end of file
  993. Index: java/net/sf/l2j/gameserver/network/clientpackets/EnterWorld.java
  994. ===================================================================
  995. --- java/net/sf/l2j/gameserver/network/clientpackets/EnterWorld.java    (revision 3)
  996. +++ java/net/sf/l2j/gameserver/network/clientpackets/EnterWorld.java    (working copy)
  997. @@ -8,6 +8,7 @@
  998.  
  999.  import net.sf.l2j.Config;
  1000.  import net.sf.l2j.L2DatabaseFactory;
  1001. +import net.sf.l2j.custom.CouponManager;
  1002.  import net.sf.l2j.gameserver.communitybbs.Manager.MailBBSManager;
  1003.  import net.sf.l2j.gameserver.data.MapRegionTable.TeleportType;
  1004.  import net.sf.l2j.gameserver.data.SkillTable.FrequentSkill;
  1005. @@ -323,6 +324,7 @@
  1006.         if (!activeChar.isGM() && (!activeChar.isInSiege() || activeChar.getSiegeState() < 2) && activeChar.isInsideZone(ZoneId.SIEGE))
  1007.             activeChar.teleToLocation(TeleportType.TOWN);
  1008.        
  1009. +       CouponManager.setCoupons(activeChar);
  1010.         activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  1011.     }
  1012.    
  1013. Index: java/net/sf/l2j/gameserver/taskmanager/CouponsTaskManager.java
  1014. ===================================================================
  1015. --- java/net/sf/l2j/gameserver/taskmanager/CouponsTaskManager.java  (nonexistent)
  1016. +++ java/net/sf/l2j/gameserver/taskmanager/CouponsTaskManager.java  (working copy)
  1017. @@ -0,0 +1,83 @@
  1018. +package net.sf.l2j.gameserver.taskmanager;
  1019. +
  1020. +import java.sql.Connection;
  1021. +import java.sql.PreparedStatement;
  1022. +import java.text.SimpleDateFormat;
  1023. +import java.util.Calendar;
  1024. +import java.util.logging.Logger;
  1025. +
  1026. +import net.sf.l2j.commons.concurrent.ThreadPool;
  1027. +
  1028. +import net.sf.l2j.Config;
  1029. +import net.sf.l2j.L2DatabaseFactory;
  1030. +import net.sf.l2j.gameserver.model.World;
  1031. +import net.sf.l2j.gameserver.model.actor.instance.Player;
  1032. +
  1033. +/**
  1034. + * @author Melron
  1035. + */
  1036. +public final class CouponsTaskManager implements Runnable
  1037. +{
  1038. +   private static final int CalendarHour = Config.COUPONS_RESET_LIMIT_AT[0];
  1039. +   private static final int CalendarMinute = Config.COUPONS_RESET_LIMIT_AT[1];
  1040. +   private static final int CalendarSecond = 0;
  1041. +   private static final long Delay = 86400000; // 1 day
  1042. +   private static final SimpleDateFormat sdf = new SimpleDateFormat("yyy MMM dd (EEEE) HH:mm:ss");
  1043. +   private static final Logger _log = Logger.getLogger(CouponsTaskManager.class.getName());
  1044. +  
  1045. +   public static final CouponsTaskManager getInstance()
  1046. +   {
  1047. +       return SingletonHolder._instance;
  1048. +   }
  1049. +  
  1050. +   protected CouponsTaskManager()
  1051. +   {
  1052. +       ThreadPool.scheduleAtFixedRate(this, getTime(), Delay);
  1053. +       _log.info("Next lift: " + sdf.format(System.currentTimeMillis() + getTime()));
  1054. +   }
  1055. +  
  1056. +   /**
  1057. +    * @return
  1058. +    */
  1059. +   private static long getTime()
  1060. +   {
  1061. +       Calendar calendar = Calendar.getInstance();
  1062. +       calendar.set(Calendar.HOUR_OF_DAY, CalendarHour);
  1063. +       calendar.set(Calendar.MINUTE, CalendarMinute);
  1064. +       calendar.set(Calendar.SECOND, CalendarSecond);
  1065. +      
  1066. +       if (calendar.getTimeInMillis() <= System.currentTimeMillis())
  1067. +           calendar.add(Calendar.DATE, 1);
  1068. +      
  1069. +       return calendar.getTimeInMillis() - System.currentTimeMillis();
  1070. +   }
  1071. +  
  1072. +   @Override
  1073. +   public final void run()
  1074. +   {
  1075. +       if (Config.COUPONS_LIMIT_PER_DAY > 0)
  1076. +       {
  1077. +           for (Player activeChar : World.getInstance().getPlayers())
  1078. +               if (activeChar != null)
  1079. +                   activeChar.resetCouponsUsed();
  1080. +              
  1081. +           try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  1082. +           {
  1083. +               PreparedStatement statement = con.prepareStatement("UPDATE characters SET couponsused=?");
  1084. +               statement.setInt(1, 0);
  1085. +               statement.execute();
  1086. +               statement.close();
  1087. +           }
  1088. +           catch (Exception e)
  1089. +           {
  1090. +               _log.warning("Error could not reset coupon used count: " + e);
  1091. +           }
  1092. +           _log.info("All coupons limits are lifted. Next lift: " + sdf.format(System.currentTimeMillis() + Delay));
  1093. +       }
  1094. +   }
  1095. +  
  1096. +   private static class SingletonHolder
  1097. +   {
  1098. +       protected static final CouponsTaskManager _instance = new CouponsTaskManager();
  1099. +   }
  1100. +}
  1101. \ No newline at end of file
Advertisement
Add Comment
Please, Sign In to add comment