Lottery.java 15 KB

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