Loto.java 10 KB

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