summaryrefslogtreecommitdiff
path: root/src/player.c
blob: 338942a4ad7cd38002abe20f37ba068f6b3f5e69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*****************************************************************************
 *                          ___ __  __ ___ _ __                              *
 *                         / _ \\ \/ // _ \ '_ \                             *
 *                        | (_) |>  <|  __/ | | |                            *
 *                         \___//_/\_\\___|_| |_|                            *
 *                                                                           *
 *                             The card game                                 *
 *                                                                           *
 *  Copyright (C) 2011, Reiner Herrmann <reiner@reiner-h.de>                 *
 *                      Mario Kilies <MarioKilies@GMX.net>                   *
 *                                                                           *
 *  This program is free software; you can redistribute it and/or modify     *
 *  it under the terms of the GNU General Public License as published by     *
 *  the Free Software Foundation; either version 3 of the License, or        *
 *  (at your option) any later version.                                      *
 *                                                                           *
 *  This program is distributed in the hope that it will be useful,          *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
 *  GNU General Public License for more details.                             *
 *                                                                           *
 *  You should have received a copy of the GNU General Public License        *
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.     *
 *                                                                           *
 *****************************************************************************/

#include "player.h"
#include <stdlib.h>
#include <assert.h>

/**
 * Search for a player in the player list
 * @param[in] pl Pointer to player list to search
 * @param[in] pid Player ID of searched player
 * @return Pointer to player_list_entry_t; NULL if none found for specified player id
 */
player_list_entry_t *get_player_list_entry_by_player_id(player_list_t *pl, const player_id_t pid)
{
	assert(pl != NULL);

	for(int i = 0; i < pl->count; i++)
	{
		if(pl->players[i].player_id != pid)
			continue;
		return &pl->players[i];
	}

	return NULL;
}

/**
 * Compare player list entries via open_card
 */
static int ple_open_card_comparator(const void *a, const void *b)
{
	player_list_entry_t ple1 = *(player_list_entry_t *)a;
	player_list_entry_t ple2 = *(player_list_entry_t *)b;
	
	return ple1.open_card - ple2.open_card;
}

/**
 * Compare player list entries via score
 */
static int ple_score_comparator(const void *a, const void *b)
{
	player_list_entry_t ple1 = *(player_list_entry_t *)a;
	player_list_entry_t ple2 = *(player_list_entry_t *)b;
	
	return ple1.score - ple2.score;
}

/**
 * Sort a player list in ascending order by open card values
 * @param[in,out] pl Pointer to layer list to sort
 * @param[in] num_entries Number of player list entries in list
 */
void player_list_sort_by_open_card(player_list_t *pl, const uint8_t num_entries)
{
	assert(pl != NULL);

	qsort(pl->players, num_entries, sizeof(player_list_entry_t), ple_open_card_comparator);
}

/**
 * Sort a player list in ascending order by scores
 * @param[in,out] pl Pointer to layer list to sort
 * @param[in] num_entries Number of player list entries in list
 */
void player_list_sort_by_score(player_list_t *pl, const uint8_t num_entries)
{
	assert(pl != NULL);

	qsort(pl->players, num_entries, sizeof(player_list_entry_t), ple_score_comparator);
}