/************************************************************************
 * int room_index	Identifies the room to add connection(s) to.
 * char* rooms[]	Array of room names, each to correspond to a file.
 * int connections	Total number of connections the room 
 * 			identified by room_index should have.
 ***********************************************************************/
void initRoom(int room_index, char* rooms[], int connections, char* directory ) {
	char *room_name = rooms[room_index];
	FILE *file_p = NULL;
	// need to open in directory, so craft the string here
	char room_dir[100] = "";
	strcat_safe( room_dir, 100, directory );
	strcat_safe( room_dir, 100, "/" );
	strcat_safe( room_dir, 100, rooms[room_index] );

	file_p = fopen( room_dir, "a+" );
	// Check for successful file opening.
	if ( ! file_p ) {
		printf( "Error opening file in function initRoom!\n" );
		exit( 1 );
	}

	int connections_in_file = countConnections( file_p );
	int connections_to_make = connections - connections_in_file;

	while ( connections_to_make > 0 ) {
		int rand_index = randInRange( 0, 6 );
		if ( makeConnection( room_index, rand_index, rooms, directory, file_p ) ) {
			connections_to_make--;
		}
	}

	fclose(file_p);
}
void listLocations( char *room, char *directory ) {
	char other_1[100] = "";
	char other_1a[100] = "";
	char name[100] = "";
	
	char path[100] = "";
	// prepare directory string
	strcat_safe( path, 100, directory );
	strcat_safe( path, 100, "/" );
	strcat_safe( path, 100, room );
	FILE *file = fopen( path, "a+" );
	
	int connections = countConnections( file ) - 1; // countConnections actually counts the number of lines in file - 1 
	fseek( file, 0, SEEK_SET );
	
	
	int count = 0;
	printf( "POSSIBLE CONNECTIONS: " );
	while( fscanf( file, "%s %s %s\n", other_1, other_1a, name ) == 3 ) {
		if ( ! strcmp( other_1, "CONNECTION" ) ) {
			printf( "%s", name );
			if (count + 1 < connections)
				printf( ", " );
			count++;
		}
	}
	printf( ".\n" );
	
	fclose( file );
}
/******************************************************************************
 * Checks to see whether the two rooms identified by index are the same,
 * and returns 0 if they are (avoiding a room making a connection to itself).
 *
 * If the two rooms identified are different, a connection will be made
 * between them unless the connection already exists.
 *****************************************************************************/
int makeConnection( int first, int second, char* rooms[], char* directory, FILE* first_room_file ) {
	if ( first == second )
		return 0;

	// get room names
	char *first_room = rooms[first];
	char *second_room = rooms[second];
	
	// reserve room for file path strings
	char second_dir[100] = "";

	
	strcat_safe( second_dir, 100, directory );
	strcat_safe( second_dir, 100, "/" );
	strcat_safe( second_dir, 100, second_room );

	
	// Open room files for reading and writing
	FILE *second_room_file = fopen( second_dir, "a+" );

	
	if ( ! ( first_room_file && second_room_file ) ) {
		printf( "in makeConnection: second_dir: %s\n", second_dir );
		printf( "Error opening file in function makeConnection\n" );
		exit( 1 );
	}

	
	if ( connectionExists( first_room_file, second_room_file, first_room, second_room ) ) {
		return 1;
	}	

	
	int c1 = countConnections( first_room_file ) + 1;
	int c2 = countConnections( second_room_file ) + 1;
	fprintf( first_room_file, "CONNECTION %d: %s\n", c1, second_room );
	fprintf( second_room_file,"CONNECTION %d: %s\n", c2, first_room );
	
	// Close only the file this function opened
	fclose( second_room_file );

	return 1;	
}
void selectRole() {
	int nConnection = countConnections();
	if(nConnection==2) {
		printf("%s: I am the Center\n",moduleName);
		role = CENTER;
		int i, count=0;
		for(i=0;i<8;i++) {
			if(atronApi_isOtherConnectorNearby(i))  {
				char leftMsg[] = {ROLESET, LEFT};
				if(count==0) atronApi_sendMessage(leftMsg, 2, i); //select left 
				
				char rightMsg[] = {ROLESET, RIGHT};
				if(count==1) atronApi_sendMessage(rightMsg, 2, i); //select right	
				count++;
			}
		}
	}
	while(role==UNDEFINED) {  atronApi_yield(); }
}