2
0

PetitionManager.java 18 KB

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