Festival.java 12 KB

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