Loto.java 11 KB

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