Lottery.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. 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. public void run()
  217. {
  218. if (Config.DEBUG)
  219. _log.info("Lottery: Stopping ticket sell for lottery #" + getId() + ".");
  220. _isSellingTickets = false;
  221. Announcements.getInstance().announceToAll(SystemMessage.getSystemMessage(SystemMessageId.LOTTERY_TICKET_SALES_TEMP_SUSPENDED));
  222. }
  223. }
  224. private class finishLottery implements Runnable
  225. {
  226. protected finishLottery()
  227. {
  228. // Do nothing
  229. }
  230. public void run()
  231. {
  232. if (Config.DEBUG)
  233. _log.info("Lottery: Ending lottery #" + getId() + ".");
  234. int[] luckynums = new int[5];
  235. int luckynum = 0;
  236. for (int i = 0; i < 5; i++)
  237. {
  238. boolean found = true;
  239. while (found)
  240. {
  241. luckynum = Rnd.get(20) + 1;
  242. found = false;
  243. for (int j = 0; j < i; j++)
  244. if (luckynums[j] == luckynum)
  245. found = true;
  246. }
  247. luckynums[i] = luckynum;
  248. }
  249. if (Config.DEBUG)
  250. _log.info("Lottery: The lucky numbers are " + luckynums[0] + ", " + luckynums[1] + ", " + luckynums[2] + ", " + luckynums[3] + ", " + luckynums[4] + ".");
  251. int enchant = 0;
  252. int type2 = 0;
  253. for (int i = 0; i < 5; i++)
  254. {
  255. if (luckynums[i] < 17)
  256. enchant += Math.pow(2, luckynums[i] - 1);
  257. else
  258. type2 += Math.pow(2, luckynums[i] - 17);
  259. }
  260. if (Config.DEBUG)
  261. _log.info("Lottery: Encoded lucky numbers are " + enchant + ", " + type2);
  262. int count1 = 0;
  263. int count2 = 0;
  264. int count3 = 0;
  265. int count4 = 0;
  266. Connection con = null;
  267. PreparedStatement statement;
  268. try
  269. {
  270. con = L2DatabaseFactory.getInstance().getConnection();
  271. statement = con.prepareStatement(SELECT_LOTTERY_ITEM);
  272. statement.setInt(1, getId());
  273. ResultSet rset = statement.executeQuery();
  274. while (rset.next())
  275. {
  276. int curenchant = rset.getInt("enchant_level") & enchant;
  277. int curtype2 = rset.getInt("custom_type2") & type2;
  278. if (curenchant == 0 && curtype2 == 0)
  279. continue;
  280. int count = 0;
  281. for (int i = 1; i <= 16; i++)
  282. {
  283. int val = curenchant / 2;
  284. if (val != Math.round((double) curenchant / 2))
  285. count++;
  286. int val2 = curtype2 / 2;
  287. if (val2 != (double) curtype2 / 2)
  288. count++;
  289. curenchant = val;
  290. curtype2 = val2;
  291. }
  292. if (count == 5)
  293. count1++;
  294. else if (count == 4)
  295. count2++;
  296. else if (count == 3)
  297. count3++;
  298. else if (count > 0)
  299. count4++;
  300. }
  301. rset.close();
  302. statement.close();
  303. }
  304. catch (SQLException e)
  305. {
  306. _log.log(Level.WARNING, "Lottery: Could restore lottery data: " + e.getMessage(), e);
  307. }
  308. finally
  309. {
  310. L2DatabaseFactory.close(con);
  311. }
  312. long prize4 = count4 * Config.ALT_LOTTERY_2_AND_1_NUMBER_PRIZE;
  313. long prize1 = 0;
  314. long prize2 = 0;
  315. long prize3 = 0;
  316. if (count1 > 0)
  317. prize1 = (long) ((getPrize() - prize4) * Config.ALT_LOTTERY_5_NUMBER_RATE / count1);
  318. if (count2 > 0)
  319. prize2 = (long) ((getPrize() - prize4) * Config.ALT_LOTTERY_4_NUMBER_RATE / count2);
  320. if (count3 > 0)
  321. prize3 = (long) ((getPrize() - prize4) * Config.ALT_LOTTERY_3_NUMBER_RATE / count3);
  322. if (Config.DEBUG)
  323. {
  324. _log.info("Lottery: " + count1 + " players with all FIVE numbers each win " + prize1 + ".");
  325. _log.info("Lottery: " + count2 + " players with FOUR numbers each win " + prize2 + ".");
  326. _log.info("Lottery: " + count3 + " players with THREE numbers each win " + prize3 + ".");
  327. _log.info("Lottery: " + count4 + " players with ONE or TWO numbers each win " + prize4 + ".");
  328. }
  329. long newprize = getPrize() - (prize1 + prize2 + prize3 + prize4);
  330. if (Config.DEBUG)
  331. _log.info("Lottery: Jackpot for next lottery is " + newprize + ".");
  332. SystemMessage sm;
  333. if (count1 > 0)
  334. {
  335. // There are winners.
  336. sm = SystemMessage.getSystemMessage(SystemMessageId.AMOUNT_FOR_WINNER_S1_IS_S2_ADENA_WE_HAVE_S3_PRIZE_WINNER);
  337. sm.addNumber(getId());
  338. sm.addItemNumber(getPrize());
  339. sm.addItemNumber(count1);
  340. Announcements.getInstance().announceToAll(sm);
  341. }
  342. else
  343. {
  344. // There are no winners.
  345. sm = SystemMessage.getSystemMessage(SystemMessageId.AMOUNT_FOR_LOTTERY_S1_IS_S2_ADENA_NO_WINNER);
  346. sm.addNumber(getId());
  347. sm.addItemNumber(getPrize());
  348. Announcements.getInstance().announceToAll(sm);
  349. }
  350. try
  351. {
  352. con = L2DatabaseFactory.getInstance().getConnection();
  353. statement = con.prepareStatement(UPDATE_LOTTERY);
  354. statement.setLong(1, getPrize());
  355. statement.setLong(2, newprize);
  356. statement.setInt(3, enchant);
  357. statement.setInt(4, type2);
  358. statement.setLong(5, prize1);
  359. statement.setLong(6, prize2);
  360. statement.setLong(7, prize3);
  361. statement.setInt(8, getId());
  362. statement.execute();
  363. statement.close();
  364. }
  365. catch (SQLException e)
  366. {
  367. _log.log(Level.WARNING, "Lottery: Could not store finished lottery data: " + e.getMessage(), e);
  368. }
  369. finally
  370. {
  371. L2DatabaseFactory.close(con);
  372. }
  373. ThreadPoolManager.getInstance().scheduleGeneral(new startLottery(), MINUTE);
  374. _number++;
  375. _isStarted = false;
  376. }
  377. }
  378. public int[] decodeNumbers(int enchant, int type2)
  379. {
  380. int res[] = new int[5];
  381. int id = 0;
  382. int nr = 1;
  383. while (enchant > 0)
  384. {
  385. int val = enchant / 2;
  386. if (val != Math.round((double) enchant / 2))
  387. {
  388. res[id++] = nr;
  389. }
  390. enchant /= 2;
  391. nr++;
  392. }
  393. nr = 17;
  394. while (type2 > 0)
  395. {
  396. int val = type2 / 2;
  397. if (val != (double) type2 / 2)
  398. {
  399. res[id++] = nr;
  400. }
  401. type2 /= 2;
  402. nr++;
  403. }
  404. return res;
  405. }
  406. public long[] checkTicket(L2ItemInstance item)
  407. {
  408. return checkTicket(item.getCustomType1(), item.getEnchantLevel(), item.getCustomType2());
  409. }
  410. public long[] checkTicket(int id, int enchant, int type2)
  411. {
  412. long res[] = { 0, 0 };
  413. Connection con = null;
  414. PreparedStatement statement;
  415. try
  416. {
  417. con = L2DatabaseFactory.getInstance().getConnection();
  418. statement = con.prepareStatement(SELECT_LOTTERY_TICKET);
  419. statement.setInt(1, id);
  420. ResultSet rset = statement.executeQuery();
  421. if (rset.next())
  422. {
  423. int curenchant = rset.getInt("number1") & enchant;
  424. int curtype2 = rset.getInt("number2") & type2;
  425. if (curenchant == 0 && curtype2 == 0)
  426. {
  427. rset.close();
  428. statement.close();
  429. return res;
  430. }
  431. int count = 0;
  432. for (int i = 1; i <= 16; i++)
  433. {
  434. int val = curenchant / 2;
  435. if (val != Math.round((double) curenchant / 2))
  436. count++;
  437. int val2 = curtype2 / 2;
  438. if (val2 != (double) curtype2 / 2)
  439. count++;
  440. curenchant = val;
  441. curtype2 = val2;
  442. }
  443. switch (count)
  444. {
  445. case 0:
  446. break;
  447. case 5:
  448. res[0] = 1;
  449. res[1] = rset.getLong("prize1");
  450. break;
  451. case 4:
  452. res[0] = 2;
  453. res[1] = rset.getLong("prize2");
  454. break;
  455. case 3:
  456. res[0] = 3;
  457. res[1] = rset.getLong("prize3");
  458. break;
  459. default:
  460. res[0] = 4;
  461. res[1] = 200;
  462. }
  463. if (Config.DEBUG)
  464. _log.warning("count: " + count + ", id: " + id + ", enchant: " + enchant + ", type2: " + type2);
  465. }
  466. rset.close();
  467. statement.close();
  468. }
  469. catch (SQLException e)
  470. {
  471. _log.log(Level.WARNING, "Lottery: Could not check lottery ticket #" + id + ": " + e.getMessage(), e);
  472. }
  473. finally
  474. {
  475. L2DatabaseFactory.close(con);
  476. }
  477. return res;
  478. }
  479. @SuppressWarnings("synthetic-access")
  480. private static class SingletonHolder
  481. {
  482. protected static final Lottery _instance = new Lottery();
  483. }
  484. }