/// <summary> /// Notifies and removes a player session /// </summary> /// <param name="netMsg"></param> private void OnPlayerDisconnection(NetworkMessage netMsg) { Debug.Log($"A player with id {netMsg.Sender.connectionId} has disconnected from the Server"); var disconnectedPlayerSession = SessionManager.Find(netMsg.Sender.connectionId); if (disconnectedPlayerSession != null) { var removedPlayerFriends = UserFriends.GetAll(disconnectedPlayerSession.Id); List <NetworkClient> groupToSend = new List <NetworkClient>(); if (removedPlayerFriends.Count > 0) { foreach (var friend in removedPlayerFriends) { if (SessionManager.Exists(friend.Id)) { var friendSession = SessionManager.Find(friend.Id); if (friendSession != null) { groupToSend.Add(friendSession.conn); } } } } if (groupToSend.Count > 0) { Server.SendToGroup(groupToSend, LoginEvent.PLAYER_FRIENDS_LOGOUT, disconnectedPlayerSession.Id); } } SessionManager.Remove(netMsg.Sender.connectionId); }
/// <summary> /// Handler a request to0 retrieve aa player friends /// </summary> /// <param name="netMsg"></param> private void OnPlayerFriendsRequest(NetworkMessage netMsg) { var userId = SessionManager.GetPlayerId(netMsg.Sender); var friends = UserFriends.GetAll(userId); if (friends.Count > 0) { _server.SendToClient(netMsg.Sender, GameEvent.PLAYER_FRIENDS_SUCCESS, friends); } else { Debug.Warning("He has none so just send a event error message type saying it"); } }
/// <summary> /// Handler for new player authentication request /// </summary> /// <param name="netMsg"></param> private void OnPlayerAuthRequest(NetworkMessage netMsg) { var authdata = netMsg.ReadMessage <AuthData>(); var profileData = Users.Select(authdata.username, authdata.password); if (profileData != null) { if (SessionManager.Exists(profileData.Id)) { SessionManager.Remove(profileData.Id); //Also kick the guy socket Debug.Warning("Removed a session active with the account asking to Auth"); } SessionManager.New(netMsg.Sender, profileData.Id); Server.SendToClient(netMsg.Sender, LoginEvent.LOGIN_SUCCESS, profileData); //Lets notify friends he has logged in List <NetworkClient> groupToSend = new List <NetworkClient>(); var friends = UserFriends.GetAll(profileData.Id); if (friends.Count > 0) { foreach (var friend in friends) { if (SessionManager.Exists(friend.Id)) { var friendSession = SessionManager.Find(friend.Id); if (friendSession != null) { groupToSend.Add(friendSession.conn); } } } } if (groupToSend.Count > 0) { Server.SendToGroup(groupToSend, LoginEvent.PLAYER_FRIENDS_LOGIN, profileData.Id); } } else { Server.SendToClient(netMsg.Sender, LoginEvent.LOGIN_FAIL, new ActionError { code = 100, message = "User does not exist or password is wrong" }); } }
/// <summary> /// Manual Logout Handling /// </summary> /// <param name="netMsg"></param> private void OnPlayerLogoutRequest(NetworkMessage netMsg) { if (netMsg.Sender.connectionId != null) { Debug.Log("Disconnecting user id = " + netMsg.Sender.connectionId); var disconnectedPlayerSession = SessionManager.Find(netMsg.Sender.connectionId); //Notify all friends he has if (disconnectedPlayerSession != null) { var removedPlayerFriends = UserFriends.GetAll(disconnectedPlayerSession.Id); List <NetworkClient> groupToSend = new List <NetworkClient>(); if (removedPlayerFriends.Count > 0) { foreach (var friend in removedPlayerFriends) { if (SessionManager.Exists(friend.Id)) { var friendSession = SessionManager.Find(friend.Id); if (friendSession != null) { groupToSend.Add(friendSession.conn); } } } } if (groupToSend.Count > 0) { Server.SendToGroup(groupToSend, LoginEvent.PLAYER_FRIENDS_LOGOUT, disconnectedPlayerSession.Id); } SessionManager.Remove(disconnectedPlayerSession.Id); } } }