Lottery.java 15 KB

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