コード例 #1
0
void sendLeaderboard(int socket_id)
{
	int i, j, count = 0;
	userpw_t* usersPlayed[10];
	pthread_mutex_lock(&leaderboardLock);
	for (i = 0; i < NUMBER_OF_USERS; i++)
	{
		if (*(userHolder[i]->gamesPlayed) > 0)
		{
			usersPlayed[count] = userHolder[i];
			count++;
		}
	}
	pthread_mutex_unlock(&leaderboardLock);
	send_number(socket_id, count);
	bubble_sort(usersPlayed, count);
	for (j = 0; j < count; j++)
	{		
		int gamesWon = (*(usersPlayed[j]->gamesPlayed));
		int gamesPlayed  = (*(usersPlayed[j]->gamesWon));
		char* username  = usersPlayed[j]->username;
		int username_length = strlen(username);
		send_number(socket_id, username_length);
		send_word(socket_id, username);
		send_number(socket_id, gamesWon);
		send_number(socket_id, gamesPlayed);
	}
}
コード例 #2
0
current_user_t* userLogin(int socket_id)
{
	int loggedIn;// = (int*)malloc(sizeof(int));
	loggedIn = 0;
	char* userName = (char*)malloc(15);
	int numbytes = recv(socket_id, userName, 15, 0);
	fflush(stdout);
	
	current_user_t* logInUser = (current_user_t*)malloc(sizeof(current_user_t));
	
	if (userExists(userName) == 1)
	{
		send_number(socket_id, 1);
		userpw_t * User;
		User = node_find_name(userName);
		char* password = (char*)malloc(15);
		recv(socket_id, password, 15, 0);
		int pwc = passwordCorrect(User, password);
		free(password);
		if (pwc == 1)
		{
			send_number(socket_id, 1);
			loggedIn = 1;
			logInUser->loggedIn=loggedIn;
			logInUser->user=User;
		} else {
			send_number(socket_id, 0);
			close(socket_id);
		}
	} else {
		send_number(socket_id, 0);
		close(socket_id);
		//perror("wrong user");
		//exit(1);
	}
	//free(userName);
	return logInUser;	
}
コード例 #3
0
void play_hangman(int socket_id, userpw_t* User)
{
	
	word_t* hangmanWord = getRandomWord();
	send_number(socket_id, strlen(hangmanWord->object));
	send_number(socket_id, strlen(hangmanWord->objectType));
	char* objGuessing = obfuscate_string(hangmanWord->object);
	char* objTypeGuessing = obfuscate_string(hangmanWord->objectType);

	send_word(socket_id, objGuessing);
	send_word(socket_id, objTypeGuessing);
	int guessesAllowedServer = determineWordLength(hangmanWord);
	int correct = 2;

	do 
	{
		char received = receive_char(socket_id);
		reveal_string(hangmanWord->object, objGuessing, received);
		reveal_string(hangmanWord->objectType, objTypeGuessing, received);
		send_word(socket_id, objGuessing);
		send_word(socket_id, objTypeGuessing);
		guessesAllowedServer--;
		if(wordGuessed(objGuessing) == 1 && wordGuessed(objTypeGuessing) == 1)
		{
			correct = 1;
			pthread_mutex_lock(&leaderboardLock);
			int* gamesWon = User->gamesWon;
			(*gamesWon)++;
			pthread_mutex_unlock(&leaderboardLock);

		}
	} while (guessesAllowedServer > 0 && correct == 2);
	int* gamesPlayed = User->gamesPlayed;
	(*gamesPlayed)++;
	
}
コード例 #4
0
ファイル: scene.c プロジェクト: Antonito/Raytracer
static void	count_scenes(int fd, t_scene *scenes)
{
  static int	i = 0;

  if (!i)
    while (scenes)
      {
	++i;
	scenes = scenes->next;
      }
  if (send_number(fd, i) || read_ok(fd))
    return ;
  write(1, "Sent new scene\n", 15);
  return ;
}