PetitionManager.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * Copyright (C) 2004-2013 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.List;
  24. import java.util.Map;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastList;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.gameserver.datatables.AdminTable;
  29. import com.l2jserver.gameserver.idfactory.IdFactory;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.network.SystemMessageId;
  32. import com.l2jserver.gameserver.network.clientpackets.Say2;
  33. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  34. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  35. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  36. import com.l2jserver.gameserver.network.serverpackets.PetitionVotePacket;
  37. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  38. import com.l2jserver.util.StringUtil;
  39. /**
  40. * Petition Manager
  41. * @author Tempy
  42. */
  43. public final class PetitionManager
  44. {
  45. protected static final Logger _log = Logger.getLogger(PetitionManager.class.getName());
  46. private final Map<Integer, Petition> _pendingPetitions;
  47. private final Map<Integer, Petition> _completedPetitions;
  48. private static enum PetitionState
  49. {
  50. Pending,
  51. Responder_Cancel,
  52. Responder_Missing,
  53. Responder_Reject,
  54. Responder_Complete,
  55. Petitioner_Cancel,
  56. Petitioner_Missing,
  57. In_Process,
  58. Completed
  59. }
  60. private static enum PetitionType
  61. {
  62. Immobility,
  63. Recovery_Related,
  64. Bug_Report,
  65. Quest_Related,
  66. Bad_User,
  67. Suggestions,
  68. Game_Tip,
  69. Operation_Related,
  70. Other
  71. }
  72. public static PetitionManager getInstance()
  73. {
  74. return SingletonHolder._instance;
  75. }
  76. private class Petition
  77. {
  78. private final long _submitTime = System.currentTimeMillis();
  79. private final int _id;
  80. private final PetitionType _type;
  81. private PetitionState _state = PetitionState.Pending;
  82. private final String _content;
  83. private final List<CreatureSay> _messageLog = new FastList<>();
  84. private final L2PcInstance _petitioner;
  85. private L2PcInstance _responder;
  86. public Petition(L2PcInstance petitioner, String petitionText, int petitionType)
  87. {
  88. petitionType--;
  89. _id = IdFactory.getInstance().getNextId();
  90. if (petitionType >= PetitionType.values().length)
  91. {
  92. _log.warning(getClass().getSimpleName() + ": Petition : invalid petition type (received type was +1) : " + petitionType);
  93. }
  94. _type = PetitionType.values()[petitionType];
  95. _content = petitionText;
  96. _petitioner = petitioner;
  97. }
  98. protected boolean addLogMessage(CreatureSay cs)
  99. {
  100. return _messageLog.add(cs);
  101. }
  102. protected List<CreatureSay> getLogMessages()
  103. {
  104. return _messageLog;
  105. }
  106. public boolean endPetitionConsultation(PetitionState endState)
  107. {
  108. setState(endState);
  109. if ((getResponder() != null) && getResponder().isOnline())
  110. {
  111. if (endState == PetitionState.Responder_Reject)
  112. {
  113. getPetitioner().sendMessage("Your petition was rejected. Please try again later.");
  114. }
  115. else
  116. {
  117. // Ending petition consultation with <Player>.
  118. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PETITION_ENDED_WITH_C1);
  119. sm.addString(getPetitioner().getName());
  120. getResponder().sendPacket(sm);
  121. if (endState == PetitionState.Petitioner_Cancel)
  122. {
  123. // Receipt No. <ID> petition cancelled.
  124. sm = SystemMessage.getSystemMessage(SystemMessageId.RECENT_NO_S1_CANCELED);
  125. sm.addNumber(getId());
  126. getResponder().sendPacket(sm);
  127. }
  128. }
  129. }
  130. // End petition consultation and inform them, if they are still online. And if petitioner is online, enable Evaluation button
  131. if ((getPetitioner() != null) && getPetitioner().isOnline())
  132. {
  133. getPetitioner().sendPacket(SystemMessageId.THIS_END_THE_PETITION_PLEASE_PROVIDE_FEEDBACK);
  134. getPetitioner().sendPacket(PetitionVotePacket.STATIC_PACKET);
  135. }
  136. getCompletedPetitions().put(getId(), this);
  137. return (getPendingPetitions().remove(getId()) != null);
  138. }
  139. public String getContent()
  140. {
  141. return _content;
  142. }
  143. public int getId()
  144. {
  145. return _id;
  146. }
  147. public L2PcInstance getPetitioner()
  148. {
  149. return _petitioner;
  150. }
  151. public L2PcInstance getResponder()
  152. {
  153. return _responder;
  154. }
  155. public long getSubmitTime()
  156. {
  157. return _submitTime;
  158. }
  159. public PetitionState getState()
  160. {
  161. return _state;
  162. }
  163. public String getTypeAsString()
  164. {
  165. return _type.toString().replace("_", " ");
  166. }
  167. public void sendPetitionerPacket(L2GameServerPacket responsePacket)
  168. {
  169. if ((getPetitioner() == null) || !getPetitioner().isOnline())
  170. {
  171. // Allows petitioners to see the results of their petition when
  172. // they log back into the game.
  173. // endPetitionConsultation(PetitionState.Petitioner_Missing);
  174. return;
  175. }
  176. getPetitioner().sendPacket(responsePacket);
  177. }
  178. public void sendResponderPacket(L2GameServerPacket responsePacket)
  179. {
  180. if ((getResponder() == null) || !getResponder().isOnline())
  181. {
  182. endPetitionConsultation(PetitionState.Responder_Missing);
  183. return;
  184. }
  185. getResponder().sendPacket(responsePacket);
  186. }
  187. public void setState(PetitionState state)
  188. {
  189. _state = state;
  190. }
  191. public void setResponder(L2PcInstance respondingAdmin)
  192. {
  193. if (getResponder() != null)
  194. {
  195. return;
  196. }
  197. _responder = respondingAdmin;
  198. }
  199. }
  200. protected PetitionManager()
  201. {
  202. _pendingPetitions = new HashMap<>();
  203. _completedPetitions = new HashMap<>();
  204. }
  205. public void clearCompletedPetitions()
  206. {
  207. int numPetitions = getPendingPetitionCount();
  208. getCompletedPetitions().clear();
  209. _log.info(getClass().getSimpleName() + ": Completed petition data cleared. " + numPetitions + " petition(s) removed.");
  210. }
  211. public void clearPendingPetitions()
  212. {
  213. int numPetitions = getPendingPetitionCount();
  214. getPendingPetitions().clear();
  215. _log.info(getClass().getSimpleName() + ": Pending petition queue cleared. " + numPetitions + " petition(s) removed.");
  216. }
  217. public boolean acceptPetition(L2PcInstance respondingAdmin, int petitionId)
  218. {
  219. if (!isValidPetition(petitionId))
  220. {
  221. return false;
  222. }
  223. Petition currPetition = getPendingPetitions().get(petitionId);
  224. if (currPetition.getResponder() != null)
  225. {
  226. return false;
  227. }
  228. currPetition.setResponder(respondingAdmin);
  229. currPetition.setState(PetitionState.In_Process);
  230. // Petition application accepted. (Send to Petitioner)
  231. currPetition.sendPetitionerPacket(SystemMessage.getSystemMessage(SystemMessageId.PETITION_APP_ACCEPTED));
  232. // Petition application accepted. Reciept No. is <ID>
  233. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PETITION_ACCEPTED_RECENT_NO_S1);
  234. sm.addNumber(currPetition.getId());
  235. currPetition.sendResponderPacket(sm);
  236. // Petition consultation with <Player> underway.
  237. sm = SystemMessage.getSystemMessage(SystemMessageId.STARTING_PETITION_WITH_C1);
  238. sm.addString(currPetition.getPetitioner().getName());
  239. currPetition.sendResponderPacket(sm);
  240. // Set responder name on petitioner instance
  241. currPetition.getPetitioner().setLastPetitionGmName(currPetition.getResponder().getName());
  242. return true;
  243. }
  244. public boolean cancelActivePetition(L2PcInstance player)
  245. {
  246. for (Petition currPetition : getPendingPetitions().values())
  247. {
  248. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId()))
  249. {
  250. return (currPetition.endPetitionConsultation(PetitionState.Petitioner_Cancel));
  251. }
  252. if ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId()))
  253. {
  254. return (currPetition.endPetitionConsultation(PetitionState.Responder_Cancel));
  255. }
  256. }
  257. return false;
  258. }
  259. public void checkPetitionMessages(L2PcInstance petitioner)
  260. {
  261. if (petitioner != null)
  262. {
  263. for (Petition currPetition : getPendingPetitions().values())
  264. {
  265. if (currPetition == null)
  266. {
  267. continue;
  268. }
  269. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == petitioner.getObjectId()))
  270. {
  271. for (CreatureSay logMessage : currPetition.getLogMessages())
  272. {
  273. petitioner.sendPacket(logMessage);
  274. }
  275. return;
  276. }
  277. }
  278. }
  279. }
  280. public boolean endActivePetition(L2PcInstance player)
  281. {
  282. if (!player.isGM())
  283. {
  284. return false;
  285. }
  286. for (Petition currPetition : getPendingPetitions().values())
  287. {
  288. if (currPetition == null)
  289. {
  290. continue;
  291. }
  292. if ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId()))
  293. {
  294. return (currPetition.endPetitionConsultation(PetitionState.Completed));
  295. }
  296. }
  297. return false;
  298. }
  299. protected Map<Integer, Petition> getCompletedPetitions()
  300. {
  301. return _completedPetitions;
  302. }
  303. protected Map<Integer, Petition> getPendingPetitions()
  304. {
  305. return _pendingPetitions;
  306. }
  307. public int getPendingPetitionCount()
  308. {
  309. return getPendingPetitions().size();
  310. }
  311. public int getPlayerTotalPetitionCount(L2PcInstance player)
  312. {
  313. if (player == null)
  314. {
  315. return 0;
  316. }
  317. int petitionCount = 0;
  318. for (Petition currPetition : getPendingPetitions().values())
  319. {
  320. if (currPetition == null)
  321. {
  322. continue;
  323. }
  324. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId()))
  325. {
  326. petitionCount++;
  327. }
  328. }
  329. for (Petition currPetition : getCompletedPetitions().values())
  330. {
  331. if (currPetition == null)
  332. {
  333. continue;
  334. }
  335. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId()))
  336. {
  337. petitionCount++;
  338. }
  339. }
  340. return petitionCount;
  341. }
  342. public boolean isPetitionInProcess()
  343. {
  344. for (Petition currPetition : getPendingPetitions().values())
  345. {
  346. if (currPetition == null)
  347. {
  348. continue;
  349. }
  350. if (currPetition.getState() == PetitionState.In_Process)
  351. {
  352. return true;
  353. }
  354. }
  355. return false;
  356. }
  357. public boolean isPetitionInProcess(int petitionId)
  358. {
  359. if (!isValidPetition(petitionId))
  360. {
  361. return false;
  362. }
  363. Petition currPetition = getPendingPetitions().get(petitionId);
  364. return (currPetition.getState() == PetitionState.In_Process);
  365. }
  366. public boolean isPlayerInConsultation(L2PcInstance player)
  367. {
  368. if (player != null)
  369. {
  370. for (Petition currPetition : getPendingPetitions().values())
  371. {
  372. if (currPetition == null)
  373. {
  374. continue;
  375. }
  376. if (currPetition.getState() != PetitionState.In_Process)
  377. {
  378. continue;
  379. }
  380. if (((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId())) || ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId())))
  381. {
  382. return true;
  383. }
  384. }
  385. }
  386. return false;
  387. }
  388. public boolean isPetitioningAllowed()
  389. {
  390. return Config.PETITIONING_ALLOWED;
  391. }
  392. public boolean isPlayerPetitionPending(L2PcInstance petitioner)
  393. {
  394. if (petitioner != null)
  395. {
  396. for (Petition currPetition : getPendingPetitions().values())
  397. {
  398. if (currPetition == null)
  399. {
  400. continue;
  401. }
  402. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == petitioner.getObjectId()))
  403. {
  404. return true;
  405. }
  406. }
  407. }
  408. return false;
  409. }
  410. private boolean isValidPetition(int petitionId)
  411. {
  412. return getPendingPetitions().containsKey(petitionId);
  413. }
  414. public boolean rejectPetition(L2PcInstance respondingAdmin, int petitionId)
  415. {
  416. if (!isValidPetition(petitionId))
  417. {
  418. return false;
  419. }
  420. Petition currPetition = getPendingPetitions().get(petitionId);
  421. if (currPetition.getResponder() != null)
  422. {
  423. return false;
  424. }
  425. currPetition.setResponder(respondingAdmin);
  426. return (currPetition.endPetitionConsultation(PetitionState.Responder_Reject));
  427. }
  428. public boolean sendActivePetitionMessage(L2PcInstance player, String messageText)
  429. {
  430. // if (!isPlayerInConsultation(player))
  431. // return false;
  432. CreatureSay cs;
  433. for (Petition currPetition : getPendingPetitions().values())
  434. {
  435. if (currPetition == null)
  436. {
  437. continue;
  438. }
  439. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId()))
  440. {
  441. cs = new CreatureSay(player.getObjectId(), Say2.PETITION_PLAYER, player.getName(), messageText);
  442. currPetition.addLogMessage(cs);
  443. currPetition.sendResponderPacket(cs);
  444. currPetition.sendPetitionerPacket(cs);
  445. return true;
  446. }
  447. if ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId()))
  448. {
  449. cs = new CreatureSay(player.getObjectId(), Say2.PETITION_GM, player.getName(), messageText);
  450. currPetition.addLogMessage(cs);
  451. currPetition.sendResponderPacket(cs);
  452. currPetition.sendPetitionerPacket(cs);
  453. return true;
  454. }
  455. }
  456. return false;
  457. }
  458. public void sendPendingPetitionList(L2PcInstance activeChar)
  459. {
  460. 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>");
  461. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  462. if (getPendingPetitionCount() == 0)
  463. {
  464. htmlContent.append("<tr><td>There are no currently pending petitions.</td></tr>");
  465. }
  466. else
  467. {
  468. htmlContent.append("<tr><td><font color=\"LEVEL\">Current Petitions:</font><br></td></tr>");
  469. }
  470. boolean color = true;
  471. int petcount = 0;
  472. for (Petition currPetition : getPendingPetitions().values())
  473. {
  474. if (currPetition == null)
  475. {
  476. continue;
  477. }
  478. 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())));
  479. StringUtil.append(htmlContent, "</td><td width=\"140\" align=right><font color=\"", (currPetition.getPetitioner().isOnline() ? "00FF00" : "999999"), "\">", currPetition.getPetitioner().getName(), "</font></td></tr>");
  480. StringUtil.append(htmlContent, "<tr><td width=\"130\">");
  481. if (currPetition.getState() != PetitionState.In_Process)
  482. {
  483. 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>");
  484. }
  485. else
  486. {
  487. htmlContent.append("<font color=\"" + (currPetition.getResponder().isOnline() ? "00FF00" : "999999") + "\">" + currPetition.getResponder().getName() + "</font>");
  488. }
  489. StringUtil.append(htmlContent, "</td>", currPetition.getTypeAsString(), "<td width=\"140\" align=right>", currPetition.getTypeAsString(), "</td></tr></table></td></tr>");
  490. color = !color;
  491. petcount++;
  492. if (petcount > 10)
  493. {
  494. htmlContent.append("<tr><td><font color=\"LEVEL\">There is more pending petition...</font><br></td></tr>");
  495. break;
  496. }
  497. }
  498. htmlContent.append("</table></center></body></html>");
  499. NpcHtmlMessage htmlMsg = new NpcHtmlMessage(0);
  500. htmlMsg.setHtml(htmlContent.toString());
  501. activeChar.sendPacket(htmlMsg);
  502. }
  503. public int submitPetition(L2PcInstance petitioner, String petitionText, int petitionType)
  504. {
  505. // Create a new petition instance and add it to the list of pending petitions.
  506. Petition newPetition = new Petition(petitioner, petitionText, petitionType);
  507. int newPetitionId = newPetition.getId();
  508. getPendingPetitions().put(newPetitionId, newPetition);
  509. // Notify all GMs that a new petition has been submitted.
  510. String msgContent = petitioner.getName() + " has submitted a new petition."; // (ID: " + newPetitionId + ").";
  511. AdminTable.getInstance().broadcastToGMs(new CreatureSay(petitioner.getObjectId(), Say2.HERO_VOICE, "Petition System", msgContent));
  512. return newPetitionId;
  513. }
  514. public void viewPetition(L2PcInstance activeChar, int petitionId)
  515. {
  516. if (!activeChar.isGM())
  517. {
  518. return;
  519. }
  520. if (!isValidPetition(petitionId))
  521. {
  522. return;
  523. }
  524. Petition currPetition = getPendingPetitions().get(petitionId);
  525. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  526. NpcHtmlMessage html = new NpcHtmlMessage(0);
  527. html.setFile(activeChar.getHtmlPrefix(), "data/html/admin/petition.htm");
  528. html.replace("%petition%", String.valueOf(currPetition.getId()));
  529. html.replace("%time%", dateFormat.format(new Date(currPetition.getSubmitTime())));
  530. html.replace("%type%", currPetition.getTypeAsString());
  531. html.replace("%petitioner%", currPetition.getPetitioner().getName());
  532. html.replace("%online%", (currPetition.getPetitioner().isOnline() ? "00FF00" : "999999"));
  533. html.replace("%text%", currPetition.getContent());
  534. activeChar.sendPacket(html);
  535. }
  536. private static class SingletonHolder
  537. {
  538. protected static final PetitionManager _instance = new PetitionManager();
  539. }
  540. }