-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.c
More file actions
1139 lines (1026 loc) · 33.7 KB
/
Server.c
File metadata and controls
1139 lines (1026 loc) · 33.7 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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <time.h>
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <fcntl.h>
#include <signal.h>
#include "login.h"
#include "gameLib.h"
#define MAX 1000
#define SA struct sockaddr
#define MAX_THREADS 8
#define TIMER 60
/**spawnPlayer prende in ingresso un clientSD e un puntatore a struct player come parametri.
* Si occupa di spawnare il giocatore (utilizzando alcuni criteri) e inizializza le strutture a esso associate.
*/
void spawnPlayer(int clientsd,struct player *info_player,char *username);
/*La funzione isCellGood stabilisce se un punto della mappa è adeguato per lo spawn di un giocatore*/
int isCellGood(struct cell a,int index1,int index2);
/*La funzione isCellFree stabilisce se un punto della mappa è libero (privo di oggetti,magazzini,ostacoli,giocatori) o meno*/
int isCellFree(struct cell a);
/*La funzione isCellSolid stabilisce se una cella è appunto, "non solida" , ossia una cella su cui poter effettuare uno spostamento*/
int isCellNotSolid(struct cell a);
/**La funzione isLeftFree stabilisce se la cella sinistra (rispetto alla posizione corrente) è libera o meno.
* Richiede una matrice di struct cell e la posizione corrente.
*/
int isLeftFree(int index1,int index2);
/**La funzione isRightFree stabilisce se la cella destra (rispetto alla posizione corrente) è libera o meno.
* Richiede una matrice di struct cell e la posizione corrente.
*/
int isRightFree(int index1,int index2);
/**La funzione isUpFree stabilisce se la cella superiore (rispetto alla posizione corrente) è libera o meno.
* Richiede una matrice di struct cell e la posizione corrente.
*/
int isUpFree(int index1,int index2);
/**La funzione isDownFree stabilisce se la cella inferiore (rispetto alla posizione corrente) è libera o meno.
* Richiede una matrice di struct cell e la posizione corrente.
*/
int isDownFree(int index1,int index2);
/*setLetter() assegna la prima lettera disponibile al giocatore*/
void setLetter(int clientsd);
/**getLetter() ritorna la lettera assegnata a un giocatore specifico.
* Ritorna '0' se il giocatore specificato non è presente.
*/
char getLetter(int clientsd);
/**matrixToString() invia la mappa di gioco a uno specifico giocatore in maniera tale da visualizzarla sul suo client.
* Può anche inviare un messaggio informativo.
*/
void matrixToString(char *msg, int clientsd,int *obstacles);
/**initGame() inizializza tutte le strutture necessarie per il corretto funzionamento del gioco (mapPlayers,map,scoreboard).
*/
void initGame();
/**La funzione checkMovement() viene invocata qualora l'input inviato da un giocatore è un input di movimento.
* In base al comando ricevuto, checkMovement() effettua il movimento desiderato dal giocatore nella mappa.
*/
void checkMovement(char msg,struct player *info_player,char *info);
/**La funzione checkCommand() verifica l'input inviato dal giocatore e se in grado di soddisfare la richiesta,
* costruisce un messaggio informativo ad hoc sull'array di caratteri info.
* Altrimenti affida il lavoro a checkMovement().
*/
void checkCommand(char msg, struct player *info_player,char *info);
/*changeCoordinates modifica la mappa di gioco per aggiornare la posizione del giocatore.*/
void changeCoordinates(struct player *info_player, int add_x, int add_y);
/**La funzione movement() si assicura che la cella su cui il giocatore desidera spostarsi consenta il movimento
* altrimenti, aggiorna l'array del giocatore che tiene traccia degli ostacoli visibili o non per rendere
* visibile l'ostacolo appena incontrato.
*/
void movement(struct player *info_player, int add_x, int add_y);
/*isWarehouseHere() ritorna 1 se è presente attorno al giocatore almeno un magazzino,0 altrimenti.*/
int isWarehouseHere(struct player *a);
/*checkWarehouse() restituisce 1 se la cella specificata è occupata da un magazzino, 0 altrimenti.*/
int checkWarehouse(struct player *info_player, int add_x, int add_y);
/**noBoundaryCheck() restituisce 1 se la cella selezionata,
* rispetto alla posizione corrente, rimane nei limiti della mappa, 0 altrimenti.
*/
int noBoundaryCheck(struct player *a,int add_x,int add_y);
/*sendMessage() invia un messaggio a un socket specifico. Prima di mandare il messaggio, invia l'informazione sulla lunghezza di esso*/
void sendMessage(int clientsd, char *msg);
/*gameLogou() effettua tutto ciò che è necessario quando un giocatore lascia una partita.*/
void gameLogout(int clientsd);
/*logoutStructs() rimuove tutte le informazioni di un utente che ha lasciato la partita dalle strutture del gioco.*/
void logoutStructs(int clientsd);
/*setScorePlayer() inserisce un nuovo giocatore nell'array scoreBoard*/
void setScorePlayer(struct player *info_player);
/*createScoreboard() scrive un messaggio su scoreboardString contenente la classifica*/
void createScoreboard();
/**writeLog() consente la scrittura di un messaggio di log all'interno del file gameLog.
*Se il flag è 1, la scrittura sul log viene effettuata adoperando mutex.
*/
void writeLog(char *msg,int flag);
/*Effettua l'inizializzazione dei mutex. Ritorna 1 se qualcosa è andato storto, 0 altrimenti*/
int mutexInitialization();
/**getUTCString restituisce un puntatore all'array statico str.
* Scrive su quest'ultimo un messaggio che riporta l'informazione sulla data odierna e l'orario in UTC.
*/
char * getUTCString();
/*getWinner() restituisce un puntatore a struct player che punta alla struct del giocatore che ha vinto*/
struct player *getWinner(int dim);
/*Scrive un log riguardo la consegna di un oggetto da parte di uno specifico giocatore*/
void writeLog_ItemDelivered(struct player *info);
/*Scrive un log riguardo la connessione da parte di un nuovo socket*/
void writeLog_NewConnection(char *ip);
/*Scrive un log riguardo la fine della partita e indica (se presente) lo username del vincitore.*/
void writeLog_GameOver(struct player *winner);
/*Scrive un log riguardo la partecipazione di un giocatore alla sessione di gioco*/
void writeLog_JoinGame(char *user);
/*Scrive un log riguardo l'uscita di un giocatore dalla sessione di gioco*/
void writeLog_QuitGame(char *user);
/*Scrive un log riguardo l'imminente chiusura del server*/
void writeLog_serverAbort();
/**Funzione invocata nel caso in cui un giocatore che sta
* lasciando la sessione di gioco, possiede un oggetto non consegnato.
* L'oggetto viene droppato nella posizione del giocatore se la cella non ha oggetti,
* altrimenti si cerca una posizione libera intorno.
*/
void dropItem(struct player *info);
/**Funzione invocata nel caso in cui un giocatore
* ha lasciato la sessione di gioco e non possiede oggetti da consegnare.
* La cella su cui risiedeva il giocatore, viene liberata.
*/
void initCell(int i, int j);
/*Algoritmo di ordinamento per ordinare la classifica al termine della partita*/
void selectionSort(int n);
int findMax(int i);
void quicksort(struct player* a[MAX_USERS], int first, int last);
int partiziona(struct player* a[], int low, int high);
/*dropInPosition() droppa l'item posseduto dall'utente nella posizione indicata rispetto a quella corrente*/
void dropInPosition(struct player *info, int add_x,int add_y);
/*Costruisce un messaggio che riporta gli utenti attualmente connessi*/
void buildLoggedUsersString(char *loggedUsersString);
/*Funzione adibita al popolamento dell'array arr. Ritorna la grandezza dell'array.*/
int setScoreboardArr();
/*Verifica che la porta inserita a riga di comando sia valida.*/
void checkPort(int argc,char **args);
/*Signal handler che cattura il segnale SIGPIPE*/
void clientAbort();
pthread_cond_t mapGen_cond_var = PTHREAD_COND_INITIALIZER;
pthread_mutex_t signup_mutex;
pthread_mutex_t login;
pthread_mutex_t editMatrix;
pthread_mutex_t editMapPlayers;
pthread_mutex_t mapGen;
pthread_mutex_t notifyMaxItems;
pthread_mutex_t gameLog;
pthread_mutex_t loggedUsersCountMutex;
int PORT=49153;
struct mapObjects info_map; //Informazioni numero oggetti presenti sulla mappa
struct cell **map;
int rows, cols;
int mapPlayers[MAX_USERS];
struct player* scoreboard[MAX_USERS];
int gameStarted = 0;
int gameTime = TIMER;
int MAX_ITEMS;
int maxItemsReached=0;
char scoreboardString[500]="";
int loggedUsersCount = 0;
struct player **arr;
/*Thread che genera una nuova partita*/
void *mapGenerator(void* args){
int i=0,j=0;
char *timeString;
struct player *winner;
char msg[100];
while(1){
maxItemsReached = 0;
memset(msg,'\0',sizeof(msg));
timeString=getUTCString();
sprintf(msg,"[%s]Starting new game session...\n",timeString);
writeLog(msg,1);
gameStarted = 0;
while(!loggedUsersCount);
rows = randNumb();
cols = randNumb();
printf("Rows: %d\nCols: %d\n", rows, cols);
initGame();
createMap(&info_map, rows, cols, map);
if(loggedUsersCount != 0)
MAX_ITEMS = rand()%(info_map.n_items-MAX_USERS)+(MAX_USERS/loggedUsersCount);
else
MAX_ITEMS = rand()%(info_map.n_items-MAX_USERS)+(MAX_USERS/2);
printf("Numero massimo di pacchi: %d\n", MAX_ITEMS);
sleep(5);
printf("Sblocco i threads...\n");
pthread_cond_broadcast(&mapGen_cond_var);
gameStarted = 1;
while(gameTime-- > 0){
if(maxItemsReached==1)
break;
sleep(1);
}
printf("\nCreo scoreboard...\n");
createScoreboard();
printf("\n\n--------------------------\n\n");
printf("\n\nScoreboard creata\n\n");
free(arr);
printf("\n\nLog aggiornato\n\n");
printf("\n\n---------------------------\n\n");
gameTime = TIMER;
printf("Fine sleep, genero mappa...\n");
}
}
/*Funzione invocata dal thread client-handler quando il client entra in partita*/
void game(int clientsd,char *username){
char info[350]="Benvenuto in partita, giovane avventuriero! Premi [H] per aiuto, [Q] per uscire.\nPremi un tasto qualsiasi per iniziare la partita.\n";
char command;
struct player infoplayer;
int isLogged=1;
char playerLetter;
pthread_mutex_lock(&loggedUsersCountMutex);
loggedUsersCount++;
pthread_mutex_unlock(&loggedUsersCountMutex);
while(isLogged){
if(!gameStarted){
pthread_mutex_lock(&editMatrix);
pthread_cond_wait(&mapGen_cond_var, &editMatrix);
pthread_mutex_unlock(&editMatrix);
printf("Attesa terminata\n");
}
infoplayer.obstacles=(int *)calloc(info_map.n_obstacles,sizeof(int));
pthread_mutex_lock(&editMatrix);
spawnPlayer(clientsd,&infoplayer,username);
pthread_mutex_unlock(&editMatrix);
playerLetter = getLetter(clientsd);
write(clientsd, &playerLetter, 1);
while(1){
if(infoplayer.itemsDelivered==-1)
break;
matrixToString(info, clientsd,infoplayer.obstacles);
memset(info,'\0',sizeof(info));
if(getLetter(clientsd) == '0') break;
if(read(clientsd, &command, sizeof(command))>0){
if(getLetter(clientsd) == '0') break;
checkCommand(command, &infoplayer,info);
}
else{
printf("\n\nClient disconnesso...\n\n");
if(infoplayer.hasItem)
dropItem(&infoplayer);
else
initCell(infoplayer.x, infoplayer.y);
gameLogout(clientsd);
isLogged=0;
writeLog_QuitGame(username);
pthread_mutex_lock(&loggedUsersCountMutex);
loggedUsersCount--;
pthread_mutex_unlock(&loggedUsersCountMutex);
break;
}
}
if(isLogged){
if(infoplayer.itemsDelivered>=0){
write(clientsd,&(int){0},sizeof(int));
write(clientsd,&(int){0},sizeof(int));
sendMessage(clientsd,scoreboardString);
strcpy(info,"Premi un tasto qualsiasi per continuare\n");
}
if(read(clientsd,&command,1) <= 0){
printf("\n\nClient disconnesso...\n\n");
if(infoplayer.hasItem)
dropItem(&infoplayer);
else
initCell(infoplayer.x, infoplayer.y);
gameLogout(clientsd);
isLogged=0;
writeLog_QuitGame(username);
pthread_mutex_lock(&loggedUsersCountMutex);
loggedUsersCount--;
pthread_mutex_unlock(&loggedUsersCountMutex);
}
else
printf("Comando: %c\n", command);
}
}
}
void clientAbort(){
}
/*Thread che gestisce il client*/
void *clientThread(void *sockfd)
{
signal(SIGPIPE, clientAbort);
int clientsd=*(int*)sockfd;
int log = 0;
char username[100];
char message[50];
log = loginMain(clientsd, signup_mutex, login, username);
if(log == 1){
printf("Gestisco il client con socket descriptor %d\n\n", clientsd);
writeLog_JoinGame(username);
game(clientsd,username);
}
close(clientsd);
pthread_exit(NULL);
}
/*Signal handler per la chiusura del server*/
void serverAbort(){
printf("\nChiusura server...\n");
system("rm logged_users");
writeLog_serverAbort();
exit(0);
}
int main(int argc, char **args)
{
checkPort(argc,args);
system("touch logged_users");
signal(SIGINT, serverAbort);
int sockfd, connfd, len,i=0;
struct sockaddr_in servaddr, cli;
void *result;
pthread_t tid,gameThread;
char msg[200];
char *timeString;
timeString=getUTCString(timeString);
printf("Turning on the server..\nListening on port %d..\n",PORT);
sprintf(msg,"[%s]Turning on the server... Listening on port %d...\n",timeString,PORT);
writeLog(msg,0);
memset(msg,'\0',sizeof(msg));
srand(time(NULL));
if(mutexInitialization()){
strcpy(msg,"\t-Mutex initialization FAILED\n");
writeLog(msg,0);
return 1;
}
strcpy(msg,"\t-Mutex initialized\n");
writeLog(msg,0);
memset(msg,'\0',sizeof(msg));
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
strcpy(msg,"\t-Socket creation FAILED\n");
writeLog(msg,0);
exit(0);
}
strcpy(msg,"\t-Socket successfully created\n");
writeLog(msg,0);
memset(msg,'\0',sizeof(msg));
printf("Socket successfully created..\n");
int reuse = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0)
perror("setsockopt(SO_REUSEADDR) failed");
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0)
perror("setsockopt(SO_REUSEPORT) failed");
struct timeval timeout;
timeout.tv_sec = 180;
timeout.tv_usec = 0;
if (setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
sizeof(timeout)) < 0)
perror("setsockopt failed\n");
if (setsockopt (sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
sizeof(timeout)) < 0)
perror("setsockopt failed\n");
//------------------------------------------------------------------------------------------------------------------
memset(&servaddr, '\0', sizeof(servaddr));
// Assegnazione IP, Porta
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
//Bind del socket
if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
strcpy(msg,"\t-Socket bind FAILED\n");
writeLog(msg,0);
exit(0);
}
strcpy(msg,"\t-Socket successfully binded\n");
writeLog(msg,0);
memset(msg,'\0',sizeof(msg));
printf("Socket successfully binded..\n");
// Il server ora è pronto ad ascoltare
if ((listen(sockfd,128)) != 0) {
printf("Listen failed...\n");
strcpy(msg,"\t-Socket listen FAILED\n");
writeLog(msg,0);
exit(0);
}
else
printf("Server listening..\n");
strcpy(msg,"\t-Server Listening...\n");
writeLog(msg,0);
memset(msg,'\0',sizeof(msg));
len = sizeof(cli);
pthread_create(&gameThread, NULL, mapGenerator, NULL);
while(1){
connfd = accept(sockfd, (SA*)&cli, &len);
if(connfd>0){
memset(msg,'\0',sizeof(msg));
i++;
int *thread_sd = (int*) malloc(sizeof(int));
*thread_sd = connfd;
writeLog_NewConnection(inet_ntoa(cli.sin_addr));
printf("server: new connection from %d %s\n",connfd,inet_ntoa(cli.sin_addr));
pthread_create(&tid, NULL, clientThread, (void *) thread_sd);
}
}
close(sockfd);
}
void setLetter(int clientsd){
int i;
for(i=0;i<MAX_USERS;i++){
if(mapPlayers[i]==-1){
pthread_mutex_lock(&editMapPlayers);
mapPlayers[i]=clientsd;
pthread_mutex_unlock(&editMapPlayers);
break;
}
}
}
void logoutStructs(int clientsd){
int i;
for(i=0;i<MAX_USERS;i++){
if(mapPlayers[i]==clientsd){
mapPlayers[i]=-1;
}
if(scoreboard[i] != NULL)
if(scoreboard[i]->clientsd == clientsd){
scoreboard[i]=NULL;
}
}
}
int isLeftFree(int index1,int index2){
if(index2-1>=0){
return isCellNotSolid(map[index1][index2-1]);
}
return 0;
}
int isRightFree(int index1,int index2){
if(index2+1<=cols-1){
return isCellNotSolid(map[index1][index2-1]);
}
return 0;
}
int isUpFree(int index1,int index2){
if(index1-1>=0){
return isCellNotSolid(map[index1-1][index2]);
}
return 0;
}
int isDownFree(int index1,int index2){
if(index1+1<=rows-1){
return isCellNotSolid(map[index1+1][index2]);
}
return 0;
}
int isCellFree(struct cell a){
if(a.isObstacle==0&&a.isWareHouse==0&&a.playerSD==-1&&a.object==' ')
return 1;
return 0;
}
int isCellNotSolid(struct cell a){
if(a.isObstacle==0&&a.isWareHouse==0&&a.playerSD==-1)
return 1;
return 0;
}
int isCellGood(struct cell a,int index1,int index2){
int exp;
if(isCellFree(a)){
exp=isLeftFree(index1,index2)+isRightFree(index1,index2)+isUpFree(index1,index2)+isDownFree(index1,index2);
if(exp>0)//Se almeno una cella è libera attorno al giocatore okay
return 1;
}
return 0;
}
void spawnPlayer(int clientsd,struct player *info_player,char *username){
char c;
int index1,index2;
setLetter(clientsd);
setScorePlayer(info_player);
while(1){
index1=rand()%rows;
index2=rand()%cols;
if(isCellGood(map[index1][index2],index1,index2)){
break;
}
}
strcpy(info_player->username,username);
printf("Spawn %s\n",info_player->username);
info_player->x=index1;
info_player->y=index2;
info_player->hasItem=0;
info_player->itemsDelivered=0;
info_player->pack=NULL;
info_player->clientsd = clientsd;
map[index1][index2].playerSD=clientsd;
}
void setScorePlayer(struct player *info_player){
int i=0;
while(scoreboard[i] != NULL){
i++;
}
scoreboard[i]=info_player;
}
char getLetter(int playerSD){
char c='0';
for(int i = 0; i < MAX_USERS; i++){
if(mapPlayers[i] == playerSD)
return ((char)i+65);
}
return c;
}
void matrixToString(char *info, int clientsd,int *obstacles){
int i = 0;
int j = 0;
char msg[30];
struct obstacles *a;
write(clientsd, &rows, sizeof(int));
write(clientsd, &cols, sizeof(int));
while(i < rows){
memset(msg,'\0',sizeof(msg));
while(j < cols){
if(map[i][j].playerSD >=0){
msg[j] = getLetter(map[i][j].playerSD);
}
else if(map[i][j].isObstacle){
a=(struct obstacles *)map[i][j].pointer;
if(obstacles[a->id]==1){
msg[j]='x';
}
else
msg[j]=' ';
}
else
msg[j] = map[i][j].object;
j++;
}
msg[j] = '\0';
write(clientsd, msg, cols);
j = 0;
i++;
}
sendMessage(clientsd,info);
}
void initGame(){
int i=0,j=0;
for(i=0;i<MAX_USERS;i++){
mapPlayers[i]=-1;
scoreboard[i]=NULL;
}
map=(struct cell**)malloc(rows * sizeof(struct cell *));
for(i=0;i<rows;i++){
map[i]=(struct cell*)malloc(cols*sizeof(struct cell));
}
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
map[i][j].isObstacle=0;
map[i][j].isWareHouse=0;
map[i][j].playerSD=-1; //Un socket descriptor ha valori tra 0 e 1024
map[i][j].object=' ';
map[i][j].pointer=NULL;
}
}
}
void movement(struct player *info_player, int add_x, int add_y){
int id;
struct obstacles *a;
if(isCellNotSolid(map[(info_player->x)+add_x][info_player->y+add_y])){
changeCoordinates(info_player, add_x, add_y);
}
else if(map[(info_player->x)+add_x][info_player->y+add_y].isObstacle){
a=(struct obstacles*)map[(info_player->x)+add_x][info_player->y+add_y].pointer;
id=a->id;
info_player->obstacles[id]=1;
}
}
void sendMessage(int clientsd, char *msg){
char buff[250];
int n;
strcpy(buff, msg);
n = strlen(buff);
write(clientsd, &n, sizeof(int));
if(n>0)
write(clientsd, buff, n);
}
void checkCommand(char msg, struct player *info_player,char *info){
int n;
char obj;
char loggedUsers[300];
if(msg == 't' || msg == 'T'){
sprintf(info, "Tempo rimanente: %d secondi\n", gameTime);
}
else if(msg=='p'||msg=='P'){
if(!info_player->hasItem){
if(map[info_player->x][info_player->y].pointer!=NULL){
info_player->pack=(struct items*)map[info_player->x][info_player->y].pointer;
obj=map[info_player->x][info_player->y].object;
map[info_player->x][info_player->y].object=' ';
map[info_player->x][info_player->y].pointer=NULL;
info_player->hasItem=1;
if(obj=='$')
sprintf(info, "Raccolto il seguente oggetto: Oro. Consegnalo al magazzino numero %d\n",info_player->pack->warehouse);
else if(obj=='@')
sprintf(info, "Raccolto il seguente oggetto: Cibo. Consegnalo al magazzino numero %d\n",info_player->pack->warehouse);
else
sprintf(info, "Raccolto il seguente oggetto: Spada. Consegnalo al magazzino numero %d\n",info_player->pack->warehouse);
}
}
else
{
strcpy(info,"Inventario pieno.\n");
}
}
else if(msg=='e'||msg=='E'){
if(info_player->hasItem && isWarehouseHere(info_player)){
info_player->hasItem=0;
info_player->itemsDelivered++;
writeLog_ItemDelivered(info_player);
info_player->pack=NULL;
strcpy(info,"Oggetto consegnato.\n");
if(info_player->itemsDelivered >= MAX_ITEMS){
pthread_mutex_lock(¬ifyMaxItems);
maxItemsReached = 1;
pthread_mutex_unlock(¬ifyMaxItems);
}
}
else if(info_player->hasItem==0 && isWarehouseHere(info_player))
strcpy(info,"Non hai oggetti nell'inventario.\n");
else if(info_player->hasItem && !isWarehouseHere(info_player))
strcpy(info,"Non ci sono magazzini nelle vicinanze.\n");
else
strcpy(info,"Per depositare un oggetto hai bisogno di un oggetto e di un magazzino nelle vicinanze!\n");
}
else if(msg=='i'||msg=='I'){
if(info_player->hasItem){
sprintf(info,"Sei il giocatore %c\nOggetti consegnati:%d\nHai un oggetto da consegnare al magazzino numero %d\n",getLetter(info_player->clientsd),info_player->itemsDelivered,info_player->pack->warehouse);
}
else
sprintf(info,"Sei il giocatore %c\nOggetti consegnati:%d\nNon hai oggetti da consegnare\n",getLetter(info_player->clientsd),info_player->itemsDelivered);
}
else if(msg=='h'||msg=='H'){
snprintf(info,260,"---LISTA COMANDI---\n[W]Muoversi sopra\n[A]Muoversi a sinistra\n[S]Muoversi giù\n[D]Muoversi a destra\n[I]Informazioni giocatore\n[P]Prendere oggetti\n[E]Consegnare oggetti\n[T]Tempo rimanente\n(Sono ammesse anche le lettere minuscole)\n[U]Utenti connessi\n[Q]Esci\n");
}
else if(msg=='q'||msg=='Q'){
strcpy(info,"Sei uscito dal gioco.\n");
write(info_player->clientsd, &(int){0}, sizeof(int));
write(info_player->clientsd, &(int){0}, sizeof(int));
sendMessage(info_player->clientsd,info);
close(info_player->clientsd);
info_player->itemsDelivered=-1;
}
else if(msg=='u'||msg=='U'){
buildLoggedUsersString(loggedUsers);
sprintf(info, "---UTENTI CONNESSI---\n%s", loggedUsers);
}
else
checkMovement(msg, info_player,info);
}
void checkMovement(char msg, struct player *info_player,char *info){
int clientsd=map[info_player->x][info_player->y].playerSD;
int n;
if(msg=='w'||msg=='W'){
if(info_player->x-1>=0){
movement(info_player, -1, 0);
}
}
else if(msg=='a'||msg=='A'){
if(info_player->y-1 >= 0){
movement(info_player, 0, -1);
}
}
else if(msg=='s'||msg=='S'){
if(info_player->x+1 < rows)
movement(info_player, 1, 0);
}
else if(msg=='d'||msg=='D'){
if(info_player->y+1 < cols)
movement(info_player, 0, 1);
}
strcpy(info,"");
}
void changeCoordinates(struct player *info_player, int add_x, int add_y){
int clientsd;
pthread_mutex_lock(&editMatrix);
clientsd=map[info_player->x][info_player->y].playerSD;
map[info_player->x][info_player->y].playerSD=-1;
map[info_player->x+add_x][info_player->y+add_y].playerSD=clientsd;
pthread_mutex_unlock(&editMatrix);
info_player->x+=add_x;
info_player->y+=add_y;
}
int isWarehouseHere(struct player *a){
int exp;
exp=checkWarehouse(a,-1,0)+checkWarehouse(a,1,0)+checkWarehouse(a,0,-1)+checkWarehouse(a,0,1);
return exp>0;
}
int checkWarehouse(struct player *a, int add_x,int add_y){
struct warehouse *b;
if(noBoundaryCheck(a,add_x,add_y) && a->pack!=NULL){
if(map[a->x+add_x][a->y+add_y].isWareHouse==1){
b=(struct warehouse*)map[a->x+add_x][a->y+add_y].pointer;
return a->pack->warehouse==b->id;
}
}
return 0;
}
int noBoundaryCheck(struct player *a,int add_x,int add_y){
int r=a->x+add_x;
int c=a->y+add_y;
if(r>=0 && r<rows && c>=0 && c<cols)
return 1;
return 0;
}
int mutexInitialization(){
if (pthread_mutex_init(&signup_mutex, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
if (pthread_mutex_init(&login, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
if (pthread_mutex_init(&editMatrix, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
if (pthread_mutex_init(&editMapPlayers, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
if (pthread_mutex_init(&mapGen, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
if (pthread_mutex_init(¬ifyMaxItems, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
if (pthread_mutex_init(&loggedUsersCountMutex, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
return 0;
}
void gameLogout(int clientsd){
logout(clientsd);
logoutStructs(clientsd);
}
int setScoreboardArr(){
int j = 0;
for(int i = 0; i < MAX_USERS; i++){
if(scoreboard[i] != NULL)
arr[j++] = scoreboard[i];
}
return j;
}
void createScoreboard(){
int i;
int n;
char buffer[100];
arr = (struct player **)malloc(loggedUsersCount*sizeof(struct player *));
n = setScoreboardArr();
printf("n = %d\n", n);
memset(scoreboardString,'\0',sizeof(scoreboardString)); //cappadavide
strcpy(scoreboardString," ---PARTITA FINITA---\n Classifica avventurieri:\nGIOCATORI\t\t\tOGGETTI\n");
selectionSort(n);
i = n - 1;
while(i >= 0){
memset(buffer,'\0',sizeof(buffer));
if(arr[i]->clientsd >= 0){
sprintf(buffer,"%s\t\t\t\t%d\n", arr[i]->username, arr[i]->itemsDelivered);
strcat(scoreboardString, buffer);
}
i--;
}
struct player *winner;
if(n == 0)
winner = NULL;
else
winner = getWinner(n);
writeLog_GameOver(winner);
}
struct player *getWinner(int dim){
int max = arr[dim-1]->itemsDelivered;
int i = 0;
while(i < dim-1){
if(arr[i]->itemsDelivered == max)
return NULL;
i++;
}
return arr[dim-1];
}
void copyStruct(struct player *a, struct player *temp){
memset(temp->username,'\0',sizeof(temp->username));
temp->x = a->x;
temp->y = a->y;
temp->hasItem = a->hasItem;
temp->itemsDelivered = a->itemsDelivered;
temp->pack = a->pack;
temp->obstacles = a->obstacles;
temp->clientsd = a->clientsd;
strcpy(temp->username,a->username);
}
void swapStructAddr(struct player *a, struct player *b){
struct player *temp;
temp = a;
a = b;
b = temp;
}
void swapStruct(struct player *a, struct player *b){
struct player *temp = (struct player *)malloc(sizeof(struct player));
copyStruct(a, temp);
copyStruct(b, a);
copyStruct(temp, b);
free(temp);
}
int partiziona(struct player* a[], int low, int high){
int pivot = a[high]->itemsDelivered;
int i = (low - 1);
for(int j = low; j <= high- 1; j++){
if (a[j]->itemsDelivered < pivot)
{
i++; // increment index of smaller element
swapStructAddr(a[i], a[j]);
}
}
swapStructAddr(a[i + 1], a[high]);
return (i + 1);
}
void selectionSort(int n){
int max;
for(int i=n-1;i>0;i--){
max=findMax(i);
swapStruct(arr[max], arr[i]);
}
}
int findMax(int i){
int max = 0;
for(int j=1; j<=i; j++){
if(arr[max]->itemsDelivered < arr[j]->itemsDelivered)
max=j;
}
return max;
}
void quicksort(struct player* a[MAX_USERS], int low, int high){
int pi;
if (low < high)
{
pi = partiziona(a, low, high);
quicksort(a, low, pi - 1); // Before pi
quicksort(a, pi + 1, high); // After pi
}
}
void writeLog(char *msg,int flag){
int fd;
if(flag){
pthread_mutex_lock(&gameLog);
fd=open("./gameLog",O_RDWR | O_APPEND|O_CREAT, 0666);
if(fd<0){
perror("Errore creazione file gameLog");
exit(1);
}
write(fd,msg,strlen(msg));
close(fd);
pthread_mutex_unlock(&gameLog);
}
else{
fd=open("./gameLog",O_RDWR | O_APPEND|O_CREAT, 0666);
if(fd<0){
perror("Errore creazione file gameLog");
exit(1);
}
write(fd,msg,strlen(msg));
close(fd);
}
}
void writeLog_GameOver(struct player *winner){
char str[30];
char msg[200];
time_t connTime;
struct tm *infoTime;
memset(str,'\0',sizeof(str));
time(&connTime);
infoTime=gmtime(&connTime);
strftime(str,sizeof(str),"%c",infoTime);
if(winner!=NULL){
sprintf(msg,"\t-[%s] GAME OVER: Winner is %s with %d items delivered.\n",str,winner->username,winner->itemsDelivered);
}
else
sprintf(msg,"\t-[%s] GAME OVER: No winner.\n",str);
writeLog(msg,1);
}
void writeLog_serverAbort(){
char str[30];
char msg[200];
time_t connTime;
struct tm *infoTime;
memset(str,'\0',sizeof(str));
time(&connTime);
infoTime=gmtime(&connTime);
strftime(str,sizeof(str),"%c",infoTime);
sprintf(msg,"[%s]Server closed.\n",str);