/* The GetFriendList MessageHandler * It handles messages of UPDATE_FRIENDSHIP_REQ type. */ private static void UpdateFriendship(RMessage message, TcpClient connection) { Console.WriteLine("UpdateFriendship"); FriendshipData friendship = (FriendshipData)message.Data; RMessage replyMessageFormer, replyMessageLatter; if (friendship.Status) { FriendshipConnector.ConfirmFriendship(friendship); replyMessageFormer = new RMessage(MessageType.CONFIRM_FRIENDSHIP_REPLY, friendship.Latter); replyMessageLatter = new RMessage(MessageType.CONFIRM_FRIENDSHIP_REPLY, friendship.Former); UserConnector.UpdateUserRank(friendship.Latter, Ranking.ADD_FRIEND); UserConnector.UpdateUserRank(friendship.Former, Ranking.ADD_FRIEND); } else { FriendshipConnector.DeleteFriendship(friendship); replyMessageFormer = new RMessage(MessageType.DENY_FRIENDSHIP_REPLY, friendship.Latter); replyMessageLatter = new RMessage(MessageType.DENY_FRIENDSHIP_REPLY, friendship.Former); } ClientWorker formerWorker = ServerCore.GetWorkerById(friendship.Former); if (formerWorker != null) { formerWorker.SendMessage(replyMessageFormer); } ClientWorker latterWorker = ServerCore.GetWorkerById(friendship.Latter); if (latterWorker != null) { latterWorker.SendMessage(replyMessageLatter); } }
/* The GetFriendList MessageHandler * It handles messages of ADD_SAY_REQUEST type. */ private static void AddSay(RMessage message, TcpClient connection) { Console.WriteLine("AddSay"); SayData say = (SayData)(message.Data); uint id = say.Id; say.Id = ServerCore.GetIdByConnection(connection); if (ServerCore.GetWorkerById(id) != null) { RMessage replyMessage = new RMessage(MessageType.ADD_SAY_REPLY, say); ServerCore.GetWorkerById(id).SendMessage(replyMessage); } UserConnector.UpdateUserRank(say.Id, Ranking.ADD_SAY); }
/* * On connection crash, makes the user offline */ private static void MakeUserOffline(TcpClient connection) { uint id = ServerCore.GetIdByConnection(connection); UserData user = UserConnector.UpdateUserState(id, false); List <UserData> friendList = UserConnector.GetFriendList(id); RMessage replyMessage = new RMessage(MessageType.CHANGE_USER_STATE_REPLY, user); foreach (UserData friend in friendList) { ClientWorker worker = ServerCore.GetWorkerById(friend.Id); if (worker != null) { ServerCore.GetWorkerById(friend.Id).SendMessage(replyMessage); } } }
/* The GetFriendList MessageHandler * It handles messages of ADD_FRIEND_REQ type. */ private static void AddFriend(RMessage message, TcpClient connection) { Console.WriteLine("AddFriend"); uint thisUserId = ServerCore.GetIdByConnection(connection); UserData friend = UserConnector.GetUser((String)message.Data); UserData thisUser = UserConnector.GetUser(thisUserId); if (friend == null) { return; } if (friend.Id == thisUserId) { return; } List <FriendshipData> friendships = FriendshipConnector.GetFriendshipList(thisUserId); foreach (FriendshipData friendshipIt in friendships) { if (friendshipIt.Former == friend.Id || friendshipIt.Latter == friend.Id) { return; } } FriendshipData friendship = new FriendshipData(thisUserId, friend.Id, false); FriendshipConnector.AddFriendship(friendship); ClientWorker formerWorker = ServerCore.GetWorkerById(friendship.Former); ClientWorker latterWorker = ServerCore.GetWorkerById(friendship.Latter); RMessage replyMessageFormer = new RMessage(MessageType.ADD_FRIEND_REPLY, friend); RMessage replyMessageLatter = new RMessage(MessageType.ADD_FRIEND_REPLY, thisUser); RMessage replyMessage = new RMessage(MessageType.ADD_FRIENDSHIP_REPLY, friendship); if (formerWorker != null) { formerWorker.SendMessage(replyMessageFormer); formerWorker.SendMessage(replyMessage); } if (latterWorker != null) { latterWorker.SendMessage(replyMessageLatter); latterWorker.SendMessage(replyMessage); } }
/* The GetFriendList MessageHandler * It handles messages of CHANGE_USER_STATE_REQUEST type. */ private static void ChangeUserState(RMessage message, TcpClient connection) { Console.WriteLine("ChangeUserState"); uint id = ServerCore.GetIdByConnection(connection); UserData user = UserConnector.UpdateUserState(id, (bool)message.Data); List <UserData> friendList = UserConnector.GetFriendList(id); RMessage replyMessage = new RMessage(MessageType.CHANGE_USER_STATE_REPLY, user); foreach (UserData friend in friendList) { ClientWorker worker = ServerCore.GetWorkerById(friend.Id); if (worker != null) { ServerCore.GetWorkerById(friend.Id).SendMessage(replyMessage); } } ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage); }