Lottery.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager.games;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.Calendar;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  30. import com.l2jserver.gameserver.ThreadPoolManager;
  31. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  32. import com.l2jserver.gameserver.network.SystemMessageId;
  33. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  34. import com.l2jserver.gameserver.util.Broadcast;
  35. import com.l2jserver.util.Rnd;
  36. public class Lottery
  37. {
  38. public static final long SECOND = 1000;
  39. public static final long MINUTE = 60000;
  40. protected static final Logger _log = Logger.getLogger(Lottery.class.getName());
  41. private static final String INSERT_LOTTERY = "INSERT INTO games(id, idnr, enddate, prize, newprize) VALUES (?, ?, ?, ?, ?)";
  42. private static final String UPDATE_PRICE = "UPDATE games SET prize=?, newprize=? WHERE id = 1 AND idnr = ?";
  43. private static final String UPDATE_LOTTERY = "UPDATE games SET finished=1, prize=?, newprize=?, number1=?, number2=?, prize1=?, prize2=?, prize3=? WHERE id=1 AND idnr=?";
  44. private static final String SELECT_LAST_LOTTERY = "SELECT idnr, prize, newprize, enddate, finished FROM games WHERE id = 1 ORDER BY idnr DESC LIMIT 1";
  45. private static final String SELECT_LOTTERY_ITEM = "SELECT enchant_level, custom_type2 FROM items WHERE item_id = 4442 AND custom_type1 = ?";
  46. private static final String SELECT_LOTTERY_TICKET = "SELECT number1, number2, prize1, prize2, prize3 FROM games WHERE id = 1 and idnr = ?";
  47. protected int _number;
  48. protected long _prize;
  49. protected boolean _isSellingTickets;
  50. protected boolean _isStarted;
  51. protected long _enddate;
  52. protected Lottery()
  53. {
  54. _number = 1;
  55. _prize = Config.ALT_LOTTERY_PRIZE;
  56. _isSellingTickets = false;
  57. _isStarted = false;
  58. _enddate = System.currentTimeMillis();
  59. if (Config.ALLOW_LOTTERY)
  60. {
  61. (new startLottery()).run();
  62. }
  63. }
  64. public static Lottery getInstance()
  65. {
  66. return SingletonHolder._instance;
  67. }
  68. public int getId()
  69. {
  70. return _number;
  71. }
  72. public long getPrize()
  73. {
  74. return _prize;
  75. }
  76. public long getEndDate()
  77. {
  78. return _enddate;
  79. }
  80. public void increasePrize(long count)
  81. {
  82. _prize += count;
  83. try (Connection con = ConnectionFactory.getInstance().getConnection();
  84. PreparedStatement ps = con.prepareStatement(UPDATE_PRICE))
  85. {
  86. ps.setLong(1, getPrize());
  87. ps.setLong(2, getPrize());
  88. ps.setInt(3, getId());
  89. ps.execute();
  90. }
  91. catch (SQLException e)
  92. {
  93. _log.log(Level.WARNING, "Lottery: Could not increase current lottery prize: " + e.getMessage(), e);
  94. }
  95. }
  96. public boolean isSellableTickets()
  97. {
  98. return _isSellingTickets;
  99. }
  100. public boolean isStarted()
  101. {
  102. return _isStarted;
  103. }
  104. private class startLottery implements Runnable
  105. {
  106. protected startLottery()
  107. {
  108. // Do nothing
  109. }
  110. @Override
  111. public void run()
  112. {
  113. try (Connection con = ConnectionFactory.getInstance().getConnection();
  114. Statement statement = con.createStatement();
  115. ResultSet rset = statement.executeQuery(SELECT_LAST_LOTTERY))
  116. {
  117. if (rset.next())
  118. {
  119. _number = rset.getInt("idnr");
  120. if (rset.getInt("finished") == 1)
  121. {
  122. _number++;
  123. _prize = rset.getLong("newprize");
  124. }
  125. else
  126. {
  127. _prize = rset.getLong("prize");
  128. _enddate = rset.getLong("enddate");
  129. if (_enddate <= (System.currentTimeMillis() + (2 * MINUTE)))
  130. {
  131. (new finishLottery()).run();
  132. return;
  133. }
  134. if (_enddate > System.currentTimeMillis())
  135. {
  136. _isStarted = true;
  137. ThreadPoolManager.getInstance().scheduleGeneral(new finishLottery(), _enddate - System.currentTimeMillis());
  138. if (_enddate > (System.currentTimeMillis() + (12 * MINUTE)))
  139. {
  140. _isSellingTickets = true;
  141. ThreadPoolManager.getInstance().scheduleGeneral(new stopSellingTickets(), _enddate - System.currentTimeMillis() - (10 * MINUTE));
  142. }
  143. return;
  144. }
  145. }
  146. }
  147. }
  148. catch (SQLException e)
  149. {
  150. _log.log(Level.WARNING, "Lottery: Could not restore lottery data: " + e.getMessage(), e);
  151. }
  152. if (Config.DEBUG)
  153. {
  154. _log.info("Lottery: Starting ticket sell for lottery #" + getId() + ".");
  155. }
  156. _isSellingTickets = true;
  157. _isStarted = true;
  158. Broadcast.toAllOnlinePlayers("Lottery tickets are now available for Lucky Lottery #" + getId() + ".");
  159. Calendar finishtime = Calendar.getInstance();
  160. finishtime.setTimeInMillis(_enddate);
  161. finishtime.set(Calendar.MINUTE, 0);
  162. finishtime.set(Calendar.SECOND, 0);
  163. if (finishtime.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
  164. {
  165. finishtime.set(Calendar.HOUR_OF_DAY, 19);
  166. _enddate = finishtime.getTimeInMillis();
  167. _enddate += 604800000;
  168. }
  169. else
  170. {
  171. finishtime.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  172. finishtime.set(Calendar.HOUR_OF_DAY, 19);
  173. _enddate = finishtime.getTimeInMillis();
  174. }
  175. ThreadPoolManager.getInstance().scheduleGeneral(new stopSellingTickets(), _enddate - System.currentTimeMillis() - (10 * MINUTE));
  176. ThreadPoolManager.getInstance().scheduleGeneral(new finishLottery(), _enddate - System.currentTimeMillis());
  177. try (Connection con = ConnectionFactory.getInstance().getConnection();
  178. PreparedStatement ps = con.prepareStatement(INSERT_LOTTERY))
  179. {
  180. ps.setInt(1, 1);
  181. ps.setInt(2, getId());
  182. ps.setLong(3, getEndDate());
  183. ps.setLong(4, getPrize());
  184. ps.setLong(5, getPrize());
  185. ps.execute();
  186. }
  187. catch (SQLException e)
  188. {
  189. _log.log(Level.WARNING, "Lottery: Could not store new lottery data: " + e.getMessage(), e);
  190. }
  191. }
  192. }
  193. private class stopSellingTickets implements Runnable
  194. {
  195. protected stopSellingTickets()
  196. {
  197. // Do nothing
  198. }
  199. @Override
  200. public void run()
  201. {
  202. if (Config.DEBUG)
  203. {
  204. _log.info("Lottery: Stopping ticket sell for lottery #" + getId() + ".");
  205. }
  206. _isSellingTickets = false;
  207. Broadcast.toAllOnlinePlayers(SystemMessage.getSystemMessage(SystemMessageId.LOTTERY_TICKET_SALES_TEMP_SUSPENDED));
  208. }
  209. }
  210. private class finishLottery implements Runnable
  211. {
  212. protected finishLottery()
  213. {
  214. // Do nothing
  215. }
  216. @Override
  217. public void run()
  218. {
  219. if (Config.DEBUG)
  220. {
  221. _log.info("Lottery: Ending lottery #" + getId() + ".");
  222. }
  223. int[] luckynums = new int[5];
  224. int luckynum = 0;
  225. for (int i = 0; i < 5; i++)
  226. {
  227. boolean found = true;
  228. while (found)
  229. {
  230. luckynum = Rnd.get(20) + 1;
  231. found = false;
  232. for (int j = 0; j < i; j++)
  233. {
  234. if (luckynums[j] == luckynum)
  235. {
  236. found = true;
  237. }
  238. }
  239. }
  240. luckynums[i] = luckynum;
  241. }
  242. if (Config.DEBUG)
  243. {
  244. _log.info("Lottery: The lucky numbers are " + luckynums[0] + ", " + luckynums[1] + ", " + luckynums[2] + ", " + luckynums[3] + ", " + luckynums[4] + ".");
  245. }
  246. int enchant = 0;
  247. int type2 = 0;
  248. for (int i = 0; i < 5; i++)
  249. {
  250. if (luckynums[i] < 17)
  251. {
  252. enchant += Math.pow(2, luckynums[i] - 1);
  253. }
  254. else
  255. {
  256. type2 += Math.pow(2, luckynums[i] - 17);
  257. }
  258. }
  259. if (Config.DEBUG)
  260. {
  261. _log.info("Lottery: Encoded lucky numbers are " + enchant + ", " + type2);
  262. }
  263. int count1 = 0;
  264. int count2 = 0;
  265. int count3 = 0;
  266. int count4 = 0;
  267. try (Connection con = ConnectionFactory.getInstance().getConnection();
  268. PreparedStatement ps = con.prepareStatement(SELECT_LOTTERY_ITEM))
  269. {
  270. ps.setInt(1, getId());
  271. try (ResultSet rset = ps.executeQuery())
  272. {
  273. while (rset.next())
  274. {
  275. int curenchant = rset.getInt("enchant_level") & enchant;
  276. int curtype2 = rset.getInt("custom_type2") & type2;
  277. if ((curenchant == 0) && (curtype2 == 0))
  278. {
  279. continue;
  280. }
  281. int count = 0;
  282. for (int i = 1; i <= 16; i++)
  283. {
  284. int val = curenchant / 2;
  285. if (val != Math.round((double) curenchant / 2))
  286. {
  287. count++;
  288. }
  289. int val2 = curtype2 / 2;
  290. if (val2 != ((double) curtype2 / 2))
  291. {
  292. count++;
  293. }
  294. curenchant = val;
  295. curtype2 = val2;
  296. }
  297. if (count == 5)
  298. {
  299. count1++;
  300. }
  301. else if (count == 4)
  302. {
  303. count2++;
  304. }
  305. else if (count == 3)
  306. {
  307. count3++;
  308. }
  309. else if (count > 0)
  310. {
  311. count4++;
  312. }
  313. }
  314. }
  315. }
  316. catch (SQLException e)
  317. {
  318. _log.log(Level.WARNING, "Lottery: Could restore lottery data: " + e.getMessage(), e);
  319. }
  320. long prize4 = count4 * Config.ALT_LOTTERY_2_AND_1_NUMBER_PRIZE;
  321. long prize1 = 0;
  322. long prize2 = 0;
  323. long prize3 = 0;
  324. if (count1 > 0)
  325. {
  326. prize1 = (long) (((getPrize() - prize4) * Config.ALT_LOTTERY_5_NUMBER_RATE) / count1);
  327. }
  328. if (count2 > 0)
  329. {
  330. prize2 = (long) (((getPrize() - prize4) * Config.ALT_LOTTERY_4_NUMBER_RATE) / count2);
  331. }
  332. if (count3 > 0)
  333. {
  334. prize3 = (long) (((getPrize() - prize4) * Config.ALT_LOTTERY_3_NUMBER_RATE) / count3);
  335. }
  336. if (Config.DEBUG)
  337. {
  338. _log.info("Lottery: " + count1 + " players with all FIVE numbers each win " + prize1 + ".");
  339. _log.info("Lottery: " + count2 + " players with FOUR numbers each win " + prize2 + ".");
  340. _log.info("Lottery: " + count3 + " players with THREE numbers each win " + prize3 + ".");
  341. _log.info("Lottery: " + count4 + " players with ONE or TWO numbers each win " + prize4 + ".");
  342. }
  343. long newprize = getPrize() - (prize1 + prize2 + prize3 + prize4);
  344. if (Config.DEBUG)
  345. {
  346. _log.info("Lottery: Jackpot for next lottery is " + newprize + ".");
  347. }
  348. SystemMessage sm;
  349. if (count1 > 0)
  350. {
  351. // There are winners.
  352. sm = SystemMessage.getSystemMessage(SystemMessageId.AMOUNT_FOR_WINNER_S1_IS_S2_ADENA_WE_HAVE_S3_PRIZE_WINNER);
  353. sm.addInt(getId());
  354. sm.addLong(getPrize());
  355. sm.addLong(count1);
  356. Broadcast.toAllOnlinePlayers(sm);
  357. }
  358. else
  359. {
  360. // There are no winners.
  361. sm = SystemMessage.getSystemMessage(SystemMessageId.AMOUNT_FOR_LOTTERY_S1_IS_S2_ADENA_NO_WINNER);
  362. sm.addInt(getId());
  363. sm.addLong(getPrize());
  364. Broadcast.toAllOnlinePlayers(sm);
  365. }
  366. try (Connection con = ConnectionFactory.getInstance().getConnection();
  367. PreparedStatement ps = con.prepareStatement(UPDATE_LOTTERY))
  368. {
  369. ps.setLong(1, getPrize());
  370. ps.setLong(2, newprize);
  371. ps.setInt(3, enchant);
  372. ps.setInt(4, type2);
  373. ps.setLong(5, prize1);
  374. ps.setLong(6, prize2);
  375. ps.setLong(7, prize3);
  376. ps.setInt(8, getId());
  377. ps.execute();
  378. }
  379. catch (SQLException e)
  380. {
  381. _log.log(Level.WARNING, "Lottery: Could not store finished lottery data: " + e.getMessage(), e);
  382. }
  383. ThreadPoolManager.getInstance().scheduleGeneral(new startLottery(), MINUTE);
  384. _number++;
  385. _isStarted = false;
  386. }
  387. }
  388. public int[] decodeNumbers(int enchant, int type2)
  389. {
  390. int res[] = new int[5];
  391. int id = 0;
  392. int nr = 1;
  393. while (enchant > 0)
  394. {
  395. int val = enchant / 2;
  396. if (val != Math.round((double) enchant / 2))
  397. {
  398. res[id++] = nr;
  399. }
  400. enchant /= 2;
  401. nr++;
  402. }
  403. nr = 17;
  404. while (type2 > 0)
  405. {
  406. int val = type2 / 2;
  407. if (val != ((double) type2 / 2))
  408. {
  409. res[id++] = nr;
  410. }
  411. type2 /= 2;
  412. nr++;
  413. }
  414. return res;
  415. }
  416. public long[] checkTicket(L2ItemInstance item)
  417. {
  418. return checkTicket(item.getCustomType1(), item.getEnchantLevel(), item.getCustomType2());
  419. }
  420. public long[] checkTicket(int id, int enchant, int type2)
  421. {
  422. long res[] =
  423. {
  424. 0,
  425. 0
  426. };
  427. try (Connection con = ConnectionFactory.getInstance().getConnection();
  428. PreparedStatement ps = con.prepareStatement(SELECT_LOTTERY_TICKET))
  429. {
  430. ps.setInt(1, id);
  431. try (ResultSet rs = ps.executeQuery())
  432. {
  433. if (rs.next())
  434. {
  435. int curenchant = rs.getInt("number1") & enchant;
  436. int curtype2 = rs.getInt("number2") & type2;
  437. if ((curenchant == 0) && (curtype2 == 0))
  438. {
  439. return res;
  440. }
  441. int count = 0;
  442. for (int i = 1; i <= 16; i++)
  443. {
  444. int val = curenchant / 2;
  445. if (val != Math.round((double) curenchant / 2))
  446. {
  447. count++;
  448. }
  449. int val2 = curtype2 / 2;
  450. if (val2 != ((double) curtype2 / 2))
  451. {
  452. count++;
  453. }
  454. curenchant = val;
  455. curtype2 = val2;
  456. }
  457. switch (count)
  458. {
  459. case 0:
  460. break;
  461. case 5:
  462. res[0] = 1;
  463. res[1] = rs.getLong("prize1");
  464. break;
  465. case 4:
  466. res[0] = 2;
  467. res[1] = rs.getLong("prize2");
  468. break;
  469. case 3:
  470. res[0] = 3;
  471. res[1] = rs.getLong("prize3");
  472. break;
  473. default:
  474. res[0] = 4;
  475. res[1] = 200;
  476. }
  477. if (Config.DEBUG)
  478. {
  479. _log.warning("count: " + count + ", id: " + id + ", enchant: " + enchant + ", type2: " + type2);
  480. }
  481. }
  482. }
  483. }
  484. catch (SQLException e)
  485. {
  486. _log.log(Level.WARNING, "Lottery: Could not check lottery ticket #" + id + ": " + e.getMessage(), e);
  487. }
  488. return res;
  489. }
  490. private static class SingletonHolder
  491. {
  492. protected static final Lottery _instance = new Lottery();
  493. }
  494. }