PetitionManager.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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.List;
  19. import java.util.Map;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastList;
  22. import javolution.util.FastMap;
  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. private PetitionManager()
  197. {
  198. _log.info("Initializing PetitionManager");
  199. _pendingPetitions = new FastMap<Integer, Petition>();
  200. _completedPetitions = new FastMap<Integer, Petition>();
  201. }
  202. public void clearCompletedPetitions()
  203. {
  204. int numPetitions = getPendingPetitionCount();
  205. getCompletedPetitions().clear();
  206. _log.info("PetitionManager: Completed petition data cleared. " + numPetitions + " petition(s) removed.");
  207. }
  208. public void clearPendingPetitions()
  209. {
  210. int numPetitions = getPendingPetitionCount();
  211. getPendingPetitions().clear();
  212. _log.info("PetitionManager: Pending petition queue cleared. " + numPetitions + " petition(s) removed.");
  213. }
  214. public boolean acceptPetition(L2PcInstance respondingAdmin, int petitionId)
  215. {
  216. if (!isValidPetition(petitionId))
  217. return false;
  218. Petition currPetition = getPendingPetitions().get(petitionId);
  219. if (currPetition.getResponder() != null)
  220. return false;
  221. currPetition.setResponder(respondingAdmin);
  222. currPetition.setState(PetitionState.In_Process);
  223. // Petition application accepted. (Send to Petitioner)
  224. currPetition.sendPetitionerPacket(SystemMessage.getSystemMessage(SystemMessageId.PETITION_APP_ACCEPTED));
  225. // Petition application accepted. Reciept No. is <ID>
  226. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PETITION_ACCEPTED_RECENT_NO_S1);
  227. sm.addNumber(currPetition.getId());
  228. currPetition.sendResponderPacket(sm);
  229. // Petition consultation with <Player> underway.
  230. sm = SystemMessage.getSystemMessage(SystemMessageId.STARTING_PETITION_WITH_C1);
  231. sm.addString(currPetition.getPetitioner().getName());
  232. currPetition.sendResponderPacket(sm);
  233. // Set responder name on petitioner instance
  234. currPetition.getPetitioner().setLastPetitionGmName(currPetition.getResponder().getName());
  235. return true;
  236. }
  237. public boolean cancelActivePetition(L2PcInstance player)
  238. {
  239. for (Petition currPetition : getPendingPetitions().values())
  240. {
  241. if (currPetition.getPetitioner() != null && currPetition.getPetitioner().getObjectId() == player.getObjectId())
  242. return (currPetition.endPetitionConsultation(PetitionState.Petitioner_Cancel));
  243. if (currPetition.getResponder() != null && currPetition.getResponder().getObjectId() == player.getObjectId())
  244. return (currPetition.endPetitionConsultation(PetitionState.Responder_Cancel));
  245. }
  246. return false;
  247. }
  248. public void checkPetitionMessages(L2PcInstance petitioner)
  249. {
  250. if (petitioner != null)
  251. for (Petition currPetition : getPendingPetitions().values())
  252. {
  253. if (currPetition == null)
  254. continue;
  255. if (currPetition.getPetitioner() != null && currPetition.getPetitioner().getObjectId() == petitioner.getObjectId())
  256. {
  257. for (CreatureSay logMessage : currPetition.getLogMessages())
  258. petitioner.sendPacket(logMessage);
  259. return;
  260. }
  261. }
  262. }
  263. public boolean endActivePetition(L2PcInstance player)
  264. {
  265. if (!player.isGM())
  266. return false;
  267. for (Petition currPetition : getPendingPetitions().values())
  268. {
  269. if (currPetition == null)
  270. continue;
  271. if (currPetition.getResponder() != null && currPetition.getResponder().getObjectId() == player.getObjectId())
  272. return (currPetition.endPetitionConsultation(PetitionState.Completed));
  273. }
  274. return false;
  275. }
  276. protected Map<Integer, Petition> getCompletedPetitions()
  277. {
  278. return _completedPetitions;
  279. }
  280. protected Map<Integer, Petition> getPendingPetitions()
  281. {
  282. return _pendingPetitions;
  283. }
  284. public int getPendingPetitionCount()
  285. {
  286. return getPendingPetitions().size();
  287. }
  288. public int getPlayerTotalPetitionCount(L2PcInstance player)
  289. {
  290. if (player == null)
  291. return 0;
  292. int petitionCount = 0;
  293. for (Petition currPetition : getPendingPetitions().values())
  294. {
  295. if (currPetition == null)
  296. continue;
  297. if (currPetition.getPetitioner() != null && currPetition.getPetitioner().getObjectId() == player.getObjectId())
  298. petitionCount++;
  299. }
  300. for (Petition currPetition : getCompletedPetitions().values())
  301. {
  302. if (currPetition == null)
  303. continue;
  304. if (currPetition.getPetitioner() != null && currPetition.getPetitioner().getObjectId() == player.getObjectId())
  305. petitionCount++;
  306. }
  307. return petitionCount;
  308. }
  309. public boolean isPetitionInProcess()
  310. {
  311. for (Petition currPetition : getPendingPetitions().values())
  312. {
  313. if (currPetition == null)
  314. continue;
  315. if (currPetition.getState() == PetitionState.In_Process)
  316. return true;
  317. }
  318. return false;
  319. }
  320. public boolean isPetitionInProcess(int petitionId)
  321. {
  322. if (!isValidPetition(petitionId))
  323. return false;
  324. Petition currPetition = getPendingPetitions().get(petitionId);
  325. return (currPetition.getState() == PetitionState.In_Process);
  326. }
  327. public boolean isPlayerInConsultation(L2PcInstance player)
  328. {
  329. if (player != null)
  330. for (Petition currPetition : getPendingPetitions().values())
  331. {
  332. if (currPetition == null)
  333. continue;
  334. if (currPetition.getState() != PetitionState.In_Process)
  335. continue;
  336. if ((currPetition.getPetitioner() != null && currPetition.getPetitioner().getObjectId() == player.getObjectId())
  337. || (currPetition.getResponder() != null && currPetition.getResponder().getObjectId() == player.getObjectId()))
  338. return true;
  339. }
  340. return false;
  341. }
  342. public boolean isPetitioningAllowed()
  343. {
  344. return Config.PETITIONING_ALLOWED;
  345. }
  346. public boolean isPlayerPetitionPending(L2PcInstance petitioner)
  347. {
  348. if (petitioner != null)
  349. for (Petition currPetition : getPendingPetitions().values())
  350. {
  351. if (currPetition == null)
  352. continue;
  353. if (currPetition.getPetitioner() != null && currPetition.getPetitioner().getObjectId() == petitioner.getObjectId())
  354. return true;
  355. }
  356. return false;
  357. }
  358. private boolean isValidPetition(int petitionId)
  359. {
  360. return getPendingPetitions().containsKey(petitionId);
  361. }
  362. public boolean rejectPetition(L2PcInstance respondingAdmin, int petitionId)
  363. {
  364. if (!isValidPetition(petitionId))
  365. return false;
  366. Petition currPetition = getPendingPetitions().get(petitionId);
  367. if (currPetition.getResponder() != null)
  368. return false;
  369. currPetition.setResponder(respondingAdmin);
  370. return (currPetition.endPetitionConsultation(PetitionState.Responder_Reject));
  371. }
  372. public boolean sendActivePetitionMessage(L2PcInstance player, String messageText)
  373. {
  374. //if (!isPlayerInConsultation(player))
  375. //return false;
  376. CreatureSay cs;
  377. for (Petition currPetition : getPendingPetitions().values())
  378. {
  379. if (currPetition == null)
  380. continue;
  381. if (currPetition.getPetitioner() != null && currPetition.getPetitioner().getObjectId() == player.getObjectId())
  382. {
  383. cs = new CreatureSay(player.getObjectId(), Say2.PETITION_PLAYER, player.getName(), messageText);
  384. currPetition.addLogMessage(cs);
  385. currPetition.sendResponderPacket(cs);
  386. currPetition.sendPetitionerPacket(cs);
  387. return true;
  388. }
  389. if (currPetition.getResponder() != null && currPetition.getResponder().getObjectId() == player.getObjectId())
  390. {
  391. cs = new CreatureSay(player.getObjectId(), Say2.PETITION_GM, player.getName(), messageText);
  392. currPetition.addLogMessage(cs);
  393. currPetition.sendResponderPacket(cs);
  394. currPetition.sendPetitionerPacket(cs);
  395. return true;
  396. }
  397. }
  398. return false;
  399. }
  400. public void sendPendingPetitionList(L2PcInstance activeChar)
  401. {
  402. final StringBuilder htmlContent = StringUtil.startAppend(600 + getPendingPetitionCount() * 300, "<html><body><center><table width=270><tr>"
  403. + "<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>"
  404. + "<td width=180><center>Petition Menu</center></td>"
  405. + "<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>"
  406. + "<table width=\"270\">"
  407. + "<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>"
  408. + "<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>");
  409. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  410. if (getPendingPetitionCount() == 0)
  411. htmlContent.append("<tr><td>There are no currently pending petitions.</td></tr>");
  412. else
  413. htmlContent.append("<tr><td><font color=\"LEVEL\">Current Petitions:</font><br></td></tr>");
  414. boolean color = true;
  415. int petcount = 0;
  416. for (Petition currPetition : getPendingPetitions().values())
  417. {
  418. if (currPetition == null)
  419. continue;
  420. 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())));
  421. StringUtil.append(htmlContent,"</td><td width=\"140\" align=right><font color=\"",(currPetition.getPetitioner().isOnline() ? "00FF00" : "999999"),"\">",currPetition.getPetitioner().getName(),"</font></td></tr>");
  422. StringUtil.append(htmlContent,"<tr><td width=\"130\">");
  423. if (currPetition.getState() != PetitionState.In_Process)
  424. {
  425. StringUtil.append(htmlContent, "<table width=\"130\" cellpadding=\"2\"><tr>"
  426. + "<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>"
  427. + "<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>");
  428. }
  429. else
  430. htmlContent.append("<font color=\""+(currPetition.getResponder().isOnline() ? "00FF00" : "999999")+"\">"+currPetition.getResponder().getName()+"</font>");
  431. StringUtil.append(htmlContent,"</td>",currPetition.getTypeAsString(),"<td width=\"140\" align=right>",currPetition.getTypeAsString(),"</td></tr></table></td></tr>");
  432. color = !color;
  433. petcount++;
  434. if(petcount > 10)
  435. {
  436. htmlContent.append("<tr><td><font color=\"LEVEL\">There is more pending petition...</font><br></td></tr>");
  437. break;
  438. }
  439. }
  440. htmlContent.append("</table></center></body></html>");
  441. NpcHtmlMessage htmlMsg = new NpcHtmlMessage(0);
  442. htmlMsg.setHtml(htmlContent.toString());
  443. activeChar.sendPacket(htmlMsg);
  444. }
  445. public int submitPetition(L2PcInstance petitioner, String petitionText, int petitionType)
  446. {
  447. // Create a new petition instance and add it to the list of pending petitions.
  448. Petition newPetition = new Petition(petitioner, petitionText, petitionType);
  449. int newPetitionId = newPetition.getId();
  450. getPendingPetitions().put(newPetitionId, newPetition);
  451. // Notify all GMs that a new petition has been submitted.
  452. String msgContent = petitioner.getName() + " has submitted a new petition."; //(ID: " + newPetitionId + ").";
  453. AdminTable.getInstance().broadcastToGMs(new CreatureSay(petitioner.getObjectId(), Say2.HERO_VOICE, "Petition System", msgContent));
  454. return newPetitionId;
  455. }
  456. public void viewPetition(L2PcInstance activeChar, int petitionId)
  457. {
  458. if (!activeChar.isGM())
  459. return;
  460. if (!isValidPetition(petitionId))
  461. return;
  462. Petition currPetition = getPendingPetitions().get(petitionId);
  463. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  464. NpcHtmlMessage html = new NpcHtmlMessage(0);
  465. html.setFile(activeChar.getHtmlPrefix(), "data/html/admin/petition.htm");
  466. html.replace("%petition%", String.valueOf(currPetition.getId()));
  467. html.replace("%time%", dateFormat.format(new Date(currPetition.getSubmitTime())));
  468. html.replace("%type%", currPetition.getTypeAsString());
  469. html.replace("%petitioner%", currPetition.getPetitioner().getName());
  470. html.replace("%online%", (currPetition.getPetitioner().isOnline() ? "00FF00" : "999999"));
  471. html.replace("%text%", currPetition.getContent());
  472. activeChar.sendPacket(html);
  473. }
  474. @SuppressWarnings("synthetic-access")
  475. private static class SingletonHolder
  476. {
  477. protected static final PetitionManager _instance = new PetitionManager();
  478. }
  479. }