/* * Copyright (C) 2004-2013 L2J Server * * This file is part of L2J Server. * * L2J Server is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * L2J Server is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package com.l2jserver.gameserver.instancemanager; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; import com.l2jserver.Config; import com.l2jserver.gameserver.datatables.AdminTable; import com.l2jserver.gameserver.enums.PetitionState; import com.l2jserver.gameserver.model.Petition; import com.l2jserver.gameserver.model.actor.instance.L2PcInstance; import com.l2jserver.gameserver.network.SystemMessageId; import com.l2jserver.gameserver.network.clientpackets.Say2; import com.l2jserver.gameserver.network.serverpackets.CreatureSay; import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage; import com.l2jserver.gameserver.network.serverpackets.SystemMessage; import com.l2jserver.util.StringUtil; /** * Petition Manager * @author Tempy */ public final class PetitionManager { protected static final Logger _log = Logger.getLogger(PetitionManager.class.getName()); private final Map _pendingPetitions; private final Map _completedPetitions; protected PetitionManager() { _pendingPetitions = new HashMap<>(); _completedPetitions = new HashMap<>(); } public void clearCompletedPetitions() { final int numPetitions = getPendingPetitionCount(); getCompletedPetitions().clear(); _log.info(getClass().getSimpleName() + ": Completed petition data cleared. " + numPetitions + " petition(s) removed."); } public void clearPendingPetitions() { final int numPetitions = getPendingPetitionCount(); getPendingPetitions().clear(); _log.info(getClass().getSimpleName() + ": Pending petition queue cleared. " + numPetitions + " petition(s) removed."); } public boolean acceptPetition(L2PcInstance respondingAdmin, int petitionId) { if (!isValidPetition(petitionId)) { return false; } final Petition currPetition = getPendingPetitions().get(petitionId); if (currPetition.getResponder() != null) { return false; } currPetition.setResponder(respondingAdmin); currPetition.setState(PetitionState.IN_PROCESS); // Petition application accepted. (Send to Petitioner) currPetition.sendPetitionerPacket(SystemMessage.getSystemMessage(SystemMessageId.PETITION_APP_ACCEPTED)); // Petition application accepted. Reciept No. is SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PETITION_ACCEPTED_RECENT_NO_S1); sm.addNumber(currPetition.getId()); currPetition.sendResponderPacket(sm); // Petition consultation with underway. sm = SystemMessage.getSystemMessage(SystemMessageId.STARTING_PETITION_WITH_C1); sm.addString(currPetition.getPetitioner().getName()); currPetition.sendResponderPacket(sm); // Set responder name on petitioner instance currPetition.getPetitioner().setLastPetitionGmName(currPetition.getResponder().getName()); return true; } public boolean cancelActivePetition(L2PcInstance player) { for (Petition currPetition : getPendingPetitions().values()) { if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId())) { return (currPetition.endPetitionConsultation(PetitionState.PETITIONER_CANCEL)); } if ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId())) { return (currPetition.endPetitionConsultation(PetitionState.RESPONDER_CANCEL)); } } return false; } public void checkPetitionMessages(L2PcInstance petitioner) { if (petitioner != null) { for (Petition currPetition : getPendingPetitions().values()) { if (currPetition == null) { continue; } if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == petitioner.getObjectId())) { for (CreatureSay logMessage : currPetition.getLogMessages()) { petitioner.sendPacket(logMessage); } return; } } } } public boolean endActivePetition(L2PcInstance player) { if (!player.isGM()) { return false; } for (Petition currPetition : getPendingPetitions().values()) { if (currPetition == null) { continue; } if ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId())) { return (currPetition.endPetitionConsultation(PetitionState.COMPLETED)); } } return false; } public Map getCompletedPetitions() { return _completedPetitions; } public Map getPendingPetitions() { return _pendingPetitions; } public int getPendingPetitionCount() { return getPendingPetitions().size(); } public int getPlayerTotalPetitionCount(L2PcInstance player) { if (player == null) { return 0; } int petitionCount = 0; for (Petition currPetition : getPendingPetitions().values()) { if (currPetition == null) { continue; } if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId())) { petitionCount++; } } for (Petition currPetition : getCompletedPetitions().values()) { if (currPetition == null) { continue; } if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId())) { petitionCount++; } } return petitionCount; } public boolean isPetitionInProcess() { for (Petition currPetition : getPendingPetitions().values()) { if (currPetition == null) { continue; } if (currPetition.getState() == PetitionState.IN_PROCESS) { return true; } } return false; } public boolean isPetitionInProcess(int petitionId) { if (!isValidPetition(petitionId)) { return false; } final Petition currPetition = getPendingPetitions().get(petitionId); return (currPetition.getState() == PetitionState.IN_PROCESS); } public boolean isPlayerInConsultation(L2PcInstance player) { if (player != null) { for (Petition currPetition : getPendingPetitions().values()) { if (currPetition == null) { continue; } if (currPetition.getState() != PetitionState.IN_PROCESS) { continue; } if (((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId())) || ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId()))) { return true; } } } return false; } public boolean isPetitioningAllowed() { return Config.PETITIONING_ALLOWED; } public boolean isPlayerPetitionPending(L2PcInstance petitioner) { if (petitioner != null) { for (Petition currPetition : getPendingPetitions().values()) { if (currPetition == null) { continue; } if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == petitioner.getObjectId())) { return true; } } } return false; } private boolean isValidPetition(int petitionId) { return getPendingPetitions().containsKey(petitionId); } public boolean rejectPetition(L2PcInstance respondingAdmin, int petitionId) { if (!isValidPetition(petitionId)) { return false; } final Petition currPetition = getPendingPetitions().get(petitionId); if (currPetition.getResponder() != null) { return false; } currPetition.setResponder(respondingAdmin); return (currPetition.endPetitionConsultation(PetitionState.RESPONDER_REJECT)); } public boolean sendActivePetitionMessage(L2PcInstance player, String messageText) { // if (!isPlayerInConsultation(player)) // return false; CreatureSay cs; for (Petition currPetition : getPendingPetitions().values()) { if (currPetition == null) { continue; } if ((currPetition.getPetitioner() != null) && (currPetition.getPetitioner().getObjectId() == player.getObjectId())) { cs = new CreatureSay(player.getObjectId(), Say2.PETITION_PLAYER, player.getName(), messageText); currPetition.addLogMessage(cs); currPetition.sendResponderPacket(cs); currPetition.sendPetitionerPacket(cs); return true; } if ((currPetition.getResponder() != null) && (currPetition.getResponder().getObjectId() == player.getObjectId())) { cs = new CreatureSay(player.getObjectId(), Say2.PETITION_GM, player.getName(), messageText); currPetition.addLogMessage(cs); currPetition.sendResponderPacket(cs); currPetition.sendPetitionerPacket(cs); return true; } } return false; } public void sendPendingPetitionList(L2PcInstance activeChar) { final StringBuilder htmlContent = StringUtil.startAppend(600 + (getPendingPetitionCount() * 300), "
" + "" + "" + "
Petition Menu

" + "" + ""); final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); if (getPendingPetitionCount() == 0) { htmlContent.append(""); } else { htmlContent.append(""); } boolean color = true; int petcount = 0; for (Petition currPetition : getPendingPetitions().values()) { if (currPetition == null) { continue; } StringUtil.append(htmlContent, ""); color = !color; petcount++; if (petcount > 10) { htmlContent.append(""); break; } } htmlContent.append("
" + "

There are no currently pending petitions.
Current Petitions:
"); StringUtil.append(htmlContent, "", currPetition.getTypeAsString(), "
", dateFormat.format(new Date(currPetition.getSubmitTime()))); StringUtil.append(htmlContent, "", currPetition.getPetitioner().getName(), "
"); if (currPetition.getState() != PetitionState.IN_PROCESS) { StringUtil.append(htmlContent, "" + "" + "
"); } else { htmlContent.append("" + currPetition.getResponder().getName() + ""); } StringUtil.append(htmlContent, "
", currPetition.getTypeAsString(), "
There is more pending petition...
"); final NpcHtmlMessage htmlMsg = new NpcHtmlMessage(0); htmlMsg.setHtml(htmlContent.toString()); activeChar.sendPacket(htmlMsg); } public int submitPetition(L2PcInstance petitioner, String petitionText, int petitionType) { // Create a new petition instance and add it to the list of pending petitions. final Petition newPetition = new Petition(petitioner, petitionText, petitionType); int newPetitionId = newPetition.getId(); getPendingPetitions().put(newPetitionId, newPetition); // Notify all GMs that a new petition has been submitted. final String msgContent = petitioner.getName() + " has submitted a new petition."; // (ID: " + newPetitionId + ")."; AdminTable.getInstance().broadcastToGMs(new CreatureSay(petitioner.getObjectId(), Say2.HERO_VOICE, "Petition System", msgContent)); return newPetitionId; } public void viewPetition(L2PcInstance activeChar, int petitionId) { if (!activeChar.isGM()) { return; } if (!isValidPetition(petitionId)) { return; } final Petition currPetition = getPendingPetitions().get(petitionId); final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); final NpcHtmlMessage html = new NpcHtmlMessage(0); html.setFile(activeChar.getHtmlPrefix(), "data/html/admin/petition.htm"); html.replace("%petition%", String.valueOf(currPetition.getId())); html.replace("%time%", dateFormat.format(new Date(currPetition.getSubmitTime()))); html.replace("%type%", currPetition.getTypeAsString()); html.replace("%petitioner%", currPetition.getPetitioner().getName()); html.replace("%online%", (currPetition.getPetitioner().isOnline() ? "00FF00" : "999999")); html.replace("%text%", currPetition.getContent()); activeChar.sendPacket(html); } /** * Gets the single instance of {@code PetitionManager}. * @return single instance of {@code PetitionManager} */ public final static PetitionManager getInstance() { return SingletonHolder._instance; } private static class SingletonHolder { protected static final PetitionManager _instance = new PetitionManager(); } }