PetitionManager.java 18 KB

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