Festival.java 12 KB

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