TrinityCore
Loading...
Searching...
No Matches
cs_arena.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
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
18/* ScriptData
19Name: arena_commandscript
20%Complete: 100
21Comment: All arena team related commands
22Category: commandscripts
23EndScriptData */
24
25#include "ScriptMgr.h"
26#include "ArenaTeamMgr.h"
27#include "CharacterCache.h"
28#include "Chat.h"
29#include "Language.h"
30#include "Log.h"
31#include "ObjectMgr.h"
32#include "Player.h"
33#include "RBAC.h"
34#include "WorldSession.h"
35
36using namespace Trinity::ChatCommands;
37
39{
40public:
41 arena_commandscript() : CommandScript("arena_commandscript") { }
42
44 {
45 static ChatCommandTable arenaCommandTable =
46 {
53 };
54 static ChatCommandTable commandTable =
55 {
56 { "arena", arenaCommandTable },
57 };
58 return commandTable;
59 }
60
62 {
63 if (sArenaTeamMgr->GetArenaTeamByName(name))
64 {
65 handler->PSendSysMessage(LANG_ARENA_ERROR_NAME_EXISTS, name.c_str());
66 handler->SetSentErrorMessage(true);
67 return false;
68 }
69
70 if (!captain)
71 captain = PlayerIdentifier::FromTargetOrSelf(handler);
72 if (!captain)
73 return false;
74
75 if (sCharacterCache->GetCharacterArenaTeamIdByGuid(captain->GetGUID(), type) != 0)
76 {
77 handler->PSendSysMessage(LANG_ARENA_ERROR_SIZE, captain->GetName().c_str());
78 handler->SetSentErrorMessage(true);
79 return false;
80 }
81
82 ArenaTeam* arena = new ArenaTeam();
83
84 if (!arena->Create(captain->GetGUID(), type, name, 4293102085, 101, 4293253939, 4, 4284049911))
85 {
86 delete arena;
88 handler->SetSentErrorMessage(true);
89 return false;
90 }
91
92 sArenaTeamMgr->AddArenaTeam(arena);
93 handler->PSendSysMessage(LANG_ARENA_CREATE, arena->GetName().c_str(), arena->GetId(), arena->GetType(), arena->GetCaptain().ToString().c_str());
94
95 return true;
96 }
97
98 static bool HandleArenaDisbandCommand(ChatHandler* handler, uint32 teamId)
99 {
100 ArenaTeam* arena = sArenaTeamMgr->GetArenaTeamById(teamId);
101
102 if (!arena)
103 {
105 handler->SetSentErrorMessage(true);
106 return false;
107 }
108
109 if (arena->IsFighting())
110 {
112 handler->SetSentErrorMessage(true);
113 return false;
114 }
115
116 std::string name = arena->GetName();
117 arena->Disband();
118 delete arena;
119
120 handler->PSendSysMessage(LANG_ARENA_DISBAND, name.c_str(), teamId);
121 return true;
122 }
123
124 static bool HandleArenaRenameCommand(ChatHandler* handler, QuotedString oldName, QuotedString newName)
125 {
126 ArenaTeam* arena = sArenaTeamMgr->GetArenaTeamByName(oldName);
127 if (!arena)
128 {
129 handler->PSendSysMessage(LANG_ARENA_ERROR_NAME_NOT_FOUND, oldName.c_str());
130 handler->SetSentErrorMessage(true);
131 return false;
132 }
133
134 if (sArenaTeamMgr->GetArenaTeamByName(newName))
135 {
136 handler->PSendSysMessage(LANG_ARENA_ERROR_NAME_EXISTS, newName.c_str());
137 handler->SetSentErrorMessage(true);
138 return false;
139 }
140
141 if (arena->IsFighting())
142 {
144 handler->SetSentErrorMessage(true);
145 return false;
146 }
147
148 if (arena->SetName(newName))
149 {
150 handler->PSendSysMessage(LANG_ARENA_RENAME, arena->GetId(), oldName.c_str(), newName.c_str());
151 return true;
152 }
153 else
154 {
156 handler->SetSentErrorMessage(true);
157 return false;
158 }
159 }
160
162 {
163 ArenaTeam* arena = sArenaTeamMgr->GetArenaTeamById(teamId);
164 if (!arena)
165 {
167 handler->SetSentErrorMessage(true);
168 return false;
169 }
170
171 if (arena->IsFighting())
172 {
174 handler->SetSentErrorMessage(true);
175 return false;
176 }
177
178 if (!target)
179 target = PlayerIdentifier::FromTargetOrSelf(handler);
180 if (!target)
181 return false;
182
183 if (!arena->IsMember(target->GetGUID()))
184 {
185 handler->PSendSysMessage(LANG_ARENA_ERROR_NOT_MEMBER, target->GetName().c_str(), arena->GetName().c_str());
186 handler->SetSentErrorMessage(true);
187 return false;
188 }
189
190 if (arena->GetCaptain() == target->GetGUID())
191 {
192 handler->PSendSysMessage(LANG_ARENA_ERROR_CAPTAIN, target->GetName().c_str(), arena->GetName().c_str());
193 handler->SetSentErrorMessage(true);
194 return false;
195 }
196
197 CharacterCacheEntry const* oldCaptainNameData = sCharacterCache->GetCharacterCacheByGuid(arena->GetCaptain());
198 char const* oldCaptainName = oldCaptainNameData ? oldCaptainNameData->Name.c_str() : "<unknown>";
199
200 arena->SetCaptain(target->GetGUID());
201 handler->PSendSysMessage(LANG_ARENA_CAPTAIN, arena->GetName().c_str(), arena->GetId(), oldCaptainName, target->GetName().c_str());
202
203 return true;
204 }
205
206 static bool HandleArenaInfoCommand(ChatHandler* handler, uint32 teamId)
207 {
208 ArenaTeam* arena = sArenaTeamMgr->GetArenaTeamById(teamId);
209
210 if (!arena)
211 {
213 handler->SetSentErrorMessage(true);
214 return false;
215 }
216
217 handler->PSendSysMessage(LANG_ARENA_INFO_HEADER, arena->GetName().c_str(), arena->GetId(), arena->GetRating(), arena->GetType(), arena->GetType());
218 for (ArenaTeam::MemberList::iterator itr = arena->m_membersBegin(); itr != arena->m_membersEnd(); ++itr)
219 handler->PSendSysMessage(LANG_ARENA_INFO_MEMBERS, itr->Name.c_str(), itr->Guid.ToString().c_str(), itr->PersonalRating, (arena->GetCaptain() == itr->Guid ? " - Captain" : ""));
220
221 return true;
222 }
223
224 static bool HandleArenaLookupCommand(ChatHandler* handler, Tail needle)
225 {
226 if (needle.empty())
227 return false;
228
229 bool found = false;
230 for (auto [teamId, team] : sArenaTeamMgr->GetArenaTeams())
231 {
232 if (StringContainsStringI(team->GetName(), needle))
233 {
234 if (handler->GetSession())
235 {
236 handler->PSendSysMessage(LANG_ARENA_LOOKUP, team->GetName().c_str(), team->GetId(), team->GetType(), team->GetType());
237 found = true;
238 continue;
239 }
240 }
241 }
242
243 if (!found)
244 handler->PSendSysMessage(LANG_ARENA_ERROR_NAME_NOT_FOUND, std::string(needle).c_str());
245
246 return true;
247 }
248};
249
#define sArenaTeamMgr
ArenaTeamTypes
Definition ArenaTeam.h:82
#define sCharacterCache
uint32_t uint32
Definition Define.h:133
@ LANG_ARENA_DISBAND
Definition Language.h:814
@ LANG_ARENA_ERROR_NAME_EXISTS
Definition Language.h:807
@ LANG_ARENA_ERROR_NAME_NOT_FOUND
Definition Language.h:810
@ LANG_ARENA_CAPTAIN
Definition Language.h:816
@ LANG_ARENA_INFO_HEADER
Definition Language.h:817
@ LANG_ARENA_CREATE
Definition Language.h:813
@ LANG_ARENA_ERROR_CAPTAIN
Definition Language.h:812
@ LANG_ARENA_ERROR_NOT_MEMBER
Definition Language.h:811
@ LANG_ARENA_ERROR_SIZE
Definition Language.h:808
@ LANG_ARENA_RENAME
Definition Language.h:815
@ LANG_BAD_VALUE
Definition Language.h:149
@ LANG_ARENA_LOOKUP
Definition Language.h:819
@ LANG_ARENA_INFO_MEMBERS
Definition Language.h:818
@ LANG_ARENA_ERROR_NOT_FOUND
Definition Language.h:806
@ LANG_ARENA_ERROR_COMBAT
Definition Language.h:809
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
Role Based Access Control related classes definition.
bool StringContainsStringI(std::string_view haystack, std::string_view needle)
Definition Util.cpp:711
MemberList::iterator m_membersBegin()
Definition ArenaTeam.h:147
void Disband(WorldSession *session)
ObjectGuid GetCaptain() const
Definition ArenaTeam.h:133
uint32 GetRating() const
Definition ArenaTeam.h:137
bool IsMember(ObjectGuid guid) const
bool SetName(std::string const &name)
bool Create(ObjectGuid captainGuid, uint8 type, std::string const &teamName, uint32 backgroundColor, uint8 emblemStyle, uint32 emblemColor, uint8 borderStyle, uint32 borderColor)
Definition ArenaTeam.cpp:49
void SetCaptain(ObjectGuid guid)
uint32 GetType() const
Definition ArenaTeam.h:129
bool IsFighting() const
std::string const & GetName() const
Definition ArenaTeam.h:134
MemberList::iterator m_membersEnd()
Definition ArenaTeam.h:148
uint32 GetId() const
Definition ArenaTeam.h:128
WorldSession * GetSession()
Definition Chat.h:46
void SetSentErrorMessage(bool val)
Definition Chat.h:134
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:69
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition Chat.cpp:101
std::string ToString() const
static bool HandleArenaDisbandCommand(ChatHandler *handler, uint32 teamId)
Definition cs_arena.cpp:98
static bool HandleArenaCreateCommand(ChatHandler *handler, Optional< PlayerIdentifier > captain, QuotedString name, ArenaTeamTypes type)
Definition cs_arena.cpp:61
ChatCommandTable GetCommands() const override
Definition cs_arena.cpp:43
static bool HandleArenaRenameCommand(ChatHandler *handler, QuotedString oldName, QuotedString newName)
Definition cs_arena.cpp:124
static bool HandleArenaLookupCommand(ChatHandler *handler, Tail needle)
Definition cs_arena.cpp:224
static bool HandleArenaCaptainCommand(ChatHandler *handler, uint32 teamId, Optional< PlayerIdentifier > target)
Definition cs_arena.cpp:161
static bool HandleArenaInfoCommand(ChatHandler *handler, uint32 teamId)
Definition cs_arena.cpp:206
void AddSC_arena_commandscript()
Definition cs_arena.cpp:250
std::vector< ChatCommandBuilder > ChatCommandTable
Definition ChatCommand.h:50
@ RBAC_PERM_COMMAND_ARENA_LOOKUP
Definition RBAC.h:153
@ RBAC_PERM_COMMAND_ARENA_RENAME
Definition RBAC.h:154
@ RBAC_PERM_COMMAND_ARENA_INFO
Definition RBAC.h:152
@ RBAC_PERM_COMMAND_ARENA_DISBAND
Definition RBAC.h:151
@ RBAC_PERM_COMMAND_ARENA_CAPTAIN
Definition RBAC.h:149
@ RBAC_PERM_COMMAND_ARENA_CREATE
Definition RBAC.h:150
static Optional< PlayerIdentifier > FromTargetOrSelf(ChatHandler *handler)