Files
TrinityCore/src/server/scripts/Commands/cs_ticket.cpp
DDuarte 2ea31027e5 Core: Backport 6.x realm changes
Make acessible all the info about current realm (e.g name) anywhere, not only realm id
Reduce the number of differences between the two branches

Original changes by Shauren

Partial port of bacc90b6ba and 63def8aa32
2016-03-06 19:46:59 +00:00

527 lines
19 KiB
C++

/*
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
*
* This program 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 2 of the License, or (at your
* option) any later version.
*
* This program 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 <http://www.gnu.org/licenses/>.
*/
/* ScriptData
Name: ticket_commandscript
%Complete: 100
Comment: All ticket related commands
Category: commandscripts
EndScriptData */
#include "AccountMgr.h"
#include "Chat.h"
#include "Language.h"
#include "ObjectMgr.h"
#include "Opcodes.h"
#include "Player.h"
#include "TicketMgr.h"
#include "ScriptMgr.h"
class ticket_commandscript : public CommandScript
{
public:
ticket_commandscript() : CommandScript("ticket_commandscript") { }
std::vector<ChatCommand> GetCommands() const override
{
static std::vector<ChatCommand> ticketResponseCommandTable =
{
{ "append", rbac::RBAC_PERM_COMMAND_TICKET_RESPONSE_APPEND, true, &HandleGMTicketResponseAppendCommand, "" },
{ "appendln", rbac::RBAC_PERM_COMMAND_TICKET_RESPONSE_APPENDLN, true, &HandleGMTicketResponseAppendLnCommand, "" },
};
static std::vector<ChatCommand> ticketCommandTable =
{
{ "assign", rbac::RBAC_PERM_COMMAND_TICKET_ASSIGN, true, &HandleGMTicketAssignToCommand, "" },
{ "close", rbac::RBAC_PERM_COMMAND_TICKET_CLOSE, true, &HandleGMTicketCloseByIdCommand, "" },
{ "closedlist", rbac::RBAC_PERM_COMMAND_TICKET_CLOSEDLIST, true, &HandleGMTicketListClosedCommand, "" },
{ "comment", rbac::RBAC_PERM_COMMAND_TICKET_COMMENT, true, &HandleGMTicketCommentCommand, "" },
{ "complete", rbac::RBAC_PERM_COMMAND_TICKET_COMPLETE, true, &HandleGMTicketCompleteCommand, "" },
{ "delete", rbac::RBAC_PERM_COMMAND_TICKET_DELETE, true, &HandleGMTicketDeleteByIdCommand, "" },
{ "escalate", rbac::RBAC_PERM_COMMAND_TICKET_ESCALATE, true, &HandleGMTicketEscalateCommand, "" },
{ "escalatedlist", rbac::RBAC_PERM_COMMAND_TICKET_ESCALATEDLIST, true, &HandleGMTicketListEscalatedCommand, "" },
{ "list", rbac::RBAC_PERM_COMMAND_TICKET_LIST, true, &HandleGMTicketListCommand, "" },
{ "onlinelist", rbac::RBAC_PERM_COMMAND_TICKET_ONLINELIST, true, &HandleGMTicketListOnlineCommand, "" },
{ "reset", rbac::RBAC_PERM_COMMAND_TICKET_RESET, true, &HandleGMTicketResetCommand, "" },
{ "response", rbac::RBAC_PERM_COMMAND_TICKET_RESPONSE, true, NULL, "", ticketResponseCommandTable },
{ "togglesystem", rbac::RBAC_PERM_COMMAND_TICKET_TOGGLESYSTEM, true, &HandleToggleGMTicketSystem, "" },
{ "unassign", rbac::RBAC_PERM_COMMAND_TICKET_UNASSIGN, true, &HandleGMTicketUnAssignCommand, "" },
{ "viewid", rbac::RBAC_PERM_COMMAND_TICKET_VIEWID, true, &HandleGMTicketGetByIdCommand, "" },
{ "viewname", rbac::RBAC_PERM_COMMAND_TICKET_VIEWNAME, true, &HandleGMTicketGetByNameCommand, "" },
};
static std::vector<ChatCommand> commandTable =
{
{ "ticket", rbac::RBAC_PERM_COMMAND_TICKET, false, NULL, "", ticketCommandTable },
};
return commandTable;
}
static bool HandleGMTicketAssignToCommand(ChatHandler* handler, char const* args)
{
if (!*args)
return false;
char* ticketIdStr = strtok((char*)args, " ");
uint32 ticketId = atoi(ticketIdStr);
char* targetStr = strtok(NULL, " ");
if (!targetStr)
return false;
std::string target(targetStr);
if (!normalizePlayerName(target))
return false;
GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
if (!ticket || ticket->IsClosed())
{
handler->SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
return true;
}
ObjectGuid targetGuid = sObjectMgr->GetPlayerGUIDByName(target);
uint32 accountId = sObjectMgr->GetPlayerAccountIdByGUID(targetGuid);
// Target must exist and have administrative rights
if (!AccountMgr::HasPermission(accountId, rbac::RBAC_PERM_COMMANDS_BE_ASSIGNED_TICKET, realm.Id.Realm))
{
handler->SendSysMessage(LANG_COMMAND_TICKETASSIGNERROR_A);
return true;
}
// If already assigned, leave
if (ticket->IsAssignedTo(targetGuid))
{
handler->PSendSysMessage(LANG_COMMAND_TICKETASSIGNERROR_B, ticket->GetId());
return true;
}
// If assigned to different player other than current, leave
//! Console can override though
Player* player = handler->GetSession() ? handler->GetSession()->GetPlayer() : nullptr;
if (player && ticket->IsAssignedNotTo(player->GetGUID()))
{
handler->PSendSysMessage(LANG_COMMAND_TICKETALREADYASSIGNED, ticket->GetId(), target.c_str());
return true;
}
// Assign ticket
SQLTransaction trans = SQLTransaction(NULL);
ticket->SetAssignedTo(targetGuid, AccountMgr::IsAdminAccount(AccountMgr::GetSecurity(accountId, realm.Id.Realm)));
ticket->SaveToDB(trans);
sTicketMgr->UpdateLastChange();
std::string msg = ticket->FormatMessageString(*handler, NULL, target.c_str(), NULL, NULL, NULL);
handler->SendGlobalGMSysMessage(msg.c_str());
return true;
}
static bool HandleGMTicketCloseByIdCommand(ChatHandler* handler, char const* args)
{
if (!*args)
return false;
uint32 ticketId = atoi(args);
GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
if (!ticket || ticket->IsClosed() || ticket->IsCompleted())
{
handler->SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
return true;
}
// Ticket should be assigned to the player who tries to close it.
// Console can override though
Player* player = handler->GetSession() ? handler->GetSession()->GetPlayer() : nullptr;
if (player && ticket->IsAssignedNotTo(player->GetGUID()))
{
handler->PSendSysMessage(LANG_COMMAND_TICKETCANNOTCLOSE, ticket->GetId());
return true;
}
sTicketMgr->ResolveAndCloseTicket(ticket->GetId(), player ? player->GetGUID() : ObjectGuid(uint64(0)));
sTicketMgr->UpdateLastChange();
std::string msg = ticket->FormatMessageString(*handler, player ? player->GetName().c_str() : "Console", NULL, NULL, NULL, NULL);
handler->SendGlobalGMSysMessage(msg.c_str());
// Inform player, who submitted this ticket, that it is closed
if (Player* submitter = ticket->GetPlayer())
{
WorldPacket data(SMSG_GMTICKET_DELETETICKET, 4);
data << uint32(GMTICKET_RESPONSE_TICKET_DELETED);
submitter->GetSession()->SendPacket(&data);
}
return true;
}
static bool HandleGMTicketCommentCommand(ChatHandler* handler, char const* args)
{
if (!*args)
return false;
char* ticketIdStr = strtok((char*)args, " ");
uint32 ticketId = atoi(ticketIdStr);
char* comment = strtok(NULL, "\n");
if (!comment)
return false;
GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
if (!ticket || ticket->IsClosed())
{
handler->PSendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
return true;
}
// Cannot comment ticket assigned to someone else
//! Console excluded
Player* player = handler->GetSession() ? handler->GetSession()->GetPlayer() : nullptr;
if (player && ticket->IsAssignedNotTo(player->GetGUID()))
{
handler->PSendSysMessage(LANG_COMMAND_TICKETALREADYASSIGNED, ticket->GetId());
return true;
}
SQLTransaction trans = SQLTransaction(NULL);
ticket->SetComment(comment);
ticket->SaveToDB(trans);
sTicketMgr->UpdateLastChange();
std::string msg = ticket->FormatMessageString(*handler, NULL, ticket->GetAssignedToName().c_str(), NULL, NULL, NULL);
msg += handler->PGetParseString(LANG_COMMAND_TICKETLISTADDCOMMENT, player ? player->GetName().c_str() : "Console", comment);
handler->SendGlobalGMSysMessage(msg.c_str());
return true;
}
static bool HandleGMTicketListClosedCommand(ChatHandler* handler, char const* /*args*/)
{
sTicketMgr->ShowClosedList(*handler);
return true;
}
static bool HandleGMTicketCompleteCommand(ChatHandler* handler, char const* args)
{
if (!*args)
return false;
char* ticketIdStr = strtok((char*)args, " ");
uint32 ticketId = atoi(ticketIdStr);
GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
if (!ticket || ticket->IsClosed() || ticket->IsCompleted())
{
handler->SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
return true;
}
char* response = strtok(NULL, "\n");
if (response)
{
// Cannot add response to ticket, assigned to someone else
//! Console excluded
Player* player = handler->GetSession() ? handler->GetSession()->GetPlayer() : nullptr;
if (player && ticket->IsAssignedNotTo(player->GetGUID()))
{
handler->PSendSysMessage(LANG_COMMAND_TICKETALREADYASSIGNED, ticket->GetId());
return true;
}
ticket->AppendResponse(response);
}
if (Player* player = ticket->GetPlayer())
ticket->SendResponse(player->GetSession());
Player* gm = handler->GetSession() ? handler->GetSession()->GetPlayer() : nullptr;
SQLTransaction trans = SQLTransaction(NULL);
ticket->SetResolvedBy(gm ? gm->GetGUID() : ObjectGuid(uint64(0)));
ticket->SetCompleted();
ticket->SaveToDB(trans);
std::string msg = ticket->FormatMessageString(*handler, NULL, NULL,
NULL, NULL, handler->GetSession() ? handler->GetSession()->GetPlayer()->GetName().c_str() : "Console");
handler->SendGlobalGMSysMessage(msg.c_str());
sTicketMgr->UpdateLastChange();
return true;
}
static bool HandleGMTicketDeleteByIdCommand(ChatHandler* handler, char const* args)
{
if (!*args)
return false;
uint32 ticketId = atoi(args);
GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
if (!ticket)
{
handler->SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
return true;
}
if (!ticket->IsClosed())
{
handler->SendSysMessage(LANG_COMMAND_TICKETCLOSEFIRST);
return true;
}
std::string msg = ticket->FormatMessageString(*handler, NULL, NULL, NULL, handler->GetSession() ? handler->GetSession()->GetPlayer()->GetName().c_str() : "Console", NULL);
handler->SendGlobalGMSysMessage(msg.c_str());
sTicketMgr->RemoveTicket(ticket->GetId());
sTicketMgr->UpdateLastChange();
if (Player* player = ticket->GetPlayer())
{
// Force abandon ticket
WorldPacket data(SMSG_GMTICKET_DELETETICKET, 4);
data << uint32(GMTICKET_RESPONSE_TICKET_DELETED);
player->GetSession()->SendPacket(&data);
}
return true;
}
static bool HandleGMTicketEscalateCommand(ChatHandler* handler, char const* args)
{
if (!*args)
return false;
uint32 ticketId = atoi(args);
GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
if (!ticket || ticket->IsClosed() || ticket->IsCompleted() || ticket->GetEscalatedStatus() != TICKET_UNASSIGNED)
{
handler->SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
return true;
}
ticket->SetEscalatedStatus(TICKET_IN_ESCALATION_QUEUE);
if (Player* player = ticket->GetPlayer())
sTicketMgr->SendTicket(player->GetSession(), ticket);
sTicketMgr->UpdateLastChange();
return true;
}
static bool HandleGMTicketListEscalatedCommand(ChatHandler* handler, char const* /*args*/)
{
sTicketMgr->ShowEscalatedList(*handler);
return true;
}
static bool HandleGMTicketListCommand(ChatHandler* handler, char const* /*args*/)
{
sTicketMgr->ShowList(*handler, false);
return true;
}
static bool HandleGMTicketListOnlineCommand(ChatHandler* handler, char const* /*args*/)
{
sTicketMgr->ShowList(*handler, true);
return true;
}
static bool HandleGMTicketResetCommand(ChatHandler* handler, char const* /*args*/)
{
if (sTicketMgr->GetOpenTicketCount())
{
handler->SendSysMessage(LANG_COMMAND_TICKETPENDING);
return true;
}
else
{
sTicketMgr->ResetTickets();
handler->SendSysMessage(LANG_COMMAND_TICKETRESET);
}
return true;
}
static bool HandleToggleGMTicketSystem(ChatHandler* handler, char const* /*args*/)
{
bool status = !sTicketMgr->GetStatus();
sTicketMgr->SetStatus(status);
handler->PSendSysMessage(status ? LANG_ALLOW_TICKETS : LANG_DISALLOW_TICKETS);
return true;
}
static bool HandleGMTicketUnAssignCommand(ChatHandler* handler, char const* args)
{
if (!*args)
return false;
uint32 ticketId = atoi(args);
GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
if (!ticket || ticket->IsClosed())
{
handler->SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
return true;
}
// Ticket must be assigned
if (!ticket->IsAssigned())
{
handler->PSendSysMessage(LANG_COMMAND_TICKETNOTASSIGNED, ticket->GetId());
return true;
}
// Get security level of player, whom this ticket is assigned to
uint32 security = SEC_PLAYER;
Player* assignedPlayer = ticket->GetAssignedPlayer();
if (assignedPlayer)
security = assignedPlayer->GetSession()->GetSecurity();
else
{
ObjectGuid guid = ticket->GetAssignedToGUID();
uint32 accountId = sObjectMgr->GetPlayerAccountIdByGUID(guid);
security = AccountMgr::GetSecurity(accountId, realm.Id.Realm);
}
// Check security
//! If no m_session present it means we're issuing this command from the console
uint32 mySecurity = handler->GetSession() ? handler->GetSession()->GetSecurity() : SEC_CONSOLE;
if (security > mySecurity)
{
handler->SendSysMessage(LANG_COMMAND_TICKETUNASSIGNSECURITY);
return true;
}
std::string assignedTo = ticket->GetAssignedToName(); // copy assignedto name because we need it after the ticket has been unnassigned
SQLTransaction trans = SQLTransaction(NULL);
ticket->SetUnassigned();
ticket->SaveToDB(trans);
sTicketMgr->UpdateLastChange();
std::string msg = ticket->FormatMessageString(*handler, NULL, assignedTo.c_str(),
handler->GetSession() ? handler->GetSession()->GetPlayer()->GetName().c_str() : "Console", NULL, NULL);
handler->SendGlobalGMSysMessage(msg.c_str());
return true;
}
static bool HandleGMTicketGetByIdCommand(ChatHandler* handler, char const* args)
{
if (!*args)
return false;
uint32 ticketId = atoi(args);
GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
if (!ticket || ticket->IsClosed() || ticket->IsCompleted())
{
handler->SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
return true;
}
SQLTransaction trans = SQLTransaction(NULL);
ticket->SetViewed();
ticket->SaveToDB(trans);
handler->SendSysMessage(ticket->FormatMessageString(*handler, true).c_str());
return true;
}
static bool HandleGMTicketGetByNameCommand(ChatHandler* handler, char const* args)
{
if (!*args)
return false;
std::string name(args);
if (!normalizePlayerName(name))
return false;
// Detect target's GUID
ObjectGuid guid;
if (Player* player = ObjectAccessor::FindPlayerByName(name))
guid = player->GetGUID();
else
guid = sObjectMgr->GetPlayerGUIDByName(name);
// Target must exist
if (!guid)
{
handler->SendSysMessage(LANG_NO_PLAYERS_FOUND);
return true;
}
// Ticket must exist
GmTicket* ticket = sTicketMgr->GetTicketByPlayer(guid);
if (!ticket)
{
handler->SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
return true;
}
SQLTransaction trans = SQLTransaction(NULL);
ticket->SetViewed();
ticket->SaveToDB(trans);
handler->SendSysMessage(ticket->FormatMessageString(*handler, true).c_str());
return true;
}
static bool _HandleGMTicketResponseAppendCommand(char const* args, bool newLine, ChatHandler* handler)
{
if (!*args)
return false;
char* ticketIdStr = strtok((char*)args, " ");
uint32 ticketId = atoi(ticketIdStr);
char* response = strtok(NULL, "\n");
if (!response)
return false;
GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
if (!ticket || ticket->IsClosed())
{
handler->PSendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
return true;
}
// Cannot add response to ticket, assigned to someone else
//! Console excluded
Player* player = handler->GetSession() ? handler->GetSession()->GetPlayer() : nullptr;
if (player && ticket->IsAssignedNotTo(player->GetGUID()))
{
handler->PSendSysMessage(LANG_COMMAND_TICKETALREADYASSIGNED, ticket->GetId());
return true;
}
SQLTransaction trans = SQLTransaction(NULL);
ticket->AppendResponse(response);
if (newLine)
ticket->AppendResponse("\n");
ticket->SaveToDB(trans);
return true;
}
static bool HandleGMTicketResponseAppendCommand(ChatHandler* handler, char const* args)
{
return _HandleGMTicketResponseAppendCommand(args, false, handler);
}
static bool HandleGMTicketResponseAppendLnCommand(ChatHandler* handler, char const* args)
{
return _HandleGMTicketResponseAppendCommand(args, true, handler);
}
};
void AddSC_ticket_commandscript()
{
new ticket_commandscript();
}