Lottery.java 15 KB

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