ReplicaNet and RNLobby  1
UserClient.h
1 /* START_LICENSE_HEADER
2 
3 Copyright (C) 2000 Martin Piper, original design and program code
4 Copyright (C) 2001 Replica Software
5 
6 This program file is copyright (C) Replica Software and can only be used under license.
7 For more information visit: http://www.replicanet.com/
8 Or email: info@replicanet.com
9 
10 END_LICENSE_HEADER */
11 #include "RNPlatform/Inc/MemoryTracking.h"
12 #ifndef _USERCLIENT_H_
13 #define _USERCLIENT_H_
14 #include "RNLobby/Inc/BaseTransportServer.h"
15 #include "RNXPSockets/Inc/XPSocket.h"
16 #include <set>
17 #include <map>
18 
19 namespace RNReplicaNet
20 {
21 
22 namespace RNLobby
23 {
24 
26 class UserInfo
27 {
28 public:
29  UserInfo();
30  virtual ~UserInfo();
31 
32  int GetLoginID(void) const;
33  std::string GetName(void) const;
34 
35  enum
36  {
37  kAnyLoginID = 0
38  };
39 
40  enum
41  {
42  kState_Friend = (1<<0),
43  kState_FriendRequest = (1<<2),
44  kState_Online = (1<<3),
45  kState_GameInvite = (1<<5),
46  kState_Speaking = (1<<6),
47  kState_Ignored = (1<<7),
48  kState_InARoom = (1<<8), // MPi: TODO: Implement
49  kState_PlayingAGame = (1<<9)
50  };
52  int GetState(void) const;
53 
54  std::string GetGameInvite(void) const;
55 
56  std::string GetLastOnlineTime(void) const;
57 
58  std::string GetPlayingAGameURL(void) const;
59 
60  int mID;
61  int mLoginID;
62  std::string mName;
63  int mState;
64  std::string mGameInvite;
65  std::string mLastOnlineTime;
66  std::string mPlayingAGameURL;
67 };
68 
72 {
73 public:
74 
75  UserClient();
76  virtual ~UserClient();
77 
79  bool Start(void);
80 
81  bool Stop(void);
82 
85  void SetServer(const XPAddress &address);
86 
94  bool CreateLoginUser(const char *name,const char *password,const char *emailAddress = "",const char *optionalID = "");
95 
99  bool LoginUser(const char *name,const char *password);
100 
101  bool ChangePassword(const char *password = "");
102 
103  bool ChangeEmail(const char *emailAddress = "");
104 
105 
107  int GetLoginID(void);
108 
110  std::string GetLoginName(void);
111 
113  bool LogoutUser(void);
114 
116  bool DeleteUser(void);
117 
119  bool SendMessageTo(const int loginID,const char *message = "");
120 
122  bool AddFriend(const int friendLoginID);
123 
125  bool AuthoriseFriendRequest(const int friendLoginID);
126 
128  bool DenyFriendRequest(const int friendLoginID);
129 
131  bool RemoveFriend(const int friendLoginID);
132 
134  void AnswerMessage(const int fromLoginID = UserInfo::kAnyLoginID);
135 
136  enum Reason
137  {
138  kReason_ClientForcedLogout = 0,
139  kReason_ServerForcedLogout,
140  kReason_ChatRoomFull,
141  kReason_ChatRoomUserLeft,
142  kReason_ChatRoomClosed,
143  kReason_ChatRoomKickedByAdmin,
144  kReason_ChatRoomWrongPassword,
145  kReason_ChatRoomDoesNotExist,
146  kReason_ChatRoomBanned,
147  kReason_IncorrectLoginDetails,
148  kReason_AccountBlocked
149  };
150 
151  enum
152  {
153  kState_Online = (1<<0),
154  kState_FriendRequest = (1<<1),
155  kState_GameInvite = (1<<2),
156  kState_Message = (1<<3)
157  };
158 
161  int GetState(void);
162 
164  bool SendGameInvite(const int friendLoginID,const char *invite = "");
165 
167  bool CancelGameInvite(const int friendLoginID);
168 
170  bool AnswerGameInvite(const int friendLoginID = UserInfo::kAnyLoginID);
171 
172  enum VariableAccess
173  {
174  kPrivateRead = 0, // Values set by the statistics server.
175  kPrivateReadWrite = 1, // Values set by the owner or the statistics server.
176  kPublicReadPrivateReadWrite = 2, // Values set by the owner or the statistics server.
177  kPublicReadPrivateRead = 3 // Values set by the statistics server.
178  };
179 
182  bool SetVariable(const int section,const char *name,const char *value = 0,const VariableAccess access = kPrivateReadWrite);
183 
186  bool GetVariable(const int section,const char *name,const VariableAccess access = kPrivateReadWrite);
187 
190  bool GetVariable(const int loginID,const int section,const char *name,const VariableAccess access = kPublicReadPrivateRead);
191 
196  bool AddUserInfo(const int loginID,const bool useCache = true,const bool trackChanges = false);
197 
199  bool RemoveUserInfo(const int loginID);
200 
205  bool GetUserInfo(const int loginID,UserInfo *result);
206 
208  bool CreateChatRoom(const char *password = 0);
209 
211  bool LeaveChatRoom(void);
212 
215  bool EnterChatRoom(const int chatRoomID,const char *password = 0);
216 
218  int GetCurrentRoom(void);
219 
223  bool ChatRoomAdminMessage(const char *text,const unsigned int flags = 0);
224 
228  bool ChatRoomSay(const char *text,const unsigned int flags = 0);
229 
233  bool ChatRoomWhisper(const int loginID,const char *text,const unsigned int flags = 0);
234 
238  bool ChatRoomKickUser(const int loginID,const bool ban = false,const char *reason = "");
239 
243  bool GlobalKickUser(const int loginID,const bool ban = false,const char *reason = "");
244 
246  bool GlobalRestoreUser(const int loginID);
247 
249  bool IsRoomAdmin(void);
250 
252  bool IsSuperAdmin(void);
253 
257  bool PlayingAGame(const bool playing = false,const char *url = "");
258 
260  bool GetPlayingAGame(void);
262  std::string GetPlayingAGameURL(void);
263 
265  std::string GetLastOnlineTime(void);
266 
269  bool RequestUserNonce(const int seconds);
270 
273  bool RequestNamesLike(const char *match = "",const bool banned = false);
274 
275  int GetPendingGetVariableRequests(void);
276 
277 protected:
279 
281  virtual void CallbackConnectionEstablished(Transport *transport) {}
282 
284  virtual void CallbackUserLogin(const int loginID) {}
285 
287  virtual void CallbackUserLoginFailed(const Reason reason) {}
288 
290  virtual void CallbackUserLogout(const Reason reason) {}
291 
293  virtual void CallbackUserAlreadyExists(const int loginID) {}
294 
296  virtual void CallbackUserLoginFromOtherClient(void) {}
297 
300  virtual void CallbackMessageReceived(const int fromLoginID,const char *message) {}
301 
303  virtual void CallbackFriendDeleted(const int loginID) {}
304 
306  virtual void CallbackFriendAdded(const UserInfo &user) {}
307 
309  virtual void CallbackFriendUpdated(const UserInfo &user) {}
310 
312  virtual void CallbackStateChange(const int previousState,const int newState) {}
313 
315  virtual void CallbackGotVariable(const int loginID,const int section,const char *name,const char *value,const VariableAccess access) {}
316 
318  virtual void CallbackVariableNotFound(const int loginID,const int section,const char *name,const VariableAccess access) {}
319 
321  virtual void CallbackUserInfoDeleted(const int loginID) {}
322 
324  virtual void CallbackUserInfoAdded(const UserInfo &user) {}
325 
327  virtual void CallbackUserInfoUpdated(const UserInfo &user) {}
328 
329  virtual void CallbackChatRoomCreated(const int chatRoomID) {}
330 
331  virtual void CallbackChatRoomEntered(const int chatRoomID) {}
332 
333  virtual void CallbackChatRoomEnterFailed(const int chatRoomID,const Reason reason) {}
334 
335  virtual void CallbackChatRoomLeft(const Reason reason) {}
336 
337  // When entering a room the first callback will be CallbackChatRoomEntered() followed by any number of CallbackChatRoomUserEntered().
338  // The last callback will be CallbackChatRoomUserEntered() with the current user ID to signify the end of the list.
339  virtual void CallbackChatRoomUserEntered(const UserInfo &user) {}
340 
341  virtual void CallbackChatRoomUserLeft(const RNReplicaNet::RNLobby::UserInfo &user,const Reason reason) {}
342 
343  virtual void CallbackChatRoomMessage(const char *text,const UserInfo &user,const bool whispered,const bool isAdminMessage,const unsigned int flags) {}
344 
345  virtual void CallbackChatRoomKicked(const char *reason,bool banned,const UserInfo &user) {}
346 
347  virtual void CallbackGlobalKicked(const char *reason,bool banned,const UserInfo &user) {}
348 
349  virtual void CallbackPasswordChanged(void) {}
350 
351  virtual void CallbackCannotChangePassword(void) {}
352 
353  virtual void CallbackEmailChanged(void) {}
354 
355  virtual void CallbackCannotChangeEmail(void) {}
356 
357  virtual void CallbackGotUserNonce(const char *userNonce,const int seconds) {}
358 
360  virtual void CallbackNewStatsAvailable(const int lastUpdatedTitleID) {}
361 
365  virtual void CallbackNamesLikeBegin(const char *match,const bool banned) {}
366 
368  virtual void CallbackNamesLike(const UserInfo &user) {}
369 
371  virtual void CallbackNamesLikeEnd(void) {}
372 
373 protected:
374  // These callbacks from BaseTransportServer are implemented by this class. If they are implemented by the user this implementation must be called.
375  void CallbackTidy(void);
376 
377  bool CallbackPoll(void);
378 
379  bool CallbackParseValidMessage(Transport *transport,DynamicMessageHelper &message,const size_t length);
380 
381 private:
382 
383  // Called after a logout or other similar event
384  void InitInternalState(void);
385 
386  XPAddress mServerAddress;
387 
388  bool mLoginTried;
389  int mMyLoginID;
390 
391  std::set<int> mPendingFriendIDs;
392 
393  MutexClass mInternalLists;
394 
395  std::map<int,UserInfo> mUsers;
396  int mPreviousState;
397  int mCurrentState;
398  bool mMessagePending;
399  bool mGameInvitePending;
400 
401  void DoStateChangeCalc(void);
402  int mInCurrentRoom;
403  bool mRoomMode;
404  bool mIsRoomAdmin;
405  bool mIsGlobalAdmin;
406  std::string mMyLoginName;
407  bool mServerAddressSet;
408  bool mLastPlayingAGame;
409  std::string mLastOnlineTime;
410  std::string mLastPlayingAGameURL;
411 
412  int mPendingGetVariables;
413 };
414 
415 } // namespace RNLobby
416 
417 } // namespace RNReplicaNet
418 
419 #endif
bool CancelGameInvite(const int friendLoginID)
Cancel a game invite to a friend.
bool Start(void)
Because this uses a TransportUDP connection it will call XPURL::RegisterDefaultTransports() ...
Definition: Transport.h:35
bool SendGameInvite(const int friendLoginID, const char *invite="")
Sends a game invite to a friend. Only friends can receive game invites from a user.
bool AuthoriseFriendRequest(const int friendLoginID)
Authorise a friend request from a UserInfo that has the bit kState_FriendRequest set in mState...
virtual void CallbackVariableNotFound(const int loginID, const int section, const char *name, const VariableAccess access)
A virtual callback that is triggered when GetVariable() has failed to read a variable from the server...
Definition: UserClient.h:318
A server base class that handles encrypted messages to and from a transport.
Definition: BaseTransportServer.h:30
std::string GetLoginName(void)
Returns the login name as stored on the server when the user is logged in.
virtual void CallbackUserLogout(const Reason reason)
A virtual callback that is triggered when the user logs out.
Definition: UserClient.h:290
bool CreateLoginUser(const char *name, const char *password, const char *emailAddress="", const char *optionalID="")
bool ChatRoomKickUser(const int loginID, const bool ban=false, const char *reason="")
bool GetVariable(const int section, const char *name, const VariableAccess access=kPrivateReadWrite)
bool EnterChatRoom(const int chatRoomID, const char *password=0)
Definition: XPSocket.h:91
virtual void CallbackNamesLike(const UserInfo &user)
A virtual callback that is triggered when RequestNamesLike is used.
Definition: UserClient.h:368
void AnswerMessage(const int fromLoginID=UserInfo::kAnyLoginID)
Call this to clear the state for a received message.
bool DenyFriendRequest(const int friendLoginID)
Deny a friend request from a UserInfo that has the bit kState_FriendRequest set in mState...
virtual void CallbackConnectionEstablished(Transport *transport)
User extensible callbacks.
Definition: UserClient.h:281
virtual void CallbackFriendUpdated(const UserInfo &user)
A virtual callback that is triggered when a UserInfo is changed.
Definition: UserClient.h:309
std::string GetLastOnlineTime(void)
Gets the last time this user account logged in.
bool CallbackParseValidMessage(Transport *transport, DynamicMessageHelper &message, const size_t length)
int GetLoginID(void)
Returns the login ID when the user is logged in or UserInfo::kAnyLoginID when the user is not logged ...
virtual void CallbackMessageReceived(const int fromLoginID, const char *message)
Definition: UserClient.h:300
bool IsSuperAdmin(void)
If the user is a super admin this will return true.
bool RemoveUserInfo(const int loginID)
Stops tracking a loginID for changes that have been requested from using AddUserInfo(). If the user is a friend this will not remove the user from the friend list.
bool SetVariable(const int section, const char *name, const char *value=0, const VariableAccess access=kPrivateReadWrite)
bool AnswerGameInvite(const int friendLoginID=UserInfo::kAnyLoginID)
Call this to clear the state for a received game invite from a specific friend or by default all game...
bool CreateChatRoom(const char *password=0)
Create a new chat room and if successful makes this user join the room as the room admin...
virtual void CallbackFriendDeleted(const int loginID)
A virtual callback that is triggered when the user deletes a friend.
Definition: UserClient.h:303
virtual void CallbackNewStatsAvailable(const int lastUpdatedTitleID)
A virtual callback that is triggered when the StatServer updates any variables for this user...
Definition: UserClient.h:360
bool AddFriend(const int friendLoginID)
Request to add someone as a friend.
bool LoginUser(const char *name, const char *password)
bool LeaveChatRoom(void)
Leave a chat room. If the user created the room and the room has no admins left then the room will be...
std::string GetPlayingAGameURL(void)
Gets the url state set by PlayingAGame()
bool ChatRoomAdminMessage(const char *text, const unsigned int flags=0)
bool RemoveFriend(const int friendLoginID)
Remove a user from the friend list. The user info changes will stop tracking.
void SetServer(const XPAddress &address)
bool RequestUserNonce(const int seconds)
bool ChatRoomSay(const char *text, const unsigned int flags=0)
bool RequestNamesLike(const char *match="", const bool banned=false)
virtual void CallbackUserInfoDeleted(const int loginID)
A virtual callback that is triggered when the user deletes a user info request.
Definition: UserClient.h:321
virtual void CallbackNamesLikeBegin(const char *match, const bool banned)
Definition: UserClient.h:365
int GetCurrentRoom(void)
Returns the current chatRoomID.
virtual void CallbackUserLogin(const int loginID)
A virtual callback that is triggered when the user successfully logs in.
Definition: UserClient.h:284
bool DeleteUser(void)
Deletes a user that is currently logged in.
bool SendMessageTo(const int loginID, const char *message="")
Sends a message from a logged in user to a login ID.
virtual void CallbackUserInfoUpdated(const UserInfo &user)
A virtual callback that is triggered when a UserInfo is changed.
Definition: UserClient.h:327
bool GetPlayingAGame(void)
Gets the state set by PlayingAGame()
virtual void CallbackGotVariable(const int loginID, const int section, const char *name, const char *value, const VariableAccess access)
A virtual callback that is triggered when GetVariable() has read a variable from the server...
Definition: UserClient.h:315
Definition: ThreadClass.h:39
This class is used to communicate with the UserServer class.
Definition: UserClient.h:26
virtual void CallbackUserInfoAdded(const UserInfo &user)
A virtual callback that is triggered when a AddUserInfo request has succeeded.
Definition: UserClient.h:324
bool GlobalRestoreUser(const int loginID)
If a user account has been globally banned this will restore the account to active service...
bool ChatRoomWhisper(const int loginID, const char *text, const unsigned int flags=0)
bool PlayingAGame(const bool playing=false, const char *url="")
bool AddUserInfo(const int loginID, const bool useCache=true, const bool trackChanges=false)
virtual void CallbackStateChange(const int previousState, const int newState)
A virtual callback that is triggered when the state changes for this instance.
Definition: UserClient.h:312
virtual void CallbackFriendAdded(const UserInfo &user)
A virtual callback that is triggered when a friend UserInfo is added. A UserInfo can be added when Us...
Definition: UserClient.h:306
Definition: MessageHelper.h:211
virtual void CallbackUserAlreadyExists(const int loginID)
A virtual callback that is triggered when the user account already exists.
Definition: UserClient.h:293
bool LogoutUser(void)
Logs out a user.
bool GlobalKickUser(const int loginID, const bool ban=false, const char *reason="")
bool GetUserInfo(const int loginID, UserInfo *result)
virtual void CallbackNamesLikeEnd(void)
A virtual callback that is triggered when RequestNamesLike is used.
Definition: UserClient.h:371
virtual void CallbackUserLoginFromOtherClient(void)
A virtual callback that is triggered when the user is logged out due to the same account being logged...
Definition: UserClient.h:296
virtual void CallbackUserLoginFailed(const Reason reason)
A virtual callback that is triggered when the user login attempt fails.
Definition: UserClient.h:287
Definition: UserClient.h:71
bool IsRoomAdmin(void)
If the user is a room admin this will return true.