1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (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 Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef RYZOM_NAME_MANAGER_H
19 #define RYZOM_NAME_MANAGER_H
22 #include "nel/misc/common.h"
23 #include "nel/misc/ucstring.h"
25 #include "game_share/character_sync_itf.h"
27 #include "server_share/mysql_wrapper.h"
36 * Manage unique name for character, forbidden names and reserved name
37 * \author Alain Saffray
38 * \author Nevrax France
41 * Update 2005/2006 by Boris Boucher
42 * Added support for unifier guild names
45 class CNameManager
: public NLMISC::ICommandsHandler
47 friend class CHARSYNC::CCharacterSync
;
50 typedef uint32 TCharId
;
51 typedef uint32 TGuildId
;
52 typedef uint32 TUserId
;
53 typedef std::string TName
;
59 // assign a name to owner, return true if name is accepted
60 bool assignName(uint32 charId
, const ucstring
& ucName
, uint32 homeSessionId
, bool skipTest
= false);
63 void liberateName(uint32 charId
, const ucstring
& ucName
);
65 // liberate any name associated to a character
66 void liberateName(uint32 charId
);
68 // check character slot, remove duplicated entries if needed
69 // void checkCharacterSlot(uint32 charId, const ucstring & ucName);
71 // return true if name is free or usable
72 CHARSYNC::TCharacterNameResult
isNameUsable(const ucstring
& ucName
, uint32 userId
, uint8 charIndex
, uint32 homeSessionId
);
74 // return true if name is free or usable
75 CHARSYNC::TCharacterNameResult
isGuildNameUsable(const ucstring
& ucName
, uint32 guildId
);
77 // register a list of loaded guild, update the name table accordingly and fill an output
78 // vector with renamed guild (because of name conflict).
79 void registerLoadedGuildNames(uint32 shardId
, const std::map
<uint32
, ucstring
> &guilds
, std::vector
<uint32
> &renamedGuildIds
);
81 // A new guild has been created, return true if the name is valid, false if
82 // the guild has been renamed
83 bool assignGuildName(uint32 shardId
, uint32 guildId
, const ucstring
&guildName
);
85 // A guild as been deleted, release the name
86 void releaseGuildName(uint32 shardId
, uint32 guildId
);
88 // save names of characters
89 // void saveCharacterNames();
91 // save the guild names
92 void saveGuildNames();
94 // load names of characters, reserved and forbidden names
97 // rename a character with a default name
98 ucstring
renameCharacter(uint32 charId
, uint32 homeSessionId
);
100 // regular update (used to clean up the temporary reserved name)
104 // return the name associated to a guild
105 const TName
&getGuildName(uint32 guildId
) const;
107 /// Try to find a shard id from a name and session id. Return 0 if not found
108 uint32
findCharId(const std::string
&charName
, uint32 homeSessionId
);
109 /// Try to find a shard id from a name without session id, return 0 if 0 or more than one match.
110 uint32
findCharId(const std::string
&charName
);
114 bool loadAccountNamesFromTxt();
115 bool loadAccountNamesFromDatabase();
116 // bool loadAccountNamesFromXML();
117 // bool loadAccountNamesFromEIDTranslator();
118 bool loadCharacterNamesFromTxt();
119 bool loadCharacterNamesFromDatabase();
120 // bool loadCharacterNamesFromXML();
121 bool loadGuildsNamesFromTxt();
122 bool loadForbiddenNames();
123 bool loadReservedNames(const char * fileNameWithoutPath
);
125 // generate a valid default name
126 ucstring
generateDefaultName(uint32 charId
, uint32 homeSessionId
);
127 // generate a valid default guild name
128 ucstring
generateDefaultGuildName(uint32 guildId
);
131 /** This methods implemented by CCommandHandler is used by the
132 * command registry to retrieve the name of the object instance.
134 virtual const std::string
&getCommandHandlerName() const;
148 TCharSlot(uint32 userId
, uint8 charIndex
)
151 CharIndex
= charIndex
;
154 TCharSlot(uint32 charId
)
156 UserId
= charId
>> 4;
157 CharIndex
= uint8( charId
& 0xf );
160 uint32
getCharId() const
162 return (UserId
<< 4) + CharIndex
;
165 bool operator == (const TCharSlot
& charSlot
) const
167 return (UserId
== charSlot
.UserId
&& CharIndex
== charSlot
.CharIndex
);
170 bool operator != (const TCharSlot
& charSlot
) const
172 return (UserId
!= charSlot
.UserId
|| CharIndex
!= charSlot
.CharIndex
);
175 bool operator < (const TCharSlot
& charSlot
) const
177 return ((UserId
< charSlot
.UserId
) || (UserId
== charSlot
.UserId
&& CharIndex
< charSlot
.CharIndex
));
180 void serial(NLMISC::IStream
& f
)
183 f
.serial( CharIndex
);
195 : ShardId(0), GuildId(0)
199 TGuildSlot(uint32 shardId
, uint32 guildId
)
200 : ShardId(shardId
), GuildId(guildId
)
204 bool operator < (const TGuildSlot
&other
)
206 if (ShardId
== other
.ShardId
)
207 return GuildId
< other
.GuildId
;
209 return ShardId
< other
.ShardId
;
212 void serial(NLMISC::IStream
& f
)
222 TSessionId HomeSessionId
;
228 TFullName(const std::string
&name
, uint32 homeSessionId
)
230 HomeSessionId(homeSessionId
)
233 bool operator < (const TFullName
&other
) const
235 // use the old C style cmp that give in one operation the <, == and > relation
236 int cmp
= NLMISC::nlstricmp(Name
, other
.Name
);
242 return HomeSessionId
< other
.HomeSessionId
;
246 struct TTemporaryReservedNameInfo
250 uint32 HomeSessionId
;
255 // reserve names for 5 mn
256 TEMPORARY_RESERVED_NAME_EXPIRATION
= 60*5,
259 // typedef std::map<TName,TCharSlot> TNames;
260 typedef NLMISC::CTwinMap
<TFullName
,TCharSlot
> TNamesIndex
;
261 typedef std::map
<TName
,TGuildSlot
> TGuildNames
;
262 typedef std::map
<TGuildId
,TName
> TGuildIndex
;
263 typedef std::map
<TUserId
,TName
> TAccountNames
;
264 typedef std::map
<TName
,TUserId
> TReservedNames
;
265 typedef std::vector
<TName
> TForbiddenNames
;
266 typedef std::map
<TCharSlot
,std::set
<TName
> > TDuplicatedSlots
;
267 typedef std::map
<TName
, TTemporaryReservedNameInfo
> TTempReservedNames
;
270 TAccountNames _AccountNames
;
271 TGuildNames _GuildNames
;
272 TGuildIndex _GuildIndex
;
273 TReservedNames _ReservedNames
;
274 TForbiddenNames _ForbiddenNames
;
275 TDuplicatedSlots _DuplicatedSlots
;
276 /** Temporary reserved names. When a client create a character, it must first validate
277 * the character name.
278 * Then, later, the player send the character creation request.
279 * The temporary reservation guarantee that a validated name will not be validated
280 * or used by another client for the next 5 minutes, giving the time to the
281 * first client to send the character creation request.
282 * After the expiration period, the name is removed from the temporary reservation.
284 TTempReservedNames _TemporaryReservedNames
;
286 /// the list of changed character to broadcast to clients at next update (cleared each tick loop)
287 std::set
<TCharId
> _ChangedNames
;
288 /// The list of released names to broadcast at next update (cleared each tick loop)
289 std::set
<TCharId
> _ReleasedNames
;
291 /// the database connection to load data from database
292 MSW::CConnection
*_Database
;
294 // commands handler table
295 NLMISC_COMMAND_HANDLER_TABLE_BEGIN(CNameManager
)
296 NLMISC_COMMAND_HANDLER_ADD(CNameManager
, dump
, "display internal data about the name manager", "[all]")
297 NLMISC_COMMAND_HANDLER_ADD(CNameManager
, releaseGuildNamesForShard
, "Release all the guild name that belong to the specified shard", "<shardId>")
298 NLMISC_COMMAND_HANDLER_TABLE_END
300 NLMISC_CLASS_COMMAND_DECL(dump
);
301 NLMISC_CLASS_COMMAND_DECL(releaseGuildNamesForShard
);
305 #endif // RYZOM_NAME_MANAGER_H