PageRenderTime 723ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/src/server/game/Chat/Commands/TicketCommands.cpp

http://github.com/darkman1983/TrinityCore
C++ | 457 lines | 346 code | 74 blank | 37 comment | 67 complexity | eb00c16997ba510c09708c8029457c0e MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, BSD-3-Clause
  1. /*
  2. * Copyright (C) 2008-2012 TrinityCore <http://www.trinitycore.org/>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "Common.h"
  18. #include "DatabaseEnv.h"
  19. #include "WorldPacket.h"
  20. #include "WorldSession.h"
  21. #include "ObjectMgr.h"
  22. #include "TicketMgr.h"
  23. #include "AccountMgr.h"
  24. #include "Chat.h"
  25. #include "Player.h"
  26. bool ChatHandler::HandleGMTicketListCommand(const char* /*args*/)
  27. {
  28. sTicketMgr->ShowList(*this, false);
  29. return true;
  30. }
  31. bool ChatHandler::HandleGMTicketListOnlineCommand(const char* /*args*/)
  32. {
  33. sTicketMgr->ShowList(*this, true);
  34. return true;
  35. }
  36. bool ChatHandler::HandleGMTicketListClosedCommand(const char* /*args*/)
  37. {
  38. sTicketMgr->ShowClosedList(*this);
  39. return true;
  40. }
  41. bool ChatHandler::HandleGMTicketListEscalatedCommand(const char* /*args*/)
  42. {
  43. sTicketMgr->ShowEscalatedList(*this);
  44. return true;
  45. }
  46. bool ChatHandler::HandleGMTicketGetByIdCommand(const char* args)
  47. {
  48. if (!*args)
  49. return false;
  50. uint32 ticketId = atoi(args);
  51. GmTicket *ticket = sTicketMgr->GetTicket(ticketId);
  52. if (!ticket || ticket->IsClosed() || ticket->IsCompleted())
  53. {
  54. SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
  55. return true;
  56. }
  57. SQLTransaction trans = SQLTransaction(NULL);
  58. ticket->SetViewed();
  59. ticket->SaveToDB(trans);
  60. SendSysMessage(ticket->FormatMessageString(*this, true).c_str());
  61. return true;
  62. }
  63. bool ChatHandler::HandleGMTicketGetByNameCommand(const char* args)
  64. {
  65. if (!*args)
  66. return false;
  67. std::string name(args);
  68. if (!normalizePlayerName(name))
  69. return false;
  70. // Detect target's GUID
  71. uint64 guid = 0;
  72. if (Player* player = sObjectAccessor->FindPlayerByName(name.c_str()))
  73. guid = player->GetGUID();
  74. else
  75. guid = sObjectMgr->GetPlayerGUIDByName(name);
  76. // Target must exist
  77. if (!guid)
  78. {
  79. SendSysMessage(LANG_NO_PLAYERS_FOUND);
  80. return true;
  81. }
  82. // Ticket must exist
  83. GmTicket *ticket = sTicketMgr->GetTicketByPlayer(guid);
  84. if (!ticket)
  85. {
  86. SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
  87. return true;
  88. }
  89. SQLTransaction trans = SQLTransaction(NULL);
  90. ticket->SetViewed();
  91. ticket->SaveToDB(trans);
  92. SendSysMessage(ticket->FormatMessageString(*this, true).c_str());
  93. return true;
  94. }
  95. bool ChatHandler::HandleGMTicketCloseByIdCommand(const char* args)
  96. {
  97. if (!*args)
  98. return false;
  99. uint32 ticketId = atoi(args);
  100. GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
  101. if (!ticket || ticket->IsClosed() || ticket->IsCompleted())
  102. {
  103. SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
  104. return true;
  105. }
  106. // Ticket should be assigned to the player who tries to close it.
  107. // Console can override though
  108. Player* player = m_session ? m_session->GetPlayer() : NULL;
  109. if (player && ticket->IsAssignedNotTo(player->GetGUID()))
  110. {
  111. PSendSysMessage(LANG_COMMAND_TICKETCANNOTCLOSE, ticket->GetId());
  112. return true;
  113. }
  114. sTicketMgr->CloseTicket(ticket->GetId(), player ? player->GetGUID() : -1);
  115. sTicketMgr->UpdateLastChange();
  116. std::string msg = ticket->FormatMessageString(*this, player ? player->GetName() : "Console", NULL, NULL, NULL);
  117. SendGlobalGMSysMessage(msg.c_str());
  118. // Inform player, who submitted this ticket, that it is closed
  119. if (Player* submitter = ticket->GetPlayer())
  120. {
  121. if (submitter->IsInWorld())
  122. {
  123. WorldPacket data(SMSG_GMTICKET_DELETETICKET, 4);
  124. data << uint32(GMTICKET_RESPONSE_TICKET_DELETED);
  125. submitter->GetSession()->SendPacket(&data);
  126. }
  127. }
  128. return true;
  129. }
  130. bool ChatHandler::HandleGMTicketAssignToCommand(const char* args)
  131. {
  132. if (!*args)
  133. return false;
  134. char* sTicketId = strtok((char*)args, " ");
  135. uint32 ticketId = atoi(sTicketId);
  136. char* sTarget = strtok(NULL, " ");
  137. if (!sTarget)
  138. return false;
  139. std::string target(sTarget);
  140. if (!normalizePlayerName(target))
  141. return false;
  142. GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
  143. if (!ticket || ticket->IsClosed())
  144. {
  145. SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
  146. return true;
  147. }
  148. // Get target information
  149. uint64 targetGuid = sObjectMgr->GetPlayerGUIDByName(target.c_str());
  150. uint64 targetAccId = sObjectMgr->GetPlayerAccountIdByGUID(targetGuid);
  151. uint32 targetGmLevel = AccountMgr::GetSecurity(targetAccId, realmID);
  152. // Target must exist and have administrative rights
  153. if (!targetGuid || AccountMgr::IsPlayerAccount(targetGmLevel))
  154. {
  155. SendSysMessage(LANG_COMMAND_TICKETASSIGNERROR_A);
  156. return true;
  157. }
  158. // If already assigned, leave
  159. if (ticket->IsAssignedTo(targetGuid))
  160. {
  161. PSendSysMessage(LANG_COMMAND_TICKETASSIGNERROR_B, ticket->GetId());
  162. return true;
  163. }
  164. // If assigned to different player other than current, leave
  165. //! Console can override though
  166. Player* player = m_session ? m_session->GetPlayer() : NULL;
  167. if (player && ticket->IsAssignedNotTo(player->GetGUID()))
  168. {
  169. PSendSysMessage(LANG_COMMAND_TICKETALREADYASSIGNED, ticket->GetId(), target.c_str());
  170. return true;
  171. }
  172. // Assign ticket
  173. SQLTransaction trans = SQLTransaction(NULL);
  174. ticket->SetAssignedTo(targetGuid, AccountMgr::IsAdminAccount(targetGmLevel));
  175. ticket->SaveToDB(trans);
  176. sTicketMgr->UpdateLastChange();
  177. std::string msg = ticket->FormatMessageString(*this, NULL, target.c_str(), NULL, NULL);
  178. SendGlobalGMSysMessage(msg.c_str());
  179. return true;
  180. }
  181. bool ChatHandler::HandleGMTicketUnAssignCommand(const char* args)
  182. {
  183. if (!*args)
  184. return false;
  185. uint32 ticketId = atoi(args);
  186. GmTicket *ticket = sTicketMgr->GetTicket(ticketId);
  187. if (!ticket || ticket->IsClosed())
  188. {
  189. SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
  190. return true;
  191. }
  192. // Ticket must be assigned
  193. if (!ticket->IsAssigned())
  194. {
  195. PSendSysMessage(LANG_COMMAND_TICKETNOTASSIGNED, ticket->GetId());
  196. return true;
  197. }
  198. // Get security level of player, whom this ticket is assigned to
  199. uint32 security = SEC_PLAYER;
  200. Player* assignedPlayer = ticket->GetAssignedPlayer();
  201. if (assignedPlayer && assignedPlayer->IsInWorld())
  202. security = assignedPlayer->GetSession()->GetSecurity();
  203. else
  204. {
  205. uint64 guid = ticket->GetAssignedToGUID();
  206. uint32 accountId = sObjectMgr->GetPlayerAccountIdByGUID(guid);
  207. security = AccountMgr::GetSecurity(accountId, realmID);
  208. }
  209. // Check security
  210. //! If no m_session present it means we're issuing this command from the console
  211. uint32 mySecurity = m_session ? m_session->GetSecurity() : SEC_CONSOLE;
  212. if (security > mySecurity)
  213. {
  214. SendSysMessage(LANG_COMMAND_TICKETUNASSIGNSECURITY);
  215. return true;
  216. }
  217. SQLTransaction trans = SQLTransaction(NULL);
  218. ticket->SetUnassigned();
  219. ticket->SaveToDB(trans);
  220. sTicketMgr->UpdateLastChange();
  221. std::string msg = ticket->FormatMessageString(*this, NULL, ticket->GetAssignedToName().c_str(),
  222. m_session ? m_session->GetPlayer()->GetName() : "Console", NULL);
  223. SendGlobalGMSysMessage(msg.c_str());
  224. return true;
  225. }
  226. bool ChatHandler::HandleGMTicketCommentCommand(const char* args)
  227. {
  228. if (!*args)
  229. return false;
  230. char* tguid = strtok((char*)args, " ");
  231. uint32 ticketId = atoi(tguid);
  232. char* comment = strtok(NULL, "\n");
  233. if (!comment)
  234. return false;
  235. GmTicket *ticket = sTicketMgr->GetTicket(ticketId);
  236. if (!ticket || ticket->IsClosed())
  237. {
  238. PSendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
  239. return true;
  240. }
  241. // Cannot comment ticket assigned to someone else
  242. //! Console excluded
  243. Player* player = m_session ? m_session->GetPlayer() : NULL;
  244. if (player && ticket->IsAssignedNotTo(player->GetGUID()))
  245. {
  246. PSendSysMessage(LANG_COMMAND_TICKETALREADYASSIGNED, ticket->GetId());
  247. return true;
  248. }
  249. SQLTransaction trans = SQLTransaction(NULL);
  250. ticket->SetComment(comment);
  251. ticket->SaveToDB(trans);
  252. sTicketMgr->UpdateLastChange();
  253. std::string msg = ticket->FormatMessageString(*this, NULL, ticket->GetAssignedToName().c_str(), NULL, NULL);
  254. msg += PGetParseString(LANG_COMMAND_TICKETLISTADDCOMMENT, player ? player->GetName() : "Console", comment);
  255. SendGlobalGMSysMessage(msg.c_str());
  256. return true;
  257. }
  258. bool ChatHandler::HandleGMTicketDeleteByIdCommand(const char* args)
  259. {
  260. if (!*args)
  261. return false;
  262. uint32 ticketId = atoi(args);
  263. GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
  264. if (!ticket)
  265. {
  266. SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
  267. return true;
  268. }
  269. if (!ticket->IsClosed())
  270. {
  271. SendSysMessage(LANG_COMMAND_TICKETCLOSEFIRST);
  272. return true;
  273. }
  274. std::string msg = ticket->FormatMessageString(*this, NULL, NULL, NULL, m_session ? m_session->GetPlayer()->GetName() : "Console");
  275. SendGlobalGMSysMessage(msg.c_str());
  276. sTicketMgr->RemoveTicket(ticket->GetId());
  277. sTicketMgr->UpdateLastChange();
  278. if (Player* player = ticket->GetPlayer())
  279. {
  280. if (player->IsInWorld())
  281. {
  282. // Force abandon ticket
  283. WorldPacket data(SMSG_GMTICKET_DELETETICKET, 4);
  284. data << uint32(GMTICKET_RESPONSE_TICKET_DELETED);
  285. player->GetSession()->SendPacket(&data);
  286. }
  287. }
  288. return true;
  289. }
  290. bool ChatHandler::HandleGMTicketResetCommand(const char* /* args */)
  291. {
  292. if (sTicketMgr->GetOpenTicketCount() > 0)
  293. {
  294. SendSysMessage(LANG_COMMAND_TICKETPENDING);
  295. return true;
  296. }
  297. else
  298. {
  299. sTicketMgr->ResetTickets();
  300. SendSysMessage(LANG_COMMAND_TICKETRESET);
  301. }
  302. return true;
  303. }
  304. bool ChatHandler::HandleToggleGMTicketSystem(const char* /* args */)
  305. {
  306. bool status = !sTicketMgr->GetStatus();
  307. sTicketMgr->SetStatus(status);
  308. PSendSysMessage(status ? LANG_ALLOW_TICKETS : LANG_DISALLOW_TICKETS);
  309. return true;
  310. }
  311. bool ChatHandler::HandleGMTicketEscalateCommand(const char *args)
  312. {
  313. if (!*args)
  314. return false;
  315. uint32 ticketId = atoi(args);
  316. GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
  317. if (!ticket || !ticket->IsClosed() || ticket->IsCompleted() || ticket->GetEscalatedStatus() != TICKET_UNASSIGNED)
  318. {
  319. SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
  320. return true;
  321. }
  322. ticket->SetEscalatedStatus(TICKET_IN_ESCALATION_QUEUE);
  323. if (Player* player = ticket->GetPlayer())
  324. if (player->IsInWorld())
  325. sTicketMgr->SendTicket(player->GetSession(), ticket);
  326. sTicketMgr->UpdateLastChange();
  327. return true;
  328. }
  329. bool ChatHandler::HandleGMTicketCompleteCommand(const char* args)
  330. {
  331. if (!*args)
  332. return false;
  333. uint32 ticketId = atoi(args);
  334. GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
  335. if (!ticket || !ticket->IsClosed() || ticket->IsCompleted())
  336. {
  337. SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
  338. return true;
  339. }
  340. if (Player* player = ticket->GetPlayer())
  341. if (player->IsInWorld())
  342. ticket->SendResponse(player->GetSession());
  343. sTicketMgr->UpdateLastChange();
  344. return true;
  345. }
  346. inline bool ChatHandler::_HandleGMTicketResponseAppendCommand(const char* args, bool newLine)
  347. {
  348. if (!*args)
  349. return false;
  350. char* sTicketId = strtok((char*)args, " ");
  351. uint32 ticketId = atoi(sTicketId);
  352. char* response = strtok(NULL, "\n");
  353. if (!response)
  354. return false;
  355. GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
  356. if (!ticket || !ticket->IsClosed())
  357. {
  358. PSendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
  359. return true;
  360. }
  361. // Cannot add response to ticket, assigned to someone else
  362. //! Console excluded
  363. Player* player = m_session ? m_session->GetPlayer() : NULL;
  364. if (player && ticket->IsAssignedNotTo(player->GetGUID()))
  365. {
  366. PSendSysMessage(LANG_COMMAND_TICKETALREADYASSIGNED, ticket->GetId());
  367. return true;
  368. }
  369. SQLTransaction trans = SQLTransaction(NULL);
  370. ticket->AppendResponse(response);
  371. if (newLine)
  372. ticket->AppendResponse("\n");
  373. ticket->SaveToDB(trans);
  374. return true;
  375. }
  376. bool ChatHandler::HandleGMTicketResponseAppendCommand(const char* args)
  377. {
  378. return _HandleGMTicketResponseAppendCommand(args, false);
  379. }
  380. bool ChatHandler::HandleGMTicketResponseAppendLnCommand(const char* args)
  381. {
  382. return _HandleGMTicketResponseAppendCommand(args, true);
  383. }