void update_score(Leaderboard *leaderboard, char *username, bool win)
{
	Score *score;

	if (contains_user(leaderboard, username)) {
		score = get_score(leaderboard, username);
	} else {
		score = add_user(leaderboard, username);
	}

	score->games_played++;
	score->games_won += win;

	sort_leaderboard(leaderboard);
}
Esempio n. 2
0
/* watch for users signing ON and OFF */
void *watchuser(void *args) {
	struct userarg *userargs;
	struct userelement *users;
	pthread_mutex_t lock;
	struct utmpx *up;
	int contains;
	time_t last_check;
	struct userelement *tmp;

	/* get the current time */
	time(&last_check);
	while(1) {
		userargs = (struct userarg *)args;
		users = (struct userelement *)userargs->users;
		lock = (pthread_mutex_t)userargs->lock;
		setutxent();
		while(up = getutxent()) {

			/* if we have a user process */
			if(up->ut_type == USER_PROCESS) {

				/* check if the user is a watched user */
				pthread_mutex_lock(&lock);
				contains = contains_user(users, up->ut_user);
				pthread_mutex_unlock(&lock);

				/* if the user is watched and has logged on since we last checked, print */
				if(contains == 1 && (int)up->ut_tv.tv_sec > (int)last_check) {
					printf("\n%s has logged on %s from %s\n", up->ut_user, up->ut_line, up->ut_host);
					/* Mark the user as having logged in */
					pthread_mutex_lock(&lock);
					users = login(users, up->ut_user);
					pthread_mutex_unlock(&lock);
				}
				if (contains == 1) {
					/* If the user is in the loop they are logged on. Mark as checked regardless of when they logged on */
					pthread_mutex_lock(&lock);
					users = check(users, up->ut_user);
					pthread_mutex_unlock(&lock);
				}
			}
		}

		/* Check for users that have logged off */
		pthread_mutex_lock(&lock);
		tmp = users;
		while(tmp != NULL) {
			/* If we didn't see the user logged in this time but they had logged on before consider the user logged off */
			if(tmp->checked == 0 && tmp->logged_on) {
				/* Mark the user as logged off, and print (in the logoff function) */
				users = logoff(users, tmp->username);
			}
			tmp = tmp->next;
		}
		/* Uncheck all users for the next loop */
		users = uncheckAll(users);	
		pthread_mutex_unlock(&lock);

		/* update when we last checked */
		time(&last_check);
		sleep(10);
	}
}