-
Notifications
You must be signed in to change notification settings - Fork 0
/
PokemonClient.c
490 lines (382 loc) · 13 KB
/
PokemonClient.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include <stdio.h> /* for printf() and fprintf() and scanf()*/
#include <sys/socket.h> /* for socket(), bind() and connect() */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#include <pthread.h> /* for POSIX threads */
#include <my_global.h>
#include <mysql.h>
#include "DieWithError.h" /* error handling function */
#include "MessageDefinition.h"
#define SERV_PORT 9999 /* Server will run on this port */
#define RCVBUFSIZEC 8000
#define sqluser "root"
#define sqlpass NULL
void DieWithError(char *errorMessage); /*Error handling function*/
int askYes_No();
void sendErrorCode(int clntSocket, char* message);
char* getPokemonName(unsigned char idPokemon);
void DieSQLError(MYSQL *con);
int checkGivenUserData(char* user);
int getUserID(char* user);
int checkPokedex(int idUsuario, unsigned char idPokemon);
void registerInPokedex(int idUsuario, unsigned char idPokemon);
int main(int argc, char *argv[])
{
if((argc != 3)){ /*temp*/
fprintf(stderr, "Usage: %s <ServerIP> <Port>\n",argv[0]);
exit(1);
}
/* Cosas para "Inicio de Sesion"*/
//Por ahora solo basta con el nombre de usuario, se puede poner la contraseña facil....
printf("Inicia Sesion;\n");
int failed = 1;
char user[32];
while(failed){
printf("Nombre de Usuario:\n");
scanf("%s",user);
failed = checkGivenUserData(user);
}
int idUsuario = getUserID(user);
int sock;
struct sockaddr_in pokeServAddr;
unsigned short servPort;
char *servIP;
char *pokeString;
char pokeBuffer[RCVBUFSIZEC];
unsigned int pokeStringLen;
int bytesRcvd, bytesSend;
unsigned char code = 10;
servIP = argv[1];
servPort = atoi(argv[2]); /*cast String to int*/
/*CREATING MSG STRUCTURE*/
msg_type1 *msg_t1 = (struct msg_type1 *)(unsigned char *)malloc(sizeof (unsigned char));
memcpy(msg_t1->code, &code, sizeof(msg_t1->code));
/*CREATING SOCKET*/
if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket () failed");
memset(&pokeServAddr,0,sizeof(pokeServAddr));
pokeServAddr.sin_family = AF_INET;
pokeServAddr.sin_addr.s_addr = inet_addr(servIP);
pokeServAddr.sin_port = htons(servPort);
/* Set the timeouts for send and receive respectively */
struct timeval timeout;
timeout.tv_sec = 5; /* wait 5 seconds */
timeout.tv_usec = 0; /* and 0 microseconds */
/* for receive operations */
if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
DieWithError("setsockopt failed\n");
/* for send operations */
if (setsockopt (sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
DieWithError("setsockopt failed\n");
/*CONNECTING SOCKET*/
if((connect(sock,(struct sockaddr *)&pokeServAddr, sizeof(pokeServAddr))) < 0)
DieWithError("connect () failed");
pokeStringLen = sizeof(msg_type1); //el PokeCliente solo envia mensajes de tipo 1
/*Estado S0*/
/*SENDING MSG*/
if((bytesSend = send(sock, msg_t1, sizeof(msg_type1), 0)) != pokeStringLen) //sending msg10
DieWithError("send() sent a different number of bytes than expected");
else
printf("Message 10: Bytes send to server: %i \n",bytesSend);
/*RECEIVING MSG (STARTING POKEMON CAPTURE SECRET PROTOCOL: "Catch 'em all")*/
//¿Capturar Pokemon X?
/*Estado S2*/
if((bytesRcvd=recv(sock,pokeBuffer,RCVBUFSIZEC-1,0)) < 0)
DieWithError("recv () failed or connection died prematurely");
msg_type2 *msg20 = (struct msg_type2 *)pokeBuffer;
code = *((unsigned char *)msg20->code);
/* Expected code at this point: 20 */
if(code != 20){
sendErrorCode(sock, "Error: Unexpected code, expected code 20");
exit(-1);
}
unsigned char pokemon = *((unsigned char *)msg20->idPokemon);
printf("¿Deseas Capturar el Pokemon %s?\n",getPokemonName(pokemon));
int already;
if(already=checkPokedex(idUsuario,pokemon)){
printf("Parece que ya tienes a %s registrado en tu Pokedex\n",getPokemonName(pokemon));
}
printf("Already %d\n",already);
int captured;
int canceled;
int runOutOfTries;
code = 30;
/*CREATING MSG STRUCTURE*/
msg_type1 *msg30 = (struct msg_type1 *)(unsigned char *)malloc(sizeof (unsigned char));
memcpy(msg30->code, &code, sizeof(msg30->code));
code = 31;
/*CREATING MSG STRUCTURE*/
msg_type1 *msg31 = (struct msg_type1 *)(unsigned char *)malloc(sizeof (unsigned char));
memcpy(msg31->code, &code, sizeof(msg31->code));
code = 32;
/*CREATING MSG STRUCTURE*/
msg_type1 *msg32 = (struct msg_type1 *)(unsigned char *)malloc(sizeof (unsigned char));
memcpy(msg32->code, &code, sizeof(msg32->code));
//obtener input standar del usuario (¿Quieres intentar capturar a X?)
//if SI enviar mensaje 30
if(askYes_No()==0){ //SI
printf("¡¡¡Seras mio!!!\n");
/*SENDING MSG*/
if((bytesSend = send(sock, msg30, sizeof(msg_type1), 0)) != pokeStringLen) //sending msg30
DieWithError("send() sent a different number of bytes than expected");
else
printf("Message 30: Bytes send to server: %i \n",bytesSend);
captured = 0;
canceled = 0;
runOutOfTries = 0;
while(!captured && !canceled && !runOutOfTries){
//Recibir mensaje del servidor (checar si es 21 o 22 o 23)
if((bytesRcvd=recv(sock,pokeBuffer,RCVBUFSIZEC-1,0)) < 0)
DieWithError("recv () failed or connection died prematurely");
msg_type1 *msgX = (struct msg_type1 *)pokeBuffer;
code = *((unsigned char *)msgX->code);
/*Estado S4 (se recibió 21)*/
if(code == 21){
msg_type3 *msg21 = (struct msg_type3 *)pokeBuffer;
printf("Te quedan %i intentos\n",*((int *)msg21->numAttemps));
printf("¿Intentarlo de nuevo?\n");
if(askYes_No()==0){
printf("¡¡Intentemos de nuevo!!\n");
/*SENDING MSG*/
if((bytesSend = send(sock, msg30, sizeof(msg_type1), 0)) != pokeStringLen) //sending msg30
DieWithError("send() sent a different number of bytes than expected");
else
printf("Message 30: Bytes send to server: %i \n",bytesSend);
}else{
printf("¡¡Me Rindo!!\n");
/*SENDING MSG*/
if((bytesSend = send(sock, msg31, sizeof(msg_type1), 0)) != pokeStringLen) //sending msg31
DieWithError("send() sent a different number of bytes than expected");
else
printf("Message 31: Bytes send to server: %i \n",bytesSend);
canceled = 1; //this should break the cycle
}
}else if (code == 22){/*Estado S5 (se recibió 22)*/
msg_type4 *msg22 = (struct msg_type4 *)pokeBuffer;
printf("¡Enhorabuena! Haz capturado a: %s\n",getPokemonName(*((unsigned char *)msg22->idPokemon))); //NOTA: Es mejor que no se escriba solo el id, si no tambien el nombre del pokemon (base de datos?)
int imageSizeR = *((int *)msg22->imageSize);
char imageR[imageSizeR];
char filename[32]; // The filename buffer.
snprintf(filename, sizeof(char) * 32, "%u.png", *((unsigned char *)msg22->idPokemon));
FILE *image;
image = fopen(filename, "w");
fwrite(msg22->image,1,sizeof(imageR),image);
fclose(image);
captured = 1; //this should break the cycle
}else if (code == 23){ /*Estado S6 (se recibió 23)*/
printf("¡Diablos! Se ha escapado.....\n");
runOutOfTries = 1; //this should break the cycle
}else{
sendErrorCode(sock, "Error: Unexpected code, expected code 21, 22 or 23");
exit(-1);
}
}
}else{
printf("Mejor no....\n");
/*SENDING MSG*/
if((bytesSend = send(sock, msg31, sizeof(msg_type1), 0)) != pokeStringLen) //sending msg31
DieWithError("send() sent a different number of bytes than expected");
else
printf("Message 31: Bytes send to server: %i \n",bytesSend);
}
if(captured){
printf("Guardando en Pokedex y cerrando sesion.\n"); //Asegurarse que el pokemon no aparezca de nuevo o que la proxima vez que se encuentre se avise que ya fue capturado con anterioridad.
if(!already)
registerInPokedex(idUsuario,pokemon);
}
else if(canceled)
printf("Cerrando Sesion.\n");
else //runOutOfTries
printf("Algun dia lo encontraremos de nuevo, cerrando sesion.\n");
/*SENDING MSG*/
if((bytesSend = send(sock, msg32, sizeof(msg_type1), 0)) != pokeStringLen) //sending msg31
DieWithError("send() sent a different number of bytes than expected");
else
printf("Message 32: Bytes send to server: %i \n",bytesSend);
/*Estado S7*/
close(sock);
exit(0);
}
//Returns 0 if yes, 1 if no
int askYes_No(){
printf("Selecciona:\n");
int answer;
int unanswered = 1;
while(unanswered){
printf("0.Si\n");
printf("1.No\n");
scanf("%d",&answer);
if(answer!=0 && answer!=1)
printf("Opción no valida, por favor eliga una de las anteriores\n");
else
//Aqui esta asegurado que la respuesta es 1 o 0
unanswered = 0;
}
return answer;
}
void sendErrorCode(int clntSocket, char* message){//by Alephsis
int code = 40;
msg_typeErr *err = (struct msg_typeErr *)(unsigned char *)malloc(sizeof(unsigned char));
memcpy(err->code, &code, sizeof(err->code));
memcpy(err->errString, &message, sizeof(err->errString));
if(send(clntSocket, err, sizeof(err), 0) != sizeof(err))
DieWithError("send() failed");
}
char* getPokemonName(unsigned char idPokemon){
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "127.0.0.1", sqluser, sqlpass, "PokeBase", 0, NULL, 0) == NULL)
{
DieSQLError(con);
}
char query[128]; // The query buffer.
snprintf(query, sizeof(char) * 128, "SELECT nombre FROM Pokemon WHERE idPokemon = %u;", idPokemon);
if (mysql_query(con, query))
{
DieSQLError(con);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL)
{
DieSQLError(con);
}
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
row = mysql_fetch_row(result);
char* returnvalue = row[0];
mysql_free_result(result);
mysql_close(con);
if(returnvalue){
return returnvalue;
}else{
return "NULL";
}
}
int checkGivenUserData(char* user){
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "127.0.0.1", sqluser, sqlpass, "PokeBase", 0, NULL, 0) == NULL)
{
DieSQLError(con);
}
char query[128]; // The query buffer.
snprintf(query, sizeof(char) * 128, "SELECT nombre_usuario FROM Usuario WHERE nombre_usuario = \"%s\";" ,user);
if (mysql_query(con, query))
{
DieSQLError(con);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL)
{
DieSQLError(con);
}
MYSQL_ROW row;
int failed = 1;
if((row = mysql_fetch_row(result)))
{
failed = 0;
}
mysql_free_result(result);
mysql_close(con);
return failed;
}
int getUserID(char* user){
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "127.0.0.1", sqluser, sqlpass, "PokeBase", 0, NULL, 0) == NULL)
{
DieSQLError(con);
}
char query[128]; // The query buffer.
snprintf(query, sizeof(char) * 128, "SELECT idUsuario FROM Usuario WHERE nombre_usuario = \"%s\";" ,user);
if (mysql_query(con, query))
{
DieSQLError(con);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL)
{
DieSQLError(con);
}
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
row = mysql_fetch_row(result);
int returnvalue = atoi(row[0]);
mysql_free_result(result);
mysql_close(con);
if(returnvalue){
return returnvalue;
}else{
return -1;
}
}
int checkPokedex(int idUsuario, unsigned char idPokemon){
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "127.0.0.1", sqluser, sqlpass, "PokeBase", 0, NULL, 0) == NULL)
{
DieSQLError(con);
}
char query[128]; // The query buffer.
snprintf(query, sizeof(char) * 128, "SELECT idPokedex FROM Pokedex WHERE idPokemon = %u AND idUsuario = %d;", idPokemon, idUsuario);
if (mysql_query(con, query))
{
DieSQLError(con);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL)
{
DieSQLError(con);
}
MYSQL_ROW row;
int registered = 0;
if((row = mysql_fetch_row(result)))
{
registered = 1;
}
mysql_free_result(result);
mysql_close(con);
return registered;
}
void registerInPokedex(int idUsuario, unsigned char idPokemon){
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "127.0.0.1", sqluser, sqlpass, "PokeBase", 0, NULL, 0) == NULL)
{
DieSQLError(con);
}
char query[128]; // The query buffer.
snprintf(query, sizeof(char) * 128, "INSERT INTO Pokedex (idUsuario,idPokemon) VALUES (%d,%u);",idUsuario, idPokemon);
if (mysql_query(con, query)) {
DieSQLError(con);
}
mysql_close(con);
return;
}
void DieSQLError(MYSQL *con){
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}