Lottery.java 15 KB

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