Festival.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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.util.Calendar;
  21. import java.util.List;
  22. import java.util.logging.Level;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.SevenSigns;
  25. import com.l2jserver.gameserver.SevenSignsFestival;
  26. import com.l2jserver.gameserver.handler.IBypassHandler;
  27. import com.l2jserver.gameserver.model.L2Party;
  28. import com.l2jserver.gameserver.model.L2Party.messageType;
  29. import com.l2jserver.gameserver.model.StatsSet;
  30. import com.l2jserver.gameserver.model.actor.L2Character;
  31. import com.l2jserver.gameserver.model.actor.instance.L2FestivalGuideInstance;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  34. import com.l2jserver.gameserver.network.SystemMessageId;
  35. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  36. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  37. import com.l2jserver.util.StringUtil;
  38. public class Festival implements IBypassHandler
  39. {
  40. private static final String[] COMMANDS =
  41. {
  42. "festival",
  43. "festivaldesc"
  44. };
  45. @Override
  46. public boolean useBypass(String command, L2PcInstance activeChar, L2Character target)
  47. {
  48. if (!(target instanceof L2FestivalGuideInstance))
  49. {
  50. return false;
  51. }
  52. final L2FestivalGuideInstance npc = (L2FestivalGuideInstance) target;
  53. try
  54. {
  55. final int val;
  56. if (command.toLowerCase().startsWith(COMMANDS[1]))
  57. {
  58. val = Integer.parseInt(command.substring(13));
  59. npc.showChatWindow(activeChar, val, null, true);
  60. return true;
  61. }
  62. final L2Party party;
  63. val = Integer.parseInt(command.substring(9, 10));
  64. switch (val)
  65. {
  66. case 1: // Become a Participant
  67. // Check if the festival period is active, if not then don't allow registration.
  68. if (SevenSigns.getInstance().isSealValidationPeriod())
  69. {
  70. npc.showChatWindow(activeChar, 2, "a", false);
  71. return true;
  72. }
  73. // Check if a festival is in progress, then don't allow registration yet.
  74. if (SevenSignsFestival.getInstance().isFestivalInitialized())
  75. {
  76. activeChar.sendMessage("You cannot sign up while a festival is in progress.");
  77. return true;
  78. }
  79. // Check if the player is in a formed party already.
  80. if (!activeChar.isInParty())
  81. {
  82. npc.showChatWindow(activeChar, 2, "b", false);
  83. return true;
  84. }
  85. party = activeChar.getParty();
  86. // Check if the player is the party leader.
  87. if (!party.isLeader(activeChar))
  88. {
  89. npc.showChatWindow(activeChar, 2, "c", false);
  90. return true;
  91. }
  92. // Check to see if the party has at least 5 members.
  93. if (party.getMemberCount() < Config.ALT_FESTIVAL_MIN_PLAYER)
  94. {
  95. npc.showChatWindow(activeChar, 2, "b", false);
  96. return true;
  97. }
  98. // Check if all the party members are in the required level range.
  99. if (party.getLevel() > SevenSignsFestival.getMaxLevelForFestival(npc.getFestivalType()))
  100. {
  101. npc.showChatWindow(activeChar, 2, "d", false);
  102. return true;
  103. }
  104. // Check to see if the player has already signed up
  105. if (activeChar.isFestivalParticipant())
  106. {
  107. SevenSignsFestival.getInstance().setParticipants(npc.getFestivalOracle(), npc.getFestivalType(), party);
  108. npc.showChatWindow(activeChar, 2, "f", false);
  109. return true;
  110. }
  111. npc.showChatWindow(activeChar, 1, null, false);
  112. break;
  113. case 2: // Seal Stones
  114. final int stoneType = Integer.parseInt(command.substring(11));
  115. final int stoneCount = npc.getStoneCount(stoneType);
  116. if (stoneCount <= 0)
  117. {
  118. return false;
  119. }
  120. if (!activeChar.destroyItemByItemId("SevenSigns", stoneType, stoneCount, npc, true))
  121. {
  122. return false;
  123. }
  124. SevenSignsFestival.getInstance().setParticipants(npc.getFestivalOracle(), npc.getFestivalType(), activeChar.getParty());
  125. SevenSignsFestival.getInstance().addAccumulatedBonus(npc.getFestivalType(), stoneType, stoneCount);
  126. npc.showChatWindow(activeChar, 2, "e", false);
  127. break;
  128. case 3: // Score Registration
  129. // Check if the festival period is active, if not then don't register the score.
  130. if (SevenSigns.getInstance().isSealValidationPeriod())
  131. {
  132. npc.showChatWindow(activeChar, 3, "a", false);
  133. return true;
  134. }
  135. // Check if a festival is in progress, if it is don't register the score.
  136. if (SevenSignsFestival.getInstance().isFestivalInProgress())
  137. {
  138. activeChar.sendMessage("You cannot register a score while a festival is in progress.");
  139. return true;
  140. }
  141. // Check if the player is in a party.
  142. if (!activeChar.isInParty())
  143. {
  144. npc.showChatWindow(activeChar, 3, "b", false);
  145. return true;
  146. }
  147. final List<Integer> prevParticipants = SevenSignsFestival.getInstance().getPreviousParticipants(npc.getFestivalOracle(), npc.getFestivalType());
  148. // Check if there are any past participants.
  149. if ((prevParticipants == null) || prevParticipants.isEmpty() || !prevParticipants.contains(activeChar.getObjectId()))
  150. {
  151. npc.showChatWindow(activeChar, 3, "b", false);
  152. return true;
  153. }
  154. // Check if this player was the party leader in the festival.
  155. if (activeChar.getObjectId() != prevParticipants.get(0))
  156. {
  157. npc.showChatWindow(activeChar, 3, "b", false);
  158. return true;
  159. }
  160. final L2ItemInstance bloodOfferings = activeChar.getInventory().getItemByItemId(SevenSignsFestival.FESTIVAL_OFFERING_ID);
  161. // Check if the player collected any blood offerings during the festival.
  162. if (bloodOfferings == null)
  163. {
  164. activeChar.sendMessage("You do not have any blood offerings to contribute.");
  165. return true;
  166. }
  167. final long offeringScore = bloodOfferings.getCount() * SevenSignsFestival.FESTIVAL_OFFERING_VALUE;
  168. if (!activeChar.destroyItem("SevenSigns", bloodOfferings, npc, false))
  169. {
  170. return true;
  171. }
  172. final boolean isHighestScore = SevenSignsFestival.getInstance().setFinalScore(activeChar, npc.getFestivalOracle(), npc.getFestivalType(), offeringScore);
  173. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CONTRIB_SCORE_INCREASED_S1);
  174. sm.addLong(offeringScore);
  175. activeChar.sendPacket(sm);
  176. if (isHighestScore)
  177. {
  178. npc.showChatWindow(activeChar, 3, "c", false);
  179. }
  180. else
  181. {
  182. npc.showChatWindow(activeChar, 3, "d", false);
  183. }
  184. break;
  185. case 4: // Current High Scores
  186. final StringBuilder strBuffer = StringUtil.startAppend(500, "<html><body>Festival Guide:<br>These are the top scores of the week, for the ");
  187. final StatsSet dawnData = SevenSignsFestival.getInstance().getHighestScoreData(SevenSigns.CABAL_DAWN, npc.getFestivalType());
  188. final StatsSet duskData = SevenSignsFestival.getInstance().getHighestScoreData(SevenSigns.CABAL_DUSK, npc.getFestivalType());
  189. final StatsSet overallData = SevenSignsFestival.getInstance().getOverallHighestScoreData(npc.getFestivalType());
  190. final int dawnScore = dawnData.getInt("score");
  191. final int duskScore = duskData.getInt("score");
  192. int overallScore = 0;
  193. // If no data is returned, assume there is no record, or all scores are 0.
  194. if (overallData != null)
  195. {
  196. overallScore = overallData.getInt("score");
  197. }
  198. StringUtil.append(strBuffer, SevenSignsFestival.getFestivalName(npc.getFestivalType()), " festival.<br>");
  199. if (dawnScore > 0)
  200. {
  201. StringUtil.append(strBuffer, "Dawn: ", calculateDate(dawnData.getString("date")), ". Score ", String.valueOf(dawnScore), "<br>", dawnData.getString("members"), "<br>");
  202. }
  203. else
  204. {
  205. strBuffer.append("Dawn: No record exists. Score 0<br>");
  206. }
  207. if (duskScore > 0)
  208. {
  209. StringUtil.append(strBuffer, "Dusk: ", calculateDate(duskData.getString("date")), ". Score ", String.valueOf(duskScore), "<br>", duskData.getString("members"), "<br>");
  210. }
  211. else
  212. {
  213. strBuffer.append("Dusk: No record exists. Score 0<br>");
  214. }
  215. if ((overallScore > 0) && (overallData != null))
  216. {
  217. final String cabalStr;
  218. if (overallData.getString("cabal").equals("dawn"))
  219. {
  220. cabalStr = "Children of Dawn";
  221. }
  222. else
  223. {
  224. cabalStr = "Children of Dusk";
  225. }
  226. StringUtil.append(strBuffer, "Consecutive top scores: ", calculateDate(overallData.getString("date")), ". Score ", String.valueOf(overallScore), "<br>Affilated side: ", cabalStr, "<br>", overallData.getString("members"), "<br>");
  227. }
  228. else
  229. {
  230. strBuffer.append("Consecutive top scores: No record exists. Score 0<br>");
  231. }
  232. StringUtil.append(strBuffer, "<a action=\"bypass -h npc_", String.valueOf(npc.getObjectId()), "_Chat 0\">Go back.</a></body></html>");
  233. final NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  234. html.setHtml(strBuffer.toString());
  235. activeChar.sendPacket(html);
  236. break;
  237. case 8: // Increase the Festival Challenge
  238. if (!activeChar.isInParty())
  239. {
  240. return true;
  241. }
  242. if (!SevenSignsFestival.getInstance().isFestivalInProgress())
  243. {
  244. return true;
  245. }
  246. party = activeChar.getParty();
  247. if (!party.isLeader(activeChar))
  248. {
  249. npc.showChatWindow(activeChar, 8, "a", false);
  250. return true;
  251. }
  252. if (SevenSignsFestival.getInstance().increaseChallenge(npc.getFestivalOracle(), npc.getFestivalType()))
  253. {
  254. npc.showChatWindow(activeChar, 8, "b", false);
  255. }
  256. else
  257. {
  258. npc.showChatWindow(activeChar, 8, "c", false);
  259. }
  260. break;
  261. case 9: // Leave the Festival
  262. if (!activeChar.isInParty())
  263. {
  264. return true;
  265. }
  266. party = activeChar.getParty();
  267. if (party.isLeader(activeChar))
  268. {
  269. SevenSignsFestival.getInstance().updateParticipants(activeChar, null);
  270. }
  271. else
  272. {
  273. if (party.getMemberCount() > Config.ALT_FESTIVAL_MIN_PLAYER)
  274. {
  275. party.removePartyMember(activeChar, messageType.Expelled);
  276. }
  277. else
  278. {
  279. activeChar.sendMessage("Only the party leader can leave a festival when a party has minimum number of members.");
  280. }
  281. }
  282. break;
  283. case 0: // Distribute Accumulated Bonus
  284. if (!SevenSigns.getInstance().isSealValidationPeriod())
  285. {
  286. activeChar.sendMessage("Bonuses cannot be paid during the competition period.");
  287. return true;
  288. }
  289. if (SevenSignsFestival.getInstance().distribAccumulatedBonus(activeChar) > 0)
  290. {
  291. npc.showChatWindow(activeChar, 0, "a", false);
  292. }
  293. else
  294. {
  295. npc.showChatWindow(activeChar, 0, "b", false);
  296. }
  297. break;
  298. default:
  299. npc.showChatWindow(activeChar, val, null, false);
  300. }
  301. return true;
  302. }
  303. catch (Exception e)
  304. {
  305. _log.log(Level.WARNING, "Exception in " + getClass().getSimpleName(), e);
  306. }
  307. return false;
  308. }
  309. private final String calculateDate(String milliFromEpoch)
  310. {
  311. long numMillis = Long.valueOf(milliFromEpoch);
  312. Calendar calCalc = Calendar.getInstance();
  313. calCalc.setTimeInMillis(numMillis);
  314. return calCalc.get(Calendar.YEAR) + "/" + calCalc.get(Calendar.MONTH) + "/" + calCalc.get(Calendar.DAY_OF_MONTH);
  315. }
  316. @Override
  317. public String[] getBypassList()
  318. {
  319. return COMMANDS;
  320. }
  321. }