Mana
Loading...
Searching...
No Matches
guildhandler.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2008-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include "event.h"
25#include "guild.h"
26#include "log.h"
27#include "localplayer.h"
28#include "channel.h"
29#include "channelmanager.h"
30
31#include "gui/socialwindow.h"
32
34
35#include "net/net.h"
36
41
42#include "utils/gettext.h"
43#include "utils/stringutils.h"
44
46
47namespace ManaServ {
48
49extern Connection *chatServerConnection;
50
69
71{
72 switch (msg.getId())
73 {
75 {
76 Log::info("Received CPMSG_GUILD_CREATE_RESPONSE");
77 if (msg.readInt8() == ERRMSG_OK)
78 {
79 // TODO - Acknowledge guild was created
80 serverNotice(_("Guild created."));
81 joinedGuild(msg);
82 }
83 else
84 {
85 serverNotice(_("Error creating guild."));
86 }
87 } break;
88
90 {
91 Log::info("Received CPMSG_GUILD_INVITE_RESPONSE");
92 const unsigned char response = msg.readInt8();
93 if (response == ERRMSG_OK)
94 {
95 // TODO - Acknowledge invite was sent
96 serverNotice(_("Invite sent."));
97 }
98 else if (response == ERRMSG_ALREADY_MEMBER)
99 {
100 serverNotice(_("Invited player is already in that guild."));
101 }
102 else if (response == ERRMSG_LIMIT_REACHED)
103 {
104 serverNotice(_("Invited player can't join another guild."));
105 }
106 else // any other failure
107 {
108 serverNotice(_("Invite failed."));
109 }
110 } break;
111
113 {
114 Log::info("Received CPMSG_GUILD_ACCEPT_RESPONSE");
115 if (msg.readInt8() == ERRMSG_OK)
116 {
117 // TODO - Acknowledge accepted into guild
118 joinedGuild(msg);
119 }
120 } break;
121
123 {
124 Log::info("Received CPMSG_GUILD_GET_MEMBERS_RESPONSE");
125 if (msg.readInt8() == ERRMSG_OK)
126 {
127 std::string name;
128 bool online;
129 Guild *guild;
130 GuildMember *member;
131
132 short guildId = msg.readInt16();
133 guild = local_player->getGuild(guildId);
134
135 if (!guild)
136 return;
137
138 guild->clearMembers();
139
140 while (msg.getUnreadLength())
141 {
142 name = msg.readString();
143 online = msg.readInt8();
144 if (!name.empty())
145 {
146 member = guild->addMember(name);
147 member->setOnline(online);
148 }
149 }
150 }
151 } break;
152
154 {
155 Log::info("Received CPMSG_GUILD_UPDATE_LIST");
156 short guildId = msg.readInt16();
157 std::string name = msg.readString();
158 char eventId = msg.readInt8();
159 GuildMember *member;
160
161 Guild *guild = local_player->getGuild(guildId);
162 if (guild)
163 {
164 switch(eventId)
165 {
167 member = guild->addMember(name);
168 member->setOnline(true);
169 break;
170
172 guild->removeMember(name);
173 break;
174
176 member = guild->getMember(name);
177 if (member)
178 {
179 member->setOnline(true);
180 }
181 break;
182
184 member = guild->getMember(name);
185 if (member)
186 {
187 member->setOnline(false);
188 }
189 break;
190
191 default:
192 Log::info("Invalid guild event");
193 }
194 }
195 } break;
196
198 {
199 Log::info("Received CPMSG_GUILD_INVITED");
200 std::string inviterName = msg.readString();
201 std::string guildName = msg.readString();
202 int guildId = msg.readInt16();
203
204 // Open a dialog asking if the player accepts joining the guild.
205 socialWindow->showGuildInvite(guildName, guildId, inviterName);
206 } break;
207
209 {
210 Log::info("Received CPMSG_GUILD_PROMOTE_MEMBER_RESPONSE");
211
212 if (msg.readInt8() == ERRMSG_OK)
213 {
214 // promotion succeeded
215 serverNotice(_("Member was promoted successfully."));
216 }
217 else
218 {
219 // promotion failed
220 serverNotice(_("Failed to promote member."));
221 }
222 }
223
225 {
226 Log::info("Received CPMSG_GUILD_REJOIN");
227
228 joinedGuild(msg);
229 } break;
230
232 {
233 Log::info("Received CPMSG_GUILD_QUIT_RESPONSE");
234
235 if (msg.readInt8() == ERRMSG_OK)
236 {
237 // Must remove tab first, as it wont find the guild
238 // name after its removed from the player
239 int guildId = msg.readInt16();
240 if (Guild *guild = local_player->getGuild(guildId))
241 {
242 Channel *channel = channelManager->findByName(guild->getName());
244 local_player->removeGuild(guildId);
245 }
246 }
247 } break;
249 {
250 Log::info("Received CPMSG_GUILD_KICK_NOTIFICATION");
251
252 const int guildId = msg.readInt16();
253 std::string player = msg.readString();
254 if (Guild *guild = local_player->getGuild(guildId))
255 {
256 Channel *channel = channelManager->findByName(guild->getName());
258 local_player->removeGuild(guildId);
259 serverNotice(strprintf(_("Player %s kicked you out of guild %s."),
260 player.c_str(), guild->getName().c_str()));
261 }
262 } break;
263 }
264}
265
267{
268 std::string guildName = msg.readString();
269 short guildId = msg.readInt16();
270 short permissions = msg.readInt16();
271 short channelId = msg.readInt16();
272 std::string announcement = msg.readString();
273
274 // Add guild to player
275 Guild *guild = Guild::getGuild(guildId);
276 guild->setName(guildName);
277 guild->setRights(permissions);
278 local_player->addGuild(guild);
280
281 // Automatically create the guild channel
282 // COMMENT: Should this go here??
283 auto *channel = new Channel(channelId, guildName, announcement);
284 channelManager->addChannel(channel);
285 channel->getTab()->chatLog(strprintf(_("Topic: %s"), announcement.c_str()),
286 BY_CHANNEL);
287}
288
289void GuildHandler::create(const std::string &name)
290{
292 msg.writeString(name);
294}
295
296void GuildHandler::invite(int guildId, const std::string &name)
297{
299 msg.writeInt16(guildId);
300 msg.writeString(name);
302}
303
304void GuildHandler::invite(int guildId, Being *being)
305{
306 invite(guildId, being->getName());
307}
308
309void GuildHandler::inviteResponse(int guildId, bool response)
310{
312 msg.writeInt16(guildId);
313 msg.writeInt8(response ? 1 : 0);
315}
316
317void GuildHandler::leave(int guildId)
318{
320 msg.writeInt16(guildId);
322}
323
324void GuildHandler::kick(GuildMember *member, std::string reason)
325{
326 // TODO
327}
328
329void GuildHandler::chat(int guildId, const std::string &text)
330{
331 // TODO
332}
333
335{
337 msg.writeInt16(guildId);
339}
340
342{
343 /*MessageOut msg(PCMSG_GUILD_PROMOTE_MEMBER);
344 msg.writeInt16(guildId);
345 msg.writeString(name);
346 msg.writeInt8(level);
347 chatServerConnection->send(msg);*/
348}
349
350void GuildHandler::requestAlliance(int guildId, int otherGuildId)
351{
352 // TODO
353}
354
355void GuildHandler::requestAllianceResponse(int guildId, int otherGuildId,
356 bool response)
357{
358 // TODO
359}
360
361void GuildHandler::endAlliance(int guildId, int otherGuildId)
362{
363 // TODO
364}
365
366} // namespace ManaServ
ChannelManager * channelManager
Definition game.cpp:111
@ BY_CHANNEL
Definition chatwindow.h:51
void setOnline(bool online)
Set the avatar's online status.
Definition avatar.h:49
Definition being.h:65
const std::string & getName() const
Returns the name of the being.
Definition being.h:190
void addGuild(Guild *guild)
Adds a guild to the being.
Definition being.cpp:495
void removeGuild(int id)
Removers a guild from the being.
Definition being.cpp:504
Guild * getGuild(const std::string &guildName) const
Returns a pointer to the specified guild that the being is in.
Definition being.cpp:518
Channel * findByName(const std::string &name) const
void addChannel(Channel *channel)
void removeChannel(Channel *channel)
Definition guild.h:55
GuildMember * addMember(int id, const std::string &name)
Adds member to the list.
Definition guild.cpp:44
void setName(const std::string &name)
Set the guild's name.
Definition guild.h:61
GuildMember * getMember(int id) const
Find a member by ID.
Definition guild.cpp:74
void setRights(short rights)
Definition guild.cpp:141
void clearMembers()
Definition guild.h:125
static Guild * getGuild(int id)
Definition guild.cpp:183
void removeMember(GuildMember *member)
Removes a member from the guild.
Definition guild.cpp:92
void send(const ManaServ::MessageOut &msg)
Sends a message.
void kick(GuildMember *member, std::string reason=std::string()) override
void changeMemberPostion(GuildMember *member, int level) override
void inviteResponse(int guidId, bool response) override
void invite(int guildId, const std::string &name) override
void endAlliance(int guildId, int otherGuildId) override
void chat(int guildId, const std::string &text) override
void memberList(int guildId) override
void requestAllianceResponse(int guildId, int otherGuildId, bool response) override
void joinedGuild(MessageIn &msg)
void handleMessage(MessageIn &msg) override
void requestAlliance(int guildId, int otherGuildId) override
void leave(int guildId) override
void create(const std::string &name) override
Used for parsing an incoming message from manaserv.
Definition messagein.h:37
uint16_t readInt16()
Reads an unsigned 16-bit integer from the message.
Definition messagein.cpp:58
unsigned int getUnreadLength() const
Returns the length of unread data.
Definition messagein.h:54
std::string readString(int length=-1)
Reads a string.
Definition messagein.cpp:92
uint16_t getId() const
Returns the message ID.
Definition messagein.h:44
uint8_t readInt8()
Reads an unsigned 8-bit integer from the message.
Definition messagein.cpp:43
Used for building an outgoing message to manaserv.
Definition messageout.h:37
void writeInt16(uint16_t value)
Writes an unsigned 16-bit integer to the message.
void writeInt8(uint8_t value)
Writes an unsigned 8-bit integer to the message.
void writeString(const std::string &string, int length=-1)
Writes a string.
virtual void memberList(int guildId)=0
const uint16_t * handledMessages
void showGuildInvite(const std::string &guildName, int guildId, const std::string &inviterName)
void serverNotice(const std::string &message)
Definition event.h:319
SocialWindow * socialWindow
Definition game.cpp:108
#define _(s)
Definition gettext.h:38
LocalPlayer * local_player
Net::GuildHandler * guildHandler
Definition net.cpp:53
void info(const char *log_text,...) LOG_PRINTF_ATTR
Connection * chatServerConnection
@ CPMSG_GUILD_ACCEPT_RESPONSE
@ CPMSG_GUILD_CREATE_RESPONSE
@ CPMSG_GUILD_KICK_NOTIFICATION
@ CPMSG_GUILD_PROMOTE_MEMBER_RESPONSE
@ CPMSG_GUILD_INVITE_RESPONSE
@ CPMSG_GUILD_GET_MEMBERS_RESPONSE
GuildHandler * getGuildHandler()
Definition net.cpp:85
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.