-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatches.c
More file actions
60 lines (52 loc) · 1.48 KB
/
matches.c
File metadata and controls
60 lines (52 loc) · 1.48 KB
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
#include "matches.h"
int trouver_index_joueur(const char* pseudo)
{
for (int i = 0; i < nb_joueurs_actifs; i++)
{
if (strcmp(joueurs_actifs[i].pseudo, pseudo) == 0)
{
return i;
}
}
return -1;
}
int trouver_match_index(const char* sud, const char* nord) {
for (int i = 0; i < nb_matches; i++) {
if (matches[i].actif &&
strcmp(matches[i].sud, sud) == 0 &&
strcmp(matches[i].nord, nord) == 0) {
return i;
}
}
return -1;
}
int creer_match(const char* sud, const char* nord) {
int idx = trouver_match_index(sud, nord);
if (idx != -1) {
// Déjà créé et actif
return idx;
}
// Tenter de réutiliser un slot inactif
int free_idx = -1;
for (int i = 0; i < nb_matches; i++) {
if (!matches[i].actif) {
free_idx = i;
break;
}
}
// Sinon, allouer un nouveau slot si possible
if (free_idx == -1) {
if (nb_matches >= MAX_MATCHES) {
return -1;
}
free_idx = nb_matches++;
}
// Initialiser le match
strncpy(matches[free_idx].sud, sud, MAX_PSEUDO_LEN - 1);
matches[free_idx].sud[MAX_PSEUDO_LEN - 1] = '\0';
strncpy(matches[free_idx].nord, nord, MAX_PSEUDO_LEN - 1);
matches[free_idx].nord[MAX_PSEUDO_LEN - 1] = '\0';
awale_init(&matches[free_idx].partie); // Initialise partie locale serveur
matches[free_idx].actif = 1;
return free_idx;
}