PetitionManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server 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 Server 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 com.l2jserver.gameserver.instancemanager;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Date;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.logging.Logger;
  25. import com.l2jserver.Config;
  26. import com.l2jserver.gameserver.data.xml.impl.AdminData;
  27. import com.l2jserver.gameserver.enums.PetitionState;
  28. import com.l2jserver.gameserver.model.Petition;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.network.SystemMessageId;
  31. import com.l2jserver.gameserver.network.clientpackets.Say2;
  32. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  33. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  34. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  35. import com.l2jserver.util.StringUtil;
  36. /**
  37. * Petition Manager
  38. * @author Tempy
  39. */
  40. public final class PetitionManager
  41. {
  42. protected static final Logger _log = Logger.getLogger(PetitionManager.class.getName());
  43. private final Map<Integer, Petition> _pendingPetitions;
  44. private final Map<Integer, Petition> _completedPetitions;
  45. protected PetitionManager()
  46. {
  47. _pendingPetitions = new HashMap<>();
  48. _completedPetitions = new HashMap<>();
  49. }
  50. public void clearCompletedPetitions()
  51. {
  52. final int numPetitions = getPendingPetitionCount();
  53. getCompletedPetitions().clear();
  54. _log.info(getClass().getSimpleName() + ": Completed petition data cleared. " + numPetitions + " petition(s) removed.");
  55. }
  56. public void clearPendingPetitions()
  57. {
  58. final int numPetitions = getPendingPetitionCount();
  59. getPendingPetitions().clear();
  60. _log.info(getClass().getSimpleName() + ": Pending petition queue cleared. " + numPetitions + " petition(s) removed.");
  61. }
  62. public boolean acceptPetition(L2PcInstance respondingAdmin, int petitionId)
  63. {
  64. if (!isValidPetition(petitionId))
  65. {
  66. return false;
  67. }
  68. final Petition currPetition = getPendingPetitions().get(petitionId);
  69. if (currPetition.getResponder() != null)
  70. {
  71. return false;
  72. }
  73. currPetition.setResponder(respondingAdmin);
  74. currPetition.setState(PetitionState.IN_PROCESS);
  75. // Petition application accepted. (Send to Petitioner)
  76. currPetition.sendPetitionerPacket(SystemMessage.getSystemMessage(SystemMessageId.PETITION_APP_ACCEPTED));
  77. // Petition application accepted. Reciept No. is <ID>
  78. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PETITION_ACCEPTED_RECENT_NO_S1);
  79. sm.addInt(currPetition.getId());
  80. currPetition.sendResponderPacket(sm);
  81. // Petition consultation with <Player> underway.
  82. sm = SystemMessage.getSystemMessage(SystemMessageId.STARTING_PETITION_WITH_C1);
  83. sm.addString(currPetition.getPetitioner().getName());
  84. currPetition.sendResponderPacket(sm);
  85. // Set responder name on petitioner instance
  86. currPetition.getPetitioner().setLastPetitionGmName(currPetition.getResponder().getName());
  87. return true;
  88. }
  89. public boolean cancelActivePetition(L2PcInstance player)
  90. {
  91. for (Petition currPetition : getPendingPetitions().values())
  92. {
  93. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId()))
  94. {
  95. return (currPetition.endPetitionConsultation(PetitionState.PETITIONER_CANCEL));
  96. }
  97. if ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId()))
  98. {
  99. return (currPetition.endPetitionConsultation(PetitionState.RESPONDER_CANCEL));
  100. }
  101. }
  102. return false;
  103. }
  104. public void checkPetitionMessages(L2PcInstance petitioner)
  105. {
  106. if (petitioner != null)
  107. {
  108. for (Petition currPetition : getPendingPetitions().values())
  109. {
  110. if (currPetition == null)
  111. {
  112. continue;
  113. }
  114. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == petitioner.getObjectId()))
  115. {
  116. for (CreatureSay logMessage : currPetition.getLogMessages())
  117. {
  118. petitioner.sendPacket(logMessage);
  119. }
  120. return;
  121. }
  122. }
  123. }
  124. }
  125. public boolean endActivePetition(L2PcInstance player)
  126. {
  127. if (!player.isGM())
  128. {
  129. return false;
  130. }
  131. for (Petition currPetition : getPendingPetitions().values())
  132. {
  133. if (currPetition == null)
  134. {
  135. continue;
  136. }
  137. if ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId()))
  138. {
  139. return (currPetition.endPetitionConsultation(PetitionState.COMPLETED));
  140. }
  141. }
  142. return false;
  143. }
  144. public Map<Integer, Petition> getCompletedPetitions()
  145. {
  146. return _completedPetitions;
  147. }
  148. public Map<Integer, Petition> getPendingPetitions()
  149. {
  150. return _pendingPetitions;
  151. }
  152. public int getPendingPetitionCount()
  153. {
  154. return getPendingPetitions().size();
  155. }
  156. public int getPlayerTotalPetitionCount(L2PcInstance player)
  157. {
  158. if (player == null)
  159. {
  160. return 0;
  161. }
  162. int petitionCount = 0;
  163. for (Petition currPetition : getPendingPetitions().values())
  164. {
  165. if (currPetition == null)
  166. {
  167. continue;
  168. }
  169. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId()))
  170. {
  171. petitionCount++;
  172. }
  173. }
  174. for (Petition currPetition : getCompletedPetitions().values())
  175. {
  176. if (currPetition == null)
  177. {
  178. continue;
  179. }
  180. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId()))
  181. {
  182. petitionCount++;
  183. }
  184. }
  185. return petitionCount;
  186. }
  187. public boolean isPetitionInProcess()
  188. {
  189. for (Petition currPetition : getPendingPetitions().values())
  190. {
  191. if (currPetition == null)
  192. {
  193. continue;
  194. }
  195. if (currPetition.getState() == PetitionState.IN_PROCESS)
  196. {
  197. return true;
  198. }
  199. }
  200. return false;
  201. }
  202. public boolean isPetitionInProcess(int petitionId)
  203. {
  204. if (!isValidPetition(petitionId))
  205. {
  206. return false;
  207. }
  208. final Petition currPetition = getPendingPetitions().get(petitionId);
  209. return (currPetition.getState() == PetitionState.IN_PROCESS);
  210. }
  211. public boolean isPlayerInConsultation(L2PcInstance player)
  212. {
  213. if (player != null)
  214. {
  215. for (Petition currPetition : getPendingPetitions().values())
  216. {
  217. if (currPetition == null)
  218. {
  219. continue;
  220. }
  221. if (currPetition.getState() != PetitionState.IN_PROCESS)
  222. {
  223. continue;
  224. }
  225. if (((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId())) || ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId())))
  226. {
  227. return true;
  228. }
  229. }
  230. }
  231. return false;
  232. }
  233. public boolean isPetitioningAllowed()
  234. {
  235. return Config.PETITIONING_ALLOWED;
  236. }
  237. public boolean isPlayerPetitionPending(L2PcInstance petitioner)
  238. {
  239. if (petitioner != null)
  240. {
  241. for (Petition currPetition : getPendingPetitions().values())
  242. {
  243. if (currPetition == null)
  244. {
  245. continue;
  246. }
  247. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == petitioner.getObjectId()))
  248. {
  249. return true;
  250. }
  251. }
  252. }
  253. return false;
  254. }
  255. private boolean isValidPetition(int petitionId)
  256. {
  257. return getPendingPetitions().containsKey(petitionId);
  258. }
  259. public boolean rejectPetition(L2PcInstance respondingAdmin, int petitionId)
  260. {
  261. if (!isValidPetition(petitionId))
  262. {
  263. return false;
  264. }
  265. final Petition currPetition = getPendingPetitions().get(petitionId);
  266. if (currPetition.getResponder() != null)
  267. {
  268. return false;
  269. }
  270. currPetition.setResponder(respondingAdmin);
  271. return (currPetition.endPetitionConsultation(PetitionState.RESPONDER_REJECT));
  272. }
  273. public boolean sendActivePetitionMessage(L2PcInstance player, String messageText)
  274. {
  275. // if (!isPlayerInConsultation(player))
  276. // return false;
  277. CreatureSay cs;
  278. for (Petition currPetition : getPendingPetitions().values())
  279. {
  280. if (currPetition == null)
  281. {
  282. continue;
  283. }
  284. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId()))
  285. {
  286. cs = new CreatureSay(player.getObjectId(), Say2.PETITION_PLAYER, player.getName(), messageText);
  287. currPetition.addLogMessage(cs);
  288. currPetition.sendResponderPacket(cs);
  289. currPetition.sendPetitionerPacket(cs);
  290. return true;
  291. }
  292. if ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId()))
  293. {
  294. cs = new CreatureSay(player.getObjectId(), Say2.PETITION_GM, player.getName(), messageText);
  295. currPetition.addLogMessage(cs);
  296. currPetition.sendResponderPacket(cs);
  297. currPetition.sendPetitionerPacket(cs);
  298. return true;
  299. }
  300. }
  301. return false;
  302. }
  303. public void sendPendingPetitionList(L2PcInstance activeChar)
  304. {
  305. final StringBuilder htmlContent = StringUtil.startAppend(600 + (getPendingPetitionCount() * 300), "<html><body><center><table width=270><tr>" + "<td width=45><button value=\"Main\" action=\"bypass -h admin_admin\" width=45 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>" + "<td width=180><center>Petition Menu</center></td>" + "<td width=45><button value=\"Back\" action=\"bypass -h admin_admin7\" width=45 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr></table><br>" + "<table width=\"270\">" + "<tr><td><table width=\"270\"><tr><td><button value=\"Reset\" action=\"bypass -h admin_reset_petitions\" width=\"80\" height=\"21\" back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>" + "<td align=right><button value=\"Refresh\" action=\"bypass -h admin_view_petitions\" width=\"80\" height=\"21\" back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr></table><br></td></tr>");
  306. final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  307. if (getPendingPetitionCount() == 0)
  308. {
  309. htmlContent.append("<tr><td>There are no currently pending petitions.</td></tr>");
  310. }
  311. else
  312. {
  313. htmlContent.append("<tr><td><font color=\"LEVEL\">Current Petitions:</font><br></td></tr>");
  314. }
  315. boolean color = true;
  316. int petcount = 0;
  317. for (Petition currPetition : getPendingPetitions().values())
  318. {
  319. if (currPetition == null)
  320. {
  321. continue;
  322. }
  323. StringUtil.append(htmlContent, "<tr><td width=\"270\"><table width=\"270\" cellpadding=\"2\" bgcolor=", (color ? "131210" : "444444"), "><tr><td width=\"130\">", dateFormat.format(new Date(currPetition.getSubmitTime())));
  324. StringUtil.append(htmlContent, "</td><td width=\"140\" align=right><font color=\"", (currPetition.getPetitioner().isOnline() ? "00FF00" : "999999"), "\">", currPetition.getPetitioner().getName(), "</font></td></tr>");
  325. StringUtil.append(htmlContent, "<tr><td width=\"130\">");
  326. if (currPetition.getState() != PetitionState.IN_PROCESS)
  327. {
  328. StringUtil.append(htmlContent, "<table width=\"130\" cellpadding=\"2\"><tr>" + "<td><button value=\"View\" action=\"bypass -h admin_view_petition ", String.valueOf(currPetition.getId()), "\" width=\"50\" height=\"21\" back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>" + "<td><button value=\"Reject\" action=\"bypass -h admin_reject_petition ", String.valueOf(currPetition.getId()), "\" width=\"50\" height=\"21\" back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr></table>");
  329. }
  330. else
  331. {
  332. htmlContent.append("<font color=\"" + (currPetition.getResponder().isOnline() ? "00FF00" : "999999") + "\">" + currPetition.getResponder().getName() + "</font>");
  333. }
  334. StringUtil.append(htmlContent, "</td>", currPetition.getTypeAsString(), "<td width=\"140\" align=right>", currPetition.getTypeAsString(), "</td></tr></table></td></tr>");
  335. color = !color;
  336. petcount++;
  337. if (petcount > 10)
  338. {
  339. htmlContent.append("<tr><td><font color=\"LEVEL\">There is more pending petition...</font><br></td></tr>");
  340. break;
  341. }
  342. }
  343. htmlContent.append("</table></center></body></html>");
  344. final NpcHtmlMessage htmlMsg = new NpcHtmlMessage();
  345. htmlMsg.setHtml(htmlContent.toString());
  346. activeChar.sendPacket(htmlMsg);
  347. }
  348. public int submitPetition(L2PcInstance petitioner, String petitionText, int petitionType)
  349. {
  350. // Create a new petition instance and add it to the list of pending petitions.
  351. final Petition newPetition = new Petition(petitioner, petitionText, petitionType);
  352. int newPetitionId = newPetition.getId();
  353. getPendingPetitions().put(newPetitionId, newPetition);
  354. // Notify all GMs that a new petition has been submitted.
  355. final String msgContent = petitioner.getName() + " has submitted a new petition."; // (ID: " + newPetitionId + ").";
  356. AdminData.getInstance().broadcastToGMs(new CreatureSay(petitioner.getObjectId(), Say2.HERO_VOICE, "Petition System", msgContent));
  357. return newPetitionId;
  358. }
  359. public void viewPetition(L2PcInstance activeChar, int petitionId)
  360. {
  361. if (!activeChar.isGM())
  362. {
  363. return;
  364. }
  365. if (!isValidPetition(petitionId))
  366. {
  367. return;
  368. }
  369. final Petition currPetition = getPendingPetitions().get(petitionId);
  370. final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  371. final NpcHtmlMessage html = new NpcHtmlMessage();
  372. html.setFile(activeChar.getHtmlPrefix(), "data/html/admin/petition.htm");
  373. html.replace("%petition%", String.valueOf(currPetition.getId()));
  374. html.replace("%time%", dateFormat.format(new Date(currPetition.getSubmitTime())));
  375. html.replace("%type%", currPetition.getTypeAsString());
  376. html.replace("%petitioner%", currPetition.getPetitioner().getName());
  377. html.replace("%online%", (currPetition.getPetitioner().isOnline() ? "00FF00" : "999999"));
  378. html.replace("%text%", currPetition.getContent());
  379. activeChar.sendPacket(html);
  380. }
  381. /**
  382. * Gets the single instance of {@code PetitionManager}.
  383. * @return single instance of {@code PetitionManager}
  384. */
  385. public static final PetitionManager getInstance()
  386. {
  387. return SingletonHolder._instance;
  388. }
  389. private static class SingletonHolder
  390. {
  391. protected static final PetitionManager _instance = new PetitionManager();
  392. }
  393. }