Loto.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (C) 2004-2015 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package handlers.bypasshandlers;
  20. import java.text.DateFormat;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.handler.IBypassHandler;
  23. import com.l2jserver.gameserver.idfactory.IdFactory;
  24. import com.l2jserver.gameserver.instancemanager.games.Lottery;
  25. import com.l2jserver.gameserver.model.actor.L2Character;
  26. import com.l2jserver.gameserver.model.actor.L2Npc;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  29. import com.l2jserver.gameserver.network.SystemMessageId;
  30. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  31. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  32. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  33. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  34. public class Loto implements IBypassHandler
  35. {
  36. private static final String[] COMMANDS =
  37. {
  38. "Loto"
  39. };
  40. @Override
  41. public boolean useBypass(String command, L2PcInstance activeChar, L2Character target)
  42. {
  43. if (!target.isNpc())
  44. {
  45. return false;
  46. }
  47. int val = 0;
  48. try
  49. {
  50. val = Integer.parseInt(command.substring(5));
  51. }
  52. catch (IndexOutOfBoundsException ioobe)
  53. {
  54. }
  55. catch (NumberFormatException nfe)
  56. {
  57. }
  58. if (val == 0)
  59. {
  60. // new loto ticket
  61. for (int i = 0; i < 5; i++)
  62. {
  63. activeChar.setLoto(i, 0);
  64. }
  65. }
  66. showLotoWindow(activeChar, (L2Npc) target, val);
  67. return false;
  68. }
  69. /**
  70. * Open a Loto window on client with the text of the L2NpcInstance.<BR>
  71. * <BR>
  72. * <B><U> Actions</U> :</B><BR>
  73. * <BR>
  74. * <li>Get the text of the selected HTML file in function of the npcId and of the page number</li> <li>Send a Server->Client NpcHtmlMessage containing the text of the L2NpcInstance to the L2PcInstance</li> <li>Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the
  75. * client wait another packet</li><BR>
  76. * @param player The L2PcInstance that talk with the L2NpcInstance
  77. * @param npc L2Npc loto instance
  78. * @param val The number of the page of the L2NpcInstance to display
  79. */
  80. // 0 - first buy lottery ticket window
  81. // 1-20 - buttons
  82. // 21 - second buy lottery ticket window
  83. // 22 - selected ticket with 5 numbers
  84. // 23 - current lottery jackpot
  85. // 24 - Previous winning numbers/Prize claim
  86. // >24 - check lottery ticket by item object id
  87. public static final void showLotoWindow(L2PcInstance player, L2Npc npc, int val)
  88. {
  89. int npcId = npc.getTemplate().getId();
  90. String filename;
  91. SystemMessage sm;
  92. final NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  93. if (val == 0) // 0 - first buy lottery ticket window
  94. {
  95. filename = (npc.getHtmlPath(npcId, 1));
  96. html.setFile(player.getHtmlPrefix(), filename);
  97. }
  98. else if ((val >= 1) && (val <= 21)) // 1-20 - buttons, 21 - second buy lottery ticket window
  99. {
  100. if (!Lottery.getInstance().isStarted())
  101. {
  102. // tickets can't be sold
  103. player.sendPacket(SystemMessageId.NO_LOTTERY_TICKETS_CURRENT_SOLD);
  104. return;
  105. }
  106. if (!Lottery.getInstance().isSellableTickets())
  107. {
  108. // tickets can't be sold
  109. player.sendPacket(SystemMessageId.NO_LOTTERY_TICKETS_AVAILABLE);
  110. return;
  111. }
  112. filename = (npc.getHtmlPath(npcId, 5));
  113. html.setFile(player.getHtmlPrefix(), filename);
  114. int count = 0;
  115. int found = 0;
  116. // counting buttons and unsetting button if found
  117. for (int i = 0; i < 5; i++)
  118. {
  119. if (player.getLoto(i) == val)
  120. {
  121. // unsetting button
  122. player.setLoto(i, 0);
  123. found = 1;
  124. }
  125. else if (player.getLoto(i) > 0)
  126. {
  127. count++;
  128. }
  129. }
  130. // if not rearched limit 5 and not unseted value
  131. if ((count < 5) && (found == 0) && (val <= 20))
  132. {
  133. for (int i = 0; i < 5; i++)
  134. {
  135. if (player.getLoto(i) == 0)
  136. {
  137. player.setLoto(i, val);
  138. break;
  139. }
  140. }
  141. }
  142. // setting pusshed buttons
  143. count = 0;
  144. for (int i = 0; i < 5; i++)
  145. {
  146. if (player.getLoto(i) > 0)
  147. {
  148. count++;
  149. String button = String.valueOf(player.getLoto(i));
  150. if (player.getLoto(i) < 10)
  151. {
  152. button = "0" + button;
  153. }
  154. String search = "fore=\"L2UI.lottoNum" + button + "\" back=\"L2UI.lottoNum" + button + "a_check\"";
  155. String replace = "fore=\"L2UI.lottoNum" + button + "a_check\" back=\"L2UI.lottoNum" + button + "\"";
  156. html.replace(search, replace);
  157. }
  158. }
  159. if (count == 5)
  160. {
  161. String search = "0\">Return";
  162. String replace = "22\">Your lucky numbers have been selected above.";
  163. html.replace(search, replace);
  164. }
  165. }
  166. else if (val == 22) // 22 - selected ticket with 5 numbers
  167. {
  168. if (!Lottery.getInstance().isStarted())
  169. {
  170. // tickets can't be sold
  171. player.sendPacket(SystemMessageId.NO_LOTTERY_TICKETS_CURRENT_SOLD);
  172. return;
  173. }
  174. if (!Lottery.getInstance().isSellableTickets())
  175. {
  176. // tickets can't be sold
  177. player.sendPacket(SystemMessageId.NO_LOTTERY_TICKETS_AVAILABLE);
  178. return;
  179. }
  180. long price = Config.ALT_LOTTERY_TICKET_PRICE;
  181. int lotonumber = Lottery.getInstance().getId();
  182. int enchant = 0;
  183. int type2 = 0;
  184. for (int i = 0; i < 5; i++)
  185. {
  186. if (player.getLoto(i) == 0)
  187. {
  188. return;
  189. }
  190. if (player.getLoto(i) < 17)
  191. {
  192. enchant += Math.pow(2, player.getLoto(i) - 1);
  193. }
  194. else
  195. {
  196. type2 += Math.pow(2, player.getLoto(i) - 17);
  197. }
  198. }
  199. if (player.getAdena() < price)
  200. {
  201. sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA);
  202. player.sendPacket(sm);
  203. return;
  204. }
  205. if (!player.reduceAdena("Loto", price, npc, true))
  206. {
  207. return;
  208. }
  209. Lottery.getInstance().increasePrize(price);
  210. sm = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  211. sm.addItemName(4442);
  212. player.sendPacket(sm);
  213. L2ItemInstance item = new L2ItemInstance(IdFactory.getInstance().getNextId(), 4442);
  214. item.setCount(1);
  215. item.setCustomType1(lotonumber);
  216. item.setEnchantLevel(enchant);
  217. item.setCustomType2(type2);
  218. player.getInventory().addItem("Loto", item, player, npc);
  219. InventoryUpdate iu = new InventoryUpdate();
  220. iu.addItem(item);
  221. L2ItemInstance adenaupdate = player.getInventory().getItemByItemId(57);
  222. iu.addModifiedItem(adenaupdate);
  223. player.sendPacket(iu);
  224. filename = (npc.getHtmlPath(npcId, 6));
  225. html.setFile(player.getHtmlPrefix(), filename);
  226. }
  227. else if (val == 23) // 23 - current lottery jackpot
  228. {
  229. filename = (npc.getHtmlPath(npcId, 3));
  230. html.setFile(player.getHtmlPrefix(), filename);
  231. }
  232. else if (val == 24) // 24 - Previous winning numbers/Prize claim
  233. {
  234. filename = (npc.getHtmlPath(npcId, 4));
  235. html.setFile(player.getHtmlPrefix(), filename);
  236. int lotonumber = Lottery.getInstance().getId();
  237. String message = "";
  238. for (L2ItemInstance item : player.getInventory().getItems())
  239. {
  240. if (item == null)
  241. {
  242. continue;
  243. }
  244. if ((item.getId() == 4442) && (item.getCustomType1() < lotonumber))
  245. {
  246. message = message + "<a action=\"bypass -h npc_%objectId%_Loto " + item.getObjectId() + "\">" + item.getCustomType1() + " Event Number ";
  247. int[] numbers = Lottery.getInstance().decodeNumbers(item.getEnchantLevel(), item.getCustomType2());
  248. for (int i = 0; i < 5; i++)
  249. {
  250. message += numbers[i] + " ";
  251. }
  252. long[] check = Lottery.getInstance().checkTicket(item);
  253. if (check[0] > 0)
  254. {
  255. switch ((int) check[0])
  256. {
  257. case 1:
  258. message += "- 1st Prize";
  259. break;
  260. case 2:
  261. message += "- 2nd Prize";
  262. break;
  263. case 3:
  264. message += "- 3th Prize";
  265. break;
  266. case 4:
  267. message += "- 4th Prize";
  268. break;
  269. }
  270. message += " " + check[1] + "a.";
  271. }
  272. message += "</a><br>";
  273. }
  274. }
  275. if (message.isEmpty())
  276. {
  277. message += "There has been no winning lottery ticket.<br>";
  278. }
  279. html.replace("%result%", message);
  280. }
  281. else if (val == 25) // 25 - lottery instructions
  282. {
  283. filename = (npc.getHtmlPath(npcId, 2));
  284. html.setFile(player.getHtmlPrefix(), filename);
  285. }
  286. else if (val > 25) // >25 - check lottery ticket by item object id
  287. {
  288. int lotonumber = Lottery.getInstance().getId();
  289. L2ItemInstance item = player.getInventory().getItemByObjectId(val);
  290. if ((item == null) || (item.getId() != 4442) || (item.getCustomType1() >= lotonumber))
  291. {
  292. return;
  293. }
  294. long[] check = Lottery.getInstance().checkTicket(item);
  295. sm = SystemMessage.getSystemMessage(SystemMessageId.S1_DISAPPEARED);
  296. sm.addItemName(4442);
  297. player.sendPacket(sm);
  298. long adena = check[1];
  299. if (adena > 0)
  300. {
  301. player.addAdena("Loto", adena, npc, true);
  302. }
  303. player.destroyItem("Loto", item, npc, false);
  304. return;
  305. }
  306. html.replace("%objectId%", String.valueOf(npc.getObjectId()));
  307. html.replace("%race%", "" + Lottery.getInstance().getId());
  308. html.replace("%adena%", "" + Lottery.getInstance().getPrize());
  309. html.replace("%ticket_price%", "" + Config.ALT_LOTTERY_TICKET_PRICE);
  310. html.replace("%prize5%", "" + (Config.ALT_LOTTERY_5_NUMBER_RATE * 100));
  311. html.replace("%prize4%", "" + (Config.ALT_LOTTERY_4_NUMBER_RATE * 100));
  312. html.replace("%prize3%", "" + (Config.ALT_LOTTERY_3_NUMBER_RATE * 100));
  313. html.replace("%prize2%", "" + Config.ALT_LOTTERY_2_AND_1_NUMBER_PRIZE);
  314. html.replace("%enddate%", "" + DateFormat.getDateInstance().format(Lottery.getInstance().getEndDate()));
  315. player.sendPacket(html);
  316. // Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet
  317. player.sendPacket(ActionFailed.STATIC_PACKET);
  318. }
  319. @Override
  320. public String[] getBypassList()
  321. {
  322. return COMMANDS;
  323. }
  324. }