Beispiel #1
0
        /// <summary>
        /// Removes a(n) <see cref="IGroupMember"/> from the group.
        /// </summary>
        public bool RemoveMember(MmoGuid memberToRemoveGuid)
        {
            // if the member does not exist, just return
            if (!members.Exists(m => m.Guid == memberToRemoveGuid))
            {
                return(false);
            }

            // TODO: Send it via an event channel
            // removing the member in every world groups
            if (IsFormed)
            {
                foreach (var world in social.Worlds)
                {
                    world.GroupManager.RemoveMember(guid, memberToRemoveGuid);
                }
            }

            Reference <IGroupMember> reference;

            if (memberReferences.TryGetValue(memberToRemoveGuid, out reference))
            {
                // the member is online send an uninvite event so the client will be notified that he or she has been removed from the group
                var memberToRemove = reference.Object;
                // send an uninvite event so the client will be notified he/she has been removed from the group
                memberToRemove.Peer.SendEvent(new GroupUninvited(), new MessageParameters {
                    ChannelId = PeerSettings.GroupEventChannel
                });
                memberToRemove.Group = null;

                reference.Dispose();
                this.memberReferences.Remove(memberToRemoveGuid);
            }

            // removing the member from the profiles
            this.members.RemoveAll(m => m.Guid == memberToRemoveGuid);

            // if there are active members notify all of them
            if (memberReferences.Count > 0)
            {
                var groupMemberRemoved = new GroupMemberRemoved {
                    ObjectId = memberToRemoveGuid
                };
                // let every online players know that a player has been removed
                foreach (var memberReference in memberReferences)
                {
                    memberReference.Value.Object.Peer.SendEvent(groupMemberRemoved, new MessageParameters {
                        ChannelId = PeerSettings.GroupEventChannel
                    });
                }
            }

            return(true);
        }
        private static void DoGroupMemberRemoved(GroupMemberRemoved groupMemberRemoved)
        {
            Group group;

            if (!GroupManager.Instance.TryGetGroup(groupMemberRemoved.GroupGuid, out group))
            {
                _logger.WarnFormat("[DoGroupMemberRemoved]: Group (Guid={0}) not found.", groupMemberRemoved.GroupGuid);
                return;
            }

            group.RemoveMember(groupMemberRemoved.MemberGuid);
        }
        private static void HandleEventGroupMemberRemoved(IRpcProtocol protocol, IEventData eventData)
        {
            var groupMemberRemoved = new GroupMemberRemoved(protocol, eventData);

            if (!groupMemberRemoved.IsValid)
            {
                _logger.ErrorFormat("[HandleEventGroupMemberRemoved]: Received an invalid {0}", eventData.GetType());
                return;
            }

            MmoWorld.Instance.PrimaryFiber.Enqueue(() => DoGroupMemberRemoved(groupMemberRemoved));
        }
        private OperationResponse HandleOperationLeaveWorld(OperationRequest operationRequest)
        {
            var operation = new LeaveWorld(Protocol, operationRequest);

            if (!operation.IsValid)
            {
                return new OperationResponse(operationRequest.OperationCode)
                       {
                           ReturnCode = (short)ResultCode.InvalidOperationParameter, DebugMessage = operation.GetErrorMessage()
                       }
            }
            ;

            SocialSession session;

            if (social.SessionCache.TryGetSessionBySessionId(operation.SessionId, out session))
            {
                session.LeaveWorld(operation.WorldId);
            }
            return(null);
        }

        #endregion

        #region Implementation of IWorld

        /// <summary>
        /// Gets the group manager
        /// </summary>
        IGroupManager IWorld.GroupManager
        {
            get
            {
                return(this);
            }
        }

        #endregion

        #region Implementation of IGroupManager

        void IGroupManager.FormGroup(MmoGuid groupGuid, GroupMemberStructure leader)
        {
            var groupFormed = new GroupFormed
            {
                GroupGuid  = groupGuid,
                LeaderInfo = leader,
            };

            this.SendEvent(new EventData((byte)ServerEventCode.GroupFormed, groupFormed), new SendParameters());
            this.DoAddMember(groupGuid, leader);
        }

        void IGroupManager.DisbandGroup(MmoGuid groupGuid)
        {
            var groupDisbanded = new GroupDisbanded
            {
                GroupGuid = groupGuid,
            };

            this.SendEvent(new EventData((byte)ServerEventCode.GroupDisbanded, groupDisbanded), new SendParameters());
        }

        void IGroupManager.AddMember(MmoGuid groupGuid, GroupMemberStructure member)
        {
            this.DoAddMember(groupGuid, member);
        }

        void DoAddMember(MmoGuid groupGuid, GroupMemberStructure member)
        {
            SocialSession session;

            if (social.SessionCache.TryGetSessionBySessionName(member.Name, out session))
            {
                var groupMemberAddedSession = new GroupMemberAddedSession
                {
                    GroupGuid  = groupGuid,
                    MemberInfo = member,
                    SessionId  = session.SessionId
                };
                this.SendEvent(new EventData((byte)ServerEventCode.GroupMemberAddedSession, groupMemberAddedSession), new SendParameters());
            }
            else
            {
                var groupMemberAdded = new GroupMemberAdded
                {
                    GroupGuid  = groupGuid,
                    MemberInfo = member,
                };
                this.SendEvent(new EventData((byte)ServerEventCode.GroupMemberAdded, groupMemberAdded), new SendParameters());
            }
        }

        void IGroupManager.RemoveMember(MmoGuid groupGuid, MmoGuid memberGuid)
        {
            var groupMemberRemoved = new GroupMemberRemoved
            {
                GroupGuid  = groupGuid,
                MemberGuid = memberGuid,
            };

            this.SendEvent(new EventData((byte)ServerEventCode.GroupMemberRemoved, groupMemberRemoved), new SendParameters());
        }

        void IGroupManager.UpdateMemberStatus(MmoGuid groupGuid, MmoGuid memberGuid, OnlineStatus status)
        {
            switch (status)
            {
            case OnlineStatus.Online:
                var groupMemberOnline = new GroupMemberOnline
                {
                    GroupGuid  = groupGuid,
                    MemberGuid = memberGuid,
                };
                this.SendEvent(new EventData((byte)ServerEventCode.GroupMemberOnline, groupMemberOnline), new SendParameters());
                break;

            case OnlineStatus.Offline:
                var groupMemberOffline = new GroupMemberOffline
                {
                    GroupGuid  = groupGuid,
                    MemberGuid = memberGuid,
                };
                this.SendEvent(new EventData((byte)ServerEventCode.GroupMemberOffline, groupMemberOffline), new SendParameters());
                break;
            }
        }

        #endregion
    }