Just a few renames.
[getmangos.git] / src / game / LFGHandler.cpp
blobd0c9e6bf8ff72748820ab74a19411b40039313ba
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "WorldSession.h"
20 #include "Log.h"
21 #include "Database/DatabaseEnv.h"
22 #include "Player.h"
23 #include "WorldPacket.h"
24 #include "ObjectMgr.h"
25 #include "World.h"
27 static void AttemptJoin(Player* _player)
29 // skip not can autojoin cases and player group case
30 if(!_player->m_lookingForGroup.canAutoJoin() || _player->GetGroup())
31 return;
33 //TODO: Guard Player Map
34 HashMapHolder<Player>::MapType const& players = ObjectAccessor::Instance().GetPlayers();
35 for(HashMapHolder<Player>::MapType::const_iterator iter = players.begin(); iter != players.end(); ++iter)
37 Player *plr = iter->second;
39 // skip enemies and self
40 if(!plr || plr==_player || plr->GetTeam() != _player->GetTeam())
41 continue;
43 // skip not auto add, not group leader cases
44 if(!plr->GetSession()->LookingForGroup_auto_add || plr->GetGroup() && plr->GetGroup()->GetLeaderGUID()!=plr->GetGUID())
45 continue;
47 // skip non auto-join or empty slots, or non compatible slots
48 if(!plr->m_lookingForGroup.more.canAutoJoin() || !_player->m_lookingForGroup.HaveInSlot(plr->m_lookingForGroup.more))
49 continue;
51 // attempt create group, or skip
52 if(!plr->GetGroup())
54 Group* group = new Group;
55 if(!group->Create(plr->GetGUID(), plr->GetName()))
57 delete group;
58 continue;
61 objmgr.AddGroup(group);
64 // stop at success join
65 if(plr->GetGroup()->AddMember(_player->GetGUID(), _player->GetName()))
67 if( sWorld.getConfig(CONFIG_RESTRICTED_LFG_CHANNEL) && _player->GetSession()->GetSecurity() == SEC_PLAYER )
68 _player->LeaveLFGChannel();
69 break;
71 // full
72 else
74 if( sWorld.getConfig(CONFIG_RESTRICTED_LFG_CHANNEL) && plr->GetSession()->GetSecurity() == SEC_PLAYER )
75 plr->LeaveLFGChannel();
80 static void AttemptAddMore(Player* _player)
82 // skip not group leader case
83 if(_player->GetGroup() && _player->GetGroup()->GetLeaderGUID()!=_player->GetGUID())
84 return;
86 if(!_player->m_lookingForGroup.more.canAutoJoin())
87 return;
89 //TODO: Guard Player map
90 HashMapHolder<Player>::MapType const& players = ObjectAccessor::Instance().GetPlayers();
91 for(HashMapHolder<Player>::MapType::const_iterator iter = players.begin(); iter != players.end(); ++iter)
93 Player *plr = iter->second;
95 // skip enemies and self
96 if(!plr || plr==_player || plr->GetTeam() != _player->GetTeam())
97 continue;
99 // skip not auto join or in group
100 if(!plr->GetSession()->LookingForGroup_auto_join || plr->GetGroup() )
101 continue;
103 if(!plr->m_lookingForGroup.HaveInSlot(_player->m_lookingForGroup.more))
104 continue;
106 // attempt create group if need, or stop attempts
107 if(!_player->GetGroup())
109 Group* group = new Group;
110 if(!group->Create(_player->GetGUID(), _player->GetName()))
112 delete group;
113 return; // can't create group (??)
116 objmgr.AddGroup(group);
119 // stop at join fail (full)
120 if(!_player->GetGroup()->AddMember(plr->GetGUID(), plr->GetName()) )
122 if( sWorld.getConfig(CONFIG_RESTRICTED_LFG_CHANNEL) && _player->GetSession()->GetSecurity() == SEC_PLAYER )
123 _player->LeaveLFGChannel();
125 break;
128 // joined
129 if( sWorld.getConfig(CONFIG_RESTRICTED_LFG_CHANNEL) && plr->GetSession()->GetSecurity() == SEC_PLAYER )
130 plr->LeaveLFGChannel();
132 // and group full
133 if(_player->GetGroup()->IsFull() )
135 if( sWorld.getConfig(CONFIG_RESTRICTED_LFG_CHANNEL) && _player->GetSession()->GetSecurity() == SEC_PLAYER )
136 _player->LeaveLFGChannel();
138 break;
143 void WorldSession::HandleLfgSetAutoJoinOpcode( WorldPacket & /*recv_data*/ )
145 sLog.outDebug("CMSG_LFG_SET_AUTOJOIN");
146 LookingForGroup_auto_join = true;
148 if(!_player) // needed because STATUS_AUTHED
149 return;
151 AttemptJoin(_player);
154 void WorldSession::HandleLfgClearAutoJoinOpcode( WorldPacket & /*recv_data*/ )
156 sLog.outDebug("CMSG_LFG_CLEAR_AUTOJOIN");
157 LookingForGroup_auto_join = false;
160 void WorldSession::HandleLfmSetAutoFillOpcode( WorldPacket & /*recv_data*/ )
162 sLog.outDebug("CMSG_LFM_SET_AUTOFILL");
163 LookingForGroup_auto_add = true;
165 if(!_player) // needed because STATUS_AUTHED
166 return;
168 AttemptAddMore(_player);
171 void WorldSession::HandleLfmClearAutoFillOpcode( WorldPacket & /*recv_data*/ )
173 sLog.outDebug("CMSG_LFM_CLEAR_AUTOFILL");
174 LookingForGroup_auto_add = false;
177 void WorldSession::HandleLfgClearOpcode( WorldPacket & /*recv_data */ )
179 // empty packet
180 sLog.outDebug("CMSG_CLEAR_LOOKING_FOR_GROUP");
182 for(int i = 0; i < MAX_LOOKING_FOR_GROUP_SLOT; ++i)
183 _player->m_lookingForGroup.slots[i].Clear();
185 if( sWorld.getConfig(CONFIG_RESTRICTED_LFG_CHANNEL) && _player->GetSession()->GetSecurity() == SEC_PLAYER )
186 _player->LeaveLFGChannel();
189 void WorldSession::HandleLfmClearOpcode( WorldPacket & /*recv_data */)
191 // empty packet
192 sLog.outDebug("CMSG_CLEAR_LOOKING_FOR_MORE");
194 _player->m_lookingForGroup.more.Clear();
197 void WorldSession::HandleSetLfmOpcode( WorldPacket & recv_data )
199 CHECK_PACKET_SIZE(recv_data, 4+1+1+1+1);
201 sLog.outDebug("CMSG_SET_LOOKING_FOR_MORE");
202 //recv_data.hexlike();
203 uint32 temp, entry, type;
204 uint8 unk1;
205 uint8 unk2[3];
207 recv_data >> temp >> unk1 >> unk2[0] >> unk2[1] >> unk2[2];
209 entry = ( temp & 0x00FFFFFF);
210 type = ( (temp >> 24) & 0x000000FF);
212 _player->m_lookingForGroup.more.Set(entry,type);
213 sLog.outDebug("LFM set: temp %u, zone %u, type %u", temp, entry, type);
215 if(LookingForGroup_auto_add)
216 AttemptAddMore(_player);
218 SendLfgResult(type, entry, 1);
221 void WorldSession::HandleSetLfgCommentOpcode( WorldPacket & recv_data )
223 CHECK_PACKET_SIZE(recv_data,1);
225 sLog.outDebug("CMSG_SET_LFG_COMMENT");
226 //recv_data.hexlike();
228 std::string comment;
229 recv_data >> comment;
230 sLog.outDebug("LFG comment %s", comment.c_str());
232 _player->m_lookingForGroup.comment = comment;
235 void WorldSession::HandleLookingForGroup(WorldPacket& recv_data)
237 CHECK_PACKET_SIZE(recv_data, 4+4+4);
239 sLog.outDebug("MSG_LOOKING_FOR_GROUP");
240 //recv_data.hexlike();
241 uint32 type, entry, unk;
243 recv_data >> type >> entry >> unk;
244 sLog.outDebug("MSG_LOOKING_FOR_GROUP: type %u, entry %u, unk %u", type, entry, unk);
246 if(LookingForGroup_auto_add)
247 AttemptAddMore(_player);
249 if(LookingForGroup_auto_join)
250 AttemptJoin(_player);
252 SendLfgResult(type, entry, 0);
255 void WorldSession::SendLfgResult(uint32 type, uint32 entry, uint8 lfg_type)
257 uint32 number = 0;
259 // start prepare packet;
260 WorldPacket data(MSG_LOOKING_FOR_GROUP);
261 data << uint32(type); // type
262 data << uint32(entry); // entry from LFGDungeons.dbc
263 data << uint32(0); // count, placeholder
264 data << uint32(0); // count again, strange, placeholder
266 //TODO: Guard Player map
267 HashMapHolder<Player>::MapType const& players = ObjectAccessor::Instance().GetPlayers();
268 for(HashMapHolder<Player>::MapType::const_iterator iter = players.begin(); iter != players.end(); ++iter)
270 Player *plr = iter->second;
272 if(!plr || plr->GetTeam() != _player->GetTeam())
273 continue;
275 if(!plr->m_lookingForGroup.HaveInSlot(entry,type))
276 continue;
278 ++number;
280 data.append(plr->GetPackGUID()); // packed guid
281 data << plr->getLevel(); // level
282 data << plr->GetZoneId(); // current zone
283 data << lfg_type; // 0x00 - LFG, 0x01 - LFM
285 for(uint8 j = 0; j < MAX_LOOKING_FOR_GROUP_SLOT; ++j)
287 data << uint32( plr->m_lookingForGroup.slots[j].entry | (plr->m_lookingForGroup.slots[j].type << 24) );
289 data << plr->m_lookingForGroup.comment;
291 Group *group = plr->GetGroup();
292 if(group)
294 data << group->GetMembersCount()-1; // count of group members without group leader
295 for(GroupReference *itr = group->GetFirstMember(); itr != NULL; itr = itr->next())
297 Player *member = itr->getSource();
298 if(member && member->GetGUID() != plr->GetGUID())
300 data.append(member->GetPackGUID()); // packed guid
301 data << member->getLevel(); // player level
305 else
307 data << uint32(0x00);
311 // fill count placeholders
312 data.put<uint32>(4+4, number);
313 data.put<uint32>(4+4+4,number);
315 SendPacket(&data);
318 void WorldSession::HandleSetLfgOpcode( WorldPacket & recv_data )
320 CHECK_PACKET_SIZE(recv_data, 4+4+1+1);
322 sLog.outDebug("CMSG_SET_LOOKING_FOR_GROUP");
323 recv_data.hexlike();
324 uint32 slot, temp, entry, type;
325 uint8 roles, unk1;
327 recv_data >> slot >> temp >> roles >> unk1;
329 entry = ( temp & 0x00FFFFFF);
330 type = ( (temp >> 24) & 0x000000FF);
332 if(slot >= MAX_LOOKING_FOR_GROUP_SLOT)
333 return;
335 _player->m_lookingForGroup.slots[slot].Set(entry, type);
336 _player->m_lookingForGroup.roles = roles;
337 sLog.outDebug("LFG set: looknumber %u, temp %X, type %u, entry %u", slot, temp, type, entry);
339 if(LookingForGroup_auto_join)
340 AttemptJoin(_player);
342 SendLfgResult(type, entry, 0);
345 void WorldSession::HandleLfgSetRoles(WorldPacket &recv_data)
347 CHECK_PACKET_SIZE(recv_data, 1);
349 sLog.outDebug("CMSG_LFG_SET_ROLES");
351 uint8 roles;
352 recv_data >> roles;
354 _player->m_lookingForGroup.roles = roles;