Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/un.h>
- #include <fcntl.h>
- #include <time.h>
- #include <pthread.h>
- #include <limits.h>
- #define SOCKET_NAME "/tmp/my_first_socket"
- #define max 10
- struct clue
- {
- int x;
- int y;
- };
- struct clue sugg[9],firstClue;
- //LISTA GIOCATORI ONLINE
- struct list
- {
- char name[25];
- int numClue; //NUMERO INDIZI TROVATI
- int last; //FLAG: 1 SE E' STATO TROVATO L'ULTIMO INDIZIO
- int trovato;
- struct list *next;
- };
- struct list *players=NULL;
- int selected[9]; //TIENE TRACCIA DEGLI INDIZI SCOVATI
- pthread_mutex_t semaforo=PTHREAD_MUTEX_INITIALIZER ;
- pthread_mutex_t semaforo2=PTHREAD_MUTEX_INITIALIZER ;
- time_t start,now; //VARIABILI IMPLEMENTAZIONE TIMER
- int count1=0,count2=0; //VARIABILI DI SINCRONIZZAZIONE CLIENT
- int tesoro=1; //FLAG CHE DIVENTA 1 SE VIENE SCOVATO IL TESORO
- char map[max][max]; //MAPPA DEL GIOCO
- //RESTITUISCE NUMERO DI UTENTI ONLINE
- int numUsers(struct list *players)
- {
- if(players==NULL)
- {
- return 0;
- }
- else
- {
- return 1+numUsers(players->next);
- }
- }
- //AGGIORNA IL NUMERO DI INDIZI SCOVATI DA UN GIOCATORE
- void updateclue(struct list *players,char *username)
- {
- if(strcmp(players->name, username)==0)
- {
- players->numClue=players->numClue+1;
- }
- else
- {
- updateclue(players->next,username);
- }
- }
- //STAMPA LISTA UTENTI ONLINE
- void printUsers(int mySockFd, struct list *players)
- {
- char buffer[500];
- char app[3];
- char app2[20];
- int i;
- bzero(buffer,500);
- if(players==NULL)
- {
- write(mySockFd, "0 UTENTI ONLINE", 15);
- }
- else
- {
- struct list *p=players;
- strcat(buffer,"USERNAME->NUMERO INDIZI\n");
- while(p!=NULL)
- {
- i=0;
- while(p->name[i]!='\n')
- {
- app2[i]=p->name[i];
- i++;
- }
- app2[i]='\0';
- // strcat(buffer,"USERNAME : ");
- strcat(buffer, app2);
- strcat(buffer,"->");
- sprintf(app,"%d",p->numClue);
- strcat(buffer,app);
- strcat(buffer,"\n");
- p=p->next;
- }
- write(mySockFd,buffer,strlen(buffer));
- }
- }
- //ELIMINA UTENTE DA LISTA GIOCATORI
- struct list *deleteUser(struct list *players, char username[])
- {
- if(players!=NULL)
- {
- if(strcmp(players->name,username)==0)
- {
- struct list *elem=(struct list *)malloc(sizeof(struct list));
- elem=players;
- players=players->next;
- free(elem);
- }
- else
- {
- players->next=deleteUser(players->next, username);
- }
- }
- return players;
- }
- //CREA LISTA GIOCATORI ONLINE
- void onlineUsers(char username[])
- {
- if(players==NULL)
- {
- players=(struct list *)malloc(sizeof(struct list));
- strcpy(players->name, username);
- players->numClue=0;
- players->next=NULL;
- }
- else
- {
- struct list *appoggio=(struct list *)malloc(sizeof(struct list));
- strcpy(appoggio->name, username);
- players->numClue=0;
- players->last=0;
- players->trovato=0;
- appoggio->next=players;
- players=appoggio;
- }
- }
- //VIENE STAMPATA LA MAPPA DI GIOCO SUL SERVER E MOSTRATE LE POSIZIONI DEGLI ELEMENTI DI GIOCO
- void printServerMap(char map [max][max])
- {
- int i, j;
- char a;
- char buf[2];
- int b;
- printf("MATRICE-SERVER:\n");
- for(i=0;i<max;i++)
- {
- for(j=0;j<max;j++)
- {
- if(map [i][j]>='1' && map [i][j]<='9')
- {
- a=map[i][j];
- buf[0]=a;
- buf[1]='\0';
- printf("%d " , atoi(buf));
- }
- else
- {
- printf("%c ", map[i][j]);
- }
- }
- printf ("\n");
- }
- }
- //GENERA LA MAPPA DI GIOCO E RESTITUISCE IL PRIMO INDIZIO
- struct clue createMap(char map[max][max], struct clue* sugg)
- {
- char ind='1';
- int i=0, j=0, indice1=0, indice2=0, last1=0, last2=0, num_indizi=8, num_ostacoli=5;
- for(i=0;i<9;i++)
- selected[i]=0;
- //AZZERO MAPPA DI GIOCO
- for(i=0;i<max;i++)
- {
- for(j=0;j<max;j++)
- {
- map[i][j]='0';
- }
- }
- i=0;
- //POSIZIONO INDIZI
- //COORDINATE PRIMO INDIZIO
- firstClue.x=rand()%10;
- firstClue.y=rand()%10;
- //POSIZIONO PRIMO INDIZIO
- map[firstClue.x][firstClue.y]=ind;
- //GENERO RESTANTI INDIZI
- while(atoi(&ind)!=num_indizi+1)
- {
- indice1=rand()%10;
- indice2=rand()%10;
- //SE LA POSIZIONE E' LIBERA
- if(map[indice1][indice2]=='0')
- {
- //POSIZIONO INDIZIO
- map[indice1][indice2]=ind+1 ;
- //AGGIORNO VETTORE SUGGERIMENTI
- sugg[atoi(&ind)-1].x=indice1;
- sugg[atoi(&ind)-1].y=indice2;
- ind++;
- }
- }
- i=0;
- //POSIZIONO TESORO
- do
- {
- indice1=rand()%10;
- indice2=rand()%10;
- //SE LA POSIZIONE E' LIBERA
- if(map[indice1][indice2]=='0')
- {
- //POSIZIONO TESORO
- map[indice1][indice2]='t';
- i++;
- }
- }while(i!=1);
- //ASSEGNO LE COORDINATE DEL TESORO ALL'ULTIMO SUGGERIMENTO
- sugg[8].x=indice1,
- sugg[8].y=indice2;
- i=0;
- //POSIZIONO OSTACOLI
- while(i!=num_ostacoli)
- {
- indice1=rand()%10;
- indice2=rand()%10;
- //SE LA POSIZIONE E' LIBERA
- if(map[indice1][indice2]=='0')
- {
- map[indice1][indice2]='x';
- i++;
- }
- }
- printServerMap(map);
- return firstClue;
- }
- //IMPOSTA POSIZIONE INIZIALE DI UN GIOCATORE
- void startPosition(int mySockFd,int *cor1, int *cor2)
- {
- char buffer[256], scelta[256], scelta2[256];
- bzero(buffer, 256);
- //UTILIZZO IL SEMAFORO PER EVITARE CHE DUE GIOCATORI INIZINO NELLA STESSA POSIZIONE
- pthread_mutex_lock(&semaforo2);
- do
- {
- *cor1=rand()%10;
- *cor2=rand()%10;
- }while(map[*cor1][*cor2]!='0' && map[*cor1][*cor2]!='c');
- //SBLOCCO IL SEMAFORO
- strcpy(buffer, "GIOCATORE POSIZIONATO, PREMERE INVIO...");
- write(mySockFd, buffer, strlen(buffer));
- read(mySockFd, scelta,255);
- pthread_mutex_unlock(&semaforo2);
- bzero(buffer, 256);
- }
- //AGGIORNA POSIZIONE ATTUALE DELL'UTENTE
- void currPosition(char map[max][max], int cor1, int cor2)
- {
- map[cor1][cor2]='o';
- }
- //SCRIVE IN UN FILE DI TESTO IL MOMENTO DI RITROVAMENTO DI UN TESORO
- void updateFileTreasure(char username[])
- {
- int fd;
- char messaggio[256], orario[256];
- time_t ora;
- time(&ora);
- bzero(messaggio, 256);
- bzero(orario, 256);
- strcpy(messaggio, "RITROVAMENTO TESORO: " ) ;
- sprintf(orario, "%s", asctime(localtime(&ora)));
- strcat(messaggio, username);
- strcat(messaggio, orario);
- strcat(messaggio, "\n");
- pthread_mutex_lock(&semaforo);
- fd=open("ritrovo_tesoro.txt", O_WRONLY|O_CREAT);
- lseek(fd, 0, SEEK_END);
- write(fd, messaggio, strlen(messaggio));
- close(fd);
- pthread_mutex_unlock(&semaforo);
- }
- //AGGIORNA I VALORI NELLA MAPPA DI GIOCO
- int updateMap(int mySockFd,char map[max][max],int cor1,int cor2,int oldcor1,int oldcor2,struct clue* sugg,char *username)
- {
- int lst;
- struct list *p,g;
- int flag=0; //FLAG: 0: MOVIMENTO CONSENTITO, 1: MOVIMENTO NON CONSENTITO, 1<=FLAG-9<=9 : INDIZIO, 20:TESORO
- char buf[2];
- char app[5];
- for(p=players;p;p=p->next)
- {
- if(strcmp(p->name,username)==0)
- lst=p->last;
- }
- //SE TROVO IL TESORO
- if(map[cor1][cor2]=='t' || map[cor1][cor2]=='$')
- {
- map[cor1][cor2]='$';
- //TESORO TROVATO
- if(lst==1)
- {
- tesoro=0;
- for(p=players;p;p=p->next)
- {
- if(strcmp(p->name,username)==0)
- p->trovato=1;
- }
- updateFileTreasure(username);
- }
- if(!(map[oldcor1][oldcor2]>='1' && map[oldcor1][oldcor2]<='9'))
- {
- map[oldcor1][oldcor2]='c';
- }
- flag=20;
- }
- //SE TROVO UN INDIZIO
- else if(map[cor1][cor2]>='1' && map[cor1][cor2]<='9')
- {
- if(!(map[oldcor1][oldcor2]>='1' && map[oldcor1][oldcor2]<='9') )
- {
- if (map[oldcor1][oldcor2]!='$')
- {
- map[oldcor1][oldcor2]='c';
- }
- }
- buf[0]=map[cor1][cor2];
- buf[1]='\0';
- flag=9+atoi(buf);
- // sprintf(app,"%d",flag);
- // write(mySockFd,app,2);
- if(selected[atoi(buf)-1]==0)
- updateclue(players,username);
- selected[atoi(buf)-1]=1;
- if(map[cor1][cor2]=='9')
- {
- for(p=players;p;p=p->next)
- {
- if(strcmp(p->name,username)==0)
- {
- p->last=1;
- }
- }
- }
- }
- //SE TROVO UN OSTACOLO
- else if(map[cor1][cor2]=='x')
- {
- map[cor1][cor2]='/';
- flag=1;
- }
- //MOVIMENTO NON CONSENTITO SU UN OSTACOLO GIA SCOVATO O SU UN ALTRO GIOCATORE
- else if(map[cor1][cor2]=='/' || map[cor1][cor2]=='o' || map[cor1][cor2]=='O')
- {
- flag=1;
- }
- //MOVIMENTO SU CASELLE INESPLORATE O ESPLORATE MA NON SIGNIFICATIVE
- else if (map[cor1][cor2]=='0' || map[cor1][cor2]=='c')
- {
- if(!(map[oldcor1][oldcor2]>='1' && map[oldcor1][oldcor2]<='9') )
- {
- if (map[oldcor1][oldcor2]!='$')
- {
- map[oldcor1][oldcor2]='c';
- }
- }
- flag=0;
- }
- return flag;
- }
- //STAMPA MAPPA SUL SOCKET
- void printClientMap(int mySockFd, char map[max][max], int flag, int cor1,int cor2,char *username)
- {
- int i, j;
- char buffer[10000];
- char n_tes[5];
- char buf[2];
- char app[5];
- bzero(buffer,10000);
- int lst;
- struct list* p;
- for(p=players;p;p=p->next)
- {
- if(strcmp(p->name,username)==0)
- {
- lst=p->last;
- }
- }
- sprintf(app, "%f", difftime(time(&now), start));
- strcat(buffer, "(-)TEMPO TRASCORSO: ");
- strcat(buffer, app);
- strcat(buffer, "s \n");
- if(flag==0 || flag==20)
- {
- strcat(buffer, "*{-} MOVIMENTO CONSENTITO! {-}*");
- strcat(buffer, "\n\n");
- }
- else if(flag==-1)
- {
- strcat(buffer, "*{-} INIZIAMO A GIOCARE! {-}*");
- strcat(buffer, n_tes);
- strcat(buffer, " \n\n");
- strcat(buffer, "*** IL PRIMO INDIZIO SI TROVA IN POSIZIONE: ");
- sprintf(app, "%d", firstClue.x);
- strcat(buffer, app);
- strcat(buffer, ",");
- sprintf(app, "%d", firstClue.y);
- strcat(buffer, app);
- strcat(buffer, "***\n");
- }
- else if(flag==5)
- {
- strcat(buffer, "*{-} MOVIMENTO OLTRE MATRICE NON CONSENTITO!{-}*");
- strcat(buffer, n_tes);
- strcat(buffer, " \n\n");
- }
- else if (flag==1)
- {
- strcat( buffer , "*{-} MOVIMENTO NON CONSENTITO! {-}*");
- strcat(buffer, n_tes);
- strcat(buffer, " \n\n");
- }
- else
- {
- if(flag!=20)
- {
- if(flag==18)
- strcat(buffer, "*** ULTIMO INDIZIO SCOVATO, IL TESORO SI TROVA NELLA POSIZIONE: ");
- else
- strcat(buffer, "*** INDIZIO SCOVATO, IL PROSSIMO INDIZIO SI TROVA NELLA POSIZIONE: ");
- sprintf(n_tes, "%d", sugg[(flag-10)].x);
- strcat(buffer, n_tes);
- strcat(buffer, ",");
- sprintf(n_tes, "%d", sugg[(flag-10)].y);
- strcat(buffer, n_tes);
- strcat(buffer, "***\n\n");
- }
- }
- strcat(buffer,"\t");
- for(i=0;i<max;i++)
- {
- sprintf(app,"%d\t",i);
- strcat(buffer,app);
- }
- strcat(buffer,"\n\t");
- for(i=0;i<max-1;i++)
- {
- strcat(buffer,"_________");
- }
- strcat(buffer,"\n\n");
- for(i=0;i<max;i++)
- {
- sprintf(app,"%d |\t",i);
- strcat(buffer,app);
- for(j=0;j<max;j++)
- {
- //TESORO
- if(map[i][j]=='$' )
- {
- if(lst==1)
- strcat(buffer, "$\t");
- else
- {
- if(cor1==i && cor2==j)
- strcat(buffer, "@\t");
- else
- strcat(buffer, ".\t");
- }
- }
- //OSTACOLO
- else if(map[i][j]=='/')
- {
- strcat ( buffer , "/\t" ) ;
- }
- //CASELLE INESPLORATE
- else if( map[i][j]=='x' || map[i][j]=='t' || map[i][j]=='0')
- {
- strcat(buffer, "*\t");
- }
- // INDIZIO
- else if(map[i][j]>='1' && map[i][j]<='9')
- {
- buf[0]=map[i][j];
- buf[1]='\0';
- if(selected[atoi(buf)-1]==0)
- strcat(buffer, "*\t");
- else
- {
- if(cor1==i && cor2==j) //POSIZIONE UTENTE IN CASO DI INDIZIO TROVATO
- {
- strcat(buffer,"#\t");
- }
- else
- {
- strcat(buffer, buf);
- strcat(buffer, "\t");
- }
- }
- }
- //CASELLE ESPLORATE
- else if(map[i][j]=='c')
- {
- strcat(buffer, ".\t");
- }
- //POSIZIONE ATTUALE UTENTE
- else if(map[i][j]=='o')
- {
- strcat(buffer, "@\t");
- }
- }
- strcat(buffer, "\n");
- }
- strcat(buffer, "\n");
- if(tesoro!=0)
- {
- strcat(buffer, "*{-} VUOI MUOVERTI? [A/W/S/D], USCIRE?[EXIT] O STAMPARE LA LISTA DEGLI UTENTI? [LIST] {-}*");
- }
- else
- {
- strcat(buffer, "{$$$} TROVATO IL TESORO NASCOSTO! GENERATA NUOVA MAPPA! Premi Invio {$$$}");
- tesoro=0;
- }
- write(mySockFd, buffer, strlen(buffer));
- bzero(buffer, 10000);
- }
- //AGGIORNO MAPPA IN CASO DI ABBANDONO DI UN UTENTE
- void leaveMap(int *cor1, int *cor2)
- {
- if(map[*cor1][*cor2]=='o')
- {
- map[*cor1][*cor2]='0';
- }
- else
- {
- *cor1=-1;
- *cor2=-1;
- }
- }
- void game(int mySockFd,char username[], char map[max][max])
- {
- int cor1=0,cor2=0,maxx=INT_MIN;
- char scelta[256];
- int n, flag=0;
- char continua[256];
- int count=0;
- char charr[10];
- char buffer[1000];
- char app[20];
- struct list *p;
- startPosition(mySockFd, &cor1, &cor2);
- currPosition(map, cor1, cor2);
- printClientMap(mySockFd, map, -1,cor1,cor2,username);
- //STAMPA MAPPA DI GIOCO SUL SERVER
- while(1) //ITERA FINO A CHE NON VIENE TROVATO IL TESORO O NON SCADE IL TEMPO
- {
- flag=0;
- bzero(scelta, 256);
- bzero(buffer, 1000);
- n=read(mySockFd, scelta, 255);
- if(n<0)
- {
- perror("Errore lettura da socket scelta\n");
- exit(1);
- }
- //CONTROLLO SE IL GIOCO E' TERMINATO
- if(tesoro==0 || difftime(time(&now), start)>60)
- {
- if (difftime(time(&now), start)>60 && tesoro==1) //SE IL GIOCO E' TERMINATO PER TEMPO SCADUTO
- {
- for(p=players;p!=NULL;p=p->next)
- {
- if(p->numClue>maxx)
- {
- maxx=p->numClue;
- strcpy(app,p->name);
- }
- }
- strcat(buffer, "\nTEMPO TERMINAT0\n");
- for(p=players;p!=NULL;p=p->next)
- {
- if(p->numClue==maxx)
- {
- count++;
- }
- }
- if(count>1)
- {
- strcat (buffer, "PAREGGIO, VINCONO...\n"); //CASO IN CUI CI SIA UN PAREGGIO
- for(p=players;p!=NULL;p=p->next)
- {
- if(p->numClue==maxx)
- {
- strcat(buffer, p->name);
- strcat(buffer,"\n");
- }
- }
- }
- else
- {
- strcat(buffer, "VINCE...\n"); //COMUNICO CHI HA VINTO LA PARTITA
- strcat(buffer, app);
- }
- count=0;
- maxx=INT_MIN;
- }
- else
- {
- strcat (buffer, "TESORO TROVATO, VINCE...\n");
- for(p=players;p;p=p->next)
- {
- if(p->trovato==1)
- strcat(buffer,p->name);
- }
- }
- pthread_mutex_lock(&semaforo);
- count1++;
- if(count1==1)
- {
- firstClue=createMap(map, sugg);
- }
- pthread_mutex_unlock(&semaforo);
- strcat(buffer, "\nAttendi altri giocatori...");
- write(mySockFd, buffer, strlen(buffer));
- while(count1<numUsers(players) && count1!=0);
- {
- read(mySockFd, continua, 255);
- }
- startPosition(mySockFd, &cor1, &cor2);
- currPosition(map, cor1, cor2);
- for(p=players;p!=NULL;p=p->next)
- {
- p->numClue=0;
- p->last=0;
- p->trovato=0;
- }
- {
- tesoro=1;
- count1=0;
- flag=-1;
- time(&start);
- }
- }
- else
- {
- //SPOSTAMENTO A SINISTRA
- if(strcmp(scelta, "A\n")==0 || strcmp(scelta, "a\n")==0)
- {
- //CONTROLLO CHE NON SI ESCA DALLA MATRICE
- if((cor2-1)>-1)
- {
- flag=updateMap(mySockFd, map, cor1, cor2-1, cor1, cor2, sugg,username);
- if(flag==0 && tesoro==1)
- {
- currPosition(map, cor1, cor2-1);
- cor2=cor2-1;
- }
- else if(flag>=10)
- {
- //currPosition_tesoro(map, cor1, cor2-1);
- //updateclue(players,username);
- cor2=cor2-1;
- }
- }
- else
- {
- flag=5; //MOVIMENTO FUORI MATRICE
- }
- }
- //SPOSTAMENTO IN ALTO
- else if(strcmp(scelta, "W\n")==0 || strcmp(scelta, "w\n")==0)
- {
- //CONTROLLO CHE NON SI ESCA DALLA MATRICE
- if((cor1-1)>-1)
- {
- flag=updateMap(mySockFd,map,cor1-1,cor2,cor1,cor2,sugg,username);
- if (flag==0 && tesoro==1)
- {
- currPosition(map,cor1-1,cor2);
- cor1=cor1-1;
- }
- else if(flag>=10)
- {
- //updateclue(players,username);
- cor1=cor1-1;
- }
- }
- else
- {
- flag=5; //MOVIMENTO FUORI MATRICE
- }
- }
- //SPOSTAMENTO IN BASSO
- else if(strcmp(scelta, "S\n")==0 || strcmp(scelta, "s\n")==0)
- {
- //CONTROLLO CHE NON SI ESCA DALLA MATRICE
- if((cor1+1)<10)
- {
- flag=updateMap(mySockFd, map, cor1+1, cor2, cor1, cor2, sugg,username);
- if(flag==0 && tesoro==1)
- {
- currPosition(map, cor1+1, cor2);
- cor1=cor1+1;
- }
- else if(flag>=10)
- {
- // updateclue(players,username);
- cor1=cor1+1;
- }
- }
- else
- {
- flag=5; //MOVIMENTO FUORI MATRICE
- }
- }
- //SPOSTAMENTO A DESTRA
- else if(strcmp(scelta, "D\n")==0 || strcmp(scelta, "d\n")==0)
- {
- //CONTROLLO CHE NON SI ESCA DALLA MATRICE
- if((cor2+1)<10)
- {
- flag=updateMap(mySockFd, map, cor1, cor2+1, cor1, cor2, sugg,username);
- if(flag==0 && tesoro==1)
- {
- currPosition(map, cor1, cor2+1);
- cor2=cor2+1;
- }
- else if(flag>=10)
- {
- // updateclue(players,username);
- cor2=cor2 + 1;
- }
- }
- else
- {
- flag=5; //MOVIMENTO FUORI MATRICE
- }
- }
- else if(strcmp(scelta, "EXIT\n")==0 || strcmp(scelta, "exit\n")==0)
- {
- printf("Mi disconnetto\n");
- players=deleteUser(players, username);
- leaveMap(&cor1, &cor2);
- pthread_exit(NULL);
- }
- else if(strcmp(scelta, "LIST\n")==0 || strcmp(scelta, "list\n")==0)
- {
- printUsers(mySockFd,players);
- }
- }
- printClientMap(mySockFd, map, flag,cor1,cor2,username);
- }
- }
- //MENU UTENTE
- void home(int mySockFd, char username[])
- {
- char scelta[256], buffer[256],app[256];
- int n, opzione=0;
- do
- {
- bzero(buffer, 256);
- strcpy(buffer, "\n[*]MENU - SCEGLI OPZIONE:\n[1]GIOCA!\n[2]VISUALIZZA UTENTI CONNESSI\n[3]DISCONNETTI\nSCELTA: ");
- n=write(mySockFd, buffer, strlen(buffer)); // INVIO AL CLIENT LE ALTERNATIVE DELL'UTENTE
- if(n<0)
- {
- perror("Errore scrittura su socket\n");
- exit(1);
- }
- bzero(scelta, 256);
- n=read(mySockFd ,scelta ,255);
- opzione=atoi(scelta);
- if(n<0)
- {
- perror("Errore lettura da socket\n");
- exit(1);
- }
- if(strcmp(scelta, "1\n")==0)
- {
- printf("FUNZIONE GIOCO:\n");
- game(mySockFd, username, map);
- break;
- }
- else if(strcmp(scelta, "2\n")==0)
- {
- printf("FUNZIONE LISTA UTENTI:\n");
- printUsers(mySockFd, players);
- read(mySockFd ,app ,255);
- }
- else if(strcmp(scelta, "3\n")==0)
- {
- break;
- }
- else
- {
- strcpy(buffer, "Operazione non valida. Premere un tasto per continuare.");
- write(mySockFd, buffer, strlen(buffer));
- bzero(buffer, 256);
- read(mySockFd, buffer, 256);
- }
- }while(opzione<=0 || opzione==2 || opzione>3);
- bzero(buffer, 256);
- players=deleteUser(players, username);
- strcpy(buffer, "\nAccount disconnesso. Arrivederci! Premere un tasto per continuare." );
- write(mySockFd, buffer, strlen(buffer));
- bzero(buffer, 256);
- read(mySockFd, buffer, 256);
- }
- //CONTROLLO DISPONIBILITA' USERNAME
- int checkUsername(char username[])
- {
- int fd;
- fd=open(username, O_RDONLY);
- if(fd<0)
- {
- return 0 ;
- }
- else
- {
- close(fd);
- return 1;
- }
- }
- //MEMORIZZA DATA E ORA DI ACCESSO DEGLI UTENTI
- void updateFile ( char username[] )
- {
- int fd;
- char messaggio[256], orario[256];
- time_t ora;
- time(&ora);
- bzero(messaggio, 256);
- bzero(orario, 256);
- strcpy(messaggio, "Effettua l'accesso: ");
- sprintf(orario, "%s", asctime(localtime(&ora)));
- strcat(messaggio, username);
- strcat(messaggio, orario);
- strcat(messaggio, "\n");
- pthread_mutex_lock(&semaforo);
- fd=open("logging.txt", O_WRONLY|O_CREAT) ;
- lseek(fd, 0, SEEK_END);
- write(fd, messaggio, strlen(messaggio));
- close(fd);
- pthread_mutex_unlock(&semaforo);
- }
- //ACCESSO E REGISTRAZIONE
- void login(int mySockFd)
- {
- int n, n2, m, trovato,cont=0;
- int filedes;
- char *res;
- struct list *app;
- char buffer[256], buffer2[256], username[256], password[256], confronta_passwd[256], confronta_user[256];
- do
- {
- write(mySockFd, "\nVuoi [r]egistrarti, [a]ccedere o uscire[EXIT] ? ", 53);
- bzero(buffer, 256);
- n=read(mySockFd, buffer, 255);
- if(n<0)
- {
- perror("Errore lettura da socket\n");
- exit(1);
- }
- if(strcmp(buffer, "EXIT\n")==0)
- {
- printf("Mi disconnetto\n");
- pthread_exit(NULL);
- }
- //SE VOGLIO REGISTRARMI
- else if ( strcmp ( buffer , "r\n" ) == 0 )
- {
- pthread_mutex_lock(&semaforo);
- filedes=open("accesso.txt", O_RDWR | O_CREAT);
- lseek(filedes, 0, SEEK_END);
- //ACQUISIZIONE USERNAME
- do
- {
- write(mySockFd, "[*]Username:", 12);
- bzero(username, 256);
- n=read(mySockFd, username, 255);
- if(n<0)
- {
- perror ("Errore lettura socket\n");
- exit(1);
- }
- trovato=checkUsername(username) ; //CONTROLLO CHE L'USERNAME NON SIA GIA' IN USO
- if(trovato==1)
- {
- bzero(buffer,256);
- strcpy(buffer, "\nUsername gia' in uso. Premi un tasto per provarne un altro!");
- write(mySockFd, buffer, strlen(buffer));
- read(mySockFd, buffer, 256);
- }
- }while(trovato==1);
- n=write(filedes, username, strlen(username));
- // ACQUISIZIONE PASSWORD
- write(mySockFd, "[*]Password:", 12);
- bzero(password, 256);
- n=read(mySockFd, password, 255);
- if(n<0)
- {
- perror("Errore lettura socket\n");
- exit(1);
- }
- n=write(filedes, password, strlen(password));
- bzero(buffer, 256);
- strcpy(buffer, "*** Registrazione effettuata con SUCCESSO! ***\nPremere un tasto per continuare");
- write(mySockFd, buffer, strlen(buffer));
- bzero(buffer, 256);
- read(mySockFd, buffer, 256);
- close(filedes);
- pthread_mutex_unlock(&semaforo); //SBLOCCO SEMAFORO
- }
- else if(strcmp(buffer, "a\n")==0)
- {
- n = write ( mySockFd , "Username:" , 12 ) ;
- if ( n < 0 )
- {
- perror ("Errore scrittura socket");
- exit (1);
- }
- bzero ( username , 256 );
- n=read(mySockFd, username, 255);
- if(n< 0)
- {
- perror ( "Errore lettura socket\n");
- exit (1);
- }
- for(app=players;app;app=app->next)
- {
- if(strcmp(app->name,username)==0)
- break;
- }
- if(app)
- cont=2;
- else
- {
- n=write(mySockFd, "Password:", 12);
- if(n<0)
- {
- perror("Errore scrittura socket");
- exit(1);
- }
- bzero(password, 256);
- n=read(mySockFd, password, 255);
- if(n<0)
- {
- perror("Errore lettura socket\n");
- exit(1);
- }
- pthread_mutex_lock(&semaforo);
- filedes=open("accesso.txt", O_RDWR | O_CREAT);
- lseek(filedes, 0, SEEK_SET);
- int i=0, j=0;
- bzero(confronta_user, 256);
- bzero(confronta_passwd, 256);
- while(n=read(filedes, buffer, 1)>0)
- {
- if(buffer[0]!='\n')
- { // PRENDO L'USERNAME DAL FILE ACCESSO
- confronta_user[i]=buffer[0];
- i+=1;
- }
- else
- {
- confronta_user[i]='\n';
- if(strcmp(confronta_user, username)==0)
- {
- while (m=read(filedes, buffer2, 1)>0)
- { // PRENDO LA PASSWORD DELL'USERNAME TROVATO
- if(buffer2[0]!='\n')
- {
- confronta_passwd[j]=buffer2[0];
- j+=1;
- }
- else
- {
- confronta_passwd[j]='\n';
- if(strcmp(confronta_passwd,password)!=0) //LA PASSWORD NON COINCIDE
- break;
- else if(strcmp(confronta_passwd, password)==0)
- {
- //LA PASSWORD COINCIDE
- onlineUsers(username);
- cont = 1 ;
- break;
- }
- }
- }
- break;
- }
- else
- {
- i=0;
- bzero(confronta_user, 256);
- }
- }
- }
- close ( filedes ) ;
- pthread_mutex_unlock (&semaforo);
- }
- if(cont==0)
- {
- // NOME UTENTE E PASSWORD NON SONO VALIDI
- n=write(mySockFd, "Credenziali invalide! Premere un tasto per continuare", 53);
- if(n<0)
- {
- perror("Errore scrittura socket");
- exit(1);
- }
- bzero(buffer, 256);
- n=read(mySockFd, buffer, 255);
- if(n<0)
- {
- perror("Errore lettura da socket");
- exit(1);
- }
- }
- else if(cont==2)
- {
- // UN UTENTE CON NOME IMMESSO E' GIA' IN PARTITA
- n=write(mySockFd, "Utente gia' in gioco con questo Username! Premere un tasto per continuare", 72);
- if(n<0)
- {
- perror("Errore scrittura socket");
- exit(1);
- }
- bzero(buffer, 256);
- n=read(mySockFd, buffer, 255);
- if(n<0)
- {
- perror("Errore lettura da socket");
- exit(1);
- }
- }
- else
- { // NOME UTENTE E PASSWORD CORRETTE
- n=write(mySockFd, "*** Login effettuato con successo! ***\nPremere un tasto per continuare", 70);
- if(n<0)
- {
- perror("Errore scrittura socket");
- exit(1);
- }
- bzero(buffer, 256);
- n=read(mySockFd, buffer, 255);
- if(n<0)
- {
- perror("Errore lettura da socket");
- exit(1);
- }
- updateFile(username);
- printf("\n %s \n",username);
- home(mySockFd, username ) ;
- }
- }
- }while(n>=0);
- }
- //FUNZIONE INIZIALE PER LA GESTIONE DEI THREAD
- void * manageThread(void * arg)
- {
- int sd=*(int*)arg;
- login(sd);
- return;
- }
- int main(int argc, char **argv)
- {
- srand((unsigned)time(NULL));
- if(argc<2)
- {
- perror("Errore parametri\n");
- exit(1);
- }
- int porta=atoi(argv[1]);
- int listen_sd, connect_sd;
- pthread_t tid;
- int *thread_sd;
- struct sockaddr_in my_addr, client_addr;
- socklen_t client_len;
- my_addr.sin_family=AF_INET;
- my_addr.sin_addr.s_addr=INADDR_ANY;
- if((listen_sd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
- {
- perror("socket");
- exit(1);
- }
- unlink(SOCKET_NAME);
- my_addr.sin_port=htons(porta);
- //EFFETTUO OPERAZIONE DI BINDING
- if(bind(listen_sd, (struct sockaddr *)&my_addr, sizeof(my_addr))<0)
- {
- perror("bind");
- exit(1);
- }
- //SETTO IL SOCKET CREATO IN STATO DI LISTEN
- if(listen(listen_sd, 1)<0)
- {
- perror("listen");
- exit(1);
- }
- //GENERO LA MAPPA DI GIOCO
- firstClue=createMap(map,sugg);
- time(&start);
- while(1)
- {
- client_len = sizeof(client_addr);
- if((connect_sd=accept(listen_sd, (struct sockaddr *)&client_addr, &client_len))<0)
- {
- perror("accept");
- exit(1);
- }
- thread_sd=(int*)malloc(sizeof(int));
- fprintf(stderr, " new connection \n");
- *thread_sd = connect_sd;
- printf("server: new connection from %d \n",connect_sd);
- pthread_create(&tid, NULL, manageThread, (void *) thread_sd);
- }
- close(connect_sd);
- close(listen_sd);
- return 0;
- }
Add Comment
Please, Sign In to add comment