public void HandleMessage(LobbyPlayer player, Message m) { var rtn = Message.Create(m.Type); switch (m.Type) { case "getCrew": { this.LoadCrew(Regex.Replace(m.GetString(0), @"\s+", "").ToLower(), crew => crew.SendGetMessage(player)); break; } case "getMyCrews": { this.GetMyCrews(rtn, player); break; } case "createCrew": { this.CreateCrew(rtn, player, m.GetString(0).Trim()); break; } case "getCrewInvites": { InvitationHelper.GetInvitationsTo(this.client.BigDB, InvitationType.Crew, player.Name, invites => { var invitesIds = invites.Where(it => it.Status == InvitationStatus.Pending) .Select(it => it.Sender) .ToArray(); if (invitesIds.Length > 0) { this.client.BigDB.LoadKeys("Crews", invitesIds, crews => { foreach (var crew in crews.Where(crew => crew != null)) { rtn.Add(crew.Key); rtn.Add(crew.GetString("Name")); rtn.Add(crew.GetString("LogoWorld", "")); } player.Send(rtn); }); } else { player.Send(rtn); } }); break; } case "blockCrewInvites": { var crewId = m.GetString(0).ToLower(); var shouldBlock = m.GetBoolean(1); InvitationBlocking.BlockCrew(this.client.BigDB, player.ConnectUserId, crewId, shouldBlock, () => { if (shouldBlock) { InvitationHelper.GetInvitation(this.client.BigDB, InvitationType.Crew, crewId, player.Name, invitation => { if (invitation.Exists) { invitation.Status = InvitationStatus.Rejected; invitation.Save(() => player.Send(m.Type, true)); } else { player.Send(m.Type, true); } }); } else { player.Send(m.Type, true); } }); break; } case "blockAllCrewInvites": { var shouldBlock = m.GetBoolean(0); InvitationBlocking.BlockAllCrews(this.client.BigDB, player.ConnectUserId, shouldBlock); player.Send(m.Type, shouldBlock); break; } } }
public void ProcessMessages(QueueItem item, LobbyPlayer player) { switch (item.Method) { case "getFriends": { this.GetFriendKeys(player.ConnectUserId, keys => { if (keys.Count <= 0) { player.Send(item.Method); return; } OnlineStatus.GetOnlineStatus(this.client, keys.ToArray(), status => { var rtn = Message.Create(item.Method); foreach (var stat in status.Where(stat => stat != null)) { stat.ToMessage(rtn); } player.Send(rtn); }); }); break; } case "getPending": { InvitationHelper.GetInvitationsFrom(this.client.BigDB, InvitationType.Friend, this.name, invites => { var rtn = Message.Create(item.Method); foreach (var invite in invites) { rtn.Add(invite.Recipient); rtn.Add((int)invite.Status); } player.Send(rtn); }); break; } case "getInvitesToMe": { InvitationHelper.GetInvitationsTo(this.client.BigDB, InvitationType.Friend, this.name, invites => { var rtn = Message.Create(item.Method); foreach (var invite in invites.Where(it => it.Status == InvitationStatus.Pending)) { rtn.Add(invite.Sender); } player.Send(rtn); }); break; } case "getBlockedUsers": { InvitationBlocking.GetBlockedUsers(this.client.BigDB, this.connectUserId, blockedUsers => { var rtn = Message.Create(item.Method); foreach (var blockedUser in blockedUsers) { rtn.Add(blockedUser); } player.Send(rtn); }); break; } case "createInvite": { if (!player.HasFriendFeatures) { player.Send(item.Method, false); return; } var friendName = item.Message.GetString(0).ToLower(); this.CreateInvitation(friendName, () => player.Send(item.Method, true), error => { switch (error) { case InvitationError.PlayerNotFound: { player.Send(item.Method, false, "Unknown user. Please check your spelling."); break; } case InvitationError.AlreadyAdded: { player.Send(item.Method, false, "This user is already on your friendslist!"); break; } case InvitationError.AlreadySent: { player.Send(item.Method, false, "You already have a pending invitation for this user."); break; } case InvitationError.LimitReached: { player.Send(item.Method, false, "You cannot have more than " + this.MaxFriendsAllowed + " friends and invites."); break; } case InvitationError.Blocked: { player.Send(item.Method, false, "This user is blocking friend requests."); break; } case InvitationError.SendingToSelf: { player.Send(item.Method, false, "You cannot add yourself."); break; } } }); break; } case "answerInvite": { if (!player.HasFriendFeatures) { player.Send(item.Method, false); return; } var invitedBy = item.Message.GetString(0).ToLower(); var accept = item.Message.GetBoolean(1); this.AnswerInvitation(invitedBy, accept, senderId => { OnlineStatus.GetOnlineStatus(this.client, senderId, onlineStatus => { var rtn = Message.Create(item.Method, true); player.Send(onlineStatus.ToMessage(rtn)); }); }, error => { switch (error) { case InvitationError.PlayerNotFound: { player.Send(item.Method, false, "Sorry, the sender of this friend request does not exist."); break; } case InvitationError.InvitationNotFound: { player.Send(item.Method, false, "Sorry, the friend request does not exist anymore."); break; } case InvitationError.LimitReached: { player.Send(item.Method, false, "You cannot have more than " + this.MaxFriendsAllowed + " friends."); break; } } }); break; } case "deleteInvite": { var recipientName = item.Message.GetString(0).ToLower(); InvitationHelper.DeleteInvitation(this.client.BigDB, InvitationType.Friend, this.name, recipientName, success => { if (!success) { this.client.ErrorLog.WriteError( "Error deleting invitation from " + player.Name + " to " + recipientName, "Invite not found", "Error deleting pending invitation", null); } player.Send(item.Method, success); }); break; } case "blockUserInvites": { var invitedByName = item.Message.GetString(0).ToLower(); var shouldBlock = item.Message.GetBoolean(1); InvitationBlocking.BlockUser(this.client.BigDB, this.connectUserId, invitedByName, shouldBlock, () => { if (shouldBlock) { InvitationHelper.GetInvitation(this.client.BigDB, InvitationType.Friend, invitedByName, this.name, invitation => { if (invitation.Exists) { invitation.Status = InvitationStatus.Rejected; invitation.Save(() => player.Send(item.Method, true)); } else { player.Send(item.Method, true); } }); } else { player.Send(item.Method, true); } }); break; } case "getBlockStatus": { InvitationBlocking.IsBlockingAllUsers(this.client.BigDB, this.connectUserId, isBlocking => { player.Send(item.Method, isBlocking); }); break; } case "blockAllInvites": { var shouldBlock = item.Message.GetBoolean(0); InvitationBlocking.BlockAllFriends(this.client.BigDB, this.connectUserId, shouldBlock); player.Send(item.Method, shouldBlock); break; } case "deleteFriend": { CommonPlayer.GetId(this.client.BigDB, item.Message.GetString(0).ToLower(), friendId => { this.AddOrRemoveFriend(friendId, false, delegate { player.Send(item.Method, true); }); }); break; } case "GetOnlineStatus": { var id = item.Message.Count > 0 ? item.Message.GetString(0) : player.ConnectUserId; OnlineStatus.GetOnlineStatus(this.client, id, onlineStatus => player.Send(onlineStatus.ToMessage(item.Method))); break; } } }