-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
144 lines (117 loc) · 4.18 KB
/
client.c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <pthread.h>
#include "logic.h"
#define ASSERVER 1
#define ASCLIENT 2
char publicKeyWord[20] = "puissance4";
char colorLabels[12][12] = {"RED", "GREEN", "YELLOW", "BLUE", "MAGENTA", "CYAN", "BOLD_RED", "BOLD_GREEN", "BOLD_YELLOW", "BOLD_BLUE", "BOLD_MAGENTA", "BOLD_CYAN"};
char colors[12][10] = {RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, BOLD_RED, BOLD_GREEN, BOLD_YELLOW, BOLD_BLUE, BOLD_MAGENTA, BOLD_CYAN};
void reset(generic *quoi);
int askForColumn(int col);
int main()
{
key_t publicKey;
char filename[8] = "serveur";
word2Key(5000, &publicKey, filename);
printf("Connecting to the server...\n");
connecterClt2Srv(publicKey);
printf("Connecting to the server...\n");
pid_t clientPID = getpid();
char keyword[20];
sprintf(keyword, "%d", clientPID);
// Création de la clé IPC
key_t privateKey;
memset(filename, 0, sizeof(filename));
strcpy(filename, "client");
word2Key(clientPID, &privateKey, filename);
// Receiving the game infos
buffer_t buffer;
memset(buffer, 0, sizeof(buffer_t));
recevoir(privateKey, buffer, ASSERVER);
printf("Receiving game infos...\n");
int line = atoi(strtok(buffer, "::"));
int col = atoi(strtok(NULL, "::"));
int nbPlayers = atoi(strtok(NULL, "::"));
printf("Game infos received !\n");
printf("Line : %d\n", line);
printf("Column : %d\n", col);
printf("Nb players : %d\n", nbPlayers);
memset(buffer, 0, sizeof(buffer_t));
plateau_t plateau = creerPlateau(line, col);
printf("Plateau created !\n");
int i = 0;
maillon_t *playerList = NULL;
// receiving the player data in the following format : id::colorIndex::pseudo
memset(buffer, 0, sizeof(buffer_t));
recevoir(privateKey, buffer, ASSERVER);
while (i < nbPlayers)
{
printf("Storing player %d infos...\n", i + 1);
maillon_t *temp = malloc(sizeof(maillon_t));
memset(temp, 0, sizeof(maillon_t));
player_t player;
memset(&player, 0, sizeof(player_t));
char *pseudo = malloc(200 * sizeof(char));
memset(pseudo, 0, 200 * sizeof(char));
// Creation of the player and adding it to the player list
if(i == 0) player.id = atoi(strtok(&buffer[2], "::"));
else player.id = atoi(strtok(NULL, "::"));
player.colorIndex = atoi(strtok(NULL, "::"));
strcpy(player.pseudo, strtok(NULL, "::"));
temp->suivant = playerList;
memcpy(&temp->player, &player, sizeof(player_t));
printf("Player details : %d::%d::%s\n", temp->player.id, temp->player.colorIndex, temp->player.pseudo);
playerList = temp;
i++;
}
afficherPlateau(plateau, line, col, colors);
while (true)
{
memset(buffer, 0, sizeof(buffer_t));
recevoir(privateKey, buffer, ASSERVER);
char *keyword = strtok(buffer, "::");
if(strcmp(keyword, "YOURTURN") == 0)
{
int selectedCol = askForColumn(col);
memset(buffer, 0, sizeof(buffer_t));
sprintf(buffer, "PLAYED::%d", selectedCol);
envoyer(privateKey, buffer, ASCLIENT);
}
else if(strcmp(keyword, "PLAY") == 0)
{
int playerId = atoi(strtok(NULL, "::"));
int selectedCol = atoi(strtok(NULL, "::"));
jouerJeton(plateau, line, selectedCol, playerId);
effacerShell();
afficherPlateau(plateau, line, col, colors);
}
else if(strcmp(keyword, "VICTORY") == 0)
{
int playerId = atoi(strtok(NULL, "::"));
maillon_t *temp = playerList;
while(temp != NULL)
{
if(temp->player.id == playerId)
{
printf("Player %s won !\n", temp->player.pseudo);
break;
}
temp = temp->suivant;
}
break;
}
}
return 0;
}
void reset(generic *quoi)
{
memset(quoi, 0, sizeof(generic));
}
int askForColumn(int col)
{
char temp[4];
printf("Enter column number (1-%d): ", col);
fgets(temp, 4, stdin);
int res = atoi(temp) - 1;
while(res > col - 1) return askForColumn(col);
return res;
}