PetitionManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright © 2004-2020 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 static com.l2jserver.gameserver.config.Configuration.character;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.gameserver.data.xml.impl.AdminData;
  27. import com.l2jserver.gameserver.enums.PetitionState;
  28. import com.l2jserver.gameserver.model.Petition;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.network.SystemMessageId;
  31. import com.l2jserver.gameserver.network.clientpackets.Say2;
  32. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  33. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  34. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  35. import com.l2jserver.gameserver.util.StringUtil;
  36. /**
  37. * Petition Manager
  38. * @author Tempy
  39. */
  40. public final class PetitionManager {
  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. protected PetitionManager() {
  45. _pendingPetitions = new HashMap<>();
  46. _completedPetitions = new HashMap<>();
  47. }
  48. public void clearCompletedPetitions() {
  49. final int numPetitions = getPendingPetitionCount();
  50. getCompletedPetitions().clear();
  51. _log.info(getClass().getSimpleName() + ": Completed petition data cleared. " + numPetitions + " petition(s) removed.");
  52. }
  53. public void clearPendingPetitions() {
  54. final int numPetitions = getPendingPetitionCount();
  55. getPendingPetitions().clear();
  56. _log.info(getClass().getSimpleName() + ": Pending petition queue cleared. " + numPetitions + " petition(s) removed.");
  57. }
  58. public boolean acceptPetition(L2PcInstance respondingAdmin, int petitionId) {
  59. if (!isValidPetition(petitionId)) {
  60. return false;
  61. }
  62. final Petition currPetition = getPendingPetitions().get(petitionId);
  63. if (currPetition.getResponder() != null) {
  64. return false;
  65. }
  66. currPetition.setResponder(respondingAdmin);
  67. currPetition.setState(PetitionState.IN_PROCESS);
  68. // Petition application accepted. (Send to Petitioner)
  69. currPetition.sendPetitionerPacket(SystemMessage.getSystemMessage(SystemMessageId.PETITION_APP_ACCEPTED));
  70. // Petition application accepted. Reciept No. is <ID>
  71. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PETITION_ACCEPTED_RECENT_NO_S1);
  72. sm.addInt(currPetition.getId());
  73. currPetition.sendResponderPacket(sm);
  74. // Petition consultation with <Player> underway.
  75. sm = SystemMessage.getSystemMessage(SystemMessageId.STARTING_PETITION_WITH_C1);
  76. sm.addString(currPetition.getPetitioner().getName());
  77. currPetition.sendResponderPacket(sm);
  78. // Set responder name on petitioner instance
  79. currPetition.getPetitioner().setLastPetitionGmName(currPetition.getResponder().getName());
  80. return true;
  81. }
  82. public boolean cancelActivePetition(L2PcInstance player) {
  83. for (Petition currPetition : getPendingPetitions().values()) {
  84. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId())) {
  85. return (currPetition.endPetitionConsultation(PetitionState.PETITIONER_CANCEL));
  86. }
  87. if ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId())) {
  88. return (currPetition.endPetitionConsultation(PetitionState.RESPONDER_CANCEL));
  89. }
  90. }
  91. return false;
  92. }
  93. public void checkPetitionMessages(L2PcInstance petitioner) {
  94. if (petitioner != null) {
  95. for (Petition currPetition : getPendingPetitions().values()) {
  96. if (currPetition == null) {
  97. continue;
  98. }
  99. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == petitioner.getObjectId())) {
  100. for (CreatureSay logMessage : currPetition.getLogMessages()) {
  101. petitioner.sendPacket(logMessage);
  102. }
  103. return;
  104. }
  105. }
  106. }
  107. }
  108. public boolean endActivePetition(L2PcInstance player) {
  109. if (!player.isGM()) {
  110. return false;
  111. }
  112. for (Petition currPetition : getPendingPetitions().values()) {
  113. if (currPetition == null) {
  114. continue;
  115. }
  116. if ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId())) {
  117. return (currPetition.endPetitionConsultation(PetitionState.COMPLETED));
  118. }
  119. }
  120. return false;
  121. }
  122. public Map<Integer, Petition> getCompletedPetitions() {
  123. return _completedPetitions;
  124. }
  125. public Map<Integer, Petition> getPendingPetitions() {
  126. return _pendingPetitions;
  127. }
  128. public int getPendingPetitionCount() {
  129. return getPendingPetitions().size();
  130. }
  131. public int getPlayerTotalPetitionCount(L2PcInstance player) {
  132. if (player == null) {
  133. return 0;
  134. }
  135. int petitionCount = 0;
  136. for (Petition currPetition : getPendingPetitions().values()) {
  137. if (currPetition == null) {
  138. continue;
  139. }
  140. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId())) {
  141. petitionCount++;
  142. }
  143. }
  144. for (Petition currPetition : getCompletedPetitions().values()) {
  145. if (currPetition == null) {
  146. continue;
  147. }
  148. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId())) {
  149. petitionCount++;
  150. }
  151. }
  152. return petitionCount;
  153. }
  154. public boolean isPetitionInProcess() {
  155. for (Petition currPetition : getPendingPetitions().values()) {
  156. if (currPetition == null) {
  157. continue;
  158. }
  159. if (currPetition.getState() == PetitionState.IN_PROCESS) {
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. public boolean isPetitionInProcess(int petitionId) {
  166. if (!isValidPetition(petitionId)) {
  167. return false;
  168. }
  169. final Petition currPetition = getPendingPetitions().get(petitionId);
  170. return (currPetition.getState() == PetitionState.IN_PROCESS);
  171. }
  172. public boolean isPlayerInConsultation(L2PcInstance player) {
  173. if (player != null) {
  174. for (Petition currPetition : getPendingPetitions().values()) {
  175. if (currPetition == null) {
  176. continue;
  177. }
  178. if (currPetition.getState() != PetitionState.IN_PROCESS) {
  179. continue;
  180. }
  181. if (((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId())) || ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId()))) {
  182. return true;
  183. }
  184. }
  185. }
  186. return false;
  187. }
  188. public boolean isPetitioningAllowed() {
  189. return character().petitioningAllowed();
  190. }
  191. public boolean isPlayerPetitionPending(L2PcInstance petitioner) {
  192. if (petitioner != null) {
  193. for (Petition currPetition : getPendingPetitions().values()) {
  194. if (currPetition == null) {
  195. continue;
  196. }
  197. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == petitioner.getObjectId())) {
  198. return true;
  199. }
  200. }
  201. }
  202. return false;
  203. }
  204. private boolean isValidPetition(int petitionId) {
  205. return getPendingPetitions().containsKey(petitionId);
  206. }
  207. public boolean rejectPetition(L2PcInstance respondingAdmin, int petitionId) {
  208. if (!isValidPetition(petitionId)) {
  209. return false;
  210. }
  211. final Petition currPetition = getPendingPetitions().get(petitionId);
  212. if (currPetition.getResponder() != null) {
  213. return false;
  214. }
  215. currPetition.setResponder(respondingAdmin);
  216. return (currPetition.endPetitionConsultation(PetitionState.RESPONDER_REJECT));
  217. }
  218. public boolean sendActivePetitionMessage(L2PcInstance player, String messageText) {
  219. // if (!isPlayerInConsultation(player))
  220. // return false;
  221. CreatureSay cs;
  222. for (Petition currPetition : getPendingPetitions().values()) {
  223. if (currPetition == null) {
  224. continue;
  225. }
  226. if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId())) {
  227. cs = new CreatureSay(player.getObjectId(), Say2.PETITION_PLAYER, player.getName(), messageText);
  228. currPetition.addLogMessage(cs);
  229. currPetition.sendResponderPacket(cs);
  230. currPetition.sendPetitionerPacket(cs);
  231. return true;
  232. }
  233. if ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId())) {
  234. cs = new CreatureSay(player.getObjectId(), Say2.PETITION_GM, player.getName(), messageText);
  235. currPetition.addLogMessage(cs);
  236. currPetition.sendResponderPacket(cs);
  237. currPetition.sendPetitionerPacket(cs);
  238. return true;
  239. }
  240. }
  241. return false;
  242. }
  243. public void sendPendingPetitionList(L2PcInstance activeChar) {
  244. final StringBuilder htmlContent = StringUtil.startAppend(600 + (getPendingPetitionCount() * 300), "<html><body><center><table width=270><tr>"
  245. + "<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>"
  246. + "<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\">"
  247. + "<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>"
  248. + "<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>");
  249. final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  250. if (getPendingPetitionCount() == 0) {
  251. htmlContent.append("<tr><td>There are no currently pending petitions.</td></tr>");
  252. } else {
  253. htmlContent.append("<tr><td><font color=\"LEVEL\">Current Petitions:</font><br></td></tr>");
  254. }
  255. boolean color = true;
  256. int petcount = 0;
  257. for (Petition currPetition : getPendingPetitions().values()) {
  258. if (currPetition == null) {
  259. continue;
  260. }
  261. 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())));
  262. StringUtil.append(htmlContent, "</td><td width=\"140\" align=right><font color=\"", (currPetition.getPetitioner().isOnline() ? "00FF00" : "999999"), "\">", currPetition.getPetitioner().getName(), "</font></td></tr>");
  263. StringUtil.append(htmlContent, "<tr><td width=\"130\">");
  264. if (currPetition.getState() != PetitionState.IN_PROCESS) {
  265. 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>"
  266. + "<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>");
  267. } else {
  268. htmlContent.append("<font color=\"" + (currPetition.getResponder().isOnline() ? "00FF00" : "999999") + "\">" + currPetition.getResponder().getName() + "</font>");
  269. }
  270. StringUtil.append(htmlContent, "</td>", currPetition.getTypeAsString(), "<td width=\"140\" align=right>", currPetition.getTypeAsString(), "</td></tr></table></td></tr>");
  271. color = !color;
  272. petcount++;
  273. if (petcount > 10) {
  274. htmlContent.append("<tr><td><font color=\"LEVEL\">There is more pending petition...</font><br></td></tr>");
  275. break;
  276. }
  277. }
  278. htmlContent.append("</table></center></body></html>");
  279. final NpcHtmlMessage htmlMsg = new NpcHtmlMessage();
  280. htmlMsg.setHtml(htmlContent.toString());
  281. activeChar.sendPacket(htmlMsg);
  282. }
  283. public int submitPetition(L2PcInstance petitioner, String petitionText, int petitionType) {
  284. // Create a new petition instance and add it to the list of pending petitions.
  285. final Petition newPetition = new Petition(petitioner, petitionText, petitionType);
  286. int newPetitionId = newPetition.getId();
  287. getPendingPetitions().put(newPetitionId, newPetition);
  288. // Notify all GMs that a new petition has been submitted.
  289. final String msgContent = petitioner.getName() + " has submitted a new petition."; // (ID: " + newPetitionId + ").";
  290. AdminData.getInstance().broadcastToGMs(new CreatureSay(petitioner.getObjectId(), Say2.HERO_VOICE, "Petition System", msgContent));
  291. return newPetitionId;
  292. }
  293. public void viewPetition(L2PcInstance activeChar, int petitionId) {
  294. if (!activeChar.isGM()) {
  295. return;
  296. }
  297. if (!isValidPetition(petitionId)) {
  298. return;
  299. }
  300. final Petition currPetition = getPendingPetitions().get(petitionId);
  301. final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  302. final NpcHtmlMessage html = new NpcHtmlMessage();
  303. html.setFile(activeChar.getHtmlPrefix(), "data/html/admin/petition.htm");
  304. html.replace("%petition%", String.valueOf(currPetition.getId()));
  305. html.replace("%time%", dateFormat.format(new Date(currPetition.getSubmitTime())));
  306. html.replace("%type%", currPetition.getTypeAsString());
  307. html.replace("%petitioner%", currPetition.getPetitioner().getName());
  308. html.replace("%online%", (currPetition.getPetitioner().isOnline() ? "00FF00" : "999999"));
  309. html.replace("%text%", currPetition.getContent());
  310. activeChar.sendPacket(html);
  311. }
  312. /**
  313. * Gets the single instance of {@code PetitionManager}.
  314. * @return single instance of {@code PetitionManager}
  315. */
  316. public static PetitionManager getInstance() {
  317. return SingletonHolder._instance;
  318. }
  319. private static class SingletonHolder {
  320. protected static final PetitionManager _instance = new PetitionManager();
  321. }
  322. }