Пример #1
0
        public void RemoveClient(Client client)
        {
            lock (clientListLock)
            {
                _globalClientList.Remove(client);
            }

            var db = Database.GetInstance();

            if (client.UserInfo == null)
            {
                return;
            }

            var user = db.GetUserByEmail(client.UserInfo.EmailAddress);

            var friend = MessageReceiver.GetFriendFromUser(user);


            //
            // Remove the user from his room in case he is still part of one.
            //

            var roomManager = RoomManager.GetInstance();

            roomManager.RemoveMemberfromRoom(client.Room.Host, client);


            //
            // Send a message saying that the user logged off to all of his friends who are online.
            //

            var friends = db.GetFriends(client.UserInfo.EmailAddress);

            if (null == friends)
            {
                return;
            }

            var onlineFriends = new List <Client>();

            foreach (user frd in friends)
            {
                var c = GetClientFromEmailAddress(frd.email);
                if (null != c)
                {
                    onlineFriends.Add(c);
                }
            }

            MessageSender.BroadcastMessage(
                onlineFriends,
                PacketType.s_UserLoggedOut,
                friend,
                client);
        }
Пример #2
0
        public void AddClient(Client client)
        {
            lock (clientListLock)
            {
                _globalClientList.Add(client);
            }

            var db = Database.GetInstance();

            var user = db.GetUserByEmail(client.UserInfo.EmailAddress);

            if (user == null)
            {
                return;
            }

            var friend = MessageReceiver.GetFriendFromUser(user);


            //
            // Send a message saying that the user logged on to all of his friends who are online.
            //

            var friends       = db.GetFriends(client.UserInfo.EmailAddress);
            var onlineFriends = new List <Client>();

            if (friends == null)
            {
                return;
            }

            foreach (user frd in friends)
            {
                var c = GetClientFromEmailAddress(frd.email);
                if (null != c)
                {
                    onlineFriends.Add(c);
                }
            }

            MessageSender.BroadcastMessage(
                onlineFriends,
                PacketType.s_UserLoggedIn,
                friend,
                client);
        }
Пример #3
0
        public void ChangeToRandomNewHost()
        {
            var random = new Random();

            lock (roomLock)
            {
                if (_members.Count > 0)
                {
                    var index = random.Next(0, _members.Count - 1);
                    _host = _members[index];
                }

                MessageSender.BroadcastMessage(
                    _members,
                    PacketType.s_HostTransfer,
                    _host.UserInfo);
            }
        }
Пример #4
0
        public void SignalClientIsReadyForGame(Client client)
        {
            // ToDo: Add some sort of time out guard here and drop clients if they take too long to respond.
            _clientsReadyForGame.Add(client);

            //
            //  If this condition has been met, it means that all of the clients are ready.
            //

            if (_clientsReadyForGame.Count == _members.Count)
            {
                MessageSender.BroadcastMessage(_members, PacketType.s_GameStart, null);

                _clientsReadyForGame.Clear();

                RoomStatus = RoomStatus.InGame;
            }
        }
Пример #5
0
        public void RemoveMemberfromRoom(Client roomHost, Client memberToRemove)
        {
            Room room;

            lock (_roomListLock)
            {
                room = _roomList.FirstOrDefault(myRoom => myRoom.Host == roomHost);
            }

            if (room == null)
            {
                return;
            }

            room.Members.Remove(memberToRemove);

            if (roomHost == memberToRemove)
            {
                room.ChangeToRandomNewHost();
            }

            if (0 == room.Members.Count)
            {
                lock (_roomListLock)
                {
                    _roomList.Remove(room);
                }
            }
            else
            {
                MessageSender.BroadcastMessage(
                    room.Members,
                    PacketType.s_UserLeftRoom,
                    memberToRemove.UserInfo,
                    memberToRemove);
            }
        }
Пример #6
0
        public void AddMemberToRoom(Client roomHost, ref Client memberToAdd)
        {
            Room room;

            lock (_roomListLock)
            {
                room = _roomList.FirstOrDefault(myRoom => myRoom.Host == roomHost);
            }

            if (room == null)
            {
                return;
            }


            //
            // First, remove the member from his previous room.
            //

            RemoveMemberfromRoom(memberToAdd.Room.Host, memberToAdd);


            //
            // Now add the member to the new room.
            //

            room.Members.Add(memberToAdd);
            memberToAdd.Room = room;


            //
            // Send a message to all the existing members in the room indicating who has joined.
            //

            MessageSender.BroadcastMessage(
                room.Members,
                PacketType.s_UserJoinedRoom,
                memberToAdd.UserInfo,
                memberToAdd);


            //
            // Now send a message to the person who has joined containing the list of room
            // members.
            //

            var friendList = new FriendList();

            foreach (var c in room.Members)
            {
                friendList.Friends.Add(c.UserInfo);
            }

            MessageSender.SendMessage(
                memberToAdd,
                PacketType.s_RoomList,
                friendList);


            //
            // Now tell the joining person who the room host is.
            //

            MessageSender.SendMessage(
                memberToAdd,
                PacketType.s_HostTransfer,
                room.Host.UserInfo);
        }