Example #1
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));;
        }
Example #2
0
        public async Task <ResponseStatus> LeaveRoom(DbUserModel dbUserModel)
        {
            if (GameConfiguration.RoomStatus == RoomStatus.Open)
            {
                // Check if the player leaving was the host.
                if (GameConfiguration.Creator.Id == dbUserModel.UserModel.Id)
                {
                    // Delete the game
                    await MongoConnector.GetGameRoomCollection().DeleteOneAsync(it => it.Id == GameConfiguration.Id);

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

                // Otherwise, just remove the player from the player list.
                GameConfiguration.Players.Remove(dbUserModel.AsUser());
                await MongoConnector.GetGameRoomCollection().ReplaceOneAsync((it => it.Id == GameConfiguration.Id), new GameConfigurationMapper(GameConfiguration));

                return(ResponseFactory.createResponse(ResponseType.SUCCESS));
            }
            // TODO: Player left the game while ongoing.
            // Create a player leave game event and push to event list

            return(ResponseFactory.createResponse(ResponseType.INVALID_REQUEST));
        }
Example #3
0
 public Boolean IsPlayerInRoom(DbUserModel player)
 {
     return(GameConfiguration.Players.Contains(player.AsUser()));
 }