Beispiel #1
0
        public async Task <CreateMessageGroupResponse> CreateMessageGroup(List <String> groupMembers)
        {
            // Ensure all members are in the room.
            if (groupMembers.Except(GameConfiguration.Players.Select(it => it.Id)).Any())
            {
                // A player in the group is not in the game.
                return(new CreateMessageGroupResponse()
                {
                    Status = ResponseFactory.createResponse(ResponseType.INVALID_REQUEST),
                });;
            }

            // Get all group chats for the room
            List <GroupChat> roomGroupChats = (await MongoConnector.GetMessageGroupCollection()
                                               .FindAsync(group => group.RoomId == GameConfiguration.Id))
                                              .ToList()
                                              .Select(group => new GroupChat(this, group.ToProto()))
                                              .ToList();

            foreach (GroupChat groupChat in roomGroupChats)
            {
                if (groupChat.MessageGroup.GroupMembers.Count() == groupMembers.Count())
                {
                    // If the group chats are the same size and removing all the duplicates results in an empty list,
                    // We have a duplicate group
                    if (!groupChat.MessageGroup.GroupMembers.Except(groupMembers).Any())
                    {
                        return(new CreateMessageGroupResponse()
                        {
                            Status = ResponseFactory.createResponse(ResponseType.DUPLICATE),
                        });
                    }
                }
            }

            // Otherwise, create the group
            GroupModel newGroup = new GroupModel();

            newGroup.Id     = Guid.NewGuid().ToString();
            newGroup.RoomId = GameConfiguration.Id;
            foreach (string s in groupMembers)
            {
                DbUserModel model = await DbUserModel.GetUserFromGuid(s);

                if (model != null)
                {
                    newGroup.GroupMembers.Add(model.UserModel.Id);
                }
            }

            await MongoConnector.GetMessageGroupCollection().InsertOneAsync(new GroupModelMapper(newGroup));

            return(new CreateMessageGroupResponse()
            {
                GroupId = newGroup.Id,
                Status = ResponseFactory.createResponse(ResponseType.SUCCESS),
            });
        }
        public async Task <MessageGroup> asMessageGroup(int messagesPagination = 1)
        {
            MessageGroup model = new MessageGroup()
            {
                GroupId = MessageGroup.Id,
            };

            // Convert UserIds to Users.
            foreach (string userId in MessageGroup.GroupMembers)
            {
                model.GroupMembers.Add((await DbUserModel.GetUserFromGuid(userId)).AsUser());
            }

            model.Messages.AddRange(await GetMessages(messagesPagination));
            return(model);
        }
Beispiel #3
0
        public async Task <ResponseStatus> JoinRoom(DbUserModel dbUserModel)
        {
            if (IsRoomFull())
            {
                return(ResponseFactory.createResponse(ResponseType.ROOM_IS_FULL));
            }

            if (IsPlayerInRoom(dbUserModel))
            {
                return(ResponseFactory.createResponse(ResponseType.DUPLICATE));
            }

            if (GameConfiguration.RoomStatus != RoomStatus.Open)
            {
                return(ResponseFactory.createResponse(ResponseType.GAME_ALREADY_STARTED));
            }

            List <DbUserModel> playersInRoom = new List <DbUserModel>();

            foreach (User user in GameConfiguration.Players)
            {
                playersInRoom.Add(await DbUserModel.GetUserFromGuid(user.Id));
            }

            // Check if any players in the room have the same device identifier
            if (playersInRoom.Any(it => it.UserModel.DeviceIdentifier == dbUserModel.UserModel.DeviceIdentifier))
            {
                return(ResponseFactory.createResponse(ResponseType.PERMISSION_DENIED));
            }

            GameConfiguration.Players.Add(dbUserModel.AsUser());
            MongoConnector.GetGameRoomCollection().ReplaceOne((it => it.Id == GameConfiguration.Id), new GameConfigurationMapper(GameConfiguration));

            // Check if the player joining the game is the last player.
            if (GameConfiguration.Players.Count == GameConfiguration.GameSettings.MaxPlayers)
            {
                return(await StartGame());
            }

            return(ResponseFactory.createResponse(ResponseType.SUCCESS));;
        }