PetitionManager.java 19 KB

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